01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.selection;
18:
19: import org.apache.avalon.framework.configuration.Configurable;
20: import org.apache.avalon.framework.configuration.Configuration;
21: import org.apache.avalon.framework.configuration.ConfigurationException;
22: import org.apache.avalon.framework.logger.AbstractLogEnabled;
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.avalon.framework.thread.ThreadSafe;
25:
26: import org.apache.cocoon.environment.Cookie;
27: import org.apache.cocoon.environment.ObjectModelHelper;
28:
29: import java.util.Map;
30:
31: /**
32: * A <code>Selector</code> that matches a string against a configurable cookie's value.
33: *
34: * <p><b>Global and local configuration</b></p>
35: * <table border="1">
36: * <tr>
37: * <td><code>cookie-name</code></td>
38: * <td>Name of the cookie whose value to match against</td>
39: * </tr>
40: * </table>
41: *
42: * @author <a href="mailto:matteodg@infinito.it">Matteo Di Giovinazzo</a>
43: * @version CVS $Id: CookieSelector.java 433543 2006-08-22 06:22:54Z crossley $
44: */
45: public class CookieSelector extends AbstractLogEnabled implements
46: Configurable, Selector, ThreadSafe {
47:
48: protected String defaultName;
49:
50: public void configure(Configuration config)
51: throws ConfigurationException {
52: this .defaultName = config.getChild("cookie-name")
53: .getValue(null);
54: }
55:
56: public boolean select(String expression, Map objectModel,
57: Parameters parameters) {
58:
59: String name = parameters.getParameter("cookie-name",
60: this .defaultName);
61: if (name == null) {
62: getLogger().warn("No cookie name given -- failing.");
63: return false;
64: }
65:
66: Cookie[] cookies = ObjectModelHelper.getRequest(objectModel)
67: .getCookies();
68: if (cookies == null) {
69: getLogger().debug(
70: "Cookie '" + name + "' not set -- failing");
71: return false;
72: }
73:
74: // TODO: this is not optimized
75: String value = null;
76: for (int i = 0; i < cookies.length; i++) {
77: if (cookies[i].getName().equals(name)) {
78: value = cookies[i].getValue();
79: break;
80: }
81: }
82:
83: if (value == null) {
84: getLogger().debug(
85: "Cookie '" + name + "' not set -- failing");
86: return false;
87: }
88:
89: return value.equals(expression);
90: }
91: }
|