public Request<?, ?> createRequest(MethodType methodType, String url) throws CredentialsRequiredException { if (requestFactory instanceof RequestFactory) { return ((RequestFactory) requestFactory).createRequest(methodType, url); } return ((ApplicationLinkRequestFactory) requestFactory).createRequest(methodType, url); }
@Override public URI getAuthorisationURI(URI callback) { return delegate.getAuthorisationURI(callback); }
try { permissionState = authenticatedRequestFactory .createRequest(Request.MethodType.GET, url) .executeAndReturn(new ReturningResponseHandler<com.atlassian.sal.api.net.Response, PermissionCode>() { public PermissionCode handle(final com.atlassian.sal.api.net.Response response) throws ResponseException { case AUTHENTICATION_FAILED: case NO_AUTHENTICATION: return response(permissionState, authenticatedRequestFactory.getAuthorisationURI()); default: return response(permissionState);
try { requestFactory .createRequest(Request.MethodType.GET, ENDPOINT) .execute(new ApplicationLinkResponseHandler() { public Void handle(final Response response) throws ResponseException { errorMessage.set(re.getMessage()); final String authorisationURL = requestFactory.getAuthorisationURI() == null ? null : requestFactory.getAuthorisationURI(getCurrentLocation(request)).toString(); return new AuthenticationInformation(username.get(), errorMessage.get(), authorisationURL, status.get());
@Override public ApplicationLinkRequest createRequest(Request.MethodType methodType, String url) throws CredentialsRequiredException { return delegate.createRequest(methodType, url) .addBasicAuthentication(configuration.get("username"), configuration.get("password")); }
public URI getAuthorisationURI(final URI callback) { return requestFactory.getAuthorisationURI(callback); } }
private String getRequestType(ApplicationLinkRequestFactory factory) { ApplicationLinkRequest request; try { request = factory.createRequest(Request.MethodType.GET, "/blah"); } catch (CredentialsRequiredException cre) { // Only 3LO currently throws this exception so we know for sure what it is. // This is the name of class {@link com.atlassian.applinks.oauth.auth.ThreeLeggedOAuthRequest} but // I have to use string literal instead since the pacakge is not exported by AppLink plugin. return "com.atlassian.applinks.oauth.auth.ThreeLeggedOAuthRequest"; } // In case of Trusted App, the request is wrapped multiple times. It's easier to just look at headers. if (request.getHeaders().containsKey("X-Seraph-Trusted-App-Version")) { return TrustedRequest.class.getName(); } return request.getClass().getName(); }
@Override public URI getAuthorisationURI() { return delegate.getAuthorisationURI(); }
/** * This is a convenience method for creating anonymous (unauthenticated) requests without having to catch the * {@link CredentialsRequiredException} declared on the {@link ApplicationLinkRequestFactory} interface * * @param link the {@link ApplicationLink} that is the target of the request * @param methodType The HTTP method type * @param url The url to request * @return the (unauthenticated) request object * @see ApplicationLinkRequestFactory#createRequest */ public static Request createAnonymousRequest(final ApplicationLink link, final Request.MethodType methodType, final String url) { try { return link.createAuthenticatedRequestFactory(Anonymous.class).createRequest(methodType, url); } catch (CredentialsRequiredException e) { throw new RuntimeException("Unexpected CredentialsRequiredException encountered for Anonymous AuthenticationProvider", e); } }
public URI getAuthorisationURI() { return requestFactory.getAuthorisationURI(); }
private Iterable<EntityReference> getEntities(final ApplicationLinkRequestFactory requestFactory) throws CredentialsRequiredException, ResponseException { final Request req = requestFactory.createRequest(Request.MethodType.GET, RestUtil.REST_APPLINKS_URL + EntityResource.CONTEXT); final List<EntityReference> entities = new ArrayList<EntityReference>(); req.execute(new ResponseHandler<Response>() { public void handle(final Response response) throws ResponseException { if (response.getStatusCode() == 200) { Iterables.addAll(entities, response.getEntity(ReferenceEntityList.class).getEntities(typeAccessor)); } else { throw new ResponseException(String.format("Failed to retrieve entity list, received %s response: %s", response.getStatusCode(), response.getStatusText())); } } }); return entities; }
appName = appLink.getName(), appUri = appLink.getDisplayUrl().toString(), authUri = appLink.createAuthenticatedRequestFactory().getAuthorisationURI().toString();
private static ApplicationLinkRequest createRequest(final ApplicationLink applicationLink, final String bodyXml) throws CredentialsRequiredException { final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); final ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.POST, XMLRPC_URL); request.setRequestBody(bodyXml, MediaType.APPLICATION_XML); return request; }
/** * Execute a Get Request on the {@link ApplicationLink} given * * @param url the target service url * @param timeout timeout in milliseconds * @return the {@link RestResponse} response * @throws CredentialsRequiredException in case the {@link ApplicationLink} credentials are outdated */ public RestResponse executeGetRequest(String url, int timeout) throws CredentialsRequiredException { ApplicationLinkRequestFactory requestFactory = getApplicationLinkRequestFactory(); return executeRequest(requestFactory.createRequest(Request.MethodType.GET, url), requestFactory, timeout); }
/** * Request jira by anonymous user * * @param appLink jira server app link * @param url rest url * @return response */ private String requestJiraByAnonymousUser(ReadOnlyApplicationLink appLink, String url) { try { final ApplicationLinkRequestFactory requestFactory = appLink.createAuthenticatedRequestFactory(Anonymous.class); ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, url); return request.execute(); } catch (Exception e) { LOG.info("Can not retrieve data from jira server by anonymous user", e); return null; } }
private String getAuthenticatedUsername(ApplicationLink appLink) throws ResponseException { ApplicationLinkRequestFactory requestFactory = appLink.createAuthenticatedRequestFactory(OAuthAuthenticationProvider.class); try { ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, "/plugins/servlet/applinks/whoami"); String response = request.execute(new ApplicationLinkResponseHandler<String>() { public String handle(final Response response) throws ResponseException { return response.getResponseBodyAsString(); } public String credentialsRequired(final Response response) throws ResponseException { return null; } }); if (response != null && response.equals("")) { response = null; } return response; } catch (CredentialsRequiredException e) { return null; } }
private static ApplicationLinkRequest createGetURLRequest(final ApplicationLink applicationLink, final String url, final Map<String, String> params) throws CredentialsRequiredException { final UrlBuilder urlBuilder = new UrlBuilder(url); if (params != null) { for (final Map.Entry<String, String> param : params.entrySet()) { urlBuilder.addParameter(param.getKey(), param.getValue()); } } final String restUrl = urlBuilder.asUrlString(); final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); return requestFactory.createRequest(Request.MethodType.GET, restUrl); }
/** * Execute a Get Request on the {@link ApplicationLink} given * * @param url the target services url * @param headers request headers * @param timeout timeout in milliseconds * @return the {@link RestResponse} response * @throws com.atlassian.applinks.api.CredentialsRequiredException * in case the {@link ApplicationLink} credentials are outdated */ private RestResponse executeGetRequest(String url, Map<String, String> headers, int timeout) throws CredentialsRequiredException { ApplicationLinkRequestFactory requestFactory = getAuthenticatedRequestFactory(); ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, url); for (Map.Entry<String, String> header : headers.entrySet()) { request.addHeader(header.getKey(), header.getValue()); } return executeRequest(request, requestFactory, timeout); }
public ApplicationLinkRequest createRequest(final Request.MethodType methodType, final String uri) throws CredentialsRequiredException { checkNotNull(uri); boolean isAbsoluteUri = false; try { isAbsoluteUri = new URI(uri).isAbsolute(); } catch (URISyntaxException e) { log.warn("Couldn't parse uri '{}' supplied to RequestFactory.createRequest(), assuming relative.", uri); } final URI updatedUri; try { if (isAbsoluteUri) { updatedUri = new URI(uri); } else { updatedUri = new URI(link.getRpcUrl() + (uri.startsWith("/") ? uri : "/" + uri)); } checkArgument(SameOrigin.isSameOrigin(updatedUri, link.getRpcUrl()), "Request url '%s' isn't in the same origin as the rpc url '%s'", updatedUri, link.getRpcUrl()); checkArgument(updatedUri.getPath().startsWith(link.getRpcUrl().getPath()), "Request url '%s' doesn't match rpc url '%s'", updatedUri, link.getRpcUrl()); } catch(MalformedURLException | URISyntaxException e) { throw new IllegalArgumentException(e); } return requestFactory.createRequest(methodType, updatedUri.toString()); }
/** * Request jira by login user * * @param appLink jira server app link * @param url rest url * @return response * @throws CredentialsRequiredException when user is not mapping * @throws ResponseException when have problem request jira server */ private String requestJiraByAuthenticatedUser(ReadOnlyApplicationLink appLink, String url) throws CredentialsRequiredException, ResponseException { final ApplicationLinkRequestFactory requestFactory = appLink.createAuthenticatedRequestFactory(); ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, url); return request.execute(); } }