From c1c281d582d47643a1615a6525c4189d9788b9fc Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 30 Jul 2014 19:08:36 +0200 Subject: [PATCH] Upgraded all dependencies --- pom.xml | 24 +-- .../java/de/juplo/facebook/FacebookUtils.java | 35 ----- .../juplo/facebook/GraphApiErrorHandler.java | 112 +++++++++++--- .../facebook/GraphApiErrorHandlerTest.java | 138 +++++++++++------- 4 files changed, 190 insertions(+), 119 deletions(-) diff --git a/pom.xml b/pom.xml index bc9069f..e5e0440 100644 --- a/pom.xml +++ b/pom.xml @@ -58,15 +58,14 @@ 1.7 - 2.3.2 - 4.11 - 1.1.2 + 2.6.2 + 4.12 + 1.1.3 3.0.1 - 1.7.6 - 3.2.4.RELEASE - 3.2.4.RELEASE - 3.1.3.RELEASE - 1.0.5.RELEASE + 1.7.12 + 4.2.1.RELEASE + 4.0.2.RELEASE + 2.0.8.RELEASE @@ -132,7 +131,7 @@ org.springframework spring-webmvc - ${spring.version} + ${springframework.version} commons-logging @@ -147,7 +146,6 @@ com.fasterxml.jackson.core jackson-databind ${jackson.version} - provided org.springframework.security @@ -224,6 +222,12 @@ logback-classic ${logback.version} test + + + org.slf4j + slf4j-api + + diff --git a/src/main/java/de/juplo/facebook/FacebookUtils.java b/src/main/java/de/juplo/facebook/FacebookUtils.java index b55eb3b..a0e6738 100644 --- a/src/main/java/de/juplo/facebook/FacebookUtils.java +++ b/src/main/java/de/juplo/facebook/FacebookUtils.java @@ -5,7 +5,6 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; import javax.annotation.PostConstruct; -import org.codehaus.jackson.map.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; @@ -13,8 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; import org.springframework.security.oauth2.client.token.AccessTokenProvider; @@ -22,7 +19,6 @@ import org.springframework.security.oauth2.client.token.AccessTokenProviderChain import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider; import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider; import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @@ -39,41 +35,11 @@ public class FacebookUtils @Autowired(required=false) private List accessTokenProviderChain; - /** - * Needed, to extract Jackson-ObjectMapper. - * Defined by - */ - @Autowired - private RequestMappingHandlerAdapter requestMappingHandlerAdapter; - - private ObjectMapper objectMapper; @PostConstruct public void init() { - for (HttpMessageConverter messageConverter : requestMappingHandlerAdapter.getMessageConverters()) - { - if (messageConverter instanceof MappingJacksonHttpMessageConverter) - { - MappingJacksonHttpMessageConverter m = - (MappingJacksonHttpMessageConverter)messageConverter; - objectMapper = m.getObjectMapper(); - log.info( - "found ObjectMapper {} ({})", - objectMapper, - objectMapper.getClass().getCanonicalName() - ); - break; - } - } - if (objectMapper == null) - { - throw new RuntimeException( - "Unable to find MappingJAcksonHttpMessageConverter!" - ); - } - if (accessTokenProviderChain == null) { log.info("no AccessTokenProviderChain configured, creating default-chain"); @@ -110,7 +76,6 @@ public class FacebookUtils SignedRequestAwareAuthorizationCodeAccessTokenProvider provider = new SignedRequestAwareAuthorizationCodeAccessTokenProvider(); provider.setSecret(clientSecret); - provider.setObjectMapper(objectMapper); chain.add(provider); template.setAccessTokenProvider(new AccessTokenProviderChain(chain)); log.info("injecting GraphApiErrorHandler"); diff --git a/src/main/java/de/juplo/facebook/GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/GraphApiErrorHandler.java index ce4c98c..2864843 100644 --- a/src/main/java/de/juplo/facebook/GraphApiErrorHandler.java +++ b/src/main/java/de/juplo/facebook/GraphApiErrorHandler.java @@ -1,11 +1,16 @@ package de.juplo.facebook; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.List; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; +import org.springframework.util.FileCopyUtils; import org.springframework.web.client.HttpMessageConverterExtractor; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -45,35 +50,98 @@ public class GraphApiErrorHandler extends OAuth2ErrorHandler @Override public boolean hasError(ClientHttpResponse response) throws IOException { - return errorHandler.hasError(response); + return + HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series()) + || this.errorHandler.hasError(response); } @Override - public void handleError(ClientHttpResponse response) throws IOException + public void handleError(final ClientHttpResponse response) throws IOException { - HttpMessageConverterExtractor extractor = - new HttpMessageConverterExtractor<>( - GraphApiException.class, - messageConverters - ); - - try + if (!HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())) { - GraphApiException body = extractor.extractData(response); - 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. - body.setHttpErrorCode(response.getRawStatusCode()); - throw body; - } + // We should only care about 400 level errors. Ex: A 500 server error shouldn't + // be an oauth related error. + errorHandler.handleError(response); } - catch (RestClientException|HttpMessageNotReadableException e) + else { - // ignore - } + // Need to use buffered response because input stream may need to be consumed multiple times. + ClientHttpResponse bufferedResponse = new ClientHttpResponse() + { + private byte[] lazyBody; + + @Override + public HttpStatus getStatusCode() throws IOException + { + return response.getStatusCode(); + } + + @Override + 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); + } + + @Override + public HttpHeaders getHeaders() + { + return response.getHeaders(); + } + + @Override + public String getStatusText() throws IOException + { + return response.getStatusText(); + } + + @Override + public void close() + { + response.close(); + } - errorHandler.handleError(response); + @Override + public int getRawStatusCode() throws IOException + { + return response.getRawStatusCode(); + } + }; + + + HttpMessageConverterExtractor extractor = + new HttpMessageConverterExtractor<>( + GraphApiException.class, + messageConverters + ); + + try + { + GraphApiException 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. + body.setHttpErrorCode(response.getRawStatusCode()); + throw body; + } + } + catch (RestClientException|HttpMessageNotReadableException e) + { + // ignore + } + + errorHandler.handleError(bufferedResponse); + } } } diff --git a/src/test/java/de/juplo/facebook/GraphApiErrorHandlerTest.java b/src/test/java/de/juplo/facebook/GraphApiErrorHandlerTest.java index d93121b..ef849cf 100644 --- a/src/test/java/de/juplo/facebook/GraphApiErrorHandlerTest.java +++ b/src/test/java/de/juplo/facebook/GraphApiErrorHandlerTest.java @@ -11,7 +11,6 @@ import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; -import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; @@ -23,9 +22,9 @@ import org.springframework.security.oauth2.client.token.AccessTokenRequest; import org.springframework.security.oauth2.common.OAuth2AccessToken; import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE; import org.springframework.security.oauth2.common.OAuth2RefreshToken; -import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.HttpClientErrorException; @@ -256,11 +255,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -278,11 +279,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -301,11 +304,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -323,11 +328,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -346,11 +353,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -369,11 +378,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -392,11 +403,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -414,11 +427,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -429,11 +444,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -444,11 +461,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -459,11 +478,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -474,11 +495,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -489,11 +512,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -504,11 +529,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -519,11 +546,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -534,11 +563,13 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(OAuth2Exception e) + catch(HttpClientErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); - assertFalse(e instanceof GraphApiException); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); } @@ -549,11 +580,14 @@ public class GraphApiErrorHandlerTest clientTemplate.getForObject("ANY", SOME.class); fail("The expected exception was not thrown"); } - catch(HttpMessageNotReadableException e) + catch(HttpClientErrorException e) { - // TODO: OAuth2ErrorHandler fails, if body contains no valid JSON! log.debug("{}", e.toString()); } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); + } } -- 2.20.1