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: * HeaderAttributeModule accesses request header attributes. If the
036: * attribute name contains an askerisk "*" this is considered a
037: * wildcard and all attributes that would match this wildcard are
038: * considered to be part of an array of that name for
039: * getAttributeValues. Only one "*" is allowed.
040: *
041: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
042: * @version $Id: HeaderAttributeModule.java 433543 2006-08-22 06:22:54Z crossley $ */
043: public class HeaderAttributeModule extends AbstractInputModule
044: implements ThreadSafe {
045:
046: public Object getAttribute(String name, Configuration modeConf,
047: Map objectModel) throws ConfigurationException {
048:
049: String pname = (String) this .settings.get("parameter", name);
050: if (modeConf != null) {
051: pname = modeConf.getAttribute("parameter", pname);
052: // preferred
053: pname = modeConf.getChild("parameter").getValue(pname);
054: }
055: return ObjectModelHelper.getRequest(objectModel).getHeader(
056: pname);
057: }
058:
059: public Iterator getAttributeNames(Configuration modeConf,
060: Map objectModel) throws ConfigurationException {
061:
062: return new IteratorHelper(ObjectModelHelper.getRequest(
063: objectModel).getHeaderNames());
064: }
065:
066: public Object[] getAttributeValues(String name,
067: Configuration modeConf, Map objectModel)
068: throws ConfigurationException {
069:
070: Request request = ObjectModelHelper.getRequest(objectModel);
071: String wildcard = (String) this .settings.get("parameter", name);
072: if (modeConf != null) {
073: wildcard = modeConf.getAttribute("parameter", wildcard);
074: // preferred
075: wildcard = modeConf.getChild("parameter")
076: .getValue(wildcard);
077: }
078: int wildcardIndex = wildcard.indexOf("*");
079: if (wildcardIndex != -1) {
080: // "*" contained in attribute name => combine all
081: // attributes' values that match prefix, suffix
082:
083: // split the attribute's name so that the "*" could be
084: // determined by looking at the attributes' names that
085: // start with the prefix and end with the suffix
086: //
087: String prefix = wildcard.substring(0, wildcardIndex);
088: String suffix;
089: if (wildcard.length() >= wildcardIndex + 1) {
090: suffix = wildcard.substring(wildcardIndex + 1);
091: } else {
092: suffix = "";
093: }
094: SortedSet names = new TreeSet();
095: Enumeration allNames = request.getHeaderNames();
096:
097: while (allNames.hasMoreElements()) {
098: String pname = (String) allNames.nextElement();
099: if (pname.startsWith(prefix) && pname.endsWith(suffix)) {
100: names.add(pname);
101: }
102: }
103:
104: List values = new LinkedList();
105: Iterator j = names.iterator();
106: while (j.hasNext()) {
107: String pname = (String) j.next();
108: values.add(request.getHeader(pname));
109: }
110:
111: return values.toArray();
112:
113: } else {
114: // no "*" in attribute name => just return all values of
115: // this one attribute. Make sure, it's an array.
116:
117: Object value = request.getHeader(wildcard);
118: if (value != null && !value.getClass().isArray()) {
119: Object[] values = new Object[1];
120: values[0] = value;
121: return values;
122: } else {
123: return (Object[]) value;
124: }
125:
126: }
127:
128: }
129:
130: }
|