001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.acting;
018:
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.avalon.framework.thread.ThreadSafe;
025:
026: import org.apache.cocoon.environment.ObjectModelHelper;
027: import org.apache.cocoon.environment.Redirector;
028: import org.apache.cocoon.environment.Request;
029: import org.apache.cocoon.environment.SourceResolver;
030:
031: /**
032: * This action makes some request details available to the sitemap via parameter
033: * substitution.
034: *
035: * {context} - is the context path of the servlet (usually "/cocoon")
036: * {requestURI} - is the requested URI without parameters
037: * {requestQuery} - is the query string like "?param1=test" if there is one
038: *
039: * Additionlly all request parameters can be made available for use in the sitemap.
040: * if the parameter "parameters" is set to true.
041: * (A variable is created for each request parameter (only if it doesn't exist)
042: * with the same name as the parameter itself)
043: *
044: * Default values can be set for request parameters, by including sitemap parameters
045: * named "default.<parameter-name>".
046: *
047: * Sitemap definition:
048: *
049: * <pre>
050: * <map:action name="request" src="org.apache.cocoon.acting.RequestParamAction"/>
051: * </pre>
052: *
053: * <p>
054: *
055: * Example use:
056: *
057: * <pre>
058: * <map:match pattern="some-resource">
059: * <map:act type="request">
060: * <map:parameter name="parameters" value="true"/>
061: * <map:parameter name="default.dest" value="invalid-destination.html"/>
062: * <map:redirect-to uri="{context}/somewhereelse/{dest}"/>
063: * </map:act>
064: * </map:match>
065: * </pre>
066: *
067: * Redirection is only one example, another use:
068: *
069: * <pre>
070: * <map:match pattern="some-resource">
071: * <map:act type="request">
072: * <map:parameter name="parameters" value="true"/>
073: * <map:generate src="users/menu-{id}.xml"/>
074: * </map:act>
075: * <map:transform src="menus/personalisation.xsl"/>
076: * <map:serialize/>
077: * </map:match>
078: * </pre>
079: *
080: * etc, etc.
081: *
082: * @author <a href="mailto:Marcus.Crafter@osa.de">Marcus Crafter</a>
083: * @author <a href="mailto:tcurdt@dff.st">Torsten Curdt</a>
084: * @version CVS $Id: RequestParamAction.java 433543 2006-08-22 06:22:54Z crossley $
085: */
086: public class RequestParamAction extends ServiceableAction implements
087: ThreadSafe {
088:
089: public final static String MAP_URI = "requestURI";
090: public final static String MAP_QUERY = "requestQuery";
091: public final static String MAP_CONTEXTPATH = "context";
092:
093: public final static String PARAM_PARAMETERS = "parameters";
094: public final static String PARAM_DEFAULT_PREFIX = "default.";
095:
096: public Map act(Redirector redirector, SourceResolver resolver,
097: Map objectModel, String source, Parameters param)
098: throws Exception {
099:
100: Request request = ObjectModelHelper.getRequest(objectModel);
101:
102: Map map = new HashMap();
103:
104: map.put(MAP_URI, request.getRequestURI());
105:
106: String query = request.getQueryString();
107: if (query != null && query.length() > 0) {
108: map.put(MAP_QUERY, "?" + query);
109: } else {
110: map.put(MAP_QUERY, "");
111: }
112:
113: map.put(MAP_CONTEXTPATH, request.getContextPath());
114:
115: if ("true".equalsIgnoreCase(param.getParameter(
116: PARAM_PARAMETERS, null))) {
117: Enumeration e = request.getParameterNames();
118: while (e.hasMoreElements()) {
119: String name = (String) e.nextElement();
120: String value = request.getParameter(name);
121:
122: if (value != null && !map.containsKey(name)) {
123: map.put(name, value);
124: }
125: }
126:
127: String[] paramNames = param.getNames();
128: for (int i = 0; i < paramNames.length; i++) {
129: if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX)
130: && (request.getParameter(paramNames[i]
131: .substring(PARAM_DEFAULT_PREFIX
132: .length())) == null)) {
133: map.put(paramNames[i]
134: .substring(PARAM_DEFAULT_PREFIX.length()),
135: param.getParameter(paramNames[i]));
136: }
137: }
138: }
139: return (map);
140: }
141:
142: }
|