@Override
 public void handleError(ClientHttpResponse response) throws IOException {
   HttpStatus statusCode = response.getStatusCode();
   switch (statusCode.series()) {
     case CLIENT_ERROR:
       CloudFoundryException exception =
           new CloudFoundryException(statusCode, response.getStatusText());
       ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
       if (response.getBody() != null) {
         try {
           @SuppressWarnings("unchecked")
           Map<String, Object> map = mapper.readValue(response.getBody(), Map.class);
           exception.setDescription(CloudUtil.parse(String.class, map.get("description")));
         } catch (JsonParseException e) {
           exception.setDescription("Client error");
         } catch (IOException e) {
           exception.setDescription("Client error");
         }
       } else {
         exception.setDescription("Client error");
       }
       throw exception;
     case SERVER_ERROR:
       throw new HttpServerErrorException(statusCode, response.getStatusText());
     default:
       throw new RestClientException("Unknown status code [" + statusCode + "]");
   }
 }
Esempio n. 2
0
  public OAuth2AccessToken getToken(String username, String password, String clientId) {
    OAuth2ProtectedResourceDetails resource = getResourceDetails(username, password, clientId);
    AccessTokenRequest request = createAccessTokenRequest(username, password);

    ResourceOwnerPasswordAccessTokenProvider provider =
        createResourceOwnerPasswordAccessTokenProvider();
    try {
      return provider.obtainAccessToken(resource, request);
    } catch (OAuth2AccessDeniedException oauthEx) {
      HttpStatus status = HttpStatus.valueOf(oauthEx.getHttpErrorCode());
      CloudFoundryException cfEx = new CloudFoundryException(status, oauthEx.getMessage());
      cfEx.setDescription(oauthEx.getSummary());
      throw cfEx;
    }
  }