WIP: Spring-Upgrade auf 2.2.0
authorKai Moritz <kai@juplo.de>
Tue, 29 Oct 2019 16:17:55 +0000 (17:17 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 29 Oct 2019 16:39:14 +0000 (17:39 +0100)
src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java [deleted file]
src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java [deleted file]
src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java [new file with mode: 0644]
src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java [deleted file]
src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java [deleted file]
src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java
src/test/java/de/juplo/facebook/errors/MockClientHttpRequestFactory.java

diff --git a/src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java b/src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java
deleted file mode 100644 (file)
index 82b5b15..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-package de.juplo.facebook.errors;
-
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-import org.springframework.boot.autoconfigure.AutoConfigureAfter;
-import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration;
-import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-
-/**
- * Automatic configuration for Srping-Security-OAuth2
- *
- * @author Kai Moritz
- */
-@Configuration
-@AutoConfigureAfter({
-  WebMvcAutoConfiguration.class,
-  OAuth2ClientAutoConfiguration.class
-  })
-public class FacebookErrorsOAuth2AutoConfiguration
-{
-  private static final Logger LOG =
-      LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfiguration.class);
-
-  @Bean
-  static public BeanPostProcessor errorHandlerInjectorOAuth2()
-  {
-    LOG.info("Configuring OAuth2GraphApiErrorHandler for handling error-messages");
-
-    return new BeanPostProcessor()
-    {
-      @Override
-      public Object postProcessBeforeInitialization(
-          Object bean,
-          String beanName
-          )
-          throws
-            BeansException
-      {
-        if (bean instanceof OAuth2RestTemplate)
-        {
-          LOG.debug("Injecting OAuth2GraphApiErrorHandler in {}", bean);
-          OAuth2RestTemplate template = (OAuth2RestTemplate) bean;
-          template.setErrorHandler(
-              new OAuth2GraphApiErrorHandler(
-                  (OAuth2ErrorHandler)template.getErrorHandler()
-                  )
-              );
-        }
-
-        return bean;
-      }
-
-      @Override
-      public Object postProcessAfterInitialization(
-          Object bean,
-          String beanName
-          )
-          throws
-          BeansException
-      {
-        return bean;
-      }
-    };
-  }
-}
diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java
deleted file mode 100644 (file)
index 265f145..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-package de.juplo.facebook.errors;
-
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
-import org.springframework.util.FileCopyUtils;
-import org.springframework.web.client.ResponseErrorHandler;
-
-
-
-/**
- * Error-Handler for error-messages from the Facebook Graph-API.
- * <p>
- * This error-handler handels responses withe the HTTP-status code
- * {@code 400 BAD REQUEST}. It tries to extract and parse the error-message
- * from the HTTP-body. Successfully extracted and parsed messages are mapped
- * to a hierarchy of exceptions, that reflects the hierarchy of the error-
- * codes and -types.
- * <p>
- * If the HTTP-status-code of the response is not {@code 400 BAD REQUEST} or
- * the HTTP-body could not be extracted or parsed, this error-handler
- * delegates the handling to its parent.
- *
- * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
- * @see <a href="http://fbdevwiki.com/wiki/Error_codes">Inofficial Wiki For Facebook-Developers</a>
- * @author Kai Moritz
- */
-public class GraphApiErrorHandler implements ResponseErrorHandler
-{
-  private final static Logger LOG =
-      LoggerFactory.getLogger(GraphApiErrorHandler.class);
-
-  private final ResponseErrorHandler parent;
-
-
-  public GraphApiErrorHandler(ResponseErrorHandler errorHandler)
-  {
-    this.parent = errorHandler;
-  }
-
-
-  @Override
-  public boolean hasError(ClientHttpResponse response) throws IOException
-  {
-    return
-        HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
-        || this.parent.hasError(response);
-  }
-
-  @Override
-  public void handleError(final ClientHttpResponse response) throws IOException
-  {
-    GraphApiErrorHandler.handleError(parent, response);
-  }
-
-  public static void handleError(
-      final ResponseErrorHandler parent,
-      final ClientHttpResponse response
-      )
-      throws
-        IOException
-  {
-    if (response.getBody() == null)
-    {
-      // There is no body to interpret in the HTTP-message
-      LOG.warn("Could not convert the response into an exception, because there is no message-body.");
-      parent.handleError(response);
-      return;
-    }
-
-    final byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
-    GraphApiException error;
-
-    try
-    {
-      error = GraphApiException.create(response.getStatusCode(), response.getHeaders(), body);
-      if (LOG.isInfoEnabled())
-        LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
-    }
-    catch (Exception e)
-    {
-      // The body of the HTTP-message could not be parsed.
-      // Let the parent error-handler try to handle the response.
-
-      LOG.warn(
-          "Could not convert the response into an exception, " +
-          "because the body is unparsable: error={}, body={}",
-          e.toString(),
-          new String(body, Charset.forName("UTF-8"))
-          );
-
-      // To do so, we have to wrap the original response to fill in
-      // the buffered body, if needed
-      ClientHttpResponse buffered = new ClientHttpResponse()
-      {
-        @Override
-        public HttpStatus getStatusCode() throws IOException
-        {
-          return response.getStatusCode();
-        }
-
-        @Override
-        public synchronized InputStream getBody() throws IOException
-        {
-          return new ByteArrayInputStream(body);
-        }
-
-        @Override
-        public HttpHeaders getHeaders()
-        {
-          return response.getHeaders();
-        }
-
-        @Override
-        public String getStatusText() throws IOException
-        {
-          return response.getStatusText();
-        }
-
-        @Override
-        public void close()
-        {
-          response.close();
-        }
-
-        @Override
-        public int getRawStatusCode() throws IOException
-        {
-          return response.getRawStatusCode();
-        }
-      };
-
-      parent.handleError(buffered);
-      return;
-    }
-
-    throw error;
-  }
-}
diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java b/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java
new file mode 100644 (file)
index 0000000..ae6e2d5
--- /dev/null
@@ -0,0 +1,150 @@
+package de.juplo.facebook.errors;
+
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.util.FileCopyUtils;
+import org.springframework.web.client.DefaultResponseErrorHandler;
+import org.springframework.web.client.ResponseErrorHandler;
+
+
+
+/**
+ * Error-Handler for error-messages from the Facebook Graph-API.
+ * <p>
+ * This error-handler handels responses withe the HTTP-status code
+ * {@code 400 BAD REQUEST}. It tries to extract and parse the error-message
+ * from the HTTP-body. Successfully extracted and parsed messages are mapped
+ * to a hierarchy of exceptions, that reflects the hierarchy of the error-
+ * codes and -types.
+ * <p>
+ * If the HTTP-status-code of the response is not {@code 400 BAD REQUEST} or
+ * the HTTP-body could not be extracted or parsed, this error-handler
+ * delegates the handling to its parent.
+ *
+ * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
+ * @see <a href="http://fbdevwiki.com/wiki/Error_codes">Inofficial Wiki For Facebook-Developers</a>
+ * @author Kai Moritz
+ */
+public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler
+{
+  private final static Logger LOG =
+      LoggerFactory.getLogger(GraphApiErrorResponseErrorHandler.class);
+
+  private final ResponseErrorHandler parent;
+
+
+  public GraphApiErrorResponseErrorHandler(ResponseErrorHandler errorHandler)
+  {
+    this.parent =
+        errorHandler == null
+            ? new DefaultResponseErrorHandler()
+            : errorHandler;
+  }
+
+
+  @Override
+  public boolean hasError(ClientHttpResponse response) throws IOException
+  {
+    return
+        HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
+        || this.parent.hasError(response);
+  }
+
+  @Override
+  public void handleError(final ClientHttpResponse response) throws IOException
+  {
+    GraphApiErrorResponseErrorHandler.handleError(parent, response);
+  }
+
+  public static void handleError(
+      final ResponseErrorHandler parent,
+      final ClientHttpResponse response
+      )
+      throws
+        IOException
+  {
+    if (response.getBody() == null)
+    {
+      // There is no body to interpret in the HTTP-message
+      LOG.warn("Could not convert the response into an exception, because there is no message-body.");
+      parent.handleError(response);
+      return;
+    }
+
+    final byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
+    GraphApiException error;
+
+    try
+    {
+      error = GraphApiException.create(response.getStatusCode(), response.getHeaders(), body);
+      if (LOG.isInfoEnabled())
+        LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
+    }
+    catch (Exception e)
+    {
+      // The body of the HTTP-message could not be parsed.
+      // Let the parent error-handler try to handle the response.
+
+      LOG.warn(
+          "Could not convert the response into an exception, " +
+          "because the body is unparsable: error={}, body={}",
+          e.toString(),
+          new String(body, Charset.forName("UTF-8"))
+          );
+
+      // To do so, we have to wrap the original response to fill in
+      // the buffered body, if needed
+      ClientHttpResponse buffered = new ClientHttpResponse()
+      {
+        @Override
+        public HttpStatus getStatusCode() throws IOException
+        {
+          return response.getStatusCode();
+        }
+
+        @Override
+        public synchronized InputStream getBody() throws IOException
+        {
+          return new ByteArrayInputStream(body);
+        }
+
+        @Override
+        public HttpHeaders getHeaders()
+        {
+          return response.getHeaders();
+        }
+
+        @Override
+        public String getStatusText() throws IOException
+        {
+          return response.getStatusText();
+        }
+
+        @Override
+        public void close()
+        {
+          response.close();
+        }
+
+        @Override
+        public int getRawStatusCode() throws IOException
+        {
+          return response.getRawStatusCode();
+        }
+      };
+
+      parent.handleError(buffered);
+      return;
+    }
+
+    throw error;
+  }
+}
diff --git a/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java
deleted file mode 100644 (file)
index e83e47f..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-package de.juplo.facebook.errors;
-
-
-import java.io.IOException;
-import java.util.List;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
-import org.springframework.http.converter.HttpMessageConverter;
-import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler;
-
-
-
-/**
- *
- * @author Kai Moritz
- */
-public class OAuth2GraphApiErrorHandler extends OAuth2ErrorResponseErrorHandler
-{
-  private final OAuth2ErrorResponseErrorHandler parent;
-
-
-  public OAuth2GraphApiErrorHandler(OAuth2ErrorResponseErrorHandler handler)
-  {
-    super(null);
-    parent = handler;
-  }
-
-
-  @Override
-  public boolean hasError(ClientHttpResponse response) throws IOException
-  {
-    return
-        HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
-        || parent.hasError(response);
-  }
-
-  @Override
-  public void handleError(ClientHttpResponse response) throws IOException
-  {
-    GraphApiErrorHandler.handleError(parent, response);
-  }
-
-  @Override
-  public void setMessageConverters(List<HttpMessageConverter<?>> converters)
-  {
-    parent.setMessageConverters(converters);
-  }
-}
diff --git a/src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java b/src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java
deleted file mode 100644 (file)
index 7c1e156..0000000
+++ /dev/null
@@ -1,116 +0,0 @@
-package de.juplo.facebook.errors;
-
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import org.junit.After;
-import org.junit.Test;
-import org.springframework.context.annotation.Configuration;
-import static org.junit.Assert.*;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
-import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.mock.env.MockEnvironment;
-import org.springframework.security.oauth2.client.OAuth2RestTemplate;
-import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
-
-
-
-public class FacebookErrorsOAuth2AutoConfigurationTest
-{
-  private final Logger LOG =
-      LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfigurationTest.class);
-
-
-  private ConfigurableApplicationContext context;
-
-
-  @After
-  public void tearDown()
-  {
-    if (this.context != null)
-      this.context.close();
-  }
-
-
-  @Test
-  public void defaultNonWebConfiguration()
-  {
-    LOG.info("<-- Start Of New Test-Case!");
-    Map<String, String> properties = new HashMap<>();
-    properties.put("security.oauth2.client.client-id", "CLIENT_ID");
-    context = loadNonWebApplicationContext(EmptyConfiguration.class, properties);
-    OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class);
-    assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass());
-  }
-
-  @Test
-  public void defaultWebConfiguration()
-  {
-    LOG.info("<-- Start Of New Test-Case!");
-    Map<String, String> properties = new HashMap<>();
-    properties.put("security.oauth2.client.client-id", "CLIENT_ID");
-    context = loadWebApplicationContext(EmptyConfiguration.class, properties);
-    OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class);
-    assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass());
-  }
-
-
-  @Configuration
-  static class EmptyConfiguration
-  {
-  }
-
-
-  private ConfigurableApplicationContext loadNonWebApplicationContext(
-      Class<?> config,
-      Map<String, String> properties
-      )
-  {
-    AnnotationConfigApplicationContext ctx =
-        new AnnotationConfigApplicationContext();
-    if (properties != null)
-    {
-      MockEnvironment env = new MockEnvironment();
-      for (Entry<String, String> entry : properties.entrySet())
-        env.withProperty(entry.getKey(), entry.getValue());
-      ctx.setEnvironment(env);
-    }
-    ctx.register(FacebookErrorsOAuth2AutoConfiguration.class);
-    ctx.register(OAuth2AutoConfiguration.class);
-    ctx.register(config);
-    AutoConfigurationReportLoggingInitializer report =
-        new AutoConfigurationReportLoggingInitializer();
-    report.initialize(ctx);
-    ctx.refresh();
-    return ctx;
-  }
-
-  private ConfigurableApplicationContext loadWebApplicationContext(
-      Class<?> config,
-      Map<String, String> properties
-      )
-  {
-    AnnotationConfigWebApplicationContext ctx =
-        new AnnotationConfigWebApplicationContext();
-    if (properties != null)
-    {
-      MockEnvironment env = new MockEnvironment();
-      for (Entry<String, String> entry : properties.entrySet())
-        env.withProperty(entry.getKey(), entry.getValue());
-      ctx.setEnvironment(env);
-    }
-    ctx.register(FacebookErrorsOAuth2AutoConfiguration.class);
-    ctx.register(OAuth2AutoConfiguration.class);
-    ctx.register(config);
-    AutoConfigurationReportLoggingInitializer report =
-        new AutoConfigurationReportLoggingInitializer();
-    report.initialize(ctx);
-    ctx.refresh();
-    return ctx;
-  }
-}
index dde664e..2e03df7 100644 (file)
@@ -753,7 +753,7 @@ public class GraphApiErrorHandlerTest
     clientTemplate = new RestTemplate();
     clientTemplate.setRequestFactory(requestFactory);
     clientTemplate.setErrorHandler(
-        new GraphApiErrorHandler(clientTemplate.getErrorHandler())
+        new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
         );
   }
 
index d5b24c3..0b03cd0 100644 (file)
@@ -85,6 +85,12 @@ public class MockClientHttpRequestFactory implements ClientHttpRequestFactory
       return method;
     }
 
+    @Override
+    public String getMethodValue()
+    {
+      return method.name();
+    }
+
     @Override
     public URI getURI()
     {