Example usage for org.springframework.web.client HttpServerErrorException getMessage

List of usage examples for org.springframework.web.client HttpServerErrorException getMessage

Introduction

In this page you can find the example usage for org.springframework.web.client HttpServerErrorException getMessage.

Prototype

@Override
@Nullable
public String getMessage() 

Source Link

Document

Return the detail message, including the message from the nested exception if there is one.

Usage

From source file:org.encuestame.oauth1.support.OAuth1Support.java

/**
 *
 * @param tokenUrl/*from ww w .ja v a  2  s .c o m*/
 * @param tokenRequestParameters
 * @param additionalParameters
 * @param tokenSecret
 * @return
 * @throws EnMeOAuthSecurityException
 */
protected OAuth1Token getTokenFromProvider(String tokenUrl, Map<String, String> tokenRequestParameters,
        Map<String, String> additionalParameters, String tokenSecret) throws EnMeOAuthSecurityException {
    log.debug("getTokenFromProvider TOKEN " + tokenUrl);
    log.debug("getTokenFromProvider TOKEN " + tokenRequestParameters);
    log.debug("getTokenFromProvider TOKEN " + additionalParameters);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization",
            getAuthorizationHeaderValue(tokenUrl, tokenRequestParameters, additionalParameters, tokenSecret));
    MultiValueMap<String, String> bodyParameters = new LinkedMultiValueMap<String, String>();
    bodyParameters.setAll(additionalParameters);
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(
            bodyParameters, headers);
    Map<String, String> responseMap = null;
    /*
     * org.springframework.web.client.HttpServerErrorException: 503 Service Unavailable
     * Sometimes, api service are unavailable, like the famous twitter over capacity
     */
    try {
        ResponseEntity<String> response = getRestTemplate().exchange(tokenUrl, HttpMethod.POST, request,
                String.class);
        responseMap = parseResponse(response.getBody());
    } catch (HttpServerErrorException e) {
        e.printStackTrace();
        throw new EnMeOAuthSecurityException(e);
    } catch (HttpClientErrorException e) {
        // normally if the social credentials are wrong
        e.printStackTrace();
        throw new EnMeBadCredentialsException(e.getMessage());
    } catch (Exception e) {
        // another kind of error, possible wrong configuration
        //TODO : only happends with twitter
        e.printStackTrace();
        throw new EnMeBadCredentialsException(e.getMessage());
    }

    return new OAuth1Token(responseMap.get("oauth_token"), responseMap.get("oauth_token_secret"));
}