Author: mrdon
Date: Sat Oct 27 07:28:10 2007
New Revision: 589114

URL: http://svn.apache.org/viewvc?rev=589114&view=rev
Log:
Adding support for Rails 2-stye 'animals/dog/edit'-style mappings where the 
last bit is interpreted as a method name

Modified:
    
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
    
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java?rev=589114&r1=589113&r2=589114&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/RestActionMapper.java
 Sat Oct 27 07:28:10 2007
@@ -20,21 +20,19 @@
  */
 package org.apache.struts2.rest;
 
-import java.util.HashMap;
-import java.util.Iterator;
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.struts2.StrutsConstants;
-import org.apache.struts2.dispatcher.mapper.ActionMapping;
-import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
-
 import com.opensymphony.xwork2.config.Configuration;
 import com.opensymphony.xwork2.config.ConfigurationManager;
 import com.opensymphony.xwork2.config.entities.PackageConfig;
 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Iterator;
 
 /**
  * <!-- START SNIPPET: description -->
@@ -186,8 +184,18 @@
             int lastSlashPos = fullName.lastIndexOf('/');
             String id = null;
             if (lastSlashPos > -1) {
+
+                // fun trickery to parse 'actionName/id/methodName' in the 
case of 'animals/dog/edit'
+                int prevSlashPos = fullName.lastIndexOf('/', lastSlashPos - 1);
+                if (prevSlashPos > -1) {
+                    mapping.setMethod(fullName.substring(lastSlashPos+1));
+                    fullName = fullName.substring(0, lastSlashPos);
+                    lastSlashPos = prevSlashPos;
+                }
                 id = fullName.substring(lastSlashPos+1);
             }
+
+
 
             // If a method hasn't been explicitly named, try to guess using 
ReST-style patterns
             if (mapping.getMethod() == null) {

Modified: 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java?rev=589114&r1=589113&r2=589114&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
 (original)
+++ 
struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
 Sat Oct 27 07:28:10 2007
@@ -1,28 +1,30 @@
 package org.apache.struts2.rest;
 
-import java.util.HashMap;
-
-import org.apache.struts2.dispatcher.mapper.ActionMapping;
-
 import com.opensymphony.xwork2.config.Configuration;
 import com.opensymphony.xwork2.config.ConfigurationManager;
 import com.opensymphony.xwork2.config.entities.PackageConfig;
 import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
-
 import junit.framework.TestCase;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.springframework.mock.web.MockHttpServletRequest;
 
 public class RestActionMapperTest extends TestCase {
 
     private RestActionMapper mapper;
     private ConfigurationManager configManager;
     private Configuration config;
+    private MockHttpServletRequest req;
 
     protected void setUp() throws Exception {
         super.setUp();
+        req = new MockHttpServletRequest();
+        req.setContextPath("/myapp");
+        req.setMethod("GET");
+
         mapper = new RestActionMapper();
 
         config = new DefaultConfiguration();
-        PackageConfig pkg = new PackageConfig("myns", "/my/namespace", false, 
null);
+        PackageConfig pkg = new PackageConfig("myns", "/animals", false, null);
         PackageConfig pkg2 = new PackageConfig("my", "/my", false, null);
         config.addPackageConfig("mvns", pkg);
         config.addPackageConfig("my", pkg2);
@@ -31,6 +33,93 @@
                 return config;
             }
         };
+    }
+
+    public void testGetMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog");
+        req.setServletPath("/animals/dog");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("index", mapping.getMethod());
+    }
+
+    public void testPostMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog");
+        req.setServletPath("/animals/dog");
+        req.setMethod("POST");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("create", mapping.getMethod());
+    }
+
+    public void testDeleteMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog/fido");
+        req.setServletPath("/animals/dog/fido");
+        req.setMethod("DELETE");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("destroy", mapping.getMethod());
+        assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
+    }
+
+    public void testPutMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog/fido");
+        req.setServletPath("/animals/dog/fido");
+        req.setMethod("PUT");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("update", mapping.getMethod());
+        assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
+    }
+
+    public void testGetIdMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog/fido");
+        req.setServletPath("/animals/dog/fido");
+        req.setMethod("GET");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("show", mapping.getMethod());
+        assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
+    }
+
+    public void testNewMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog/new");
+        req.setServletPath("/animals/dog/new");
+        req.setMethod("GET");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("editNew", mapping.getMethod());
+    }
+
+    public void testEditMapping() throws Exception {
+        req.setRequestURI("/myapp/animals/dog/fido/edit");
+        req.setServletPath("/animals/dog/fido/edit");
+        req.setMethod("GET");
+
+        ActionMapping mapping = mapper.getMapping(req, configManager);
+
+        assertEquals("/animals", mapping.getNamespace());
+        assertEquals("dog", mapping.getName());
+        assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
+        assertEquals("edit", mapping.getMethod());
     }
 
     public void testParseNameAndNamespace() {


Reply via email to