@Override public String apply(Response input) { log.debug("Returned ok content from: {}", url); return input.getEntity(); } }
@Override public String apply(Response input) { log.debug("Returned others: {}", url); if ("application/json".equalsIgnoreCase(input.getContentType())) { throw new ContentRetrievalException(ContentRetrievalErrors.fromJson(input.getEntity())); } else { log.debug("An unknown error occurred retrieving HTTP content. Status is {}, body content " + "is:\n{}\n", input.getStatusCode(), input.getEntity()); throw new ContentRetrievalException("Unknown error. Status is " + input.getStatusCode()); } } }
private void callSyncHandler(String addonKey, final boolean addonUsesJwtAuthentication, URI callbackUri, String jsonEventData, Optional<String> authHeader) throws LifecycleCallbackException { // try distributing prod shared secrets over http (note the lack of "s") and it shall be rejected if (!systemPropertyService.isDevMode() && !systemPropertyService.isHttpAllowedMode() && addonUsesJwtAuthentication && !callbackUri.getScheme().toLowerCase().startsWith("https")) { String message = String.format("Cannot issue callback except via HTTPS. Current URL = '%s'", callbackUri); throw new LifecycleCallbackException(message, "connect.remote.upm.install.exception"); } Response response = getSyncHandlerResponse(addonKey, callbackUri, jsonEventData, authHeader); final int statusCode = response.getStatusCode(); // a selection of 2xx response codes both indicate success and are semantically valid for this callback if (!OK_INSTALL_HTTP_CODES.contains(statusCode)) { String statusText = response.getStatusText(); String responseEntity = response.getEntity(); // calling response.getEntity() multiple times results in IllegalStateException("Entity may only be accessed once") log.error("Error contacting remote application at " + callbackUri + " " + statusCode + ":[" + statusText + "]:" + responseEntity); String message = "Error contacting remote application " + statusCode + ":[" + statusText + "]:" + responseEntity; throw new LifecycleCallbackHttpCodeException(message, findI18nKeyForHttpErrorCode(statusCode), statusCode); } }
private void callSyncHandler(String addonKey, final boolean addonUsesJwtAuthentication, URI callbackUri, String jsonEventData, Optional<String> authHeader) throws LifecycleCallbackException { // try distributing prod shared secrets over http (note the lack of "s") and it shall be rejected if (!systemPropertyService.isDevMode() && !systemPropertyService.isHttpAllowedMode() && addonUsesJwtAuthentication && !callbackUri.getScheme().toLowerCase().startsWith("https")) { String message = String.format("Cannot issue callback except via HTTPS. Current URL = '%s'", callbackUri); throw new LifecycleCallbackException(message, "connect.remote.upm.install.exception"); } Response response = getSyncHandlerResponse(addonKey, callbackUri, jsonEventData, authHeader); final int statusCode = response.getStatusCode(); // a selection of 2xx response codes both indicate success and are semantically valid for this callback if (!OK_INSTALL_HTTP_CODES.contains(statusCode)) { String statusText = response.getStatusText(); String responseEntity = response.getEntity(); // calling response.getEntity() multiple times results in IllegalStateException("Entity may only be accessed once") log.error("Error contacting remote application at " + callbackUri + " " + statusCode + ":[" + statusText + "]:" + responseEntity); String message = "Error contacting remote application " + statusCode + ":[" + statusText + "]:" + responseEntity; throw new LifecycleCallbackHttpCodeException(message, findI18nKeyForHttpErrorCode(statusCode), statusCode); } }
@Override public T handle(Response response) throws JSONException, IOException { final String body = response.getEntity(); return (T) (parser instanceof JsonObjectParser ? ((JsonObjectParser) parser).parse(new JSONObject(body)) : ((JsonArrayParser) parser).parse(new JSONArray(body))); } };
@Override public T handle(Response response) throws JSONException, IOException { final String body = response.getEntity(); return (T) (parser instanceof JsonObjectParser ? ((JsonObjectParser) parser).parse(new JSONObject(body)) : ((JsonArrayParser) parser).parse(new JSONArray(body))); } };
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(); } }
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 )); } }
@Override public T apply(Response response) { try { final String body = response.getEntity(); final Collection<ErrorCollection> errorMessages = extractErrors(response.getStatusCode(), body); throw new RestClientException(errorMessages, response.getStatusCode()); } catch (JSONException e) { throw new RestClientException(e, response.getStatusCode()); } } };
@Override public T apply(Response response) { try { final String body = response.getEntity(); final Collection<ErrorCollection> errorMessages = extractErrors(response.getStatusCode(), body); throw new RestClientException(errorMessages, response.getStatusCode()); } catch (JSONException e) { throw new RestClientException(e, response.getStatusCode()); } } };
@Override public Promise<Iterable<Transition>> getTransitions(final URI transitionsUri) { return callAndParse(client().newRequest(transitionsUri).get(), (ResponseHandler<Iterable<Transition>>) response -> { final JSONObject jsonObject = new JSONObject(response.getEntity()); if (jsonObject.has("transitions")) { return JsonParseUtil.parseJsonArray(jsonObject.getJSONArray("transitions"), transitionJsonParserV5); } else { final Collection<Transition> transitions = new ArrayList<>(jsonObject.length()); @SuppressWarnings("unchecked") final Iterator<String> iterator = jsonObject.keys(); while (iterator.hasNext()) { final String key = iterator.next(); try { final int id = Integer.parseInt(key); final Transition transition = transitionJsonParser.parse(jsonObject.getJSONObject(key), id); transitions.add(transition); } catch (JSONException e) { throw new RestClientException(e); } catch (NumberFormatException e) { throw new RestClientException( "Transition id should be an integer, but found [" + key + "]", e); } } return transitions; } } ); }
@Override public Either<ClientError, T> apply(Response r) { final String json = r.getEntity(); try { final JsonParser jsonParser = getJsonParser(json); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { if ("error".equals(jsonParser.getCurrentName())) { jsonParser.nextValue(); return Either.left(jsonParser.readValueAs(ClientError.class)); } } } catch (IOException e) { return Either.left(new ClientError(e)); } return Either.left(newJsonParsingClientError(json)); } }
@Override public Either<ClientError, T> apply(Response r) final String json = r.getEntity(); logger.debug("Parsing JSON: {}", json); try
try { response = responsePromise.claim(); responseBody = response.getEntity(); } catch (Exception e) { final long elapsedMillisecs = stopWatch.getTime();
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); } } }