Enabled autoconfiguration for Spring-Social and Spring-Security-OAuth2
[facebook-errors] / src / main / java / de / juplo / facebook / errors / FacebookErrorsSpringSocialAutoConfiguration.java
diff --git a/src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java b/src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java
new file mode 100644 (file)
index 0000000..f474be8
--- /dev/null
@@ -0,0 +1,92 @@
+package de.juplo.facebook.errors;
+
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration;
+import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.social.facebook.api.Facebook;
+import org.springframework.social.facebook.api.impl.FacebookTemplate;
+import org.springframework.web.client.ResponseErrorHandler;
+import org.springframework.web.client.RestTemplate;
+
+
+
+/**
+ * Automatic configuration for Srping-Social-Facebook
+ *
+ * @author Kai Moritz
+ */
+@Configuration
+@ConditionalOnClass(value = FacebookTemplate.class)
+@AutoConfigureAfter({
+  WebMvcAutoConfiguration.class,
+  FacebookAutoConfiguration.class
+  })
+public class FacebookErrorsSpringSocialAutoConfiguration
+{
+  private static final Logger LOG =
+      LoggerFactory.getLogger(FacebookErrorsSpringSocialAutoConfiguration.class);
+
+
+  @Bean
+  static public BeanPostProcessor errorHandlerInjectorSpringSocial()
+  {
+    LOG.info("Configuring GraphApiErrorHandler for handling error-messages");
+    return new BeanPostProcessor()
+    {
+      @Override
+      public Object postProcessBeforeInitialization(
+          Object bean,
+          String beanName
+          )
+          throws
+          BeansException
+      {
+        return bean;
+      }
+
+      @Override
+      public Object postProcessAfterInitialization(
+          Object bean,
+          String beanName
+          )
+          throws
+          BeansException
+      {
+        if (bean instanceof Facebook)
+        {
+          try
+          {
+            Facebook facebook = (Facebook) bean;
+            RestTemplate template = (RestTemplate) facebook.restOperations();
+            ResponseErrorHandler handler = template.getErrorHandler();
+            template.setErrorHandler(new GraphApiErrorHandler(handler));
+            // Be sure, that the potential exception is triggered before!
+            LOG.debug("Injecting GraphApiErrorHandler in {}", facebook);
+          }
+          catch (BeanCreationException e)
+          {
+            /**
+             * This exception is called, if the BeanPostProcessor is called
+             * with a scoped bean, while the according scope is not
+             * accessible.
+             * This happens during initialization and can safely be ignored,
+             * because we only have to inject the handler for beans, that are
+             * actually usable.
+             */
+          }
+        }
+
+        return bean;
+      }
+    };
+  }
+}
\ No newline at end of file