+++ /dev/null
-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.BeanDefinition;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-import org.springframework.beans.factory.support.BeanDefinitionRegistry;
-import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
-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;
- }
- };
- }
-
- @Bean
- static public BeanDefinitionRegistryPostProcessor test()
- {
- return new BeanDefinitionRegistryPostProcessor()
- {
- @Override
- public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException
- {
- for(String name : registry.getBeanDefinitionNames())
- {
- BeanDefinition bean = registry.getBeanDefinition(name);
- LOG.info("{}: {} - {} / {}", name, bean.getBeanClassName(), bean.getFactoryBeanName(), bean);
- }
- }
-
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException
- {
- for(String name : factory.getBeanNamesForType(Facebook.class))
- {
- BeanDefinition bean = factory.getBeanDefinition(name);
- LOG.info("{}{}", name, factory.isFactoryBean(name) ? " (factory)" : "");
- }
- }
- };
- }
-}
\ No newline at end of file