c1647fc52bb3ab97119ed3dd2baf797ece698ae1
[facebook-errors] / src / main / java / de / juplo / facebook / errors / FacebookErrorsOAuth2AutoConfiguration.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.config.BeanPostProcessor;
8 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
9 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
10 import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
13
14
15 /**
16  * Automatic configuration for Srping-Security-OAuth2
17  *
18  * @author Kai Moritz
19  */
20 @Configuration
21 @ConditionalOnClass(value = OAuth2RestTemplate.class)
22 @AutoConfigureAfter({
23   WebMvcAutoConfiguration.class,
24   OAuth2AutoConfiguration.class
25   })
26 public class FacebookErrorsOAuth2AutoConfiguration
27 {
28   private static final Logger LOG =
29       LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfiguration.class);
30
31   @Bean
32   static public BeanPostProcessor errorHandlerInjectorOAuth2()
33   {
34     LOG.info("Configuring OAuth2GraphApiErrorHandler for handling error-messages");
35
36     return new BeanPostProcessor()
37     {
38       @Override
39       public Object postProcessBeforeInitialization(
40           Object bean,
41           String beanName
42           )
43           throws
44             BeansException
45       {
46         if (bean instanceof OAuth2RestTemplate)
47         {
48           LOG.debug("Injecting OAuth2GraphApiErrorHandler in {}", bean);
49           OAuth2RestTemplate template = (OAuth2RestTemplate) bean;
50           template.setErrorHandler(
51               new OAuth2GraphApiErrorHandler(
52                   (OAuth2ErrorHandler)template.getErrorHandler()
53                   )
54               );
55         }
56
57         return bean;
58       }
59
60       @Override
61       public Object postProcessAfterInitialization(
62           Object bean,
63           String beanName
64           )
65           throws
66           BeansException
67       {
68         return bean;
69       }
70     };
71   }
72 }