usage of ApplicationLink

krishnan March 2, 2015

I am developing a jira plugin, which will invoke RESTful API in custom web application for its functionality.  The access is using basic authentication.  

I am storing the url, username and password in Application Links.  How do I use the application Links in my plugin code to invoke the REST Api in Custom Web application?

Thanks

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
krishnan March 2, 2015

Thanks Volodymyr Krupac. 

Why do we need to have REQUEST_URL? I thought we will get the URL from ApplicationLink.

Regards

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2015

REQUEST_URL is URL path like: "/rest/my-plugin/1.0/my-resource" add to the URL from the ApplicationLink. When preparing the code for your mistakenly put "http://..."; there

krishnan March 2, 2015

Thanks Volodymyr Krupac.

1 vote
Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2015

Hi krishnan!

Recently implemented the code that does the authenticated request through the application link. In my case this was oauth authentication so maybe some adjustment are needed. The simplified version of the code:

package com;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.applinks.api.ApplicationLink;
import com.atlassian.applinks.api.ApplicationLinkRequest;
import com.atlassian.applinks.api.ApplicationLinkRequestFactory;
import com.atlassian.applinks.api.ApplicationLinkService;
import com.atlassian.applinks.api.CredentialsRequiredException;
import com.atlassian.sal.api.net.Response;
import com.atlassian.sal.api.net.ResponseException;
import com.atlassian.sal.api.net.ResponseHandler;
public class DoRequest {
  private static class ConsumerInformationResponseHandler<T extends Response> implements ResponseHandler<Response> {
    /**
     * True if space created
     */
    private boolean ok;
    /**
     * @param projectKey
     */
    public ConsumerInformationResponseHandler() {
    }
    public void handle(final Response response) throws ResponseException {
      this.ok = response.getStatusCode() == HttpServletResponse.SC_OK;
    }
    /**
     * @return the spaceCreated
     */
    public boolean isOk() {
      return ok;
    }
  }
  /**
   * REQUEST_URL
   */
  private static final String REQUEST_URL = "/rest/my-plugin/1.0/my-resource";
  /**
   * Logger
   */
  private static final Logger log = LoggerFactory.getLogger(DoRequest.class);
  /**
   * ApplicationLinkService
   */
  private ApplicationLinkService applicationLinkService;
  /**
   * 
   */
  public DoRequest(ApplicationLinkService applicationLinkService) {
    this.applicationLinkService = applicationLinkService;
  }
  /**
   * @param linkId
   * @return Error string or empty
   */
  public String doRequest(String linkId) {
    String error = "";
    ApplicationLinkRequest applicationLinkRequest = null;
    ApplicationLinkRequestFactory applicationLinkRequestFactory = getAppLink(linkId)
            .createAuthenticatedRequestFactory();
    try {
      applicationLinkRequest = applicationLinkRequestFactory.createRequest(
              com.atlassian.sal.api.net.Request.MethodType.POST, REQUEST_URL);
      // applicationLinkRequest.addRequestParameters(...)
      applicationLinkRequest.setFollowRedirects(false); // true by default
      ConsumerInformationResponseHandler<Response> responseHandler = new ConsumerInformationResponseHandler<Response>();
      applicationLinkRequest.execute(responseHandler);
      if (!responseHandler.isOk()) {
        error = "My error";
      }
    } catch (CredentialsRequiredException e) {
      error = "My error: " + e.getMessage();
      log.error("My error", e);
    } catch (Exception e) {
      error = "My error: " + e.getMessage();
      log.error("My error", e);
    }
    return error;
  }
  private ApplicationLink getAppLink(String applicationLinkId) {
    ApplicationLink applicationLink = null;
    Iterable<ApplicationLink> links = applicationLinkService.getApplicationLinks();
    for (ApplicationLink link : links) {
      if (link.getId().get().equals(applicationLinkId)) {
        applicationLink = link;
        break;
      }
    }
    return applicationLink;
  }
}
TAGS
AUG Leaders

Atlassian Community Events