From 8b78564d39b0cef4476dfd949a21825cc5f2485c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 12 Jun 2016 14:17:51 +0200 Subject: [PATCH] Refactoring: generalized the GraphApiErrorHandler --- .../facebook/errors/GraphApiErrorHandler.java | 17 +-- .../facebook/errors/GraphApiException.java | 3 +- .../errors/OAuth2GraphApiErrorHandler.java | 49 +++++++ .../errors/GraphApiErrorHandlerTest.java | 132 +----------------- .../spring/test-facebook-error-handler.xml | 25 ---- 5 files changed, 64 insertions(+), 162 deletions(-) create mode 100644 src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java delete mode 100644 src/test/resources/spring/test-facebook-error-handler.xml diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java index 13a93e8..e541965 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiErrorHandler.java @@ -57,15 +57,16 @@ public class GraphApiErrorHandler implements ResponseErrorHandler @Override public void handleError(final ClientHttpResponse response) throws IOException { - if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode())) - { - // We will only handle 400 BAD REQUEST - LOG.debug("ignoring response with status-code {}.", response.getStatusCode()); - parent.handleError(response); - return; - } - + GraphApiErrorHandler.handleError(parent, response); + } + public static void handleError( + final ResponseErrorHandler parent, + final ClientHttpResponse response + ) + throws + IOException + { if (response.getBody() == null) { // There is no body to interpret in the HTTP-message diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiException.java b/src/main/java/de/juplo/facebook/errors/GraphApiException.java index 45dd38a..ac57308 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiException.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiException.java @@ -11,7 +11,6 @@ import java.io.IOException; import java.io.InputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; @@ -20,7 +19,7 @@ import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; * * @author Kai Moritz */ -public class GraphApiException extends OAuth2Exception +public class GraphApiException extends RuntimeException { public enum Type { OAuthException, GraphMethodException } diff --git a/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java new file mode 100644 index 0000000..a0b5e45 --- /dev/null +++ b/src/main/java/de/juplo/facebook/errors/OAuth2GraphApiErrorHandler.java @@ -0,0 +1,49 @@ +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; +import org.springframework.web.client.ResponseErrorHandler; + + + +/** + * + * @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/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java index 9fc7fc3..9a5d302 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiErrorHandlerTest.java @@ -2,33 +2,16 @@ package de.juplo.facebook.errors; import de.juplo.facebook.errors.GraphApiException.Type; -import java.util.Date; -import java.util.Map; -import java.util.Set; -import javax.annotation.Resource; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.oauth2.client.OAuth2RestTemplate; -import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler; -import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; -import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException; -import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException; -import org.springframework.security.oauth2.client.token.AccessTokenProvider; -import org.springframework.security.oauth2.client.token.AccessTokenRequest; -import org.springframework.security.oauth2.common.OAuth2AccessToken; -import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE; -import org.springframework.security.oauth2.common.OAuth2RefreshToken; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; @@ -36,19 +19,12 @@ import org.springframework.web.client.HttpClientErrorException; * * @author Kai Moritz */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration( - locations = { - "classpath:/spring/test-facebook-error-handler.xml" - }) public class GraphApiErrorHandlerTest { private static final Logger log = LoggerFactory.getLogger(GraphApiErrorHandlerTest.class); - @Resource - private OAuth2RestTemplate clientTemplate; - + private RestTemplate clientTemplate; private MockClientHttpRequestFactory requestFactory; @@ -76,7 +52,6 @@ public class GraphApiErrorHandlerTest catch(UnknownErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(1), e.getCode()); assertEquals("An unknown error has occurred.", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -107,7 +82,6 @@ public class GraphApiErrorHandlerTest catch(UnexpectedErrorException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(2), e.getCode()); assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -138,7 +112,6 @@ public class GraphApiErrorHandlerTest catch(PageMigratedException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(21), e.getCode()); assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -169,7 +142,6 @@ public class GraphApiErrorHandlerTest catch(UnsupportedGetRequestException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(100), e.getCode()); assertEquals("Unsupported get request.", e.getMessage()); assertEquals(Type.GraphMethodException, e.getType()); @@ -191,7 +163,6 @@ public class GraphApiErrorHandlerTest catch(AccessTokenRequiredException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(104), e.getCode()); assertEquals("An access token is required to request this resource.", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -223,7 +194,6 @@ public class GraphApiErrorHandlerTest catch(RateExceededException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(613), e.getCode()); assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -243,7 +213,6 @@ public class GraphApiErrorHandlerTest catch(CallbackVerificationFailedException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(2200), e.getCode()); assertEquals("(#2200) callback verification failed: ", e.getMessage()); assertEquals(Type.OAuthException, e.getType()); @@ -275,7 +244,6 @@ public class GraphApiErrorHandlerTest catch(GraphApiException e) { log.debug("{}", e.toString()); - assertEquals("invalid_request", e.getOAuth2ErrorCode()); assertEquals(new Integer(999999999), e.getCode()); assertEquals("This error does not exist.", e.getMessage()); try @@ -697,102 +665,12 @@ public class GraphApiErrorHandlerTest requestFactory = new MockClientHttpRequestFactory(); requestFactory.setStatus(HttpStatus.BAD_REQUEST); requestFactory.addHeader("Content-Type", "application/json"); - clientTemplate.setRequestFactory(requestFactory); + clientTemplate = new RestTemplate(); + clientTemplate.setRequestFactory(requestFactory); clientTemplate.setErrorHandler( - new GraphApiErrorHandler( - (OAuth2ErrorHandler)clientTemplate.getErrorHandler() - ) + new GraphApiErrorHandler(clientTemplate.getErrorHandler()) ); - - clientTemplate.setAccessTokenProvider(new AccessTokenProvider() - { - @Override - public OAuth2AccessToken obtainAccessToken( - OAuth2ProtectedResourceDetails details, - AccessTokenRequest parameters - ) - throws - UserRedirectRequiredException, - UserApprovalRequiredException, - AccessDeniedException - { - return new OAuth2AccessToken() { - - @Override - public Map getAdditionalInformation() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public Set getScope() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public OAuth2RefreshToken getRefreshToken() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public String getTokenType() - { - return OAUTH2_TYPE; - } - - @Override - public boolean isExpired() - { - return false; - } - - @Override - public Date getExpiration() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getExpiresIn() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public String getValue() - { - return "ANY"; - } - }; - } - - @Override - public boolean supportsResource(OAuth2ProtectedResourceDetails resource) - { - return true; - } - - @Override - public OAuth2AccessToken refreshAccessToken( - OAuth2ProtectedResourceDetails resource, - OAuth2RefreshToken refreshToken, - AccessTokenRequest request - ) - throws - UserRedirectRequiredException - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource) - { - return false; - } - }); } diff --git a/src/test/resources/spring/test-facebook-error-handler.xml b/src/test/resources/spring/test-facebook-error-handler.xml deleted file mode 100644 index e1b5629..0000000 --- a/src/test/resources/spring/test-facebook-error-handler.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - -- 2.20.1