@Override public Promise<T> apply(final ResponsePromise responsePromise) { return responsePromise.transform(this); }
private int getUrlStatusCode(String url) { return httpClient.newRequest(url).get().claim().getStatusCode(); } }
public static String rawHttp(final String url) throws Exception { final DefaultHttpClient<Void> httpClient = createDefaultHttpClient(url); try { final Header authenticateHeader = BasicScheme.authenticate(Users.getDefaultAdminCredentials(), "UTF-8", false); final Request request = httpClient.newRequest(url); request.setHeader(authenticateHeader.getName(), authenticateHeader.getValue()); final Response r = request.get().get(); return r.getEntity(); } finally { httpClient.destroy(); } }
.then(new Promise.TryConsumer<Response>() { @Override public void accept(@Nonnull Response response) {
public static void updateOndemandLicense(final JiraTestedProduct jira, final String license) throws Exception { final String baseUrl = jira.getProductInstance().getBaseUrl(); final DefaultHttpClient<Void> httpClient = createDefaultHttpClient(baseUrl); final long debugExecutionStart = System.currentTimeMillis(); try { final Header authenticateHeader = BasicScheme.authenticate(Users.getDefaultSysAdminCredentials(), "UTF-8", false); final Request request = httpClient.newRequest(baseUrl + "/rest/ondemand/license/latest/license"); request.setEntity(license).setContentType("text/plain") .setHeader(authenticateHeader.getName(), authenticateHeader.getValue()); final Response r = request.put().get(); if (r.getStatusCode() != Status.OK.getStatusCode()) { throw new IllegalStateException("Error updating OD license " + request.getUri() + ", status code =" + r.getStatusCode() + ": " + r.getEntity()); } } finally { httpClient.destroy(); final long debugExecutionEnd = System.currentTimeMillis(); System.out.println(String.format( "DEBUG com.atlassian.jira.utils.OndemandEnvironmentTestUtils.updateOndemandLicense execution took %.3f s", (debugExecutionEnd - debugExecutionStart) / 1000.0 )); } }
.fail(new FailFunction(url)) .build(); return request.execute(METHOD_MAPPING.get(method)).transform(responseTransformation);
private Response getSyncHandlerResponse(String addonKey, URI callbackUri, String jsonEventData, Optional<String> authHeader) throws LifecycleCallbackException { try { Request.Builder request = httpClient.newRequest(callbackUri); request.setAttribute("purpose", "web-hook-notification"); request.setAttribute("pluginKey", addonKey); request.setContentType(MediaType.APPLICATION_JSON); request.setEntity(jsonEventData); if (authHeader.isPresent()) { request.setHeader(AUTHORIZATION_HEADER, authHeader.get()); } request.setHeader(HttpHeaderNames.ATLASSIAN_CONNECT_VERSION, getConnectPluginVersion()); return request.execute(Request.Method.POST).claim(); } catch (Exception e) { log.error("Error contacting remote application at " + callbackUri + " [" + e.getMessage() + "]", e); String message = "Error contacting remote application [" + e.getMessage() + "]"; Throwable cause = e.getCause(); if (cause instanceof UnknownHostException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.bad.domain", callbackUri.getHost()); } else if (cause instanceof SocketTimeoutException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.timeout", removeQuery(callbackUri)); } else if (cause instanceof SSLException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.ssl", removeQuery(callbackUri), cause.getMessage()); } throw new LifecycleCallbackException(message, "connect.remote.upm.install.exception"); } }
private DownloadResult<I18nKeysMapping> download(DownloadResult<ResponsePromise> downloadInProgress) throws TranslationsDownloadException { URI uri = downloadInProgress.getUri(); try { Response response = downloadInProgress.getResult().get(); if (response.getStatusCode() != OK.getStatusCode()) { throw new TranslationsDownloadException(uri, "Expected 200 status code but got " + response.getStatusCode(), i18nResolver.getText("connect.install.error.i18n.download.status", response.getStatusCode())); } Map<String, String> translations = parseResponse(uri, response); validator.validate(uri, translations); return new DownloadResult<>(downloadInProgress.getLanguage(), uri, I18nKeysMapping.of(translations)); } catch (InterruptedException | ExecutionException e) { log.info("Downloading " + uri + " failed", e); throw new TranslationsDownloadException(e, uri, e.getMessage(), i18nResolver.getText("connect.install.error.i18n.download.fail")); } }
.fail(new FailFunction(url)) .build(); return request.execute(METHOD_MAPPING.get(method)).transform(responseTransformation);
private Response getSyncHandlerResponse(String addonKey, URI callbackUri, String jsonEventData, Optional<String> authHeader) throws LifecycleCallbackException { try { Request.Builder request = httpClient.newRequest(callbackUri); request.setCacheDisabled(); request.setAttribute("purpose", "web-hook-notification"); request.setAttribute("pluginKey", addonKey); request.setContentType(MediaType.APPLICATION_JSON); request.setEntity(jsonEventData); if (authHeader.isPresent()) { request.setHeader(AUTHORIZATION_HEADER, authHeader.get()); } request.setHeader(HttpHeaderNames.ATLASSIAN_CONNECT_VERSION, getConnectPluginVersion()); return request.execute(Request.Method.POST).claim(); } catch (Exception e) { log.error("Error contacting remote application at " + callbackUri + " [" + e.getMessage() + "]", e); String message = "Error contacting remote application [" + e.getMessage() + "]"; Throwable cause = e.getCause(); if (cause instanceof UnknownHostException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.bad.domain", callbackUri.getHost()); } else if (cause instanceof SocketTimeoutException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.timeout", removeQuery(callbackUri)); } else if (cause instanceof SSLException) { throw new LifecycleCallbackBadResponseException(message, "connect.install.error.remote.host.ssl", removeQuery(callbackUri), cause.getMessage()); } throw new LifecycleCallbackException(message, "connect.remote.upm.install.exception"); } }
protected final <T> Promise<T> callAndParse(final ResponsePromise responsePromise, final ResponseHandler<T> responseHandler) { final Function<Response, ? extends T> transformFunction = toFunction(responseHandler); return new DelegatingPromise<T>(responsePromise.<T>transform() .ok(transformFunction) .created(transformFunction) .others(AbstractAsynchronousRestClient.<T>errorFunction()) .toPromise()); }
public Either<MarketplaceInstallationError, String> getDescriptorFromMarketplace(String addonKey) { try { Option<Addon> maybeAddon = MarketplaceClientFactory .createMarketplaceClient(URI.create(getMpacUrl())) .addons() .getByKey(addonKey, AddonQuery.builder().withVersion(true).hosting(Option.some(HostingType.CLOUD)).build()); if (maybeAddon.isEmpty()) { return Either.left(MarketplaceInstallationError.NOT_FOUND); } Option<AddonVersion> maybeVersion = maybeAddon.get().getVersion(); if (maybeVersion.isEmpty()) { return Either.left(MarketplaceInstallationError.NO_PUBLIC_VERSION_AVAILABLE); } URI descriptorUri = maybeVersion.get().getArtifactUri().get(); Request.Builder request = httpClient.newRequest(descriptorUri); String json = request.execute(Request.Method.GET).claim().getEntity(); return Either.right(json); } catch (Exception e) { log.error("Exception when retrieving add-on from Marketplace", e); return Either.left(MarketplaceInstallationError.COMMUNICATION_ERROR); } } }
protected final <T> Promise<T> callAndParse(final ResponsePromise responsePromise, final ResponseHandler<T> responseHandler) { final Function<Response, T> transformFunction = toFunction(responseHandler); final ResponseTransformation<Object> responseTransformation = DefaultResponseTransformation.builder() .ok(transformFunction) .created(transformFunction) .others(AbstractAsynchronousRestClient.errorFunction()) .build(); return new DelegatingPromise(responsePromise.transform(responseTransformation)); }
@Override public Promise<Either<ClientError, List<Room>>> list() { return httpClient.newRequest("/rooms/list").get().<Either<ClientError, List<Room>>>transform() .ok(parseJson("rooms", new TypeReference<List<Room>>(){})) .others(ClientUtils.<List<Room>>error()) .fail(ClientUtils.<List<Room>>throwable()) .toPromise(); }
@Override public Promise<Either<ClientError, List<User>>> list() { return httpClient.newRequest("/users/list").get().<Either<ClientError, List<User>>>transform() .ok(parseJson("users", new TypeReference<List<User>>(){})) .others(ClientUtils.<List<User>>error()) .fail(ClientUtils.<List<User>>throwable()) .toPromise(); } }
@Override public Promise<Either<ClientError, Room>> create(String name, long ownerId, Option<Boolean> privacy, Option<String> topic, Option<Boolean> enableGuestAccess) { // TODO validate params final Map<String, String> params = createParametersAsMap(name, ownerId, privacy, topic, enableGuestAccess); return httpClient.newRequest("/rooms/create", "application/x-www-form-urlencoded", urlEncode(params)) .post().<Either<ClientError, Room>>transform() .ok(parseJson("room", Room.class)) .others(ClientUtils.<Room>error()) .fail(ClientUtils.<Room>throwable()) .toPromise(); }
protected final Promise<Void> call(final ResponsePromise responsePromise) { return new DelegatingPromise<Void>(responsePromise.<Void>transform() .ok(constant((Void) null)) .created(constant((Void) null)) .noContent(constant((Void) null)) .others(AbstractAsynchronousRestClient.<Void>errorFunction()) .toPromise()); }
protected final Promise<Void> call(final ResponsePromise responsePromise) { final ResponseTransformation<Object> responseTransformation = DefaultResponseTransformation.builder() .ok(constant((Void) null)) .created(constant((Void) null)) .noContent(constant((Void) null)) .others(AbstractAsynchronousRestClient.errorFunction()) .build(); return new DelegatingPromise(responsePromise.transform(responseTransformation)); }
@Override public Promise<Either<ClientError, Boolean>> canAuthenticate(Option<String> token) { final String uri = token.fold( Suppliers.ofInstance("/"), new Function<String, String>() { @Override public String apply(String t) { return "/?auth_token=" + t; } } ); return httpClient.newRequest(uri).get().<Either<ClientError, Boolean>>transform() .ok(constantRight(Boolean.TRUE)) .unauthorized(constantRight(Boolean.FALSE)) .others(ClientUtils.<Boolean>error()) .fail(ClientUtils.<Boolean>throwable()) .toPromise(); }
@Override public Promise<Either<ClientError, Status>> message(String roomIdOrName, String from, String message, Option<Message.Format> format, Option<Message.BackgroundColor> color, Option<Boolean> notify) { // TODO: validate params final Map<String, String> params = messageParametersAsMap(roomIdOrName, from, message, format, color, notify); return httpClient.newRequest("/rooms/message", "application/x-www-form-urlencoded", urlEncode(params)) .post().<Either<ClientError, Status>>transform() .ok(constantRight(Status.SENT)) .others(ClientUtils.<Status>error()) .fail(ClientUtils.<Status>throwable()) .toPromise(); }