KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
19
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 import org.apache.cocoon.environment.ObjectModelHelper;
24
25 /**
26  * <p>The {@link RegexpHeaderSelector} class defines a selector matching
27  * specific headers to configured regular-expression patterns.</p>
28  *
29  * <p>The configuration of an {@link RegexpHeaderSelector} follows exactly
30  * what has been outlined in {@link AbstractRegexpSelector} regarding regular
31  * expression patterns, and additionally it requires an extra configuration element
32  * specifying the header whose value needs to be matched:</p>
33  *
34  * <pre>
35  * &lt;map:components&gt;
36  * ...
37  * &lt;map:selectors default="..."&gt;
38  * &lt;map:selector name="..." SRC="org.apache.cocoon.selection...."&gt;
39  * &lt;pattern name="empty"&gt;^$&lt;/pattern&gt;
40  * &lt;pattern name="number"&gt;^[0-9]+$&lt;/pattern&gt;
41  * &lt;pattern name="string"&gt;^.+$&lt;/pattern&gt;
42  * &lt;header-name&gt;...&lt;/header-name&gt;
43  * &lt;/map:selector&gt;
44  * &lt;/map:selectors&gt;
45  * &lt;/map:components&gt;
46  * </pre>
47  *
48  * <p>If not configured, or if it needs to be overriddent, the header name can
49  * also be specified as a <code>&lt;map:parameter&nbsp;.../&gt;</code> inside the
50  * pipeline itself.</p>
51  *
52  * @version CVS $Id: RegexpHeaderSelector.java 124799 2005-01-10 13:32:27Z vgritsenko $
53  */

54 public class RegexpHeaderSelector extends AbstractRegexpSelector {
55
56     /** <p>The name of the header to work on.</p> */
57     protected String JavaDoc headerName;
58
59     /**
60      * <p>Create a new {@link RegexpHeaderSelector} instance.</p>
61      */

62     public RegexpHeaderSelector() {
63         super();
64     }
65
66     /**
67      * <p>Configure this instance parsing all regular expression patterns and
68      * storing the header name upon which selection occurs.</p>
69      *
70      * @param configuration the {@link Configuration} instance where configured
71      * patterns are defined.
72      * @throws ConfigurationException if one of the regular-expression to configure
73      * could not be compiled.
74      */

75     public void configure(Configuration configuration)
76     throws ConfigurationException {
77         super.configure(configuration);
78         this.headerName = configuration.getChild("header-name").getValue(null);
79     }
80
81     /**
82      * <p>Return the value of the header identified by the configured header
83      * name, if any.</p>
84      *
85      * @param objectModel the Cocoon object model.
86      * @param parameters the {@link Parameters} associated with the pipeline.
87      * @return the value of the configured request parameter or <b>null</b>.
88      */

89     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
90         String JavaDoc name = parameters.getParameter("header-name", this.headerName);
91         if (name == null) {
92             this.getLogger().warn("No header name given -- failing.");
93             return null;
94         }
95         return ObjectModelHelper.getRequest(objectModel).getHeader(name);
96     }
97
98     /**
99      * Selectors test pattern against some objects in a <code>Map</code>
100      * model and signals success with the returned boolean value
101      * @param expr The expression to test.
102      * @return Signals successful test.
103      */

104     public boolean select(String JavaDoc expr, Map JavaDoc objectModel, Parameters params) {
105     // Inform proxies that response varies with the selector header
106
String JavaDoc name = params.getParameter("header-name", this.headerName);
107     if (name != null)
108         ObjectModelHelper.getResponse(objectModel).addHeader("Vary", name);
109         return select(expr, getSelectorContext(objectModel, params));
110     }
111
112 }
113
Popular Tags