KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > CookieSelector


1 /*
2  * Copyright 1999-2004 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 package org.apache.cocoon.selection;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import org.apache.cocoon.environment.Cookie;
26 import org.apache.cocoon.environment.ObjectModelHelper;
27
28 import java.util.Map JavaDoc;
29
30 /**
31  * A <code>Selector</code> that matches a string against a configurable cookie's value.
32  *
33  * <p><b>Global and local configuration</b></p>
34  * <table border="1">
35  * <tr>
36  * <td><code>cookie-name</code></td>
37  * <td>Name of the cookie whose value to match against</td>
38  * </tr>
39  * </table>
40  *
41  * @author <a HREF="mailto:matteodg@infinito.it">Matteo Di Giovinazzo</a>
42  * @version CVS $Id: CookieSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public class CookieSelector extends AbstractLogEnabled
45         implements Configurable, Selector, ThreadSafe {
46
47     protected String JavaDoc defaultName;
48
49     public void configure(Configuration config) throws ConfigurationException {
50         this.defaultName = config.getChild("cookie-name").getValue(null);
51     }
52
53     public boolean select(String JavaDoc expression, Map JavaDoc objectModel, Parameters parameters) {
54
55         String JavaDoc name = parameters.getParameter("cookie-name", this.defaultName);
56         if (name == null) {
57             getLogger().warn("No cookie name given -- failing.");
58             return false;
59         }
60
61         Cookie[] cookies = ObjectModelHelper.getRequest(objectModel).getCookies();
62         if (cookies == null) {
63             getLogger().debug("Cookie '" + name + "' not set -- failing");
64             return false;
65         }
66
67         // TODO: this is not optimized
68
String JavaDoc value = null;
69         for (int i = 0; i < cookies.length; i++) {
70             if (cookies[i].getName().equals(name)) {
71                 value = cookies[i].getValue();
72                 break;
73             }
74         }
75
76         if (value == null) {
77             getLogger().debug("Cookie '" + name + "' not set -- failing");
78             return false;
79         }
80
81         return value.equals(expression);
82     }
83 }
84
Popular Tags