public <T> T findOne(URI uri, Class<T> targetClass) {
   Assert.state(uri != null, "uri must be set to use this method");
   try {
     return restOperations.getForObject(uri, targetClass);
     // TODO check this exception translation and centralize.
   } catch (HttpClientErrorException clientError) {
     if (clientError.getStatusCode() == HttpStatus.NOT_FOUND) {
       throw new DocumentRetrievalFailureException(uri.getPath());
     }
     throw new CouchUsageException(clientError);
   } catch (HttpServerErrorException serverError) {
     throw new CouchServerResourceUsageException(serverError);
   } catch (RestClientException otherError) {
     throw new UncategorizedCouchDataAccessException(otherError);
   }
 }
示例#2
0
  @Test
  public void testErrorResponseExceptionStrategy() {
    HttpMessageSender messageSender = new HttpMessageSender();
    String requestUrl = "http://localhost:8088/test";

    messageSender.setReplyMessageHandler(replyMessageHandler);

    messageSender.setRequestMethod(HttpMethod.POST);
    messageSender.setRequestUrl(requestUrl);

    messageSender.setErrorHandlingStrategy(ErrorHandlingStrategy.THROWS_EXCEPTION);

    Message<?> requestMessage =
        MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>")
            .build();

    messageSender.setRestTemplate(restTemplate);

    reset(restTemplate, replyMessageHandler);

    restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class));
    expectLastCall().once();

    expect(
            restTemplate.exchange(
                eq(requestUrl), eq(HttpMethod.POST), anyObject(HttpEntity.class), eq(String.class)))
        .andThrow(new HttpClientErrorException(HttpStatus.FORBIDDEN))
        .once();

    replay(restTemplate, replyMessageHandler);

    try {
      messageSender.send(requestMessage);

      Assert.fail("Missing exception due to http error status code");
    } catch (HttpClientErrorException e) {
      Assert.assertEquals(e.getMessage(), "403 FORBIDDEN");

      verify(restTemplate, replyMessageHandler);
    }
  }