Java Code Examples for org.springframework.web.client.HttpClientErrorException#getMessage()

The following examples show how to use org.springframework.web.client.HttpClientErrorException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SonarServerConfigurationServiceImpl.java    From code-quality-game with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Checks the current status of the server and the version running on it.
 * It also detects if the server is not reachable.
 *
 * @param config The configuration for Sonar Server
 * @return the response from server
 */
@Override
public SonarServerStatus checkServerDetails(final SonarServerConfiguration config) {
    log.info("Trying to reach Sonar server at " + config.getUrl() + API_SYSTEM_STATUS);
    try {
        final HttpHeaders authHeaders = ApiHttpUtils.getHeaders(config.getToken());
        HttpEntity<String> request = new HttpEntity<>(authHeaders);
        final ResponseEntity<SonarServerStatus> response = restTemplate
                .exchange(config.getUrl() + API_SYSTEM_STATUS,
                        HttpMethod.GET, request, SonarServerStatus.class);
        log.info("Response received from server: " + response.getBody());
        return response.getBody();
    } catch (final HttpClientErrorException clientErrorException) {
        log.error(clientErrorException);
        if (clientErrorException.getStatusCode() == HttpStatus.UNAUTHORIZED) {
            return new SonarServerStatus(SonarServerStatus.Key.UNAUTHORIZED);
        } else {
            return new SonarServerStatus(SonarServerStatus.Key.UNKNOWN_ERROR, clientErrorException.getMessage());
        }
    } catch (final ResourceAccessException resourceAccessException) {
        log.error(resourceAccessException);
        return new SonarServerStatus(SonarServerStatus.Key.CONNECTION_ERROR, resourceAccessException.getMessage());
    }
}
 
Example 2
Source File: CFUAAOAuth2ClientController.java    From tutorials with MIT License 6 votes vote down vote up
private String callResourceServer(OAuth2AuthenticationToken authenticationToken, String url) {
    OAuth2AuthorizedClient oAuth2AuthorizedClient = this.authorizedClientService.loadAuthorizedClient(authenticationToken.getAuthorizedClientRegistrationId(), authenticationToken.getName());
    OAuth2AccessToken oAuth2AccessToken = oAuth2AuthorizedClient.getAccessToken();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer " + oAuth2AccessToken.getTokenValue());

    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
    ResponseEntity<String> responseEntity = null;

    String response = null;
    try {
        responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        response = responseEntity.getBody();
    } catch (HttpClientErrorException e) {
        response = e.getMessage();
    }
    return response;
}
 
Example 3
Source File: ProductCompositeIntegration.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 5 votes vote down vote up
private String getErrorMessage(HttpClientErrorException ex) {
    try {
        return mapper.readValue(ex.getResponseBodyAsString(), HttpErrorInfo.class).getMessage();
    } catch (IOException ioex) {
        return ex.getMessage();
    }
}
 
Example 4
Source File: ProductCompositeIntegration.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 5 votes vote down vote up
private String getErrorMessage(HttpClientErrorException ex) {
    try {
        return mapper.readValue(ex.getResponseBodyAsString(), HttpErrorInfo.class).getMessage();
    } catch (IOException ioex) {
        return ex.getMessage();
    }
}
 
Example 5
Source File: ProductCompositeIntegration.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 5 votes vote down vote up
private String getErrorMessage(HttpClientErrorException ex) {
    try {
        return mapper.readValue(ex.getResponseBodyAsString(), HttpErrorInfo.class).getMessage();
    } catch (IOException ioex) {
        return ex.getMessage();
    }
}
 
Example 6
Source File: ProductCompositeIntegration.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 5 votes vote down vote up
private String getErrorMessage(HttpClientErrorException ex) {
    try {
        return mapper.readValue(ex.getResponseBodyAsString(), HttpErrorInfo.class).getMessage();
    } catch (IOException ioex) {
        return ex.getMessage();
    }
}
 
Example 7
Source File: HttpClientErrorExceptionMapper.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(HttpClientErrorException exception) {
    RestError error = new RestError(
            exception.getMessage(),
            exception.getMessage(),
            ErrorMap.from(new String(exception.getResponseBodyAsByteArray(), StandardCharsets.UTF_8)),
            exception.getStatusCode().value());
    return Response.status(exception.getStatusCode().value()).type(MediaType.APPLICATION_JSON_TYPE).entity(error).build();
}
 
Example 8
Source File: GlobalExceptionHandler.java    From data-prep with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler({ HttpClientErrorException.class })
public ResponseEntity<TdpExceptionDto> handleError(HttpClientErrorException e) {

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);

    String code = e.getStatusCode().toString();
    String message = e.getMessage();
    String localizedMessage = e.getLocalizedMessage();
    String statusText = e.getStatusText();

    TdpExceptionDto exceptionDto = new TdpExceptionDto(code, null, message, localizedMessage, statusText, null);

    return new ResponseEntity<>(exceptionDto, httpHeaders, e.getStatusCode());
}
 
Example 9
Source File: ValidationLibException.java    From CheckPoint with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new Validation lib exception.
 *
 * @param e the e
 */
public ValidationLibException(HttpClientErrorException e) {
    super(e.getMessage());
    this.statusCode = e.getStatusCode();
}
 
Example 10
Source File: SmartlingClient.java    From mojito with Apache License 2.0 2 votes vote down vote up
/**
 * For error raised through HTTP error.
 *
 * Note that 200 is not always success, {@see throwExceptionOnError}
 */
SmartlingClientException wrapIntoSmartlingException(HttpClientErrorException httpClientErrorException, String messageSummary, Object... vars) throws SmartlingClientException {
    String msg = String.format(messageSummary, vars) + "\nMessage: " + httpClientErrorException.getMessage() + "\nBody: " +  httpClientErrorException.getResponseBodyAsString();
    return new SmartlingClientException(msg, httpClientErrorException);
}