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