Refine search
private static void decompressResponse(ClientResponse response) { InputStream entityInputStream = response.getEntityInputStream(); GZIPInputStream uncompressedIS; try { uncompressedIS = new GZIPInputStream(entityInputStream); } catch (IOException ex) { try { entityInputStream.close(); } catch (IOException ignored) { } throw new ClientHandlerException(ex); } response.setEntityInputStream(uncompressedIS); }
/** * Performs a GET HTTP operation against the /status endpoint. * * @param runner the runner to perform the status operation on * * @return the result of the operation */ public static Result status( Runner runner ) { preparations( runner ); try { return newStatusOp( runner ).execute( Result.class ); } catch ( ClientHandlerException e ) { if ( e.getCause() instanceof SSLHandshakeException && e.getCause().toString().contains( "PKIX path building failed" ) ) { /* * Oddly this fails the first time but works the second time. Until * I get to the bottom of this and figure it out this is the work * around we will use to make sure this does not fail. We retry once * on the failure. */ return newStatusOp( runner ).execute( Result.class ); } } throw new RuntimeException( "If we got here then the retry also failed." ); }
private void uncompressResponseIfCompressed(final ClientResponse response) { if (response.hasEntity() && response.getHeaders().containsKey(HttpHeaders.CONTENT_ENCODING)) { final String encodings = response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if (encodings.equals(GZIP)) { response.getHeaders().remove(HttpHeaders.CONTENT_ENCODING); try { response.setEntityInputStream(new GZIPInputStream(response.getEntityInputStream())); } catch (final IOException ex) { throw new ClientHandlerException(ex); } } } }
if (clientResponse.getStatus() >= 400) { return clientResponse; Form response = clientResponse.getEntity(Form.class); String token = response.getFirst(OAuthParameters.TOKEN); parameters.token(token); if (clientResponse.getStatus() >= 400) { return clientResponse; throw new ClientHandlerException(se);
private static ClientResponse mockEntityClientResponse( TimelineClientImpl client, ClientResponse.Status status, boolean hasError, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { doThrow(new ClientHandlerException(new ConnectException())).when(client) .doPostingObject(any(TimelineEntities.class), any(String.class)); return response; } doReturn(response).when(client) .doPostingObject(any(TimelineEntities.class), any(String.class)); when(response.getClientResponseStatus()).thenReturn(status); TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError(); error.setEntityId("test entity id"); error.setEntityType("test entity type"); error.setErrorCode(TimelinePutResponse.TimelinePutError.IO_EXCEPTION); TimelinePutResponse putResponse = new TimelinePutResponse(); if (hasError) { putResponse.addError(error); } when(response.getEntity(TimelinePutResponse.class)).thenReturn(putResponse); return response; }
private <T> T getEntity(Class<T> c, Type type) { if (getStatus() == 204) { throw new UniformInterfaceException(this); MediaType mediaType = getType(); if (mediaType == null) { mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE; EMPTY_ANNOTATIONS, mediaType); if (br == null) { close(); String message = "A message body reader for Java class " + c.getName() + ", and Java type " + type + workers.readersToString(m)); throw new ClientHandlerException(message); } catch (IOException ex) { close(); throw new ClientHandlerException(ex);
@Override public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException { final HttpMethod method = getHttpMethod(cr); methodExecutor.executeMethod(method, cr); try { ClientResponse r = new ClientResponse(method.getStatusCode(), getInBoundHeaders(method), new HttpClientResponseInputStream(method), workers); if (!r.hasEntity()) { r.bufferEntity(); r.close(); } return r; } catch (Exception e) { method.releaseConnection(); throw new ClientHandlerException(e); } }
public static ClientResponse mockEntityClientResponse( TimelineWriter spyTimelineWriter, ClientResponse.Status status, boolean hasError, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { doThrow(new ClientHandlerException(new ConnectException())).when( spyTimelineWriter).doPostingObject( any(TimelineEntities.class), any(String.class)); return response; } doReturn(response).when(spyTimelineWriter) .doPostingObject(any(TimelineEntities.class), any(String.class)); when(response.getStatusInfo()).thenReturn(status); TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError(); error.setEntityId("test entity id"); error.setEntityType("test entity type"); error.setErrorCode(TimelinePutResponse.TimelinePutError.IO_EXCEPTION); TimelinePutResponse putResponse = new TimelinePutResponse(); if (hasError) { putResponse.addError(error); } when(response.getEntity(TimelinePutResponse.class)).thenReturn(putResponse); return response; }
@Override public ClientResponse handle(ClientRequest cr) throws ClientHandlerException { URIBuilder uriBuilder = new URIBuilder(cr.getURI()); String path = uriBuilder.getPath(); uriBuilder.setPath(converter.convertCommandPath(path)); try { cr.setURI(uriBuilder.build()); ClientResponse response = getNext().handle(cr); String newEntity = converter.convertResponse(path, response.getEntity(String.class)); response.setEntityInputStream(new ByteArrayInputStream(newEntity.getBytes())); return response; } catch (Exception ex) { throw new ClientHandlerException(ex); } }
if (e.getCause() instanceof ConnectException) { LOG.error("Failed to communicate with REST Server: {} ", this.url); } else { LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], e.getMessage()); if (clientRemoteResponse.getStatus() != HTTP_OK) { throw new UniformInterfaceException(HTTP_ERROR_MSG + clientRemoteResponse.getStatus(), clientRemoteResponse); clientRemoteResponse.close(); LOG.error("REST Server error. Message: {}", e.getMessage()); } catch (ClientHandlerException e) { if (e.getCause() instanceof ConnectException) { LOG.error("Failed to communicate with REST Server: {} ", this.url); } else { LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], e.getMessage()); LOG.error("REST Server error. Message: {}", e.getMessage()); } catch (ClientHandlerException e) { if (e.getCause() instanceof ConnectException) { LOG.error("Failed to communicate with REST Server: {} ", this.url); } else { LOG.error("ClientHandlerException on {}: {}", Thread.currentThread().getStackTrace()[1], e.getMessage());
throw new OpenStackConnectException(e.getMessage(), e); } catch (UniformInterfaceException e) { throw new OpenStackResponseException(e.getResponse().getClientResponseStatus().getReasonPhrase(), e .getResponse().getStatus());
/** * Close the response. * <p> * The entity input stream is closed. * * @throws ClientHandlerException if there is an error closing the response. */ public void close() throws ClientHandlerException { try { entity.close(); } catch (IOException e) { throw new ClientHandlerException(e); } }
private static ClientResponse mockDomainClientResponse( TimelineClientImpl client, ClientResponse.Status status, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { doThrow(new ClientHandlerException(new ConnectException())).when(client) .doPostingObject(any(TimelineDomain.class), any(String.class)); return response; } doReturn(response).when(client) .doPostingObject(any(TimelineDomain.class), any(String.class)); when(response.getClientResponseStatus()).thenReturn(status); return response; }
@Override public String getApplicationToken(String persistenceUnit, String externalProperties) { String applicationToken; try { log.debug("\n\nGetting Application Token..."); WebResource.Builder atBuilder = webResource.path(Constants.KUNDERA_API_PATH + "/application/" + persistenceUnit).accept(mediaType) .type(mediaType); StringBuffer sb = new StringBuffer().append(externalProperties); ClientResponse insertResponse = (ClientResponse) atBuilder.post(ClientResponse.class, sb.toString()); InputStream is = insertResponse.getEntityInputStream(); applicationToken = StreamUtils.toString(is); log.debug("Application Token:" + applicationToken); } catch (UniformInterfaceException e) { log.error("Error during getApplicationToken, Caused by:" + e.getMessage() + ", returning null"); return null; } catch (ClientHandlerException e) { log.error("Error during getApplicationToken, Caused by:" + e.getMessage() + ", returning null"); return null; } return applicationToken; }
/** * Buffer the entity. * <p> * All the bytes of the original entity input stream will be read * and stored in memory. The original entity input stream will * then be closed. * @throws ClientHandlerException if there is an error processing the response. */ public void bufferEntity() throws ClientHandlerException { if (isEntityBuffered) return; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ReaderWriter.writeTo(entity, baos); } catch(IOException ex) { throw new ClientHandlerException(ex); } finally { close(); } entity = new ByteArrayInputStream(baos.toByteArray()); isEntityBuffered = true; }
private static ClientResponse mockDomainClientResponse( TimelineWriter spyTimelineWriter, ClientResponse.Status status, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { doThrow(new ClientHandlerException(new ConnectException())).when( spyTimelineWriter).doPostingObject(any(TimelineDomain.class), any(String.class)); return response; } doReturn(response).when(spyTimelineWriter) .doPostingObject(any(TimelineDomain.class), any(String.class)); when(response.getStatusInfo()).thenReturn(status); return response; }
private void processClientHandlerException(ClientHandlerException e) { if (e.getCause() instanceof java.net.SocketTimeoutException || e.getCause() instanceof java.net.ConnectException) { LOG.error("Connection to {} failed: {}", client.getHost(), e.getMessage()); this.isInitialized = false; throw e; } else { throw e; } }
/** * Exception indicates a technical problem while processing the call on client side and should therefore be logged * with error. A typical cause could be that no connection to the server could be established. * * @param ex the exception of the failed request */ private static void handleException(final ClientHandlerException ex) { LOGGER.error("Request failed: message={}", ex.getMessage()); throw MissingServerConnectionException.createForRequestProcessingFailed(ex.getMessage()); }
private void logResponse(long id, ClientResponse response) { StringBuilder b = new StringBuilder(); printResponseLine(b, id, response); printResponseHeaders(b, id, response.getHeaders()); InputStream stream = response.getEntityInputStream(); try { if (!response.getEntityInputStream().markSupported()) { stream = new BufferedInputStream(stream); response.setEntityInputStream(stream); } stream.mark(maxEntitySize + 1); byte[] entity = new byte[maxEntitySize + 1]; int entitySize = stream.read(entity); if (entitySize > 0) { b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize))); if (entitySize > maxEntitySize) { b.append("...more..."); } b.append('\n'); stream.reset(); } } catch (IOException ex) { throw new ClientHandlerException(ex); } log(b); }
public static ClientResponse mockEntityClientResponse( TimelineWriter spyTimelineWriter, ClientResponse.Status status, boolean hasError, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { doThrow(new ClientHandlerException(new ConnectException())).when( spyTimelineWriter).doPostingObject( any(TimelineEntities.class), any(String.class)); return response; } doReturn(response).when(spyTimelineWriter) .doPostingObject(any(TimelineEntities.class), any(String.class)); when(response.getClientResponseStatus()).thenReturn(status); TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError(); error.setEntityId("test entity id"); error.setEntityType("test entity type"); error.setErrorCode(TimelinePutResponse.TimelinePutError.IO_EXCEPTION); TimelinePutResponse putResponse = new TimelinePutResponse(); if (hasError) { putResponse.addError(error); } when(response.getEntity(TimelinePutResponse.class)).thenReturn(putResponse); return response; }