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.ObjectModelHelper;
27:
28: import java.util.Map;
29:
30: /**
31: * A <code>Selector</code> that matches a string against a configurable
32: * request header, e.g. "referer".
33: *
34: * <p><b>Global and local configuration</b></p>
35: * <table border="1">
36: * <tr><td><code>header-name</code></td><td>Name of the request header to
37: * match against</td></tr>
38: * </table>
39: *
40: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
41: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
42: * @version CVS $Id: HeaderSelector.java 433543 2006-08-22 06:22:54Z crossley $
43: */
44: public class HeaderSelector extends AbstractLogEnabled implements
45: Configurable, ThreadSafe, Selector {
46:
47: protected String defaultName;
48:
49: public void configure(Configuration config)
50: throws ConfigurationException {
51: // Check old name
52: this .defaultName = config.getChild("parameter-name").getValue(
53: null);
54: if (defaultName != null) {
55: getLogger()
56: .warn(
57: "'parameter-name' is deprecated. Please use 'header-name'");
58: }
59: // Load with new one
60: this .defaultName = config.getChild("header-name").getValue(
61: this .defaultName);
62: }
63:
64: public boolean select(String expression, Map objectModel,
65: Parameters parameters) {
66: // Check old name
67: String name = parameters.getParameter("parameter-name", null);
68: if (name != null) {
69: getLogger()
70: .warn(
71: "'parameter-name' is deprecated. Please use 'header-name'");
72: } else {
73: name = this .defaultName;
74: }
75:
76: // Load with new one.
77: name = parameters.getParameter("header-name", name);
78:
79: if (name == null) {
80: getLogger().warn("No header name given -- failing.");
81: return false;
82: }
83:
84: String value = ObjectModelHelper.getRequest(objectModel)
85: .getHeader(name);
86: if (value == null) {
87: getLogger().debug(
88: "Header '" + name + "' not set -- failing.");
89: return false;
90: }
91:
92: return value.equals(expression);
93: }
94: }
|