You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mr...@apache.org on 2007/05/21 15:46:50 UTC

svn commit: r540141 - in /struts/struts2/branches/STRUTS_2_0_X/core/src: main/java/org/apache/struts2/ main/java/org/apache/struts2/dispatcher/mapper/ test/java/org/apache/struts2/dispatcher/mapper/

Author: mrdon
Date: Mon May 21 06:46:48 2007
New Revision: 540141

URL: http://svn.apache.org/viewvc?view=rev&rev=540141
Log:
Adding ability to set id parameter automatically in restful 2 action mapper
WW-1939

Modified:
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/StrutsConstants.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/StrutsConstants.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/StrutsConstants.java?view=diff&rev=540141&r1=540140&r2=540141
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/StrutsConstants.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/StrutsConstants.java Mon May 21 06:46:48 2007
@@ -151,4 +151,7 @@
     /** XWork default text provider */
     public static final String STRUTS_XWORKTEXTPROVIDER = "struts.xworkTextProvider";
 
+    /** The name of the parameter to create when mapping an id (used by some action mappers) */
+	public static final String STRUTS_ID_PARAMETER_NAME = "struts.mapper.idParameterName";
+
 }

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java?view=diff&rev=540141&r1=540140&r2=540141
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.java Mon May 21 06:46:48 2007
@@ -485,11 +485,17 @@
 
         return uri.toString();
     }
+    
 
-    /**
+	public boolean isSlashesInActionNames() {
+		return allowSlashesInActionNames;
+	}
+	
+	/**
      * Defines a parameter action prefix
      */
     interface ParameterAction {
         void execute(String key, ActionMapping mapping);
     }
+
 }

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java?view=diff&rev=540141&r1=540140&r2=540141
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java Mon May 21 06:46:48 2007
@@ -21,6 +21,7 @@
 package org.apache.struts2.dispatcher.mapper;
 
 import com.opensymphony.xwork2.config.ConfigurationManager;
+import com.opensymphony.xwork2.inject.Inject;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.HashMap;
@@ -29,6 +30,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.struts2.StrutsConstants;
 
 /**
  * <!-- START SNIPPET: description -->
@@ -84,6 +86,12 @@
 
     protected static final Log LOG = LogFactory.getLog(Restful2ActionMapper.class);
     public static final String HTTP_METHOD_PARAM = "__http_method";
+    private String idParameterName = null;
+    
+    public Restful2ActionMapper() {
+    	setSlashesInActionNames("true");
+    }
+    
 
     /*
     * (non-Javadoc)
@@ -92,6 +100,9 @@
     */
     public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
 
+    	if (!isSlashesInActionNames()) {
+    		throw new IllegalStateException("This action mapper requires the setting 'slashesInActionNames' to be set to 'true'");
+    	}
         ActionMapping mapping = super.getMapping(request, configManager);
         
         if (mapping == null) {
@@ -137,6 +148,17 @@
                     }  else if (isPut(request)) {
                         mapping.setMethod("update");
                     }
+                    
+                    if (idParameterName != null) {
+                    	if (mapping.getParams() == null) {
+                            mapping.setParams(new HashMap());
+                        }
+                    	mapping.getParams().put(idParameterName, id);
+                    }
+                }
+                
+                if (idParameterName != null && lastSlashPos > -1) {
+                	actionName = actionName.substring(0, lastSlashPos);
                 }
             }
 
@@ -204,5 +226,16 @@
             return isPost(request) && "delete".equalsIgnoreCase(request.getParameter(HTTP_METHOD_PARAM));
         }
     }
+
+	public String getIdParameterName() {
+		return idParameterName;
+	}
+
+	@Inject(StrutsConstants.STRUTS_ID_PARAMETER_NAME)
+	public void setIdParameterName(String idParameterName) {
+		this.idParameterName = idParameterName;
+	}
+    
+    
 
 }

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java?view=diff&rev=540141&r1=540140&r2=540141
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java Mon May 21 06:46:48 2007
@@ -106,7 +106,6 @@
  
     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);
@@ -121,10 +120,29 @@
         assertEquals(1, mapping.getParams().size());
         assertEquals("1", mapping.getParams().get("bar"));
     }
+    
+    public void testPutUpdateWithIdParam() throws Exception {
+
+        mapper.setIdParameterName("id");
+        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("update", mapping.getMethod());
+        assertEquals(2, mapping.getParams().size());
+        assertEquals("1", mapping.getParams().get("bar"));
+        assertEquals("2", mapping.getParams().get("id"));
+        
+    }
 
     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");
@@ -144,7 +162,6 @@
 
     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);
@@ -162,7 +179,6 @@
 
     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");