Example usage for org.springframework.web.client HttpClientErrorException getResponseBodyAsString

List of usage examples for org.springframework.web.client HttpClientErrorException getResponseBodyAsString

Introduction

In this page you can find the example usage for org.springframework.web.client HttpClientErrorException getResponseBodyAsString.

Prototype

public String getResponseBodyAsString() 

Source Link

Document

Return the response body as a string.

Usage

From source file:com.marklogic.mgmt.admin.AdminManager.java

public void installAdmin(String username, String password) {
    final URI uri = adminConfig.buildUri("/admin/v1/instance-admin");

    String json = null;//ww  w  . j  av a2  s .c  om
    if (username != null && password != null) {
        json = format("{\"admin-username\":\"%s\", \"admin-password\":\"%s\", \"realm\":\"public\"}", username,
                password);
    } else {
        json = "{}";
    }
    final String payload = json;

    logger.info("Installing admin user at: " + uri);
    invokeActionRequiringRestart(new ActionRequiringRestart() {
        @Override
        public boolean execute() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<String>(payload, headers);
            try {
                ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity,
                        String.class);
                logger.info("Admin installation response: " + response);
                // According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
                // restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
                return HttpStatus.ACCEPTED.equals(response.getStatusCode());
            } catch (HttpClientErrorException hcee) {
                if (HttpStatus.BAD_REQUEST.equals(hcee.getStatusCode())) {
                    logger.warn("Caught 400 error, assuming admin user already installed; response body: "
                            + hcee.getResponseBodyAsString());
                    return false;
                }
                throw hcee;
            }
        }
    });
}

From source file:io.spring.initializr.web.project.MainControllerIntegrationTests.java

@Test
public void missingDependencyProperException() {
    try {/*  w  ww . j  a  v  a  2  s.  c o  m*/
        downloadArchive("/starter.zip?style=foo:bar");
        fail("Should have failed");
    } catch (HttpClientErrorException ex) {
        assertEquals(HttpStatus.BAD_REQUEST, ex.getStatusCode());
        assertStandardErrorBody(ex.getResponseBodyAsString(),
                "Unknown dependency 'foo:bar' check project metadata");
    }
}

From source file:io.spring.initializr.web.project.MainControllerIntegrationTests.java

@Test
public void invalidDependencyProperException() {
    try {/*  w  w  w  .  j  a  va  2  s  .  co  m*/
        downloadArchive("/starter.zip?style=foo");
        fail("Should have failed");
    } catch (HttpClientErrorException ex) {
        assertEquals(HttpStatus.BAD_REQUEST, ex.getStatusCode());
        assertStandardErrorBody(ex.getResponseBodyAsString(),
                "Unknown dependency 'foo' check project metadata");
    }
}

From source file:org.cbioportal.genome_nexus.annotation.web.AnnotationController.java

private VariantAnnotation getVariantAnnotation(String variant) {
    VariantAnnotation variantAnnotation = variantAnnotationRepository.findOne(variant);
    String annotationJSON = null;

    if (variantAnnotation == null) {

        try {//  ww  w. jav a2  s.  co  m
            // get the annotation from the web service and save it to the DB
            //variantAnnotation = variantAnnotationService.getAnnotation(variant);
            //variantAnnotationRepository.save(variantAnnotation);

            // get the raw annotation string from the web service
            annotationJSON = variantAnnotationService.getRawAnnotation(variant);

            // construct a VariantAnnotation instance to return:
            // this does not contain all the information obtained from the web service
            // only the fields mapped to the VariantAnnotation model will be returned
            variantAnnotation = variantAnnotationRepository.mapAnnotationJson(variant, annotationJSON);

            // save everything to the cache as a properly parsed JSON
            variantAnnotationRepository.saveAnnotationJson(variant, annotationJSON);
        } catch (HttpClientErrorException e) {
            // in case of web service error, do not terminate the whole process.
            // just copy the response body (error message) for this variant
            variantAnnotation = new VariantAnnotation(variant, e.getResponseBodyAsString());
        } catch (IOException e) {
            // in case of parse error, do not terminate the whole process.
            // just send the raw annotationJSON to the client
            variantAnnotation = new VariantAnnotation(variant, annotationJSON);
        }
    }

    return variantAnnotation;
}

From source file:it.infn.mw.iam.authn.oidc.DefaultOidcTokenRequestor.java

Optional<TokenEndpointErrorResponse> parseErrorResponse(HttpClientErrorException e) {
    try {// w  w w .ja v  a 2 s  .c om
        TokenEndpointErrorResponse response = jacksonObjectMapper.readValue(e.getResponseBodyAsByteArray(),
                TokenEndpointErrorResponse.class);

        return Optional.of(response);
    } catch (Exception jsonParsingError) {
        LOG.error("Error parsing token endpoint response: {}. input: {}", jsonParsingError.getMessage(),
                e.getResponseBodyAsString(), jsonParsingError);

        return Optional.empty();
    }

}

From source file:com.brightcove.zencoder.client.ZencoderClient.java

/**
 * If a job has failed processing you may request that it be attempted again.
 *
 * @see https://app.zencoder.com/docs/api/jobs/resubmit
 * @param id/*from w w w  . j ava 2  s  . c o  m*/
 * @throws ZencoderClientException
 */
public void resubmitJob(String id) throws ZencoderClientException {
    String url = api_url + "/jobs/" + id + "/resubmit.json";

    HttpHeaders headers = getHeaders();
    @SuppressWarnings("rawtypes")
    HttpEntity entity = new HttpEntity<String>("", headers);

    try {
        rt.exchange(url, HttpMethod.PUT, entity, String.class, new HashMap<String, Object>());

    } catch (HttpClientErrorException hcee) {
        throw new ZencoderClientException(hcee.getResponseBodyAsString(), hcee);
    }
}

From source file:com.brightcove.zencoder.client.ZencoderClient.java

/**
 * If you wish to cancel a job that has not yet finished processing.
 *
 * @see https://app.zencoder.com/docs/api/jobs/cancel
 * @param id/*from www . ja v a 2s .c  o m*/
 * @throws ZencoderClientException
 */
public void cancelJob(String id) throws ZencoderClientException {
    String url = api_url + "/jobs/" + id + "/cancel.json";

    HttpHeaders headers = getHeaders();
    @SuppressWarnings("rawtypes")
    HttpEntity entity = new HttpEntity<String>("", headers);

    try {
        rt.exchange(url, HttpMethod.PUT, entity, String.class, new HashMap<String, Object>());

    } catch (HttpClientErrorException hcee) {
        throw new ZencoderClientException(hcee.getResponseBodyAsString(), hcee);
    }
}

From source file:com.brightcove.zencoder.client.ZencoderClient.java

/**
 * Finishes the input on a Live streaming job. Has no effect on non-Live jobs.
 *
 * @see https://app.zencoder.com/docs/api/jobs/finish
 * @param id/*  w  w w .j  ava  2s .c  o  m*/
 * @throws ZencoderClientException
 */
public void finishLiveJob(String id) throws ZencoderClientException {
    String url = api_url + "/jobs/" + id + "/finish";

    HttpHeaders headers = getHeaders();
    @SuppressWarnings("rawtypes")
    HttpEntity entity = new HttpEntity<String>("", headers);

    try {
        rt.exchange(url, HttpMethod.PUT, entity, String.class, new HashMap<String, Object>());

    } catch (HttpClientErrorException hcee) {
        throw new ZencoderClientException(hcee.getResponseBodyAsString(), hcee);
    }
}