public DefaultRawResponse(@Nonnull Response result, long maxBytes) { this.result = requireNonNull(result, "result"); this.statusCode = result.getStatusCode(); this.maxBytes = maxBytes; }
@Override public Response apply(Response response) { log.debug("An unknown error occurred retrieving HTTP content from: {}. Status is {}", url, response.getStatusCode()); throw new ContentRetrievalException("Unknown error. Status is " + response.getStatusCode(), response); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Unexpected response '").append(response.getStatusCode()).append("' with message '"); sb.append(response.getStatusText()).append("'"); return sb.toString(); } }
@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 O apply(Response response) { final int statusCode = response.getStatusCode(); final Map<StatusRange, Function<Response, ? extends O>> matchingFunctions = Maps.filterKeys(functions, input -> input.isIn(statusCode)); if (matchingFunctions.isEmpty()) { if (othersFunction != null) { return othersFunction.apply(response); } else { throw new IllegalStateException("Could not match any function to status " + statusCode); } } else if (matchingFunctions.size() > 1) { throw new IllegalStateException("Found multiple functions for status " + statusCode); } else { // when there we found one and only one function! return Iterables.getLast(matchingFunctions.values()).apply(response); } }
@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 int getUrlStatusCode(String url) { return httpClient.newRequest(url).get().claim().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 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")); } }
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); } }
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 )); } }