Search in sources :

Example 1 with ResponseErrorHandler

use of org.springframework.web.client.ResponseErrorHandler in project spring-boot by spring-projects.

the class TestRestTemplateTests method withBasicAuthDoesNotResetErrorHandler.

@Test
public void withBasicAuthDoesNotResetErrorHandler() throws Exception {
    TestRestTemplate originalTemplate = new TestRestTemplate("foo", "bar");
    ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
    originalTemplate.getRestTemplate().setErrorHandler(errorHandler);
    TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", "password");
    assertThat(basicAuthTemplate.getRestTemplate().getErrorHandler()).isSameAs(errorHandler);
}
Also used : ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) Test(org.junit.Test)

Example 2 with ResponseErrorHandler

use of org.springframework.web.client.ResponseErrorHandler in project spring-security-oauth by spring-projects.

the class OAuth2ErrorHandlerTests method testBodyCanBeUsedByCustomHandler.

@Test
public void testBodyCanBeUsedByCustomHandler() throws Exception {
    final String appSpecificBodyContent = "{\"some_status\":\"app error\"}";
    OAuth2ErrorHandler handler = new OAuth2ErrorHandler(new ResponseErrorHandler() {

        public boolean hasError(ClientHttpResponse response) throws IOException {
            return true;
        }

        public void handleError(ClientHttpResponse response) throws IOException {
            InputStream body = response.getBody();
            byte[] buf = new byte[appSpecificBodyContent.length()];
            int readResponse = body.read(buf);
            Assert.assertEquals(buf.length, readResponse);
            Assert.assertEquals(appSpecificBodyContent, new String(buf, "UTF-8"));
            throw new RuntimeException("planned");
        }
    }, resource);
    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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 3 with ResponseErrorHandler

use of org.springframework.web.client.ResponseErrorHandler in project spring-security-oauth by spring-projects.

the class OAuth2ErrorHandlerTests method testCustomHandler.

@Test
public void testCustomHandler() throws Exception {
    OAuth2ErrorHandler handler = new OAuth2ErrorHandler(new ResponseErrorHandler() {

        public boolean hasError(ClientHttpResponse response) throws IOException {
            return true;
        }

        public void handleError(ClientHttpResponse response) throws IOException {
            throw new RuntimeException("planned");
        }
    }, resource);
    HttpHeaders headers = new HttpHeaders();
    ClientHttpResponse response = new TestClientHttpResponse(headers, 401);
    expected.expectMessage("planned");
    handler.handleError(response);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 4 with ResponseErrorHandler

use of org.springframework.web.client.ResponseErrorHandler in project spring-security-oauth by spring-projects.

the class AbstractAuthorizationCodeProviderTests method setupAccessTokenProvider.

@BeforeOAuth2Context
public void setupAccessTokenProvider() {
    accessTokenProvider = new AuthorizationCodeAccessTokenProvider() {

        private ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor();

        private ResponseExtractor<ResponseEntity<Void>> authExtractor = super.getAuthorizationResponseExtractor();

        private ResponseErrorHandler errorHandler = super.getResponseErrorHandler();

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    errorHandler.handleError(response);
                }
            };
        }

        @Override
        protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() {
            return new ResponseExtractor<OAuth2AccessToken>() {

                public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {
                    try {
                        response.getHeaders();
                        response.getStatusCode();
                        tokenEndpointResponse = response;
                        return extractor.extractData(response);
                    } catch (ResourceAccessException e) {
                        return null;
                    }
                }
            };
        }

        @Override
        protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
            return new ResponseExtractor<ResponseEntity<Void>>() {

                public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    return authExtractor.extractData(response);
                }
            };
        }
    };
    context.setAccessTokenProvider(accessTokenProvider);
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ResponseExtractor(org.springframework.web.client.ResponseExtractor) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) ResponseEntity(org.springframework.http.ResponseEntity) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

Example 5 with ResponseErrorHandler

use of org.springframework.web.client.ResponseErrorHandler in project spring-security-oauth by spring-projects.

the class AbstractClientCredentialsProviderTests method testInvalidCredentials.

@Test
@OAuth2ContextConfiguration(resource = InvalidClientCredentials.class, initialize = false)
public void testInvalidCredentials() throws Exception {
    context.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider() {

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    responseHeaders = response.getHeaders();
                    responseStatus = response.getStatusCode();
                }
            };
        }
    });
    try {
        context.getAccessToken();
        fail("Expected ResourceAccessException");
    } catch (Exception e) {
    // System.err.println(responseHeaders);
    // ignore
    }
    String header = responseHeaders.getFirst("WWW-Authenticate");
    assertTrue("Wrong header: " + header, header.contains("Basic realm"));
    assertEquals(HttpStatus.UNAUTHORIZED, responseStatus);
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) IOException(java.io.IOException) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) IOException(java.io.IOException) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Aggregations

ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)30 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)23 IOException (java.io.IOException)19 Test (org.junit.Test)16 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)12 RestTemplate (org.springframework.web.client.RestTemplate)11 URI (java.net.URI)7 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)6 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)6 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)6 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)6 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)6 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)6 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)6 HashMap (java.util.HashMap)6 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 ResponseExtractor (org.springframework.web.client.ResponseExtractor)6 MapPropertySource (org.springframework.core.env.MapPropertySource)5