Hi,
I've written a Jira Java plugin which implements a listener for issue update events.
Once an event occurs, I need to make a REST call to the Jira server, using the current authentication (the user that initiated the issue update).
I know how to make the call to the connected Confluence server, using code similar to this:
String url = "/rest/myrest/1.0/test";
ApplicationLink appLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);
ApplicationLinkRequestFactory applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory();
ApplicationLinkRequest request = applicationLinkRequestFactory.createRequest(MethodType.GET, url);
String response = request.execute();
This works fine and performs the proper authentication.
Is there a service similar to the ApplicationLinkService which can make the authenticated REST call to my own Jira server?
I know I can make a a simple REST call using any Java REST client, but since I don't know the username & password, I can not perform the authentication.
Any ideas?
Thank You,
Shay
Yes, take a look at com.atlassian.sal.api.net.TrustedRequestFactory
Something like this:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.sal.api.net.Request;
import com.atlassian.sal.api.net.TrustedRequest;
import com.atlassian.sal.api.net.TrustedRequestFactory;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
String url = "https://example.com/path?query=value";
URI uri = UriBuilder.fromUri(url).build();
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getComponent(TrustedRequestFactory.class);
TrustedRequest request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url);
request.addTrustedTokenAuthentication(uri.getHost(), currentUser.getName());
String response = request.execute();
Hi @PD Sheehan ,
Thank you for sharing that solution. I tried it in ScriptRunner plugin Console.
TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getComponent(TrustedRequestFactory.class)
When I run the code, a trustedRequestFactory is null. Could you please share how to resolve it, because like in raised question I need to call Jira REST API from ScriptRunner plugin installed on the same Jira instance?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't recall if that code used to work on older Jira versions, or if I just made a typo in my original answer.
There are 2 ways you can get that component from scriptrunner with current versions:
TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)
Or
import com.onresolve.scriptrunner.runner.customisers.PluginModule
@PluginModule TrustedRequestFactory trustedRequestFactory
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot! First solution worked. It's very cool.
TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.