KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.opensymphony.webwork.dispatcher.mapper;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import java.net.URLDecoder JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14  * A custom action mapper using the following format:
15  * <p/>
16  * <p/>
17  * <ul><tt>http://HOST/ACTION_NAME/PARAM_NAME1/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
18  * <p/>
19  * You can have as many parameters you'd like to use. Alternatively the URL can be shortened to the following:
20  * <p/>
21  * <ul><tt>http://HOST/ACTION_NAME/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
22  * <p/>
23  * This is the same as:
24  * <p/>
25  * <ul><tt>http://HOST/ACTION_NAME/ACTION_NAME + "Id"/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
26  * <p/>
27  * Suppose for example we would like to display some articles by id at using the following URL sheme:
28  * <p/>
29  * <ul><tt>http://HOST/article/Id</tt></ul>
30  * <p/>
31  * <p/>
32  * Your action just needs a setArticleId() method, and requests such as /article/1, /article/2, etc will all map
33  * to that URL pattern.
34  *
35  * @author <a HREF="mailto:cameron@datacodex.net">Cameron Braid</a>
36  * @author <a HREF="mailto:jerome.bernard@xtremejava.com">Jerome Bernard</a>
37  * @author Patrick Lightbody
38  */

39 public class RestfulActionMapper implements ActionMapper {
40     protected static final Log LOG = LogFactory.getLog(RestfulActionMapper.class);
41
42     public ActionMapping getMapping(HttpServletRequest JavaDoc request) {
43         String JavaDoc uri = request.getServletPath();
44
45         int nextSlash = uri.indexOf('/', 1);
46         if (nextSlash == -1) {
47             return null;
48         }
49
50         String JavaDoc actionName = uri.substring(1, nextSlash);
51         HashMap JavaDoc parameters = new HashMap JavaDoc();
52         try {
53             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(uri.substring(nextSlash), "/");
54             boolean isNameTok = true;
55             String JavaDoc paramName = null;
56             String JavaDoc paramValue;
57
58             // check if we have the first parameter name
59
if ((st.countTokens() % 2) != 0) {
60                 isNameTok = false;
61                 paramName = actionName + "Id";
62             }
63
64             while (st.hasMoreTokens()) {
65                 if (isNameTok) {
66                     paramName = URLDecoder.decode(st.nextToken(), "UTF-8");
67                     isNameTok = false;
68                 } else {
69                     paramValue = URLDecoder.decode(st.nextToken(), "UTF-8");
70
71                     if ((paramName != null) && (paramName.length() > 0)) {
72                         parameters.put(paramName, paramValue);
73                     }
74
75                     isNameTok = true;
76                 }
77             }
78         } catch (Exception JavaDoc e) {
79             LOG.warn(e);
80         }
81
82         return new ActionMapping(actionName, "", "", parameters);
83     }
84
85     public String JavaDoc getUriFromActionMapping(ActionMapping mapping) {
86         String JavaDoc base = mapping.getNamespace() + mapping.getName();
87         for (Iterator JavaDoc iterator = mapping.getParams().entrySet().iterator(); iterator.hasNext();) {
88             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
89             String JavaDoc name = (String JavaDoc) entry.getKey();
90             if (name.equals(mapping.getName() + "Id")) {
91                 base = base + "/" + entry.getValue();
92                 break;
93             }
94         }
95
96         return base;
97     }
98 }
99
Popular Tags