From 041206d2a693d02c0cb2e72765275e55ba14049f Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 6 Jun 2013 06:03:26 +0000 Subject: [PATCH] WW-4095 WW-4094 Changes how pattern is compiled to be once per instance and changes default regexp to match underscore git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1490165 13f79535-47bb-0310-9956-ffa450edef68 --- .../dispatcher/mapper/DefaultActionMapper.java | 11 ++++++----- .../dispatcher/mapper/DefaultActionMapperTest.java | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java b/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java index 57b251da6..e03f0d70d 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java @@ -38,6 +38,7 @@ import org.apache.struts2.util.PrefixTrie; import javax.servlet.http.HttpServletRequest; import java.util.*; +import java.util.regex.Pattern; /** * @@ -170,7 +171,7 @@ public class DefaultActionMapper implements ActionMapper { protected boolean allowSlashesInActionNames = false; protected boolean alwaysSelectFullNamespace = false; protected PrefixTrie prefixTrie = null; - protected String allowedActionNames = "[a-z]*[A-Z]*[0-9]*[.\\-_!/]*"; + protected Pattern allowedActionNames = Pattern.compile("[a-zA-Z0-9._!/\\-]*"); protected List extensions = new ArrayList() {{ add("action"); @@ -262,7 +263,7 @@ public class DefaultActionMapper implements ActionMapper { @Inject(value = StrutsConstants.STRUTS_ALLOWED_ACTION_NAMES, required = false) public void setAllowedActionNames(String allowedActionNames) { - this.allowedActionNames = allowedActionNames; + this.allowedActionNames = Pattern.compile(allowedActionNames); } @Inject @@ -432,15 +433,15 @@ public class DefaultActionMapper implements ActionMapper { * @return safe action name */ protected String cleanupActionName(final String rawActionName) { - if (rawActionName.matches(allowedActionNames)) { + if (allowedActionNames.matcher(rawActionName).matches()) { return rawActionName; } else { if (LOG.isWarnEnabled()) { - LOG.warn("Action [#0] do not match allowed action names pattern [#1], cleaning it up!", + LOG.warn("Action [#0] does not match allowed action names pattern [#1], cleaning it up!", rawActionName, allowedActionNames); } String cleanActionName = rawActionName; - for(String chunk : rawActionName.split(allowedActionNames)) { + for(String chunk : allowedActionNames.split(rawActionName)) { cleanActionName = cleanActionName.replace(chunk, ""); } if (LOG.isDebugEnabled()) { diff --git a/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java b/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java index c6ecd4bfa..11edc4751 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java @@ -764,6 +764,12 @@ public class DefaultActionMapperTest extends StrutsTestCase { actionName = "test-action"; assertEquals("test-action", mapper.cleanupActionName(actionName)); + + actionName = "test_action"; + assertEquals("test_action", mapper.cleanupActionName(actionName)); + + actionName = "test!bar.action"; + assertEquals("test!bar.action", mapper.cleanupActionName(actionName)); } }