From 201cae17e8ce4face5f533bf03f050a146abb5c4 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 29 Oct 2019 09:03:46 +0100 Subject: [PATCH] Upgraded Spring Boot from 1.3.7.RELEASE to 2.2.0.RELEASE --- pom.xml | 20 +-- ...FacebookErrorsOAuth2AutoConfiguration.java | 75 ----------- ...okErrorsSpringSocialAutoConfiguration.java | 123 ------------------ ...=> GraphApiErrorResponseErrorHandler.java} | 19 ++- .../errors/OAuth2GraphApiErrorHandler.java | 48 ------- src/main/resources/META-INF/spring.factories | 3 - ...bookErrorsOAuth2AutoConfigurationTest.java | 116 ----------------- ...rorsSpringSocialAutoConfigurationTest.java | 111 ---------------- .../errors/GraphApiErrorHandlerTest.java | 2 +- .../errors/MockClientHttpRequestFactory.java | 6 + 10 files changed, 28 insertions(+), 495 deletions(-) delete mode 100644 src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java delete mode 100644 src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java rename src/main/java/de/juplo/facebook/errors/{GraphApiErrorHandler.java => GraphApiErrorResponseErrorHandler.java} (87%) delete mode 100644 src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java delete mode 100644 src/main/resources/META-INF/spring.factories delete mode 100644 src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java delete mode 100644 src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java diff --git a/pom.xml b/pom.xml index ede6ab5..c223f2a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 1.3.7.RELEASE + 2.2.0.RELEASE @@ -18,10 +18,6 @@ http://juplo.de/facebook-errors - - 2.0.6 - - @@ -69,18 +65,16 @@ - org.springframework.boot - spring-boot-autoconfigure - true + org.springframework.security + spring-security-oauth2-client - org.springframework.social - spring-social-facebook - true + org.springframework + spring-webmvc - org.springframework.security.oauth - spring-security-oauth2 + org.springframework.boot + spring-boot-autoconfigure true diff --git a/src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java b/src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java deleted file mode 100644 index 082b0d6..0000000 --- a/src/main/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfiguration.java +++ /dev/null @@ -1,75 +0,0 @@ -package de.juplo.facebook.errors; - - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.BeansException; -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.security.oauth2.OAuth2AutoConfiguration; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.oauth2.client.OAuth2RestTemplate; -import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; - - -/** - * Automatic configuration for Srping-Security-OAuth2 - * - * @author Kai Moritz - */ -@Configuration -@ConditionalOnClass(value = OAuth2RestTemplate.class) -@AutoConfigureAfter({ - WebMvcAutoConfiguration.class, - OAuth2AutoConfiguration.class - }) -public class FacebookErrorsOAuth2AutoConfiguration -{ - private static final Logger LOG = - LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfiguration.class); - - @Bean - static public BeanPostProcessor errorHandlerInjectorOAuth2() - { - LOG.info("Configuring OAuth2GraphApiErrorHandler for handling error-messages"); - - return new BeanPostProcessor() - { - @Override - public Object postProcessBeforeInitialization( - Object bean, - String beanName - ) - throws - BeansException - { - if (bean instanceof OAuth2RestTemplate) - { - LOG.debug("Injecting OAuth2GraphApiErrorHandler in {}", bean); - OAuth2RestTemplate template = (OAuth2RestTemplate) bean; - template.setErrorHandler( - new OAuth2GraphApiErrorHandler( - (OAuth2ErrorHandler)template.getErrorHandler() - ) - ); - } - - return bean; - } - - @Override - public Object postProcessAfterInitialization( - Object bean, - String beanName - ) - throws - BeansException - { - return bean; - } - }; - } -} diff --git a/src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java b/src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java deleted file mode 100644 index 49a3101..0000000 --- a/src/main/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfiguration.java +++ /dev/null @@ -1,123 +0,0 @@ -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 diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java similarity index 87% rename from src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java rename to src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java index 265f145..123c7f3 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java @@ -11,6 +11,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.FileCopyUtils; +import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler; @@ -32,17 +33,25 @@ import org.springframework.web.client.ResponseErrorHandler; * @see Inofficial Wiki For Facebook-Developers * @author Kai Moritz */ -public class GraphApiErrorHandler implements ResponseErrorHandler +public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler { private final static Logger LOG = - LoggerFactory.getLogger(GraphApiErrorHandler.class); + LoggerFactory.getLogger(GraphApiErrorResponseErrorHandler.class); private final ResponseErrorHandler parent; - public GraphApiErrorHandler(ResponseErrorHandler errorHandler) + public GraphApiErrorResponseErrorHandler() { - this.parent = errorHandler; + this(null); + } + + public GraphApiErrorResponseErrorHandler(ResponseErrorHandler errorHandler) + { + this.parent = + errorHandler == null + ? new DefaultResponseErrorHandler() + : errorHandler; } @@ -57,7 +66,7 @@ public class GraphApiErrorHandler implements ResponseErrorHandler @Override public void handleError(final ClientHttpResponse response) throws IOException { - GraphApiErrorHandler.handleError(parent, response); + GraphApiErrorResponseErrorHandler.handleError(parent, response); } public static void handleError( diff --git a/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java deleted file mode 100644 index 9158987..0000000 --- a/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.juplo.facebook.errors; - - -import java.io.IOException; -import java.util.List; -import org.springframework.http.HttpStatus; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; - - - -/** - * - * @author Kai Moritz - */ -public class OAuth2GraphApiErrorHandler extends OAuth2ErrorHandler -{ - private final OAuth2ErrorHandler parent; - - - public OAuth2GraphApiErrorHandler(OAuth2ErrorHandler handler) - { - super(null); - parent = handler; - } - - - @Override - public boolean hasError(ClientHttpResponse response) throws IOException - { - return - HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series()) - || parent.hasError(response); - } - - @Override - public void handleError(ClientHttpResponse response) throws IOException - { - GraphApiErrorHandler.handleError(parent, response); - } - - @Override - public void setMessageConverters(List> converters) - { - parent.setMessageConverters(converters); - } -} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories deleted file mode 100644 index dd02c20..0000000 --- a/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,3 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -de.juplo.facebook.errors.FacebookErrorsSpringSocialAutoConfiguration,\ -de.juplo.facebook.errors.FacebookErrorsOAuth2AutoConfiguration diff --git a/src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java b/src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java deleted file mode 100644 index 7c1e156..0000000 --- a/src/test/java/de/juplo/facebook/errors/FacebookErrorsOAuth2AutoConfigurationTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package de.juplo.facebook.errors; - - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import org.junit.After; -import org.junit.Test; -import org.springframework.context.annotation.Configuration; -import static org.junit.Assert.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer; -import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.mock.env.MockEnvironment; -import org.springframework.security.oauth2.client.OAuth2RestTemplate; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; - - - -public class FacebookErrorsOAuth2AutoConfigurationTest -{ - private final Logger LOG = - LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfigurationTest.class); - - - private ConfigurableApplicationContext context; - - - @After - public void tearDown() - { - if (this.context != null) - this.context.close(); - } - - - @Test - public void defaultNonWebConfiguration() - { - LOG.info("<-- Start Of New Test-Case!"); - Map properties = new HashMap<>(); - properties.put("security.oauth2.client.client-id", "CLIENT_ID"); - context = loadNonWebApplicationContext(EmptyConfiguration.class, properties); - OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class); - assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass()); - } - - @Test - public void defaultWebConfiguration() - { - LOG.info("<-- Start Of New Test-Case!"); - Map properties = new HashMap<>(); - properties.put("security.oauth2.client.client-id", "CLIENT_ID"); - context = loadWebApplicationContext(EmptyConfiguration.class, properties); - OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class); - assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass()); - } - - - @Configuration - static class EmptyConfiguration - { - } - - - private ConfigurableApplicationContext loadNonWebApplicationContext( - Class config, - Map properties - ) - { - AnnotationConfigApplicationContext ctx = - new AnnotationConfigApplicationContext(); - if (properties != null) - { - MockEnvironment env = new MockEnvironment(); - for (Entry entry : properties.entrySet()) - env.withProperty(entry.getKey(), entry.getValue()); - ctx.setEnvironment(env); - } - ctx.register(FacebookErrorsOAuth2AutoConfiguration.class); - ctx.register(OAuth2AutoConfiguration.class); - ctx.register(config); - AutoConfigurationReportLoggingInitializer report = - new AutoConfigurationReportLoggingInitializer(); - report.initialize(ctx); - ctx.refresh(); - return ctx; - } - - private ConfigurableApplicationContext loadWebApplicationContext( - Class config, - Map properties - ) - { - AnnotationConfigWebApplicationContext ctx = - new AnnotationConfigWebApplicationContext(); - if (properties != null) - { - MockEnvironment env = new MockEnvironment(); - for (Entry entry : properties.entrySet()) - env.withProperty(entry.getKey(), entry.getValue()); - ctx.setEnvironment(env); - } - ctx.register(FacebookErrorsOAuth2AutoConfiguration.class); - ctx.register(OAuth2AutoConfiguration.class); - ctx.register(config); - AutoConfigurationReportLoggingInitializer report = - new AutoConfigurationReportLoggingInitializer(); - report.initialize(ctx); - ctx.refresh(); - return ctx; - } -} diff --git a/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java b/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java deleted file mode 100644 index 651ced5..0000000 --- a/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package de.juplo.facebook.errors; - - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import org.junit.After; -import org.junit.Test; -import org.springframework.context.annotation.Configuration; -import static org.junit.Assert.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer; -import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration; -import org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Scope; -import org.springframework.context.annotation.ScopedProxyMode; -import org.springframework.mock.env.MockEnvironment; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.social.connect.ConnectionRepository; -import org.springframework.social.facebook.api.Facebook; -import org.springframework.social.facebook.api.impl.FacebookTemplate; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; - - - -public class FacebookErrorsSpringSocialAutoConfigurationTest -{ - private final Logger LOG = - LoggerFactory.getLogger(FacebookErrorsSpringSocialAutoConfigurationTest.class); - - - private ConfigurableApplicationContext context; - - - @After - public void tearDown() - { - if (this.context != null) - this.context.close(); - } - - - @Test - public void defaultConfiguration() - { - LOG.info("<-- Start Of New Test-Case!"); - Map properties = new HashMap<>(); - properties.put("spring.social.facebook.app-id", "APP_ID"); - properties.put("spring.social.facebook.app-secret", "APP_SECRET"); - context = loadWebApplicationContext(EmptyConfiguration.class, properties); - Facebook facebook = context.getBean("facebook", Facebook.class); - RestTemplate template = (RestTemplate) facebook.restOperations(); - assertEquals(GraphApiErrorHandler.class, template.getErrorHandler().getClass()); - } - - - @Configuration - static class EmptyConfiguration - { - /** - * We have to provide this scoped bean for all our tests. - * Otherwise, the tests will fail, if we do not set up a complete - * environment with a connection-repository and a properly mocked request, - * that holds appropriate information to fetch a connection from that - * repository. - * @param repository - * @return - */ - @Bean - @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) - public Facebook facebook(ConnectionRepository repository) - { - return new FacebookTemplate("ACCESS_TOKEN"); - } - } - - - private ConfigurableApplicationContext loadWebApplicationContext( - Class config, - Map properties - ) - { - AnnotationConfigWebApplicationContext ctx = - new AnnotationConfigWebApplicationContext(); - if (properties != null) - { - MockEnvironment env = new MockEnvironment(); - for (Entry entry : properties.entrySet()) - env.withProperty(entry.getKey(), entry.getValue()); - ctx.setEnvironment(env); - } - ctx.register(FacebookErrorsSpringSocialAutoConfiguration.class); - ctx.register(FacebookAutoConfiguration.class); - ctx.register(SocialWebAutoConfiguration.class); - ctx.register(config); - AutoConfigurationReportLoggingInitializer report = - new AutoConfigurationReportLoggingInitializer(); - report.initialize(ctx); - MockHttpServletRequest request = new MockHttpServletRequest(); - ServletRequestAttributes attributes = new ServletRequestAttributes(request); - RequestContextHolder.setRequestAttributes(attributes); - ctx.refresh(); - return ctx; - } -} diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java index dde664e..2e03df7 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java @@ -753,7 +753,7 @@ public class GraphApiErrorHandlerTest clientTemplate = new RestTemplate(); clientTemplate.setRequestFactory(requestFactory); clientTemplate.setErrorHandler( - new GraphApiErrorHandler(clientTemplate.getErrorHandler()) + new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler()) ); } diff --git a/src/test/java/de/juplo/facebook/errors/MockClientHttpRequestFactory.java b/src/test/java/de/juplo/facebook/errors/MockClientHttpRequestFactory.java index d5b24c3..0b03cd0 100644 --- a/src/test/java/de/juplo/facebook/errors/MockClientHttpRequestFactory.java +++ b/src/test/java/de/juplo/facebook/errors/MockClientHttpRequestFactory.java @@ -85,6 +85,12 @@ public class MockClientHttpRequestFactory implements ClientHttpRequestFactory return method; } + @Override + public String getMethodValue() + { + return method.name(); + } + @Override public URI getURI() { -- 2.20.1