KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectModelHelper;
26
27 import java.util.Map JavaDoc;
28
29 /**
30  * A <code>Selector</code> that matches a string against a configurable
31  * request header, e.g. "referer".
32  *
33  * <p><b>Global and local configuration</b></p>
34  * <table border="1">
35  * <tr><td><code>header-name</code></td><td>Name of the request header to
36  * match against</td></tr>
37  * </table>
38  *
39  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
40  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
41  * @version CVS $Id: HeaderSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class HeaderSelector extends AbstractLogEnabled
44   implements Configurable, ThreadSafe, Selector {
45
46     protected String JavaDoc defaultName;
47
48     public void configure(Configuration config) throws ConfigurationException {
49         // Check old name
50
this.defaultName = config.getChild("parameter-name").getValue(null);
51         if (defaultName != null) {
52             getLogger().warn("'parameter-name' is deprecated. Please use 'header-name'");
53         }
54         // Load with new one
55
this.defaultName = config.getChild("header-name").getValue(this.defaultName);
56     }
57
58     public boolean select(String JavaDoc expression, Map JavaDoc objectModel, Parameters parameters) {
59         // Check old name
60
String JavaDoc name = parameters.getParameter("parameter-name", null);
61         if (name != null) {
62             getLogger().warn("'parameter-name' is deprecated. Please use 'header-name'");
63         } else {
64             name = this.defaultName;
65         }
66
67         // Load with new one.
68
name = parameters.getParameter("header-name", name);
69
70         if (name == null) {
71             getLogger().warn("No header name given -- failing.");
72             return false;
73         }
74
75         String JavaDoc value = ObjectModelHelper.getRequest(objectModel).getHeader(name);
76         if (value == null) {
77             getLogger().debug("Header '" + name + "' not set -- failing.");
78             return false;
79         }
80
81         return value.equals(expression);
82     }
83 }
84
Popular Tags