f474be81a2b6830e4fb7bfcd72c8f53e4a8a0ee0
[facebook-errors] / src / main / java / de / juplo / facebook / errors / FacebookErrorsSpringSocialAutoConfiguration.java
1 package de.juplo.facebook.errors;
2
3
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.BeansException;
7 import org.springframework.beans.factory.BeanCreationException;
8 import org.springframework.beans.factory.config.BeanPostProcessor;
9 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
10 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
11 import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration;
12 import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Configuration;
15 import org.springframework.social.facebook.api.Facebook;
16 import org.springframework.social.facebook.api.impl.FacebookTemplate;
17 import org.springframework.web.client.ResponseErrorHandler;
18 import org.springframework.web.client.RestTemplate;
19
20
21
22 /**
23  * Automatic configuration for Srping-Social-Facebook
24  *
25  * @author Kai Moritz
26  */
27 @Configuration
28 @ConditionalOnClass(value = FacebookTemplate.class)
29 @AutoConfigureAfter({
30   WebMvcAutoConfiguration.class,
31   FacebookAutoConfiguration.class
32   })
33 public class FacebookErrorsSpringSocialAutoConfiguration
34 {
35   private static final Logger LOG =
36       LoggerFactory.getLogger(FacebookErrorsSpringSocialAutoConfiguration.class);
37
38
39   @Bean
40   static public BeanPostProcessor errorHandlerInjectorSpringSocial()
41   {
42     LOG.info("Configuring GraphApiErrorHandler for handling error-messages");
43     return new BeanPostProcessor()
44     {
45       @Override
46       public Object postProcessBeforeInitialization(
47           Object bean,
48           String beanName
49           )
50           throws
51           BeansException
52       {
53         return bean;
54       }
55
56       @Override
57       public Object postProcessAfterInitialization(
58           Object bean,
59           String beanName
60           )
61           throws
62           BeansException
63       {
64         if (bean instanceof Facebook)
65         {
66           try
67           {
68             Facebook facebook = (Facebook) bean;
69             RestTemplate template = (RestTemplate) facebook.restOperations();
70             ResponseErrorHandler handler = template.getErrorHandler();
71             template.setErrorHandler(new GraphApiErrorHandler(handler));
72             // Be sure, that the potential exception is triggered before!
73             LOG.debug("Injecting GraphApiErrorHandler in {}", facebook);
74           }
75           catch (BeanCreationException e)
76           {
77             /**
78              * This exception is called, if the BeanPostProcessor is called
79              * with a scoped bean, while the according scope is not
80              * accessible.
81              * This happens during initialization and can safely be ignored,
82              * because we only have to inject the handler for beans, that are
83              * actually usable.
84              */
85           }
86         }
87
88         return bean;
89       }
90     };
91   }
92 }