Example usage for org.springframework.web.client RestTemplate setErrorHandler

List of usage examples for org.springframework.web.client RestTemplate setErrorHandler

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate setErrorHandler.

Prototype

public void setErrorHandler(ResponseErrorHandler errorHandler) 

Source Link

Document

Set the error handler.

Usage

From source file:org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils.java

public static BaseClientDetails createOrUpdateClient(String adminToken, String url, String switchToZoneId,
        BaseClientDetails client) throws Exception {

    RestTemplate template = new RestTemplate();
    template.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override//w ww  .j  a v a  2 s. c  o m
        protected boolean hasError(HttpStatus statusCode) {
            return statusCode.is5xxServerError();
        }
    });
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("Accept", APPLICATION_JSON_VALUE);
    headers.add("Authorization", "bearer " + adminToken);
    headers.add("Content-Type", APPLICATION_JSON_VALUE);
    if (StringUtils.hasText(switchToZoneId)) {
        headers.add(IdentityZoneSwitchingFilter.HEADER, switchToZoneId);
    }
    HttpEntity getHeaders = new HttpEntity(JsonUtils.writeValueAsBytes(client), headers);
    ResponseEntity<String> clientCreate = template.exchange(url + "/oauth/clients", HttpMethod.POST, getHeaders,
            String.class);
    if (clientCreate.getStatusCode() == HttpStatus.CREATED) {
        return JsonUtils.readValue(clientCreate.getBody(), BaseClientDetails.class);
    } else if (clientCreate.getStatusCode() == HttpStatus.CONFLICT) {
        HttpEntity putHeaders = new HttpEntity(JsonUtils.writeValueAsBytes(client), headers);
        ResponseEntity<String> clientUpdate = template.exchange(url + "/oauth/clients/" + client.getClientId(),
                HttpMethod.PUT, putHeaders, String.class);
        if (clientUpdate.getStatusCode() == HttpStatus.OK) {
            return JsonUtils.readValue(clientCreate.getBody(), BaseClientDetails.class);
        } else {
            throw new RuntimeException("Invalid update return code:" + clientUpdate.getStatusCode());
        }
    }
    throw new RuntimeException("Invalid crete return code:" + clientCreate.getStatusCode());
}

From source file:org.paxml.bean.RestTag.java

@Override
protected Object doInvoke(Context context) throws Exception {

    RestTemplate t = new RestTemplate();
    if (!simple) {
        // cancel default error handling
        t.setErrorHandler(new ResponseErrorHandler() {

            @Override/*from  w  w  w.ja  v  a2 s.  c o m*/
            public boolean hasError(ClientHttpResponse response) throws IOException {
                // always say no error
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                // do nothing
            }

        });
    }
    Object value = getValue();
    HttpHeaders hds = new HttpHeaders();
    if (username != null) {
        String[] auth = PaxmlUtils.makeHttpClientAutorizationHeader(username, password);
        hds.set(auth[0], auth[1]);
    }
    if (headers != null) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            hds.set(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
    }
    if (StringUtils.isNotBlank(contentType)) {
        hds.setContentType(org.springframework.http.MediaType.parseMediaType(contentType));
    }
    String reqBody = makeRequestBody(value);
    log.debug("REST request body=" + reqBody);
    HttpEntity<String> entity = new HttpEntity<String>(reqBody, hds);

    ResponseEntity<String> rsp = t.exchange(target, _method, entity, String.class);

    Object body = parseResponse ? parseResponse(rsp) : rsp.getBody();
    if (simple) {
        return body;
    }
    return new RestResult(body, rsp.getHeaders(), rsp.getStatusCode().value());

}

From source file:org.springframework.data.document.couchdb.core.AbstractCouchTemplateIntegrationTests.java

@Before
public void setUpTestDatabase() throws Exception {
    RestTemplate template = new RestTemplate();
    template.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override//  w ww .j ava 2  s .  com
        public void handleError(ClientHttpResponse response) throws IOException {
            // do nothing, error status will be handled in the switch statement
        }
    });
    ResponseEntity<String> response = template.getForEntity(CouchConstants.TEST_DATABASE_URL, String.class);
    HttpStatus statusCode = response.getStatusCode();
    switch (statusCode) {
    case NOT_FOUND:
        createNewTestDatabase();
        break;
    case OK:
        deleteExisitingTestDatabase();
        createNewTestDatabase();
        break;
    default:
        throw new IllegalStateException("Unsupported http status [" + statusCode + "]");
    }
}

From source file:org.springframework.data.keyvalue.riak.util.RiakClassLoader.java

protected void init(RiakTemplate riakTemplate) {
    this.riakTemplate = riakTemplate;
    RestTemplate tmpl = this.riakTemplate.getRestTemplate();
    tmpl.getMessageConverters().add(0, new JavaSerializationMessageHandler());
    tmpl.setErrorHandler(new Ignore404sErrorHandler());
}

From source file:org.springframework.social.google.api.impl.AbstractGoogleApiOperations.java

protected AbstractGoogleApiOperations(RestTemplate restTemplate, boolean isAuthorized) {
    this.restTemplate = restTemplate;
    this.isAuthorized = isAuthorized;

    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override/*from  w  w w. j  a  v  a2 s .c o m*/
        public void handleError(ClientHttpResponse response) throws IOException {
            if (logger.isWarnEnabled()) {
                String bodyText = StreamUtils.copyToString(response.getBody(), Charset.defaultCharset());
                logger.warn("Google API REST response body:" + bodyText);
            }
        }
    });
}