Author: mrdon Date: Sat Sep 8 00:07:48 2007 New Revision: 573790 URL: http://svn.apache.org/viewvc?rev=573790&view=rev Log: Make it possible to access actions without an extension WW-2163
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/FilterDispatcherTest.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java?rev=573790&r1=573789&r2=573790&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java Sat Sep 8 00:07:48 2007 @@ -22,11 +22,14 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Scanner; import java.util.Set; +import java.util.StringTokenizer; import javax.servlet.http.HttpServletRequest; @@ -178,7 +181,7 @@ protected PrefixTrie prefixTrie = null; - protected List extensions = new ArrayList() {{ add("action");}}; + protected List<String> extensions = new ArrayList<String>() {{ add("action"); add("");}}; protected Container container; @@ -270,7 +273,12 @@ @Inject(StrutsConstants.STRUTS_ACTION_EXTENSION) public void setExtensions(String extensions) { if (!"".equals(extensions)) { - this.extensions = Arrays.asList(extensions.split(",")); + List<String> list = new ArrayList<String>(); + Scanner scanner = new Scanner(extensions).useDelimiter(","); + while (scanner.hasNext()) { + list.add(scanner.next()); + } + this.extensions = Collections.unmodifiableList(list); } else { this.extensions = null; } @@ -414,13 +422,18 @@ if (extensions == null) { return name; } - Iterator it = extensions.iterator(); - while (it.hasNext()) { - String extension = "." + (String) it.next(); - if (name.endsWith(extension)) { - name = name.substring(0, name.length() - extension.length()); - return name; - } + for (String ext : extensions) { + if ("".equals(ext)) { + if (name.indexOf('.') == -1) { + return name; + } + } else { + String extension = "." + ext; + if (name.endsWith(extension)) { + name = name.substring(0, name.length() - extension.length()); + return name; + } + } } return null; } Modified: struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties?rev=573790&r1=573789&r2=573790&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties (original) +++ struts/struts2/trunk/core/src/main/resources/org/apache/struts2/default.properties Sat Sep 8 00:07:48 2007 @@ -73,7 +73,9 @@ ### Used by the DefaultActionMapper ### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do -struts.action.extension=action +### The blank extension allows you to match directory listings as well as pure action names +### without interfering with static resources. +struts.action.extension=action,, ### Used by FilterDispatcher ### If true then Struts serves static content from inside its jar. Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/FilterDispatcherTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/FilterDispatcherTest.java?rev=573790&r1=573789&r2=573790&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/FilterDispatcherTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/FilterDispatcherTest.java Sat Sep 8 00:07:48 2007 @@ -132,8 +132,8 @@ return _dispatcher; } }; - filter.setActionMapper(null); filter.init(filterConfig); + filter.setActionMapper(null); filter.doFilter(req, res, chain); assertFalse(_dispatcher.serviceRequest); Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java?rev=573790&r1=573789&r2=573790&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapperTest.java Sat Sep 8 00:07:48 2007 @@ -411,6 +411,17 @@ assertTrue("Name not right: "+name, "foo.action".equals(name)); } + + public void testDropExtensionWhenBlank() throws Exception { + DefaultActionMapper mapper = new DefaultActionMapper(); + mapper.setExtensions("action,,"); + String name = mapper.dropExtension("foo.action"); + assertTrue("Name not right: "+name, "foo".equals(name)); + name = mapper.dropExtension("foo"); + assertTrue("Name not right: "+name, "foo".equals(name)); + assertNull(mapper.dropExtension("foo.bar")); + assertNull(mapper.dropExtension("foo.")); + } public void testGetUriFromActionMapper1() throws Exception { DefaultActionMapper mapper = new DefaultActionMapper();