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:
018: package org.apache.cocoon.components.modules.input;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023: import org.apache.cocoon.environment.ObjectModelHelper;
024: import org.apache.cocoon.environment.Request;
025:
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.SortedSet;
032: import java.util.TreeSet;
033:
034: /**
035: * RequestParameterModule accesses request parameters. If the
036: * parameter name contains an askerisk "*" this is considered a
037: * wildcard and all parameters that would match this wildcard are
038: * considered to be part of an array of that name for
039: * getAttributeValues. Only one "*" is allowed. Wildcard matches take
040: * precedence over real arrays. In that case only the first value of
041: * such array is returned.
042: *
043: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
044: * @version $Id: RequestParameterModule.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class RequestParameterModule extends AbstractInputModule
047: implements ThreadSafe {
048:
049: public Object getAttribute(String name, Configuration modeConf,
050: Map objectModel) throws ConfigurationException {
051:
052: String pname = (String) this .settings.get("parameter", name);
053: if (modeConf != null) {
054: pname = modeConf.getAttribute("parameter", pname);
055: // preferred
056: pname = modeConf.getChild("parameter").getValue(pname);
057: }
058: return ObjectModelHelper.getRequest(objectModel).getParameter(
059: pname);
060: }
061:
062: public Iterator getAttributeNames(Configuration modeConf,
063: Map objectModel) throws ConfigurationException {
064:
065: return new IteratorHelper(ObjectModelHelper.getRequest(
066: objectModel).getParameterNames());
067: }
068:
069: public Object[] getAttributeValues(String name,
070: Configuration modeConf, Map objectModel)
071: throws ConfigurationException {
072:
073: Request request = ObjectModelHelper.getRequest(objectModel);
074: String wildcard = (String) this .settings.get("parameter", name);
075: if (modeConf != null) {
076: wildcard = modeConf.getAttribute("parameter", wildcard);
077: // preferred
078: wildcard = modeConf.getChild("parameter")
079: .getValue(wildcard);
080: }
081: int wildcardIndex = wildcard.indexOf("*");
082: if (wildcardIndex != -1) {
083: // "*" contained in parameter name => combine all
084: // parameters' values that match prefix, suffix
085:
086: // split the parameter's name so that the "*" could be
087: // determined by looking at the parameters' names that
088: // start with the prefix and end with the suffix
089: //
090: String prefix = wildcard.substring(0, wildcardIndex);
091: String suffix;
092: if (wildcard.length() >= wildcardIndex + 1) {
093: suffix = wildcard.substring(wildcardIndex + 1);
094: } else {
095: suffix = "";
096: }
097: SortedSet names = new TreeSet();
098: Enumeration allNames = request.getParameterNames();
099:
100: while (allNames.hasMoreElements()) {
101: String pname = (String) allNames.nextElement();
102: if (pname.startsWith(prefix) && pname.endsWith(suffix)) {
103: names.add(pname);
104: }
105: }
106:
107: List values = new LinkedList();
108: Iterator j = names.iterator();
109: while (j.hasNext()) {
110: String pname = (String) j.next();
111: values.add(request.getParameter(pname));
112: }
113:
114: return values.toArray();
115:
116: } else {
117: // no "*" in parameter name => just return all values of
118: // this one parameter.
119:
120: return request.getParameterValues(wildcard);
121:
122: }
123:
124: }
125:
126: }
|