1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon.acting;
18
19 import java.util.Enumeration;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25
26 import org.apache.cocoon.environment.ObjectModelHelper;
27 import org.apache.cocoon.environment.Redirector;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.environment.SourceResolver;
30
31 /**
32 * This action makes some request details available to the sitemap via parameter
33 * substitution.
34 *
35 * {context} - is the context path of the servlet (usually "/cocoon")
36 * {requestURI} - is the requested URI without parameters
37 * {requestQuery} - is the query string like "?param1=test" if there is one
38 *
39 * Additionlly all request parameters can be made available for use in the sitemap.
40 * if the parameter "parameters" is set to true.
41 * (A variable is created for each request parameter (only if it doesn't exist)
42 * with the same name as the parameter itself)
43 *
44 * Default values can be set for request parameters, by including sitemap parameters
45 * named "default.<parameter-name>".
46 *
47 * Sitemap definition:
48 *
49 * <pre>
50 * <map:action name="request" src="org.apache.cocoon.acting.RequestParamAction"/>
51 * </pre>
52 *
53 * <p>
54 *
55 * Example use:
56 *
57 * <pre>
58 * <map:match pattern="some-resource">
59 * <map:act type="request">
60 * <map:parameter name="parameters" value="true"/>
61 * <map:parameter name="default.dest" value="invalid-destination.html"/>
62 * <map:redirect-to uri="{context}/somewhereelse/{dest}"/>
63 * </map:act>
64 * </map:match>
65 * </pre>
66 *
67 * Redirection is only one example, another use:
68 *
69 * <pre>
70 * <map:match pattern="some-resource">
71 * <map:act type="request">
72 * <map:parameter name="parameters" value="true"/>
73 * <map:generate src="users/menu-{id}.xml"/>
74 * </map:act>
75 * <map:transform src="menus/personalisation.xsl"/>
76 * <map:serialize/>
77 * </map:match>
78 * </pre>
79 *
80 * etc, etc.
81 *
82 * @author <a href="mailto:Marcus.Crafter@osa.de">Marcus Crafter</a>
83 * @author <a href="mailto:tcurdt@dff.st">Torsten Curdt</a>
84 * @version CVS $Id: RequestParamAction.java 433543 2006-08-22 06:22:54Z crossley $
85 */
86 public class RequestParamAction extends ServiceableAction implements ThreadSafe {
87
88 public final static String MAP_URI = "requestURI";
89 public final static String MAP_QUERY = "requestQuery";
90 public final static String MAP_CONTEXTPATH = "context";
91
92 public final static String PARAM_PARAMETERS = "parameters";
93 public final static String PARAM_DEFAULT_PREFIX = "default.";
94
95 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel,
96 String source, Parameters param) throws Exception {
97
98 Request request = ObjectModelHelper.getRequest(objectModel);
99
100 Map map = new HashMap();
101
102 map.put(MAP_URI, request.getRequestURI());
103
104 String query = request.getQueryString();
105 if (query != null && query.length() > 0) {
106 map.put(MAP_QUERY, "?" + query);
107 } else {
108 map.put(MAP_QUERY, "");
109 }
110
111 map.put(MAP_CONTEXTPATH, request.getContextPath());
112
113 if ("true".equalsIgnoreCase(param.getParameter(PARAM_PARAMETERS, null))) {
114 Enumeration e = request.getParameterNames();
115 while (e.hasMoreElements()) {
116 String name = (String)e.nextElement();
117 String value = request.getParameter(name);
118
119 if (value != null && !map.containsKey(name)) {
120 map.put(name, value);
121 }
122 }
123
124 String[] paramNames = param.getNames();
125 for (int i = 0; i < paramNames.length; i++) {
126 if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX)
127 && (request.getParameter(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length())) == null)) {
128 map.put(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length()),
129 param.getParameter(paramNames[i]));
130 }
131 }
132 }
133 return (map);
134 }
135
136 }