craigmcc 2002/07/05 13:56:27
Modified: src/share/org/apache/struts/util RequestUtils.java
src/test/org/apache/struts/util TestRequestUtils.java
Log:
For completeness, define actionURL() to calculate the appropriate server
relative URL for an Action, along with associated unit tests.
Revision Changes Path
1.43 +37 -4
jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
Index: RequestUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- RequestUtils.java 4 Jul 2002 00:05:48 -0000 1.42
+++ RequestUtils.java 5 Jul 2002 20:56:27 -0000 1.43
@@ -98,6 +98,7 @@
import org.apache.struts.action.ActionServletWrapper;
import org.apache.struts.action.DynaActionFormClass;
import org.apache.struts.action.RequestProcessor;
+import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ApplicationConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ForwardConfig;
@@ -1133,6 +1134,38 @@
sb.append(ref);
return (sb.toString());
}
+
+ }
+
+
+ /**
+ * Return the server-relative URL that corresponds to the specified
+ * {@link ActionConfig}, relative to the sub-application associated
+ * with the current subapp's {@link ApplicationConfig}.
+ *
+ * @param request The servlet request we are processing
+ * @param action ActionConfig to be evaluated
+ * @param pattern URL pattern used to map the controller servlet
+ */
+ public static String actionURL(HttpServletRequest request,
+ ActionConfig action,
+ String pattern) {
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(request.getContextPath());
+ if (pattern.endsWith("/*")) {
+ sb.append(pattern.substring(0, pattern.length() - 2));
+ sb.append(action.getPath());
+ } else if (pattern.startsWith("*.")) {
+ ApplicationConfig appConfig = (ApplicationConfig)
+ request.getAttribute(Action.APPLICATION_KEY);
+ sb.append(appConfig.getPrefix());
+ sb.append(action.getPath());
+ sb.append(pattern.substring(1));
+ } else {
+ throw new IllegalArgumentException(pattern);
+ }
+ return (sb.toString());
}
1.4 +52 -4
jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java
Index: TestRequestUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestRequestUtils.java 4 Jul 2002 00:05:48 -0000 1.3
+++ TestRequestUtils.java 5 Jul 2002 20:56:27 -0000 1.4
@@ -152,6 +152,54 @@
}
+ // ------------------------------------------------------------ actionURL()
+
+
+ // Default application -- extension mapping
+ public void testActionURL1() {
+
+ request.setAttribute(Action.APPLICATION_KEY, appConfig);
+ request.setPathElements("/myapp", "/foo.do", null, null);
+ String url = RequestUtils.actionURL
+ (request, appConfig.findActionConfig("/dynamic"), "*.do");
+ assertNotNull("URL was returned", url);
+ assertEquals("URL value",
+ "/myapp/dynamic.do",
+ url);
+
+ }
+
+
+ // Second application -- extension mapping
+ public void testActionURL2() {
+
+ request.setAttribute(Action.APPLICATION_KEY, appConfig2);
+ request.setPathElements("/myapp", "/2/foo.do", null, null);
+ String url = RequestUtils.actionURL
+ (request, appConfig2.findActionConfig("/dynamic2"), "*.do");
+ assertNotNull("URL was returned", url);
+ assertEquals("URL value",
+ "/myapp/2/dynamic2.do",
+ url);
+
+ }
+
+
+ // Default application -- path mapping
+ public void testActionURL3() {
+
+ request.setAttribute(Action.APPLICATION_KEY, appConfig);
+ request.setPathElements("/myapp", "/do/foo", null, null);
+ String url = RequestUtils.actionURL
+ (request, appConfig.findActionConfig("/dynamic"), "/do/*");
+ assertNotNull("URL was returned", url);
+ assertEquals("URL value",
+ "/myapp/do/dynamic",
+ url);
+
+ }
+
+
// ---------------------------------------------------- computeParameters()
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>