View Javadoc

1   /*
2    * $Id: RestfulActionMapper.java 802942 2009-08-10 22:06:48Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dispatcher.mapper;
23  
24  import java.net.URLDecoder;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.StringTokenizer;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.struts2.RequestUtils;
33  
34  import com.opensymphony.xwork2.config.ConfigurationManager;
35  import com.opensymphony.xwork2.util.logging.Logger;
36  import com.opensymphony.xwork2.util.logging.LoggerFactory;
37  
38  
39  /***
40   * <!-- START SNIPPET: description -->
41   *
42   * A custom action mapper using the following format:
43   * <p/>
44   * <p/>
45   * <ul><tt>http://HOST/ACTION_NAME/PARAM_NAME1/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
46   * <p/>
47   * You can have as many parameters you'd like to use. Alternatively the URL can be shortened to the following:
48   * <p/>
49   * <ul><tt>http://HOST/ACTION_NAME/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
50   * <p/>
51   * This is the same as:
52   * <p/>
53   * <ul><tt>http://HOST/ACTION_NAME/ACTION_NAME + "Id"/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
54   * <p/>
55   * Suppose for example we would like to display some articles by id at using the following URL sheme:
56   * <p/>
57   * <ul><tt>http://HOST/article/Id</tt></ul>
58   * <p/>
59   * <p/>
60   * Your action just needs a setArticleId() method, and requests such as /article/1, /article/2, etc will all map
61   * to that URL pattern.
62   *
63   * <!-- END SNIPPET: description -->
64   *
65   */
66  public class RestfulActionMapper implements ActionMapper {
67      protected static final Logger LOG = LoggerFactory.getLogger(RestfulActionMapper.class);
68  
69      /* (non-Javadoc)
70       * @see org.apache.struts2.dispatcher.mapper.ActionMapper#getMapping(javax.servlet.http.HttpServletRequest)
71       */
72      public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
73          String uri = RequestUtils.getServletPath(request);
74  
75          int nextSlash = uri.indexOf('/', 1);
76          if (nextSlash == -1) {
77              return null;
78          }
79  
80          String actionName = uri.substring(1, nextSlash);
81          Map<String, Object> parameters = new HashMap<String, Object>();
82          try {
83              StringTokenizer st = new StringTokenizer(uri.substring(nextSlash), "/");
84              boolean isNameTok = true;
85              String paramName = null;
86              String paramValue;
87  
88              // check if we have the first parameter name
89              if ((st.countTokens() % 2) != 0) {
90                  isNameTok = false;
91                  paramName = actionName + "Id";
92              }
93  
94              while (st.hasMoreTokens()) {
95                  if (isNameTok) {
96                      paramName = URLDecoder.decode(st.nextToken(), "UTF-8");
97                      isNameTok = false;
98                  } else {
99                      paramValue = URLDecoder.decode(st.nextToken(), "UTF-8");
100 
101                     if ((paramName != null) && (paramName.length() > 0)) {
102                         parameters.put(paramName, paramValue);
103                     }
104 
105                     isNameTok = true;
106                 }
107             }
108         } catch (Exception e) {
109             LOG.warn("Cannot determine url parameters", e);
110         }
111 
112         return new ActionMapping(actionName, "", "", parameters);
113     }
114 
115     public ActionMapping getMappingFromActionName(String actionName) {
116         return new ActionMapping(actionName, null, null, null);
117     }
118 
119     /* (non-Javadoc)
120      * @see org.apache.struts2.dispatcher.mapper.ActionMapper#getUriFromActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping)
121      */
122     public String getUriFromActionMapping(ActionMapping mapping) {
123         StringBuilder retVal = new StringBuilder();
124         retVal.append(mapping.getNamespace());
125         retVal.append(mapping.getName());
126         Object value = mapping.getParams().get(mapping.getName() + "Id");
127         if (value != null) {
128             retVal.append("/");
129             retVal.append(value);
130         } 
131 
132         return retVal.toString();
133     }
134 }