Author: rgielen Date: Fri Aug 3 08:03:12 2012 New Revision: 1368827 URL: http://svn.apache.org/viewvc?rev=1368827&view=rev Log: WW-3858 Decouple token names from their respective session attribute names
Modified: struts/struts2/trunk/ (props changed) struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/TokenHelper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/TokenHelperTest.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/TokenTagTest.java Propchange: struts/struts2/trunk/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Fri Aug 3 08:03:12 2012 @@ -6,3 +6,5 @@ *.iws target .idea + +test-output Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java?rev=1368827&r1=1368826&r2=1368827&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ExecuteAndWaitInterceptor.java Fri Aug 3 08:03:12 2012 @@ -257,8 +257,9 @@ public class ExecuteAndWaitInterceptor e if ((!executeAfterValidationPass || !secondTime) && bp != null && !bp.isDone()) { actionInvocation.getStack().push(bp.getAction()); - if (TokenHelper.getToken() != null) { - session.put(TokenHelper.getTokenName(), TokenHelper.getToken()); + final String token = TokenHelper.getToken(); + if (token != null) { + TokenHelper.setSessionToken(TokenHelper.getTokenName(), token); } Map results = proxy.getConfig().getResults(); Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java?rev=1368827&r1=1368826&r2=1368827&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java Fri Aug 3 08:03:12 2012 @@ -121,7 +121,8 @@ public class TokenSessionStoreIntercepto params.remove(tokenName); params.remove(TokenHelper.TOKEN_NAME_FIELD); - ActionInvocation savedInvocation = InvocationSessionStore.loadInvocation(tokenName, token); + String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName); + ActionInvocation savedInvocation = InvocationSessionStore.loadInvocation(sessionTokenName, token); if (savedInvocation != null) { // set the valuestack to the request scope @@ -157,7 +158,8 @@ public class TokenSessionStoreIntercepto // we know the token name and token must be there String key = TokenHelper.getTokenName(); String token = TokenHelper.getToken(key); - InvocationSessionStore.storeInvocation(key, token, invocation); + String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(key); + InvocationSessionStore.storeInvocation(sessionTokenName, token, invocation); return invocation.invoke(); } Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/TokenHelper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/TokenHelper.java?rev=1368827&r1=1368826&r2=1368827&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/TokenHelper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/TokenHelper.java Fri Aug 3 08:03:12 2012 @@ -36,10 +36,15 @@ import com.opensymphony.xwork2.util.logg */ public class TokenHelper { - /** + /** + * The default namespace for storing token session values + */ + public static final String TOKEN_NAMESPACE = "struts.tokens"; + + /** * The default name to map the token value */ - public static final String DEFAULT_TOKEN_NAME = "struts.token"; + public static final String DEFAULT_TOKEN_NAME = "token"; /** * The name of the field which will hold the token name @@ -58,31 +63,53 @@ public class TokenHelper { return setToken(DEFAULT_TOKEN_NAME); } - /** - * Sets a transaction token into the session using the provided token name. - * - * @param tokenName the name to store into the session with the token as the value - * @return the token string - */ - public static String setToken(String tokenName) { - Map session = ActionContext.getContext().getSession(); - String token = generateGUID(); - try { - session.put(tokenName, token); - } - catch(IllegalStateException e) { - // WW-1182 explain to user what the problem is - String msg = "Error creating HttpSession due response is commited to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: " + e.getMessage(); - LOG.error(msg, e); - throw new IllegalArgumentException(msg); - } - - return token; - } - + /** + * Sets a transaction token into the session based on the provided token name. + * + * @param tokenName the token name based on which a generated token value is stored into session; for actual session + * store, this name will be prefixed by a namespace. + * + * @return the token string + */ + public static String setToken( String tokenName ) { + String token = generateGUID(); + setSessionToken(tokenName, token); + return token; + } + + /** + * Put a given named token into the session map. The token will be stored with a namespace prefix prepended. + * + * @param tokenName the token name based on which given token value is stored into session; for actual session store, + * this name will be prefixed by a namespace. + * @param token the token value to store + */ + public static void setSessionToken( String tokenName, String token ) { + Map<String, Object> session = ActionContext.getContext().getSession(); + try { + session.put(buildTokenSessionAttributeName(tokenName), token); + } catch ( IllegalStateException e ) { + // WW-1182 explain to user what the problem is + String msg = "Error creating HttpSession due response is commited to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: " + e.getMessage(); + LOG.error(msg, e); + throw new IllegalArgumentException(msg); + } + } + + + /** + * Build a name-spaced token session attribute name based on the given token name. + * + * @param tokenName the token name to prefix + * + * @return the name space prefixed session token name + */ + public static String buildTokenSessionAttributeName( String tokenName ) { + return TOKEN_NAMESPACE + "." + tokenName; + } - /** - * Gets a transaction token into the session using the default token name. + /** + * Gets a transaction token from the params in the ServletActionContext using the default token name. * * @return token */ @@ -175,7 +202,8 @@ public class TokenHelper { } Map session = ActionContext.getContext().getSession(); - String sessionToken = (String) session.get(tokenName); + String tokenSessionName = buildTokenSessionAttributeName(tokenName); + String sessionToken = (String) session.get(tokenSessionName); if (!token.equals(sessionToken)) { if (LOG.isWarnEnabled()) { @@ -188,7 +216,7 @@ public class TokenHelper { } // remove the token so it won't be used again - session.remove(tokenName); + session.remove(tokenSessionName); return true; } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/TokenHelperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/TokenHelperTest.java?rev=1368827&r1=1368826&r2=1368827&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/TokenHelperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/TokenHelperTest.java Fri Aug 3 08:03:12 2012 @@ -38,22 +38,38 @@ public class TokenHelperTest extends Tes private Map session; + public void testTokenSessionNameBuilding() throws Exception { + String name = "foo"; + String sessionName = TokenHelper.buildTokenSessionAttributeName(name); + assertEquals(TokenHelper.TOKEN_NAMESPACE + "." + name, sessionName); + } public void testSetToken() { String token = TokenHelper.setToken(); - assertEquals(token, session.get(TokenHelper.DEFAULT_TOKEN_NAME)); + final String defaultSessionTokenName = TokenHelper.buildTokenSessionAttributeName(TokenHelper.DEFAULT_TOKEN_NAME); + assertEquals(token, session.get(defaultSessionTokenName)); } public void testSetTokenWithName() { String tokenName = "myTestToken"; String token = TokenHelper.setToken(tokenName); - assertEquals(token, session.get(tokenName)); + final String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName); + assertEquals(token, session.get(sessionTokenName)); } - public void testValidToken() { + public void testSetSessionToken() { + String tokenName = "myOtherTestToken"; + String token = "foobar"; + TokenHelper.setSessionToken(tokenName, token); + final String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName); + assertEquals(token, session.get(sessionTokenName)); + } + + public void testValidToken() { String tokenName = "validTokenTest"; String token = TokenHelper.setToken(tokenName); - assertEquals(token, session.get(tokenName)); + final String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName); + assertEquals(token, session.get(sessionTokenName)); ActionContext.getContext().getParameters().put(TokenHelper.TOKEN_NAME_FIELD, new String[]{tokenName}); ActionContext.getContext().getParameters().put(tokenName, new String[]{token}); assertTrue(TokenHelper.validToken()); Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/TokenTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/TokenTagTest.java?rev=1368827&r1=1368826&r2=1368827&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/TokenTagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/TokenTagTest.java Fri Aug 3 08:03:12 2012 @@ -63,7 +63,7 @@ public class TokenTagTest extends Abstra doTokenTest(tokenName, tag); String s = writer.toString(); - assertTrue(s.indexOf("name=\"" + TokenHelper.DEFAULT_TOKEN_NAME) > -1); + assertTrue(s.indexOf("name=\"" + TokenHelper.TOKEN_NAME_FIELD) > -1); assertTrue(s.indexOf("value=\"" + tokenName + "\"") > -1); assertTrue(s.indexOf("name=\"" + tokenName + "\"") > -1); @@ -87,8 +87,9 @@ public class TokenTagTest extends Abstra tag.doEndTag(); token = (String) context.get(tokenName); - assertNotNull(token); - assertEquals(token, pageContext.getSession().getAttribute(tokenName)); + assertNotNull(token); + final String sessionTokenName = TokenHelper.buildTokenSessionAttributeName(tokenName); + assertEquals(token, pageContext.getSession().getAttribute(sessionTokenName)); } catch (JspException e) { e.printStackTrace(); fail();