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