KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > RequestParamAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.acting;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.SourceResolver;
29
30 /**
31  * This action makes some request details available to the sitemap via parameter
32  * substitution.
33  *
34  * {context} - is the context path of the servlet (usually "/cocoon")
35  * {requestURI} - is the requested URI without parameters
36  * {requestQuery} - is the query string like "?param1=test" if there is one
37  *
38  * Additionlly all request parameters can be made available for use in the sitemap.
39  * if the parameter "parameters" is set to true.
40  * (A variable is created for each request parameter (only if it doesn't exist)
41  * with the same name as the parameter itself)
42  *
43  * Default values can be set for request parameters, by including sitemap parameters
44  * named "default.<parameter-name>".
45  *
46  * Sitemap definition:
47  *
48  * <pre>
49  * &lt;map:action name="request" SRC="org.apache.cocoon.acting.RequestParamAction"/&gt;
50  * </pre>
51  *
52  * <p>
53  *
54  * Example use:
55  *
56  * <pre>
57  * &lt;map:match pattern="some-resource"&gt;
58  * &lt;map:act type="request"&gt;
59  * &lt;map:parameter name="parameters" value="true"/&gt;
60  * &lt;map:parameter name="default.dest" value="invalid-destination.html"/&gt;
61  * &lt;map:redirect-to uri="{context}/somewhereelse/{dest}"/&gt;
62  * &lt;/map:act&gt;
63  * &lt;/map:match&gt;
64  * </pre>
65  *
66  * Redirection is only one example, another use:
67  *
68  * <pre>
69  * &lt;map:match pattern="some-resource"&gt;
70  * &lt;map:act type="request"&gt;
71  * &lt;map:parameter name="parameters" value="true"/&gt;
72  * &lt;map:generate SRC="users/menu-{id}.xml"/&gt;
73  * &lt;/map:act&gt;
74  * &lt;map:transform SRC="menus/personalisation.xsl"/&gt;
75  * &lt;map:serialize/&gt;
76  * &lt;/map:match&gt;
77  * </pre>
78  *
79  * etc, etc.
80  *
81  * @author <a HREF="mailto:Marcus.Crafter@osa.de">Marcus Crafter</a>
82  * @author <a HREF="mailto:tcurdt@dff.st">Torsten Curdt</a>
83  * @version CVS $Id: RequestParamAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
84  */

85 public class RequestParamAction extends ServiceableAction implements ThreadSafe {
86
87     public final static String JavaDoc MAP_URI = "requestURI";
88     public final static String JavaDoc MAP_QUERY = "requestQuery";
89     public final static String JavaDoc MAP_CONTEXTPATH = "context";
90
91     public final static String JavaDoc PARAM_PARAMETERS = "parameters";
92     public final static String JavaDoc PARAM_DEFAULT_PREFIX = "default.";
93
94     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel,
95                    String JavaDoc source, Parameters param) throws Exception JavaDoc {
96
97         Request request = ObjectModelHelper.getRequest(objectModel);
98
99         Map JavaDoc map = new HashMap JavaDoc();
100
101         map.put(MAP_URI, request.getRequestURI());
102
103         String JavaDoc query = request.getQueryString();
104         if (query != null && query.length() > 0) {
105             map.put(MAP_QUERY, "?" + query);
106         } else {
107             map.put(MAP_QUERY, "");
108         }
109
110         map.put(MAP_CONTEXTPATH, request.getContextPath());
111
112         if ("true".equalsIgnoreCase(param.getParameter(PARAM_PARAMETERS, null))) {
113             Enumeration JavaDoc e = request.getParameterNames();
114             while (e.hasMoreElements()) {
115                 String JavaDoc name = (String JavaDoc)e.nextElement();
116                 String JavaDoc value = request.getParameter(name);
117
118                 if (value != null && !map.containsKey(name)) {
119                     map.put(name, value);
120                 }
121             }
122
123             String JavaDoc[] paramNames = param.getNames();
124             for (int i = 0; i < paramNames.length; i++) {
125                 if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX)
126                         && (request.getParameter(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length())) == null)) {
127                     map.put(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length()),
128                             param.getParameter(paramNames[i]));
129                 }
130             }
131         }
132         return (map);
133     }
134
135 }
136
Popular Tags