Author: hsaputra Date: Sun Nov 28 06:47:08 2010 New Revision: 1039836 URL: http://svn.apache.org/viewvc?rev=1039836&view=rev Log: Refactor AuthInfo class to final AuthInfoUtil class.
See CR at http://codereview.appspot.com/3305041 Added: shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfoUtil.java - copied, changed from r1038504, shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfo.java shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoUtilTest.java - copied, changed from r1038504, shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoTest.java Removed: shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfo.java shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoTest.java Modified: shindig/trunk/extras/src/test/java/org/apache/shindig/extras/as/dataservice/integration/AbstractActivityStreamsRestfulTests.java shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthenticationServletFilter.java shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/DataServiceServletTest.java shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpGadgetContext.java shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpGadgetContextTest.java shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/dataservice/integration/AbstractLargeRestfulTests.java Modified: shindig/trunk/extras/src/test/java/org/apache/shindig/extras/as/dataservice/integration/AbstractActivityStreamsRestfulTests.java URL: http://svn.apache.org/viewvc/shindig/trunk/extras/src/test/java/org/apache/shindig/extras/as/dataservice/integration/AbstractActivityStreamsRestfulTests.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/extras/src/test/java/org/apache/shindig/extras/as/dataservice/integration/AbstractActivityStreamsRestfulTests.java (original) +++ shindig/trunk/extras/src/test/java/org/apache/shindig/extras/as/dataservice/integration/AbstractActivityStreamsRestfulTests.java Sun Nov 28 06:47:08 2010 @@ -31,7 +31,7 @@ import javax.xml.stream.XMLStreamConstan import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.common.EasyMockTestCase; import org.apache.shindig.common.testing.FakeGadgetToken; import org.apache.shindig.common.testing.FakeHttpServletRequest; @@ -139,7 +139,7 @@ public abstract class AbstractActivitySt req.setPathInfo(path); req.setParameter("format",format); req.setParameter("X-HTTP-Method-Override", method); - req.setAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); + req.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); req.setContentType(contentType); for (Map.Entry<String,String> entry : extraParams.entrySet()) { req.setParameter(entry.getKey(), entry.getValue()); Copied: shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfoUtil.java (from r1038504, shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfo.java) URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfoUtil.java?p2=shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfoUtil.java&p1=shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfo.java&r1=1038504&r2=1039836&rev=1039836&view=diff ============================================================================== --- shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfo.java (original) +++ shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthInfoUtil.java Sun Nov 28 06:47:08 2010 @@ -17,8 +17,6 @@ */ package org.apache.shindig.auth; -import com.google.inject.Inject; - import javax.servlet.http.HttpServletRequest; /** @@ -28,17 +26,8 @@ import javax.servlet.http.HttpServletReq * is no way to set in a public API. This can be added in the future for testing * purposes. */ -public class AuthInfo { - private HttpServletRequest req; - - /** - * Create AuthInfo from a given HttpServletRequest - * @param req - */ - @Inject - public AuthInfo(HttpServletRequest req) { - this.req = req; - } +public final class AuthInfoUtil { + private AuthInfoUtil() {} /** * Constants for request attribute keys @@ -61,7 +50,7 @@ public class AuthInfo { * * @return The security token */ - public SecurityToken getSecurityToken() { + public static SecurityToken getSecurityTokenFromRequest(HttpServletRequest req) { return getRequestAttribute(req, Attribute.SECURITY_TOKEN); } @@ -70,30 +59,28 @@ public class AuthInfo { * * @return The domain, or {...@code null} if no domain was found */ - public String getAuthType() { + public static String getAuthTypeFromRequest(HttpServletRequest req) { return getRequestAttribute(req, Attribute.AUTH_TYPE); } /** * Set the security token for the request. * + * @param req The request object * @param token The security token - * @return This object */ - AuthInfo setSecurityToken(SecurityToken token) { + public static void setSecurityTokenForRequest(HttpServletRequest req, SecurityToken token) { setRequestAttribute(req, Attribute.SECURITY_TOKEN, token); - return this; } /** * Set the auth type for the request. * + * @param req The request object * @param authType The named auth type - * @return This object */ - AuthInfo setAuthType(String authType) { + public static void setAuthTypeForRequest(HttpServletRequest req, String authType) { setRequestAttribute(req, Attribute.AUTH_TYPE, authType); - return this; } /** Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthenticationServletFilter.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthenticationServletFilter.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthenticationServletFilter.java (original) +++ shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/AuthenticationServletFilter.java Sun Nov 28 06:47:08 2010 @@ -83,7 +83,8 @@ public class AuthenticationServletFilter for (AuthenticationHandler handler : handlers) { SecurityToken token = handler.getSecurityTokenFromRequest(req); if (token != null) { - new AuthInfo(req).setAuthType(handler.getName()).setSecurityToken(token); + AuthInfoUtil.setAuthTypeForRequest(req, handler.getName()); + AuthInfoUtil.setSecurityTokenForRequest(req, token); callChain(chain, req, resp); return; } else { Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java (original) +++ shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java Sun Nov 28 06:47:08 2010 @@ -17,7 +17,7 @@ */ package org.apache.shindig.protocol; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.auth.SecurityToken; import org.apache.shindig.common.servlet.InjectedServlet; import org.apache.shindig.config.ContainerConfig; @@ -110,7 +110,7 @@ public abstract class ApiServlet extends } protected SecurityToken getSecurityToken(HttpServletRequest servletRequest) { - return new AuthInfo(servletRequest).getSecurityToken(); + return AuthInfoUtil.getSecurityTokenFromRequest(servletRequest); } protected abstract void sendError(HttpServletResponse servletResponse, ResponseItem responseItem) Copied: shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoUtilTest.java (from r1038504, shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoTest.java) URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoUtilTest.java?p2=shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoUtilTest.java&p1=shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoTest.java&r1=1038504&r2=1039836&rev=1039836&view=diff ============================================================================== --- shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoTest.java (original) +++ shindig/trunk/java/common/src/test/java/org/apache/shindig/auth/AuthInfoUtilTest.java Sun Nov 28 06:47:08 2010 @@ -17,10 +17,6 @@ */ package org.apache.shindig.auth; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; - import org.apache.shindig.common.testing.FakeGadgetToken; import org.apache.shindig.common.testing.FakeHttpServletRequest; @@ -29,53 +25,24 @@ import org.junit.Test; import javax.servlet.http.HttpServletRequest; -public class AuthInfoTest extends Assert { +public class AuthInfoUtilTest extends Assert { @Test public void testToken() throws Exception { HttpServletRequest req = new FakeHttpServletRequest(); SecurityToken token = new FakeGadgetToken(); - AuthInfo info = new AuthInfo(req).setSecurityToken(token); + AuthInfoUtil.setSecurityTokenForRequest(req, token); - assertEquals(token, info.getSecurityToken()); - // This should work when creating a new AuthInfo from the same request - assertEquals(token, new AuthInfo(req).getSecurityToken()); + assertEquals(token, AuthInfoUtil.getSecurityTokenFromRequest(req)); } @Test public void testAuthType() throws Exception { HttpServletRequest req = new FakeHttpServletRequest(); - AuthInfo info = new AuthInfo(req).setAuthType("FakeAuth"); - - assertEquals("FakeAuth", info.getAuthType()); - // This should work when creating a new AuthInfo from the same request - assertEquals("FakeAuth", new AuthInfo(req).getAuthType()); - } + AuthInfoUtil.setAuthTypeForRequest(req, "FakeAuth"); - @Test - public void testBinding() throws Exception { - HttpServletRequest req = new FakeHttpServletRequest(); - SecurityToken token = new FakeGadgetToken(); - new AuthInfo(req).setSecurityToken(token).setAuthType("FakeAuth"); - - Injector injector = Guice.createInjector(new TestModule(req)); - AuthInfo injected = injector.getInstance(AuthInfo.class); - assertEquals(token, injected.getSecurityToken()); - assertEquals("FakeAuth", injected.getAuthType()); - } - - private static class TestModule extends AbstractModule { - private HttpServletRequest req; - - public TestModule(HttpServletRequest req) { - this.req = req; - } - @Override - protected void configure() { - bind(HttpServletRequest.class).toInstance(req); - } - + assertEquals("FakeAuth", AuthInfoUtil.getAuthTypeFromRequest(req)); } } Modified: shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/DataServiceServletTest.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/DataServiceServletTest.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/DataServiceServletTest.java (original) +++ shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/DataServiceServletTest.java Sun Nov 28 06:47:08 2010 @@ -24,7 +24,7 @@ import javax.servlet.http.HttpServletReq import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.common.testing.FakeGadgetToken; import org.apache.shindig.common.testing.FakeHttpServletRequest; import org.apache.shindig.config.ContainerConfig; @@ -156,7 +156,7 @@ public class DataServiceServletTest exte fakeReq.setPostData("", "UTF-8"); } fakeReq.setMethod(actualMethod); - fakeReq.setAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); + fakeReq.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); fakeReq.setContentType(ContentTypes.OUTPUT_JSON_CONTENT_TYPE); req = fakeReq; } Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpGadgetContext.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpGadgetContext.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpGadgetContext.java (original) +++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/HttpGadgetContext.java Sun Nov 28 06:47:08 2010 @@ -19,7 +19,7 @@ package org.apache.shindig.gadgets.servlet; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.auth.SecurityToken; import org.apache.shindig.common.uri.Uri; import org.apache.shindig.gadgets.GadgetContext; @@ -139,7 +139,7 @@ public class HttpGadgetContext extends G @Override public SecurityToken getToken() { - return new AuthInfo(request).getSecurityToken(); + return AuthInfoUtil.getSecurityTokenFromRequest(request); } @Override Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java (original) +++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java Sun Nov 28 06:47:08 2010 @@ -23,7 +23,7 @@ import com.google.inject.Provider; import com.google.inject.Singleton; import org.apache.commons.lang.StringUtils; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.auth.SecurityToken; import org.apache.shindig.common.JsonSerializer; import org.apache.shindig.common.servlet.HttpUtil; @@ -259,7 +259,7 @@ public class MakeRequestHandler { * @return A valid token for the given input. */ private SecurityToken extractAndValidateToken(HttpServletRequest request) throws GadgetException { - SecurityToken token = new AuthInfo(request).getSecurityToken(); + SecurityToken token = AuthInfoUtil.getSecurityTokenFromRequest(request); if (token == null) { // TODO: Determine appropriate external error code for this. throw new GadgetException(GadgetException.Code.INVALID_SECURITY_TOKEN); Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpGadgetContextTest.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpGadgetContextTest.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpGadgetContextTest.java (original) +++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpGadgetContextTest.java Sun Nov 28 06:47:08 2010 @@ -20,7 +20,7 @@ package org.apache.shindig.gadgets.servl import static org.easymock.EasyMock.expect; import org.apache.shindig.auth.AnonymousSecurityToken; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.auth.SecurityToken; import org.apache.shindig.gadgets.GadgetContext; import org.junit.Test; @@ -80,7 +80,7 @@ public class HttpGadgetContextTest exten @Test public void testGetSecurityToken() throws Exception { SecurityToken expected = new AnonymousSecurityToken(); - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())).andReturn(expected); + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())).andReturn(expected); replay(); GadgetContext context = new HttpGadgetContext(request); assertEquals(expected, context.getToken()); Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java (original) +++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java Sun Nov 28 06:47:08 2010 @@ -24,7 +24,7 @@ import static org.easymock.EasyMock.expe import com.google.common.collect.Lists; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.auth.SecurityToken; import org.apache.shindig.common.servlet.HttpUtilTest; import org.apache.shindig.common.testing.FakeGadgetToken; @@ -313,7 +313,7 @@ public class MakeRequestHandlerTest exte @Test public void testSignedGetRequest() throws Exception { - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())) + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); @@ -336,7 +336,7 @@ public class MakeRequestHandlerTest exte // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectPostAndReturnBody(AuthType.SIGNED, REQUEST_BODY, RESPONSE_BODY); - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())) + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); @@ -359,7 +359,7 @@ public class MakeRequestHandlerTest exte // Signing tests are in SigningFetcherTest expectGetAndReturnBody(AuthType.SIGNED, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken().setUpdatedToken("updated"); - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())) + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); @@ -380,7 +380,7 @@ public class MakeRequestHandlerTest exte // OAuth tests are in OAuthRequestTest expectGetAndReturnBody(AuthType.OAUTH, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken().setUpdatedToken("updated"); - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())) + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.OAUTH.toString()).atLeastOnce(); @@ -426,7 +426,7 @@ public class MakeRequestHandlerTest exte @Test(expected=GadgetException.class) public void testBadSecurityTokenThrows() throws Exception { - expect(request.getAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId())) + expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(null).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); Modified: shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/dataservice/integration/AbstractLargeRestfulTests.java URL: http://svn.apache.org/viewvc/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/dataservice/integration/AbstractLargeRestfulTests.java?rev=1039836&r1=1039835&r2=1039836&view=diff ============================================================================== --- shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/dataservice/integration/AbstractLargeRestfulTests.java (original) +++ shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/dataservice/integration/AbstractLargeRestfulTests.java Sun Nov 28 06:47:08 2010 @@ -17,7 +17,7 @@ */ package org.apache.shindig.social.dataservice.integration; -import org.apache.shindig.auth.AuthInfo; +import org.apache.shindig.auth.AuthInfoUtil; import org.apache.shindig.common.EasyMockTestCase; import org.apache.shindig.common.testing.FakeGadgetToken; import org.apache.shindig.common.testing.FakeHttpServletRequest; @@ -138,7 +138,7 @@ public abstract class AbstractLargeRestf req.setPathInfo(path); req.setParameter("format",format); req.setParameter("X-HTTP-Method-Override", method); - req.setAttribute(AuthInfo.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); + req.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); req.setContentType(contentType); for (Map.Entry<String,String> entry : extraParams.entrySet()) { req.setParameter(entry.getKey(), entry.getValue());
