Upgraded all dependencies
authorKai Moritz <kai@juplo.de>
Wed, 30 Jul 2014 17:08:36 +0000 (19:08 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 7 Nov 2015 16:48:52 +0000 (17:48 +0100)
pom.xml
src/main/java/de/juplo/facebook/FacebookUtils.java
src/main/java/de/juplo/facebook/GraphApiErrorHandler.java
src/test/java/de/juplo/facebook/GraphApiErrorHandlerTest.java

diff --git a/pom.xml b/pom.xml
index bc9069f..e5e0440 100644 (file)
--- a/pom.xml
+++ b/pom.xml
 
     <!-- used versions -->
     <commons-codec.version>1.7</commons-codec.version>
-    <jackson.version>2.3.2</jackson.version>
-    <junit.version>4.11</junit.version>
-    <logback.version>1.1.2</logback.version>
+    <jackson.version>2.6.2</jackson.version>
+    <junit.version>4.12</junit.version>
+    <logback.version>1.1.3</logback.version>
     <servlet-api.version>3.0.1</servlet-api.version>
-    <slf4j.version>1.7.6</slf4j.version>
-    <spring.version>3.2.4.RELEASE</spring.version>
-    <springframework.version>3.2.4.RELEASE</springframework.version>
-    <spring-security.version>3.1.3.RELEASE</spring-security.version>
-    <spring-security-oauth2.version>1.0.5.RELEASE</spring-security-oauth2.version>
+    <slf4j.version>1.7.12</slf4j.version>
+    <springframework.version>4.2.1.RELEASE</springframework.version>
+    <spring-security.version>4.0.2.RELEASE</spring-security.version>
+    <spring-security-oauth2.version>2.0.8.RELEASE</spring-security-oauth2.version>
 
   </properties>
 
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
-      <version>${spring.version}</version>
+      <version>${springframework.version}</version>
       <exclusions>
         <exclusion>
             <groupId>commons-logging</groupId>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>${jackson.version}</version>
-      <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.springframework.security</groupId>
       <artifactId>logback-classic</artifactId>
       <version>${logback.version}</version>
       <scope>test</scope>
+      <exclusions>
+        <exclusion>
+          <groupId>org.slf4j</groupId>
+          <artifactId>slf4j-api</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
 
   </dependencies>
index b55eb3b..a0e6738 100644 (file)
@@ -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<AccessTokenProvider> accessTokenProviderChain;
-  /**
-   * Needed, to extract Jackson-ObjectMapper.
-   * Defined by <mvc:annotation-driven />
-   */
-  @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");
index ce4c98c..2864843 100644 (file)
@@ -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<GraphApiException> 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<GraphApiException> 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);
+    }
   }
 }
index d93121b..ef849cf 100644 (file)
@@ -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());
+    }
   }