List of usage examples for org.springframework.http.client ClientHttpResponse getBody
InputStream getBody() throws IOException;
From source file:fr.itldev.koya.services.impl.util.AlfrescoRestErrorHandler.java
@Override public boolean hasError(ClientHttpResponse clienthttpresponse) throws IOException { if (clienthttpresponse.getStatusCode() != HttpStatus.OK) { logger.warn(/*from ww w . ja v a 2 s . co m*/ "Status code: " + clienthttpresponse.getStatusCode() + " - " + clienthttpresponse.getBody()); return true; } return false; }
From source file:com.pepaproch.gtswsdl.LoginRequestInterceptor.java
private ClientHttpResponse logResponse(ClientHttpResponse response) throws IOException { ClientHttpResponse result = new ClientHttpResponse() { InputStream in;//from w w w . j a va 2 s .co m InputStream oldIn = response.getBody(); String jsonResponse; @Override public HttpStatus getStatusCode() throws IOException { return response.getStatusCode(); } @Override public int getRawStatusCode() throws IOException { return response.getRawStatusCode(); } @Override public String getStatusText() throws IOException { return response.getStatusText(); } @Override public void close() { try { Logger.getLogger(LoginRequestInterceptor.class).log(Level.DEBUG, jsonResponse); in.close(); oldIn.close(); } catch (IOException ex) { java.util.logging.Logger.getLogger(LoginRequestInterceptor.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } } @Override public InputStream getBody() throws IOException { if (in == null) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte[] readBuffer = new byte[4 * 1024]; int lenght; while ((lenght = oldIn.read(readBuffer)) != -1) { baos.write(readBuffer, 0, lenght); } baos.flush(); jsonResponse = baos.toString(); in = new ByteArrayInputStream(baos.toByteArray()); } } return in; } @Override public HttpHeaders getHeaders() { return response.getHeaders(); } }; return result; }
From source file:org.echocat.marquardt.authority.AuthorityIntegrationTest.java
private void doPost(final String url, final Object content) throws Exception { final byte[] bytes; try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { _objectMapper.writeValue(byteArrayOutputStream, content); byteArrayOutputStream.flush();//from ww w .j av a 2 s . co m bytes = byteArrayOutputStream.toByteArray(); } final URI urlToPost = new URI(url); final ClientHttpRequest request = new OkHttpClientHttpRequestFactory().createRequest(urlToPost, HttpMethod.POST); request.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); request.getHeaders().add(X_CERTIFICATE.getHeaderName(), Base64.encodeBase64URLSafeString(CERTIFICATE)); request.getBody().write(bytes); final ClientHttpResponse response = request.execute(); _status = response.getStatusCode().value(); try (final InputStream inputStream = response.getBody()) { try (final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8)) { _response = CharStreams.toString(inputStreamReader); } } }
From source file:org.springframework.social.lastfm.api.impl.LastFmErrorHandler.java
private Map<Integer, String> extractErrorDetailsFromResponse(ClientHttpResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); try {/* w ww . ja v a 2 s . c om*/ String json = readFully(response.getBody()); Map<String, String> responseMap = mapper.<Map<String, String>>readValue(json, new TypeReference<Map<String, String>>() { }); if (responseMap.containsKey("error")) { Map<Integer, String> errorMap = new HashMap<Integer, String>(); errorMap.put(new Integer(responseMap.get("error")), responseMap.get("message")); return errorMap; } else { return null; } } catch (JsonParseException e) { return null; } }
From source file:cz.jirutka.spring.http.client.cache.internal.SizeLimitedHttpResponseReader.java
/** * Reads the original {@link ClientHttpResponse} to memory, if possible, * and returns a serializable copy. If the response's body size exceeds the * specified {@code maxBodySize} limit, then it throws * {@link ResponseSizeLimitExceededException} with a reconstructed response * that combines an already read bytes in memory and the original response. * * @param response The original response to read. * @return An in-memory copy of the original response. * @throws ResponseSizeLimitExceededException When the response's body size * exceeds the specified {@code maxBodySize} limit. * @throws IOException//from ww w . ja va2s . c o m */ public InMemoryClientHttpResponse readResponse(ClientHttpResponse response) throws ResponseSizeLimitExceededException, IOException { Assert.notNull(response, "response must not be null"); InputStream bodyStream = response.getBody(); ByteArrayOutputStream out = new ByteArrayOutputStream(initialCapacity()); long bytesTotal = 0; byte[] buffer = new byte[bufferSize]; int bytesRead = -1; while ((bytesRead = bodyStream.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); bytesTotal += bytesRead; if (bytesTotal > maxBodySize - 1) { throw new ResponseSizeLimitExceededException(createCombinedResponse(response, out, bodyStream)); } } response.close(); return createInMemoryResponse(response, out); }
From source file:org.springframework.social.exfm.api.impl.ExFmErrorHandler.java
private Status extractErrorDetailsFromResponse(ClientHttpResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); try {/* w w w . j a v a2s . co m*/ String json = readFully(response.getBody()); Status responseStatus = mapper.<Status>readValue(json, new TypeReference<Status>() { }); if (!responseStatus.getStatus_code().equals("200")) { return responseStatus; } else { return null; } } catch (JsonParseException e) { return null; } }
From source file:org.bremersee.common.web.client.ResponseErrorHandlerImpl.java
/** * Find a throwable instance by parsing the response to a {@link ThrowableMessageDto}. * * @param response the HTTP response as XML * @return the throwable// ww w. java2s . co m * @throws IOException if the response cannot be parsed */ protected Throwable findThrowableFromXml(final ClientHttpResponse response) throws IOException { final ThrowableMessageDto dto = (ThrowableMessageDto) unmarshaller .unmarshal(new StreamSource(response.getBody())); if (log.isDebugEnabled()) { log.debug("Got throwable from response xml body: " + dto); } return ExceptionRegistry.getExceptionByDto(dto); }
From source file:org.springframework.social.mixcloud.api.impl.MixcloudErrorHandler.java
@SuppressWarnings("rawtypes") private SocialException extractExceptionFromResponse(ClientHttpResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); try {/*from w w w . j ava 2 s . c o m*/ String json = readFully(response.getBody()); Map<String, String> responseMap = mapper.<Map<String, String>>readValue(json, new TypeReference<Map<String, Object>>() { }); if (responseMap.containsKey("error")) { Object error = responseMap.get("error"); if (error instanceof String) { return new UncategorizedApiException("mixcloud", responseMap.get("error"), new RuntimeException(responseMap.get("error"))); } else if (error instanceof Map) { Map errorMap = (Map) error; String type = (String) ((Map) error).get("type"); if ("OAuthException".equals(type)) { return new InvalidAuthorizationException("mixcloud", (String) errorMap.get("message")); } if ("ResourceNotFoundException".equals(type)) { return new ResourceNotFoundException("mixcloud", (String) errorMap.get("message")); } return new UncategorizedApiException("mixcloud", responseMap.get("error"), new RuntimeException(responseMap.get("error"))); } else { return null; } } else { return null; } } catch (JsonParseException e) { return null; } }
From source file:com.miserablemind.api.consumer.tradeking.api.impl.StreamingTemplate.java
private StreamReader createStream(HttpMethod method, String streamUrl, MultiValueMap<String, String> body, List<StreamListener> listeners) throws StreamCreationException { try {/* w w w . j a va 2 s .co m*/ ClientHttpResponse response = executeRequest(method, streamUrl, body); if (response.getStatusCode().value() > 200) { throw new StreamCreationException("Unable to create stream", response.getStatusCode()); } return new StreamReaderImpl(response.getBody(), listeners); } catch (IOException e) { throw new StreamCreationException("Unable to create stream.", e); } }
From source file:org.springframework.social.lastfm.api.impl.LastFmErrorHandler.java
@Override public boolean hasError(ClientHttpResponse response) throws IOException { if (super.hasError(response)) { return true; }/* w w w .j a v a2s. c om*/ // only bother checking the body for errors if we get past the default // error check String content = readFully(response.getBody()); return content.startsWith("{\"error\":"); }