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 ApplicationLinkRequest createRequest(Request.MethodType methodType, String url) throws CredentialsRequiredException { return delegate.createRequest(methodType, url) .addBasicAuthentication(configuration.get("username"), configuration.get("password")); }
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(); }
/** * 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); } }
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 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); }
/** * 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(); } }
@Override public RemoteResponse<String> getPageId(final ApplicationLink applicationLink, String pageUrl) throws CredentialsRequiredException, ResponseException { final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); // Strip fragment identifier final int hash = pageUrl.indexOf('#'); if (hash >= 0) { pageUrl = pageUrl.substring(0, hash); } final ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, pageUrl); request.setFollowRedirects(false); return request.execute(new PageResponseHandler(requestFactory)); }
private Collection<RemoteProject> getRemoteProjects(final ApplicationLink applicationLink) throws CredentialsRequiredException, ResponseException { ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); ApplicationLinkRequest req = requestFactory.createRequest(Request.MethodType.GET, "/rest/api/2/project"); return parseProjects(req.execute(), applicationLink); }
private static ApplicationLinkRequest createCreateRemoteIssueLinkRequest(final ApplicationLink applicationLink, final String issueKey) throws CredentialsRequiredException { final UrlBuilder urlBuilder = new UrlBuilder(REST_BASE_URL) .addPath(issueKey) .addPathUnsafe(REMOTE_LINK_RESOURCE); final String restUrl = urlBuilder.asUrlString(); final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); return requestFactory.createRequest(Request.MethodType.POST, restUrl); }
@Override public boolean isJenkinsPluginInstalled() { try { ApplicationLinkRequestFactory requestFactory = getApplicationLinkRequestFactory(); ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, "/jenkins-jira-plugin/static/help.html"); RestResponse response = executeRequest(request, requestFactory, CALL_TIMEOUT); LOGGER.debug("Backlink support check on {}; code: {}", applicationLink.getName(), response.getStatusCode()); return response.getStatusCode() / 100 == 2; } catch (CredentialsRequiredException e) { LOGGER.error("Authentication failure on {} [{}]: {}", new Object[] { applicationLink.getName(), applicationLink.getDisplayUrl(), e.getMessage() }); } return false; }
private AuthorisationUriAwareRequest buildRequest(@Nonnull ApplicationLinkRequestFactory requestFactory) throws CredentialsRequiredException { ApplicationLinkRequest request = requestFactory .createRequest(methodType, url.toString()) .setHeader(HttpHeaders.ACCEPT, accept) .addHeader(OVERRIDE_HEADER_NAME, OVERRIDE_HEADER_VALUE) .setConnectionTimeout(connectionTimeoutMillis) .setSoTimeout(socketTimeoutMillis) .setFollowRedirects(followRedirects); if (body != null && isMutatingMethodType()) { request.setEntity(body); request.setHeader(HttpHeaders.CONTENT_TYPE, contentType); } for (Map.Entry<String, String> header : headers.entrySet()) { request.setHeader(header.getKey(), header.getValue()); } return new DefaultAuthorisationUriAwareRequest(request, requestFactory); }
private <T> Either<NegativeResponseStatus, T> callRestService(Request.MethodType method, final String path, final AbstractJsonResponseHandler<T> handler) { final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); try { ApplicationLinkRequest request = requestFactory.createRequest(method, path); handler.modifyRequest(request); return request.execute(handler); } catch (CredentialsRequiredException ex) { return Either.left(NegativeResponseStatus.authorizationRequired(jiraLocation)); } catch (ResponseException e) { log.error(String.format("Failed to transform response from Application Link: %s (%s)", jiraLocation.getId(), e.getMessage())); return Either.left(NegativeResponseStatus.communicationFailed(jiraLocation)); } }
private static ApplicationLinkRequest createGetIssueRequest(final ApplicationLink applicationLink, final String issueKey, final RestVersion restVersion) throws CredentialsRequiredException { final String restUrl = new UrlBuilder(REST_BASE_URL) .addPathUnsafe(restVersion.toString()) .addPathUnsafe(ISSUE_RESOURCE) .addPath(issueKey) .asUrlString(); final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); return requestFactory.createRequest(Request.MethodType.GET, restUrl); }
private static ApplicationLinkRequest createCreateRemoteIssueLinkRequest(final ApplicationLink applicationLink, final String issueKey, final RestVersion restVersion) throws CredentialsRequiredException { final UrlBuilder urlBuilder = new UrlBuilder(REST_BASE_URL) .addPathUnsafe(restVersion.toString()) .addPathUnsafe(ISSUE_RESOURCE) .addPath(issueKey) .addPathUnsafe(REMOTE_LINK_RESOURCE); final String restUrl = urlBuilder.asUrlString(); final ApplicationLinkRequestFactory requestFactory = applicationLink.createAuthenticatedRequestFactory(); return requestFactory.createRequest(Request.MethodType.POST, restUrl); }
private RemoteRequestResult executeRequest() { ApplicationLinkRequestFactory requestFactory = link.createAuthenticatedRequestFactory(); try { ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, url); request.execute(new TransferringResponseHandler(output)); return RemoteRequestResult.successful(url, output); } catch (CredentialsRequiredException e) { return RemoteRequestResult.unauthorized(url, UnauthorizedApplink.from(link, requestFactory)); } catch (OAuthResponseException e) { return RemoteRequestResult.unauthorized(url, UnauthorizedApplink.from(link, requestFactory)); } catch (EmptyResponseException e) { return RemoteRequestResult.error(url); } catch (ResponseException e) { return RemoteRequestResult.error(url); } } }
private RemoteRequestResult executeRequest() { ApplicationLinkRequestFactory requestFactory = link.createAuthenticatedRequestFactory(); try { ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.GET, url); request.execute(new TransferringResponseHandler(output)); return RemoteRequestResult.successful(url, output); } catch (CredentialsRequiredException e) { return RemoteRequestResult.unauthorized(url, UnauthorizedApplink.from(link, requestFactory)); } catch (OAuthResponseException e) { return RemoteRequestResult.unauthorized(url, UnauthorizedApplink.from(link, requestFactory)); } catch (EmptyResponseException e) { return RemoteRequestResult.error(url); } catch (ResponseException e) { return RemoteRequestResult.error(url); } } }