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