@Override public void handle(ClientResponse<?> response) throws RuntimeException { final int status = response.getStatus(); String message = null; try { message = response.getEntity(String.class); } catch (Exception e) { } finally { response.releaseConnection(); } switch (status) { case HttpURLConnection.HTTP_BAD_REQUEST: throw new BadRequestException(message); case HttpURLConnection.HTTP_INTERNAL_ERROR: throw new InternalServerErrorException(message); case HttpURLConnection.HTTP_UNAUTHORIZED: throw new UnauthorisedException(message); case 426: throw new UpgradeException(message); case HttpURLConnection.HTTP_NOT_FOUND: throw new NotFoundException(message); } } }
protected void cacheIfPossible(final ClientRequest request, final ClientResponse<?> response) throws Exception { if (response.getStatus() == 200) { final String uri = request.getUri(); final String etag = (String) response.getHeaders().getFirst(HttpHeaders.ETAG); final String contentType = (String) response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (etag != null && !etag.trim().isEmpty()) { cache.put(uri, etag, contentType); } } } }
stream.reset(); if (Response.Status.FORBIDDEN.equals(response.getResponseStatus())) { error = "Error handling the Keycloak token, status is FORBIDDEN"; } else if (Response.Status.UNAUTHORIZED.equals(response.getResponseStatus())) { error = "Error handling the Keycloak token, status is UNAUTHORIZED"; } else if (Response.Status.BAD_REQUEST.equals(response.getResponseStatus())) { error = "Error handling the Keycloak token, status is BAD_REQUEST. Response data: " + getResponseData(r); } else if (Response.Status.NOT_FOUND.equals(response.getResponseStatus())) { error = "Error handling the Keycloak token, status is NOT_FOUND."; } else if (!Response.Status.OK.equals(response.getResponseStatus())) { error = "Error handling the Keycloak token. Response status is " + response.getResponseStatus() + ". Response data: " + getResponseData(r); } finally { response.releaseConnection();
public BaseResponse(ClientResponse<String> clientResponse) { if (clientResponse != null) { status = clientResponse.getStatus(); if (clientResponse.getLocationLink() != null) { location = clientResponse.getLocationLink().getHref(); } entity = clientResponse.getEntity(String.class); headers = clientResponse.getMetadata(); } }
public ClientResponseFailure(ClientResponse response) { super("Failed with status: " + response.getStatus()); this.response = BaseClientResponse.copyFromError(response); // release connection just in case it doesn't get garbage collected or manually released response.releaseConnection(); }
@Override public <T> T getEntity(Class<T> returnType) { return (T) response.getEntity(returnType); }
String contentType = String.valueOf(response.getMetadata().getFirst(HttpHeaderNames.CONTENT_TYPE)); if (response.getStatus() == 500) { Exception error = new Exception(Messages.i18n.format("UNKNOWN_SRAMP_ERROR")); //$NON-NLS-1$ if (MediaType.APPLICATION_SRAMP_ATOM_EXCEPTION.equals(contentType)) { try { SrampAtomException entity = response.getEntity(SrampAtomException.class); if (entity != null) error = entity; if (response.getStatus() == 404 || response.getStatus() == 415) { SrampAtomException error = new SrampAtomException(Messages.i18n.format("ENDPOINT_NOT_FOUND")); //$NON-NLS-1$ throw error; if (response.getStatus() == 403) { SrampAtomException error = new SrampAtomException(Messages.i18n.format("AUTHORIZATION_FAILED")); //$NON-NLS-1$ throw error; if (response.getStatus() == 401) { SrampAtomException error = new SrampAtomException(Messages.i18n.format("AUTHENTICATION_FAILED")); //$NON-NLS-1$ throw error;
public String print(ClientResponse<String> response) throws Exception { String result = null; // Check the HTTP status of the request // HTTP 200 indicates the request is OK if (response.getStatus() != 200) { throw new RuntimeException("Failed request with HTTP status: " + response.getStatus()); } // We have a good response, let's now read it BufferedReader br = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(response.getEntity().getBytes()))); // Loop over the br in order to print out the contents System.out.println("\n*** Response from Server ***\n"); String output = null; while ((output = br.readLine()) != null) { System.out.println(output); result = output; } System.out.println("\n==============================================="); return result; }
ActiveMQRestLogger.LOGGER.debug(method + " " + uri); res = request.httpMethod(method); int status = res.getStatus(); ActiveMQRestLogger.LOGGER.debug("Status of push: " + status); if (status == 503) { String retryAfter = res.getStringHeaders().getFirst("Retry-After"); if (retryAfter != null) { wait = Long.parseLong(retryAfter) * 1000; uri = res.getLocation().toString(); wait = 0; } else if ((status >= 200 && status < 299) || status == 303 || status == 304) { } finally { if (res != null) res.releaseConnection();
/** * Helper methods */ private ClientResponse<?> checkResponse(ClientResponse<?> responseObj) throws Exception { responseObj.resetStream(); int status = responseObj.getStatus(); if (status != 200) { logger.warn("Response with exception:\n" + responseObj.getEntity(String.class)); assertEquals("Status OK", 200, status); } return responseObj; }
int status = clientResponse.getStatus(); String entity = clientResponse.getEntity(String.class); getResponse().setEntity(entity); getResponse().setHeaders(clientResponse.getMetadata()); if (StringUtils.isNotBlank(entity)) { List<Object> contentType = clientResponse.getHeaders().get("Content-Type"); if (contentType != null && contentType.contains("application/jwt")) { String[] jwtParts = entity.split("\\.");
private ClientResponse cache(ClientRequest request, ClientResponse response) throws Exception { if (response.getStatus() != 200) return response; return cacheIfPossible(request, (BaseClientResponse) response); }
if (copy.getResponseHeaders().containsKey("Content-Type")) if (copy.getResponseHeaders().containsKey("Content-Type")) try byte[] bytes = (byte[]) copy.getEntity(gt); is = new ByteArrayInputStream(bytes); tmp.status = copy.getStatus(); tmp.providerFactory = ResteasyProviderFactory.getInstance(); tmp.headers = new CaseInsensitiveMap<String>(); tmp.headers.putAll(copy.getResponseHeaders()); tmp.headers.remove("Content-Encoding"); // remove encoding because we will have already extracted byte array return tmp;
protected void initialize() throws Exception { super.start(); initialized = true; initAuthentication(); ClientRequest request = executor.createRequest(registration.getTarget().getHref()); for (XmlHttpHeader header : registration.getHeaders()) { request.header(header.getName(), header.getValue()); } ClientResponse<?> res = request.head(); if (res.getStatus() != 200) { throw new RuntimeException("Failed to query REST destination for init information. Status: " + res.getStatus()); } String url = (String) res.getHeaders().getFirst("msg-create-with-id"); if (url == null) { if (res.getLinkHeader() == null) { throw new RuntimeException("Could not find create-with-id URL"); } Link link = res.getLinkHeader().getLinkByTitle("create-with-id"); if (link == null) { throw new RuntimeException("Could not find create-with-id URL"); } url = link.getHref(); } targetUri = ResteasyUriBuilder.fromTemplate(url); }
if (response.getStatus() == HttpStatus.SC_OK || response.getStatus() == HttpStatus.SC_CREATED || response.getStatus() == HttpStatus.SC_NO_CONTENT || response.getStatus() == HttpStatus.SC_ACCEPTED) { return new RESTEasyResponse(client, response); response.releaseConnection(); throw new OpenStackResponseException(response.getResponseStatus() .getReasonPhrase(), response.getStatus());
@Override public void handle(final ClientResponse<?> response) throws RuntimeException { try { final BaseClientResponse<?> r = (BaseClientResponse<?>) response; final InputStream stream = r.getStreamFactory().getInputStream(); stream.reset(); final Object entity = response.getEntity(Object.class); if (entity instanceof RuntimeException) { final RuntimeException exception = (RuntimeException) entity; throw exception; } if (entity instanceof Exception) { final Exception exception = (Exception) entity; throw new RESTBonitaRuntimeExceptionWrapper(exception); } if (Status.FORBIDDEN.getStatusCode() == response.getResponseStatus().getStatusCode()) { throw new BonitaRuntimeException("Forbidden returned"); } } catch (final IOException e) { new BonitaRuntimeException("Error while reading client response", e); } // RESTEasy will throw the original ClientResponseFailure }
/** * Quietly closes the resteasy client (releases the connection). * @param response */ private void closeQuietly(ClientResponse<?> response) { if (response != null) { response.releaseConnection(); } }
/** * Constructs a register response. */ public RegisterResponse(ClientResponse<String> clientResponse) { super(clientResponse); String entity = clientResponse.getEntity(String.class); setEntity(entity); setHeaders(clientResponse.getMetadata()); injectDataFromJson(entity); }
protected ClientResponseFailure mockForbiddenResponse() { ClientResponseFailure error = mock(ClientResponseFailure.class); ClientResponse response = mock(ClientResponse.class); Response.Status responseStatus = Response.Status.FORBIDDEN; when(error.getResponse()).thenReturn(response); when(response.getResponseStatus()).thenReturn(responseStatus); return error; }
@Override public String header(String name) { return response.getHeaders().getFirst(name).toString(); }