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

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

Introduction

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

Prototype

public String getResponseBodyAsString() 

Source Link

Document

Return the response body as a string.

Usage

From source file:org.trustedanalytics.user.common.UaaProblemReader.java

public static UaaProblem read(HttpStatusCodeException e) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {/*from ww w .j av a2s  .  c  o m*/
        return objectMapper.readValue(e.getResponseBodyAsString(), UaaProblem.class);
    } catch (IOException e1) {
        LOGGER.error(e1);
        return null;
    }
}

From source file:org.eclipse.cft.server.core.internal.CloudErrorUtil.java

/**
 * //from  w  w w. j ava  2 s.c  om
 * @param error
 * @return Error message containing description of the error. If no
 * description is found, it will return the exception error or HTTP status
 * text message, if present. May return null if no message can be resolved.
 */
protected static String getHttpErrorMessage(HttpStatusCodeException error) {
    String message = null;
    if (error instanceof CloudFoundryException) {
        message = ((CloudFoundryException) error).getDescription();
    }

    if (message == null) {
        message = error.getMessage();
        if (message == null) {
            message = error.getStatusText();
            if (message == null) {
                message = error.getResponseBodyAsString();
            }
        }
    }
    return message;
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.RestUtilities.java

public <T> ResponseEntity<T> simpleRestExchange(RequestEntity reg, Class<T> respType) {
    HttpStatus failedCode;/*  ww  w  .jav  a 2 s.  c om*/
    try {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(reg, respType);
    } catch (HttpStatusCodeException e) {
        String serverResp = e.getResponseBodyAsString();
        failedCode = e.getStatusCode();
        logger.error(String.format(
                "Exception from server! " + "With status code %s! " + "Following body was responded %s",
                failedCode.toString(), serverResp), e);
    }

    //todo: think of throwing an exception instead of returning a null object
    return new ResponseEntity(null, failedCode);
}

From source file:org.openlmis.fulfillment.service.DataRetrievalException.java

/**
 * Constructs the exception./* w w  w.j  a v a  2  s  .c  o m*/
 *
 * @param resource the resource that we were trying to retrieve
 * @param ex       exception with status code and response body from server
 */
DataRetrievalException(String resource, HttpStatusCodeException ex) {
    this(resource, ex.getStatusCode(), ex.getResponseBodyAsString());
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.KongService.java

public KongPluginResponse enablePlugin(KongApiObject apiObject, KongPlugin plugin) {
    try {/* w  w  w  . ja v a  2s  . co  m*/
        RequestEntity<KongPlugin> requestEntity = RequestEntity
                .post(URI.create(this.kongUris.getKongPluginsForApiUri(apiObject.getApiName())))
                .contentType(MediaType.APPLICATION_JSON).accept(MediaType.ALL).body(plugin);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<KongPluginResponse> resp = restTemplate.exchange(requestEntity,
                KongPluginResponse.class);

        logger.trace("Response from Kong: {}", resp.getBody());
        return resp.getBody();
    } catch (HttpStatusCodeException e) {
        String serverResp = e.getResponseBodyAsString();
        logger.error(String.format("Exception from server! " + "Following body was responded %s", serverResp),
                e);
    }
    return null;
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.KongService.java

public KongApiResponseObject registerApi(KongApiObject apiObject) {
    try {// w w  w. jav  a  2 s . c  o  m
        RequestEntity<KongApiObject> requestEntity = RequestEntity
                .post(URI.create(this.kongUris.getKongApisUri())).contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.ALL).body(apiObject);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<KongApiResponseObject> resp = restTemplate.exchange(requestEntity,
                KongApiResponseObject.class);

        logger.trace("Response from Kong: {}", resp.getBody());
        return resp.getBody();
    } catch (HttpStatusCodeException e) {
        String serverResp = e.getResponseBodyAsString();
        logger.error(String.format("Exception from server! " + "Following body was responded %s", serverResp),
                e);

        KongApiResponseObject resp = new KongApiResponseObject();
        resp.setError(true);
        resp.setErrorMsg(serverResp);
        return resp;
    }
}

From source file:org.trustedanalytics.user.common.WebErrorHandlers.java

@ExceptionHandler(HttpStatusCodeException.class)
public void handleHttpStatusCodeException(HttpStatusCodeException e, HttpServletResponse response)
        throws IOException {
    String message = extractErrorFromJSON(e.getResponseBodyAsString());
    message = StringUtils.isNotBlank(message) ? message : e.getMessage();
    logAndSendErrorResponse(response, e.getStatusCode(), message, e);
}

From source file:com.xyxy.platform.examples.showcase.demos.hystrix.service.GetUserCommand.java

/**
 * ?HystrixBadRequestException?/*from   w  w w  .  j a  va  2s .c  om*/
 */
protected Exception handleException(HttpStatusCodeException e) {
    HttpStatus status = e.getStatusCode();
    if (status.equals(HttpStatus.BAD_REQUEST)) {
        throw new HystrixBadRequestException(e.getResponseBodyAsString(), e);
    }
    throw e;
}

From source file:org.nekorp.workflow.desktop.data.access.rest.CustomerDAOImp.java

@Override
public void guardar(Customer dato) {
    try {/*from   w  w w. ja va 2 s.  co  m*/
        if (dato.getId() == null) {
            URI resource = factory.getTemplate().postForLocation(factory.getRootUlr() + "/customer", dato);
            String[] uri = StringUtils.split(resource.toString(), '/');
            String id = uri[uri.length - 1];
            CustomerPojo datoPjo = (CustomerPojo) dato;
            datoPjo.setId(Long.valueOf(id));
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("id", dato.getId());
            factory.getTemplate().postForLocation(factory.getRootUlr() + "/customer/{id}", dato, map);
        }
    } catch (HttpStatusCodeException ex) {
        try {
            BasicErrorMessage errorObj = objectMapper.readValue(ex.getResponseBodyAsString(),
                    BasicErrorMessage.class);
            CustomerDAOImp.LOGGER.error("error msg: " + Arrays.toString(errorObj.getMessage()));
        } catch (Exception exd) {
            CustomerDAOImp.LOGGER.error(exd);
        }
        throw ex;
    }
}

From source file:org.openlmis.fulfillment.service.notification.NotificationService.java

/**
 * Send an email notification./*from w w  w  .j  ava2s . com*/
 *
 * @param user    receiver of the notification
 * @param subject subject of the email
 * @param content content of the email
 * @return true if success, false if failed.
 */
public boolean notify(UserDto user, String subject, String content) {
    NotificationDto request = buildNotification(user, subject, content);
    logger.debug("Sending request:" + "\n subject:" + request.getMessages().get(EMAIL.toString()).getSubject()
            + "\n content:" + request.getMessages().get(EMAIL.toString()).getBody() + "\n to: "
            + request.getUserId());

    String url = notificationUrl + "/api/notifications";
    try {
        RequestHeaders headers;
        headers = RequestHeaders.init().setAuth(authService.obtainAccessToken());
        URI uri = RequestHelper.createUri(url);
        HttpEntity<NotificationDto> entity = RequestHelper.createEntity(request, headers);

        restTemplate.postForObject(uri, entity, Object.class);
    } catch (HttpStatusCodeException ex) {
        logger.error("Unable to send notification. Error code: {}, response message: {}", ex.getStatusCode(),
                ex.getResponseBodyAsString());
        return false;
    }

    return true;
}