Search in sources :

Example 1 with HttpMessageConverterExtractor

use of org.springframework.web.client.HttpMessageConverterExtractor in project component-runtime by Talend.

the class SpringRestClient method toException.

private WebException toException(final HttpServerErrorException hsee) {
    try {
        return new WebException(hsee, hsee.getRawStatusCode(), new HttpMessageConverterExtractor<Map<String, Object>>(mapType.getType(), delegate.getMessageConverters()).extractData(new ClientHttpResponse() {

            @Override
            public HttpStatus getStatusCode() {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() {
                return 200;
            }

            @Override
            public String getStatusText() {
                return "";
            }

            @Override
            public void close() {
            // no-op
            }

            @Override
            public InputStream getBody() {
                return new ByteArrayInputStream(hsee.getResponseBodyAsByteArray());
            }

            @Override
            public HttpHeaders getHeaders() {
                final HttpHeaders json = json();
                json.add(HttpHeaders.CONTENT_TYPE, "application/json");
                return json;
            }
        }));
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WebException(org.talend.sdk.component.form.api.WebException) HttpMessageConverterExtractor(org.springframework.web.client.HttpMessageConverterExtractor) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 2 with HttpMessageConverterExtractor

use of org.springframework.web.client.HttpMessageConverterExtractor in project commons by craftercms.

the class HttpMessageConvertingResponseErrorHandler method handleError.

@Override
public void handleError(ClientHttpResponse response) throws IOException {
    HttpStatus status = response.getStatusCode();
    HttpMessageConverterExtractor<?> responseExtractor = new HttpMessageConverterExtractor<>(responseType, messageConverters);
    Object errorDetails;
    try {
        errorDetails = responseExtractor.extractData(response);
    } catch (RestClientException e) {
        // the response body as string
        throw new RestServiceException(status, getResponseBodyAsString(response));
    }
    throw new RestServiceException(status, errorDetails);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HttpMessageConverterExtractor(org.springframework.web.client.HttpMessageConverterExtractor) RestClientException(org.springframework.web.client.RestClientException)

Aggregations

HttpMessageConverterExtractor (org.springframework.web.client.HttpMessageConverterExtractor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 RestClientException (org.springframework.web.client.RestClientException)1 WebException (org.talend.sdk.component.form.api.WebException)1