Example usage for org.springframework.web.client HttpStatusCodeException getResponseBodyAsByteArray

List of usage examples for org.springframework.web.client HttpStatusCodeException getResponseBodyAsByteArray

Introduction

In this page you can find the example usage for org.springframework.web.client HttpStatusCodeException getResponseBodyAsByteArray.

Prototype

public byte[] getResponseBodyAsByteArray() 

Source Link

Document

Return the response body as a byte array.

Usage

From source file:com.weibo.handler.ErrorCodeHandler.java

public ErrorCode handle(HttpStatusCodeException error) {
    ObjectMapper objectMapper = new ObjectMapper();
    ErrorCode errorCode = new ErrorCode();
    errorCode.setRequest(StringUtils.EMPTY);
    errorCode.setErrorCode(error.getStatusCode().toString());
    errorCode.setError(error.getStatusText());
    try {/*  w  w  w.j  a  va2  s. c o  m*/
        errorCode = objectMapper.readValue(error.getResponseBodyAsByteArray(), ErrorCode.class);
    } catch (JsonParseException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (JsonMappingException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (IOException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    }
    return errorCode;
}

From source file:com.t163.handler.T163ErrorCodeHandler.java

public T163ErrorCode handle(HttpStatusCodeException error) {
    ObjectMapper objectMapper = new ObjectMapper();
    T163ErrorCode errorCode = new T163ErrorCode();
    errorCode.setRequest(StringUtils.EMPTY);
    errorCode.setErrorCode(error.getStatusCode().toString());
    errorCode.setError(error.getStatusText());
    try {//from  ww w  .j a  v a 2s. c  o  m
        errorCode = objectMapper.readValue(error.getResponseBodyAsByteArray(), T163ErrorCode.class);
    } catch (JsonParseException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (JsonMappingException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (IOException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    }
    return errorCode;
}

From source file:com.netflix.genie.web.tasks.leader.ClusterCheckerTask.java

private boolean isNodeHealthy(final String host) {
    ///*from   w w w .  jav  a  2 s .co  m*/
    // A node is valid and healthy if all health indicators excluding the ones mentioned in healthIndicatorsToIgnore
    // are UP.
    //
    boolean result = true;
    try {
        restTemplate.getForObject(this.scheme + host + this.healthEndpoint, String.class);
    } catch (final HttpStatusCodeException e) {
        log.error("Failed validating host {}", host, e);
        try {
            final Map<String, Object> responseMap = mapper.readValue(e.getResponseBodyAsByteArray(),
                    TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class));
            for (Map.Entry<String, Object> responseEntry : responseMap.entrySet()) {
                if (responseEntry.getValue() instanceof Map
                        && !healthIndicatorsToIgnore.contains(responseEntry.getKey())
                        && !Status.UP.getCode().equals(((Map) responseEntry.getValue()).get(PROPERTY_STATUS))) {
                    result = false;
                    break;
                }
            }
        } catch (Exception ex) {
            log.error("Failed reading the error response when validating host {}", host, ex);
            result = false;
        }
    } catch (final Exception e) {
        log.error("Unable to reach {}", host, e);
        result = false;
    }
    return result;
}

From source file:am.ik.categolj2.app.authentication.AuthenticationHelper.java

void handleHttpStatusCodeException(HttpStatusCodeException e, RedirectAttributes attributes)
        throws IOException {
    if (logger.isInfoEnabled()) {
        logger.info("authentication failed (message={},X-Track={})", e.getMessage(),
                e.getResponseHeaders().get("X-Track"));
    }/*from ww w.j  av  a 2  s . c  om*/
    try {
        OAuth2Exception oAuth2Exception = objectMapper.readValue(e.getResponseBodyAsByteArray(),
                OAuth2Exception.class);
        attributes.addAttribute("error", oAuth2Exception.getMessage());
    } catch (JsonMappingException | JsonParseException ex) {
        attributes.addAttribute("error", e.getMessage());
    }
}

From source file:com.auditbucket.client.AbRestClient.java

public String getErrorMessage(HttpStatusCodeException e) {

    if (e.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) {
        logger.error(e.getResponseBodyAsString());
        return e.getResponseBodyAsString();
    }/*from   www  .  jav  a2 s.  c o m*/

    JsonNode n = null;
    try {
        n = mapper.readTree(e.getResponseBodyAsByteArray());
    } catch (IOException e1) {

        logger.error(String.valueOf(e1));
    }
    String message;
    if (n != null)
        message = String.valueOf(n.get("message"));
    else
        message = e.getMessage();

    return message;
}

From source file:org.zalando.boot.etcd.EtcdClient.java

/**
 * Executes the given method on the given location using the given request
 * data.//from ww  w  . j a v  a2  s . c o m
 * 
 * @param uri
 *            the location
 * @param method
 *            the HTTP method
 * @param requestData
 *            the request data
 * @return the etcd response
 * @throws EtcdException
 *             in case etcd returned an error
 */
private <T> T execute(UriComponentsBuilder uriTemplate, HttpMethod method,
        MultiValueMap<String, String> requestData, Class<T> responseType) throws EtcdException {
    long startTimeMillis = System.currentTimeMillis();
    int retry = -1;

    ResourceAccessException lastException = null;
    do {
        lastException = null;

        URI uri = uriTemplate.buildAndExpand(locations[locationIndex]).toUri();

        RequestEntity<MultiValueMap<String, String>> requestEntity = new RequestEntity<>(requestData, null,
                method, uri);

        try {
            ResponseEntity<T> responseEntity = template.exchange(requestEntity, responseType);
            return responseEntity.getBody();
        } catch (HttpStatusCodeException e) {
            EtcdError error = null;
            try {
                error = responseConverter.getObjectMapper().readValue(e.getResponseBodyAsByteArray(),
                        EtcdError.class);
            } catch (IOException ex) {
                error = null;
            }
            throw new EtcdException(error, "Failed to execute " + requestEntity + ".", e);
        } catch (ResourceAccessException e) {
            log.debug("Failed to execute " + requestEntity + ", retrying if possible.", e);

            if (locationIndex == locations.length - 1) {
                locationIndex = 0;
            } else {
                locationIndex++;
            }
            lastException = e;
        }
    } while (retry <= retryCount && System.currentTimeMillis() - startTimeMillis < retryDuration);

    if (lastException != null) {
        throw lastException;
    } else {
        return null;
    }
}