diff --git a/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java b/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java index c6bde988c..79cf28f33 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java @@ -58,14 +58,14 @@ import org.apache.commons.logging.LogFactory; *
* The following URL's will invoke its methods: *
- *GET: /movie => method="index"GET: /movie => method="index"GET: /movie/Thrillers => method="view", id="Thrillers"GET: /movie/Thrillers!edit => method="edit", id="Thrillers"GET: /movie/new => method="editNew"POST: /movie/Thrillers => method="create"PUT: /movie/Thrillers => method="update", id="Thrillers""DELETE: /movie/Thrillers => method="remove"GET: /movie/new => method="editNew"POST: /movie/ => method="create"PUT: /movie/Thrillers => method="update", id="Thrillers"DELETE: /movie/Thrillers => method="remove", id="Thrillers"* To simulate the HTTP methods PUT and DELETE, since they aren't supported by HTML, @@ -83,7 +83,7 @@ import org.apache.commons.logging.LogFactory; public class Restful2ActionMapper extends DefaultActionMapper { protected static final Log LOG = LogFactory.getLog(Restful2ActionMapper.class); - private static final String HTTP_METHOD_PARAM = "__http_method"; + public static final String HTTP_METHOD_PARAM = "__http_method"; /* * (non-Javadoc) @@ -116,10 +116,6 @@ public class Restful2ActionMapper extends DefaultActionMapper { // Creating a new entry on POST e.g. foo/ } else if (isPost(request)) { mapping.setMethod("create"); - - // Updating an item e.g. foo/1 - } else if (isPut(request)) { - mapping.setMethod("update"); } } else if (lastSlashPos > -1) { @@ -136,6 +132,10 @@ public class Restful2ActionMapper extends DefaultActionMapper { // Removing an item e.g. foo/1 } else if (isDelete(request)) { mapping.setMethod("remove"); + + // Updating an item e.g. foo/1 + } else if (isPut(request)) { + mapping.setMethod("update"); } } } diff --git a/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java b/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java index ab210fa4e..dd9431381 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java @@ -88,19 +88,95 @@ public class Restful2ActionMapperTest extends StrutsTestCase { assertEquals("1", mapping.getParams().get("bar")); } - public void testPutCreate() throws Exception { + public void testPostCreate() throws Exception { req.setupGetRequestURI("/my/namespace/bar/1/foo/"); req.setupGetServletPath("/my/namespace/bar/1/foo/"); req.setupGetAttribute(null); req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("POST"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/", mapping.getName()); + assertEquals("create", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testPutUpdate() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); req.setupGetMethod("PUT"); ActionMapping mapping = mapper.getMapping(req, configManager); assertEquals("/my/namespace", mapping.getNamespace()); - assertEquals("foo/", mapping.getName()); + assertEquals("foo/2", mapping.getName()); assertEquals("update", mapping.getMethod()); assertEquals(1, mapping.getParams().size()); assertEquals("1", mapping.getParams().get("bar")); } + + public void testPutUpdateWithFakePut() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("POST"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("update", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testDeleteRemove() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("DELETE"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("remove", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testDeleteRemoveWithFakeDelete() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("POST"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("remove", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } }