Make it possible to access actions without an extension

WW-2163


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@573790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2007-09-08 07:07:48 +00:00
parent 802ed18ca4
commit 733fb5e8a2
4 changed files with 37 additions and 11 deletions
@@ -22,11 +22,14 @@ package org.apache.struts2.dispatcher.mapper;
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 @@ public class DefaultActionMapper implements ActionMapper {
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 @@ public class DefaultActionMapper implements ActionMapper {
@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 @@ public class DefaultActionMapper implements ActionMapper {
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;
}
@@ -73,7 +73,9 @@ struts.multipart.maxSize=2097152
### 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.
@@ -132,8 +132,8 @@ public class FilterDispatcherTest extends StrutsTestCase {
return _dispatcher;
}
};
filter.setActionMapper(null);
filter.init(filterConfig);
filter.setActionMapper(null);
filter.doFilter(req, res, chain);
assertFalse(_dispatcher.serviceRequest);
@@ -411,6 +411,17 @@ public class DefaultActionMapperTest extends StrutsTestCase {
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();