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 java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.components.modules.input.AbstractInputModule;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030: import org.apache.cocoon.environment.http.HttpCookie;
031: import org.apache.regexp.RE;
032:
033: /**
034: * Input module for cookies. Retrieves the value of the requested cookie.
035: *
036: * @author Jon Evans <jon.evans@misgl.com>
037: * @version CVS $Id:$
038: */
039: public class CookieModule extends AbstractInputModule implements
040: ThreadSafe {
041:
042: /**
043: * @return the value of the cookie whose name matches the one requested,
044: * or <code>null</code> if there is no match.
045: */
046: public Object getAttribute(String name, Configuration modeConf,
047: Map objectModel) throws ConfigurationException {
048:
049: HttpCookie cookie = (HttpCookie) getCookieMap(objectModel).get(
050: name);
051: String value = (cookie == null ? null : cookie.getValue());
052:
053: if (getLogger().isDebugEnabled()) {
054: getLogger().debug("Cookie[" + name + "]=" + value);
055: }
056: return value;
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
063: * java.util.Map)
064: */
065: public Iterator getAttributeNames(Configuration modeConf,
066: Map objectModel) throws ConfigurationException {
067:
068: return getCookieMap(objectModel).keySet().iterator();
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
075: * org.apache.avalon.framework.configuration.Configuration,
076: * java.util.Map)
077: */
078: public Object[] getAttributeValues(String name,
079: Configuration modeConf, Map objectModel)
080: throws ConfigurationException {
081:
082: Map allCookies = getCookieMap(objectModel);
083:
084: Iterator it = allCookies.values().iterator();
085: List matched = new LinkedList();
086: RE regexp = new RE(name);
087: while (it.hasNext()) {
088: HttpCookie cookie = (HttpCookie) it.next();
089: if (regexp.match(cookie.getName())) {
090: matched.add(cookie.getValue());
091: }
092: }
093: return matched.toArray();
094: }
095:
096: /**
097: * @param objectModel
098: * Object Model for the current request
099: * @return a Map of {see: HttpCookie}s for the current request, keyed on
100: * cookie name.
101: */
102: protected Map getCookieMap(Map objectModel) {
103: return ObjectModelHelper.getRequest(objectModel).getCookieMap();
104: }
105: }
|