Package org.springframework.http.client

Examples of org.springframework.http.client.ClientHttpResponse


        factory.setReadTimeout(15 * 1000);

        URI uri = new URI("http://localhost:" + TEST_PORT + "/testConnectionFactory");
        HttpMethod method = HttpMethod.GET;
        ClientHttpRequest request = factory.createRequest(uri, method);
        ClientHttpResponse response = request.execute();
        assertEquals("Mismatched response code", HttpStatus.OK.value(), response.getRawStatusCode());

        BufferedReader rdr = new BufferedReader(new InputStreamReader(response.getBody()));
        try {
            for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
                logger.info(line);
            }
        } finally {
View Full Code Here


      // We should only care about 400 level errors. Ex: A 500 server error shouldn't
      // be an oauth related error.
      errorHandler.handleError(response);
    } else {
      // Need to use buffered response because input stream may need to be consumed multiple times.
      ClientHttpResponse bufferedResponse = new ClientHttpResponse() {
        private byte[] lazyBody;

        public HttpStatus getStatusCode() throws IOException {
          return response.getStatusCode();
        }

        public synchronized InputStream getBody() throws IOException {
          if (lazyBody == null) {
            InputStream bodyStream = response.getBody();
            if (bodyStream != null) {
              lazyBody = FileCopyUtils.copyToByteArray(bodyStream);
            } else {
              lazyBody = new byte[0];
            }
          }
          return new ByteArrayInputStream(lazyBody);
        }

        public HttpHeaders getHeaders() {
          return response.getHeaders();
        }

        public String getStatusText() throws IOException {
          return response.getStatusText();
        }

        public void close() {
          response.close();
        }

        public int getRawStatusCode() throws IOException {
          return response.getRawStatusCode();
        }
      };

      try {
        HttpMessageConverterExtractor<OAuth2Exception> extractor = new HttpMessageConverterExtractor<OAuth2Exception>(
            OAuth2Exception.class, messageConverters);
        try {
          OAuth2Exception body = extractor.extractData(bufferedResponse);
          if (body != null) {
            // If we can get an OAuth2Exception already from the body, it is likely to have more information than
            // the header does, so just re-throw it here.
            throw body;
          }
        }
        catch (RestClientException e) {
          // ignore
        }

        // first try: www-authenticate error
        List<String> authenticateHeaders = bufferedResponse.getHeaders().get("WWW-Authenticate");
        if (authenticateHeaders != null) {
          for (String authenticateHeader : authenticateHeaders) {
            maybeThrowExceptionFromHeader(authenticateHeader, OAuth2AccessToken.BEARER_TYPE);
            maybeThrowExceptionFromHeader(authenticateHeader, OAuth2AccessToken.OAUTH2_TYPE);
          }
        }

        // then delegate to the custom handler
        errorHandler.handleError(bufferedResponse);
      }
      catch (OAuth2Exception ex) {
        if (bufferedResponse.getRawStatusCode() == 403 || bufferedResponse.getRawStatusCode() == 401 || ! ex.getClass().equals(OAuth2Exception.class)) {
          // Status code 401 should always mean that we need a legitimate token.
          // Caught a specific, derived class so this is not just some generic error
          throw ex;
        } else {
          // This is not an exception that is really understood, so allow our delegate
View Full Code Here

  @Test
  public void testHandleErrorClientHttpResponse() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.set("www-authenticate", "Bearer error=foo");
    ClientHttpResponse response = new TestClientHttpResponse(headers,401);

    expected.expectMessage("foo");
    handler.handleError(response);

  }
View Full Code Here

  @Test
  public void testHandleErrorWithInvalidToken() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.set("www-authenticate", "Bearer error=\"invalid_token\", description=\"foo\"");
    ClientHttpResponse response = new TestClientHttpResponse(headers,401);

    expected.expect(AccessTokenRequiredException.class);
    expected.expectMessage("OAuth2 access denied");
    handler.handleError(response);
View Full Code Here

        throw new RuntimeException("planned");
      }
    }, resource);

    HttpHeaders headers = new HttpHeaders();
    ClientHttpResponse response = new TestClientHttpResponse(headers,401);

    expected.expectMessage("planned");
    handler.handleError(response);

  }
View Full Code Here

  }

  @Test
  public void testHandle500Error() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    ClientHttpResponse response = new TestClientHttpResponse(headers,500);

    expected.expect(HttpServerErrorException.class);
    handler.handleError(response);
  }
View Full Code Here

  }

  @Test
  public void testHandleGeneric400Error() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    ClientHttpResponse response = new TestClientHttpResponse(headers,400);

    expected.expect(HttpClientErrorException.class);
    handler.handleError(response);
  }
View Full Code Here

    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Length",""+appSpecificBodyContent.length());
    headers.set("Content-Type","application/json");
    InputStream appSpecificErrorBody =
      new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
    ClientHttpResponse response =
      new TestClientHttpResponse(headers,400,appSpecificErrorBody);

    expected.expectMessage("planned");
    handler.handleError(response);
  }
View Full Code Here

    restTemplate = new OAuth2RestTemplate(resource);
    restTemplate.setAccessTokenProvider(accessTokenProvider);
    request = Mockito.mock(ClientHttpRequest.class);
    headers = new HttpHeaders();
    Mockito.when(request.getHeaders()).thenReturn(headers);
    ClientHttpResponse response = Mockito.mock(ClientHttpResponse.class);
    HttpStatus statusCode = HttpStatus.OK;
    Mockito.when(response.getStatusCode()).thenReturn(statusCode);
    Mockito.when(request.execute()).thenReturn(response);
  }
View Full Code Here

    final ImplicitAccessTokenProvider implicitProvider = new ImplicitAccessTokenProvider();
    implicitProvider.setInterceptors(Arrays
        .<ClientHttpRequestInterceptor> asList(new ClientHttpRequestInterceptor() {
          public ClientHttpResponse intercept(HttpRequest request, byte[] body,
              ClientHttpRequestExecution execution) throws IOException {
            ClientHttpResponse result = execution.execute(request, body);
            latestHeaders = result.getHeaders();
            return result;
          }
        }));
    context.setAccessTokenProvider(implicitProvider);
    context.getAccessTokenRequest().setCookie(cookie);
View Full Code Here

TOP

Related Classes of org.springframework.http.client.ClientHttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.