You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ts...@apache.org on 2007/11/20 23:45:21 UTC

svn commit: r596862 - in /struts/sandbox/trunk/struts2-webwork2-plugin/src/main: java/org/apache/struts2/dispatcher/ java/org/apache/struts2/dispatcher/mapper/ java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java resources/struts-plugin.xml

Author: tschneider
Date: Tue Nov 20 14:45:21 2007
New Revision: 596862

URL: http://svn.apache.org/viewvc?rev=596862&view=rev
Log:
Added action mapper to mimic the webwork behavior for redirection of actions in the default namespace.

In S2, an action in the default namespace have "" being returned as the namespace, in webwork, the package name would be used.

Added:
    struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/
    struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/
    struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java
Modified:
    struts/sandbox/trunk/struts2-webwork2-plugin/src/main/resources/struts-plugin.xml

Added: struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java?rev=596862&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java (added)
+++ struts/sandbox/trunk/struts2-webwork2-plugin/src/main/java/org/apache/struts2/dispatcher/mapper/WebworkActionMapper.java Tue Nov 20 14:45:21 2007
@@ -0,0 +1,146 @@
+package org.apache.struts2.dispatcher.mapper;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.struts2.RequestUtils;
+import org.apache.struts2.dispatcher.ServletRedirectResult;
+import org.apache.struts2.util.PrefixTrie;
+
+import com.opensymphony.xwork2.config.ConfigurationManager;
+
+public class WebworkActionMapper extends DefaultActionMapper {
+	static final String METHOD_PREFIX = "method:";
+	static final String ACTION_PREFIX = "action:";
+	static final String REDIRECT_PREFIX = "redirect:";
+	static final String REDIRECT_ACTION_PREFIX = "redirect-action:";
+
+	PrefixTrie prefixTrie = new PrefixTrie() {
+		{
+			put(METHOD_PREFIX, new ParameterAction() {
+				public void execute(String key, ActionMapping mapping) {
+					mapping.setMethod(key.substring(METHOD_PREFIX.length()));
+				}
+			});
+
+			put(ACTION_PREFIX, new ParameterAction() {
+				public void execute(String key, ActionMapping mapping) {
+					String name = key.substring(ACTION_PREFIX.length());
+					int bang = name.indexOf('!');
+					if (bang != -1) {
+						String method = name.substring(bang + 1);
+						mapping.setMethod(method);
+						name = name.substring(0, bang);
+					}
+
+					mapping.setName(name);
+				}
+			});
+
+			put(REDIRECT_PREFIX, new ParameterAction() {
+				public void execute(String key, ActionMapping mapping) {
+					ServletRedirectResult redirect = new ServletRedirectResult();
+					redirect.setLocation(key
+							.substring(REDIRECT_PREFIX.length()));
+					mapping.setResult(redirect);
+				}
+			});
+
+			put(REDIRECT_ACTION_PREFIX, new ParameterAction() {
+				public void execute(String key, ActionMapping mapping) {
+					String location = key.substring(REDIRECT_ACTION_PREFIX
+							.length());
+					ServletRedirectResult redirect = new ServletRedirectResult();
+					String extension = getDefaultExtension();
+					if (extension != null) {
+						location += "." + extension;
+					}
+					redirect.setLocation(location);
+					mapping.setResult(redirect);
+				}
+			});
+		}
+	};
+
+	public ActionMapping getMapping(HttpServletRequest request,
+			ConfigurationManager manager) {
+		ActionMapping mapping = new ActionMapping();
+		String uri = getUri(request);
+
+		parseNameAndNamespace(uri, mapping);
+
+		handleSpecialParameters(request, mapping);
+
+		if (mapping.getName() == null) {
+			return null;
+		}
+
+		// handle "name!method" convention.
+		String name = mapping.getName();
+		int exclamation = name.lastIndexOf("!");
+		if (exclamation != -1) {
+			mapping.setName(name.substring(0, exclamation));
+			mapping.setMethod(name.substring(exclamation + 1));
+		}
+		mapping.setParams(new LinkedHashMap(request.getParameterMap()));
+		return mapping;
+	}
+
+	protected void parseNameAndNamespace(String uri, ActionMapping mapping) {
+		String namespace, name;
+		int lastSlash = uri.lastIndexOf("/");
+		if (lastSlash == -1) {
+			namespace = "";
+			name = uri;
+		} else if (lastSlash == 0) {
+			// ww-1046, assume it is the root namespace, it will fallback to
+			// default
+			// namespace anyway if not found in root namespace.
+			namespace = "/";
+			name = uri.substring(lastSlash + 1);
+		} else {
+			namespace = uri.substring(0, lastSlash);
+			name = uri.substring(lastSlash + 1);
+		}
+		mapping.setNamespace(namespace);
+		mapping.setName(dropExtension(name));
+	}
+
+	String dropExtension(String name) {
+		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;
+			}
+		}
+		return null;
+	}
+
+	String getUri(HttpServletRequest request) {
+		// handle http dispatcher includes.
+		String uri = (String) request
+				.getAttribute("javax.servlet.include.servlet_path");
+		if (uri != null) {
+			return uri;
+		}
+
+		uri = RequestUtils.getServletPath(request);
+		if (uri != null && !"".equals(uri)) {
+			return uri;
+		}
+
+		uri = request.getRequestURI();
+		return uri.substring(request.getContextPath().length());
+	}
+
+	interface ParameterAction {
+		void execute(String key, ActionMapping mapping);
+	}
+}

Modified: struts/sandbox/trunk/struts2-webwork2-plugin/src/main/resources/struts-plugin.xml
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-webwork2-plugin/src/main/resources/struts-plugin.xml?rev=596862&r1=596861&r2=596862&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-webwork2-plugin/src/main/resources/struts-plugin.xml (original)
+++ struts/sandbox/trunk/struts2-webwork2-plugin/src/main/resources/struts-plugin.xml Tue Nov 20 14:45:21 2007
@@ -6,6 +6,8 @@
     
 <struts>
 
-    <bean type="org.apache.struts2.views.TagLibrary" name="ww" class="org.apache.struts2.views.DefaultTagLibrary" />   
-
+    <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="webwork" class="org.apache.struts2.dispatcher.mapper.WebworkActionMapper" />   
+	<constant name="struts.mapper.class" value="webwork" />
+	
+    <bean type="org.apache.struts2.views.TagLibrary" name="ww" class="org.apache.struts2.views.DefaultTagLibrary" />
 </struts>