KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > CookieModule


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components.modules.input;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27 import org.apache.cocoon.components.modules.input.AbstractInputModule;
28 import org.apache.cocoon.environment.ObjectModelHelper;
29 import org.apache.cocoon.environment.http.HttpCookie;
30 import org.apache.regexp.RE;
31
32 /**
33  * Input module for cookies. Retrieves the value of the requested cookie.
34  *
35  * @author Jon Evans <jon.evans@misgl.com>
36  * @version CVS $Id:$
37  */

38 public class CookieModule extends AbstractInputModule implements ThreadSafe {
39     
40     /**
41      * @return the value of the cookie whose name matches the one requested,
42      * or <code>null</code> if there is no match.
43      */

44     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf,
45             Map JavaDoc objectModel) throws ConfigurationException {
46         
47         HttpCookie cookie = (HttpCookie) getCookieMap(objectModel).get(name);
48         String JavaDoc value = (cookie == null ? null : cookie.getValue());
49         
50         if (getLogger().isDebugEnabled()) {
51             getLogger().debug("Cookie[" + name + "]=" + value);
52         }
53         return value;
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
60      * java.util.Map)
61      */

62     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
63             throws ConfigurationException {
64         
65         return getCookieMap(objectModel).keySet().iterator();
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
72      * org.apache.avalon.framework.configuration.Configuration,
73      * java.util.Map)
74      */

75     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf,
76             Map JavaDoc objectModel) throws ConfigurationException {
77         
78         Map JavaDoc allCookies = getCookieMap(objectModel);
79         
80         Iterator JavaDoc it = allCookies.values().iterator();
81         List JavaDoc matched = new LinkedList JavaDoc();
82         RE regexp = new RE(name);
83         while (it.hasNext()) {
84             HttpCookie cookie = (HttpCookie) it.next();
85             if (regexp.match(cookie.getName())) {
86                 matched.add(cookie.getValue());
87             }
88         }
89         return matched.toArray();
90     }
91
92     /**
93      * @param objectModel
94      * Object Model for the current request
95      * @return a Map of {see: HttpCookie}s for the current request, keyed on
96      * cookie name.
97      */

98     protected Map JavaDoc getCookieMap(Map JavaDoc objectModel) {
99         return ObjectModelHelper.getRequest(objectModel).getCookieMap();
100     }
101 }
102
Popular Tags