KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > mapper > DefaultActionMapper


1 package com.opensymphony.webwork.dispatcher.mapper;
2
3 import com.opensymphony.webwork.config.Configuration;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
10  * Default action mapper implementation, using the standard *.[ext] (where ext
11  * usually "action") pattern. This implementation does not concern itself with
12  * parameters.
13  *
14  * @author Patrick Lightbody
15  */

16 public class DefaultActionMapper implements ActionMapper {
17     public ActionMapping getMapping(HttpServletRequest JavaDoc request) {
18         String JavaDoc uri = request.getServletPath();
19         if (uri == null) {
20             uri = request.getRequestURI();
21             uri = uri.substring(request.getContextPath().length());
22         }
23         String JavaDoc includeUri = (String JavaDoc) request.getAttribute("javax.servlet.include.servlet_path");
24         if (includeUri != null) {
25             uri = includeUri;
26         }
27
28         if (!uri.endsWith("." + Configuration.get("webwork.action.extension"))) {
29             return null;
30         }
31
32         // get the namespace
33
String JavaDoc namespace = uri.substring(0, uri.lastIndexOf("/"));
34
35         // Get action name ("Foo.action" -> "Foo" action)
36
int beginIdx = uri.lastIndexOf("/");
37         int endIdx = uri.lastIndexOf(".");
38         String JavaDoc name = uri.substring(((beginIdx == -1) ? 0 : (beginIdx + 1)), (endIdx == -1) ? uri.length() : endIdx);
39
40         String JavaDoc method = "";
41         if (name.indexOf("!") != -1) {
42             endIdx = name.lastIndexOf("!");
43             method = name.substring(endIdx + 1, name.length());
44             name = name.substring(0, endIdx);
45         } else {
46             for (Iterator JavaDoc iterator = request.getParameterMap().entrySet().iterator(); iterator.hasNext();) {
47                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
48                 String JavaDoc key = (String JavaDoc) entry.getKey();
49                 if (key.startsWith("method:")) {
50                     method = key.substring("method:".length());
51                 } else if (key.startsWith("action:")) {
52                     name = key.substring("action:".length());
53                 }
54             }
55         }
56
57         return new ActionMapping(name, namespace, method, null);
58     }
59
60     public String JavaDoc getUriFromActionMapping(ActionMapping mapping) {
61         StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
62
63         uri.append(mapping.getNamespace()).append("/").append(mapping.getName());
64         if (null != mapping.getMethod() && !"".equals(mapping.getMethod())) {
65             uri.append("!").append(mapping.getMethod());
66         }
67
68         uri.append(".").append(Configuration.get("webwork.action.extension"));
69
70         return uri.toString();
71     }
72 }
73
Popular Tags