KickJava   Java API By Example, From Geeks To Geeks.

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


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.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.cocoon.environment.ObjectModelHelper;
22
23 import java.util.Map JavaDoc;
24
25 /**
26  * A <code>Selector</code> that matches a string from within the host parameter
27  * of the HTTP request.
28  *
29  * <p>Configuration:
30  * <pre>
31  * &lt;map:selector name="host" SRC="org.apache.cocoon.selection.HostSelector"&gt;
32  * &lt;host name="uk-site" value="www.foo.co.uk"/&gt;
33  * &lt;/map:selector&gt;
34  * </pre>
35  * <p>Usage:
36  * <pre>
37  * &lt;map:select type="host"&gt;
38  * &lt;map:when test="uk-site"&gt;
39  * &lt;map:transform SRC="stylesheets/page/uk.xsl"/&gt;
40  * &lt;/map:when&gt;
41  * &lt;map:otherwise&gt;
42  * &lt;map:transform SRC="stylesheets/page/us.xsl"/&gt;
43  * &lt;/map:otherwise&gt;
44  * &lt;/map:select&gt;
45  * </pre>
46  *
47  * @author <a HREF="mailto:cbritton@centervilletech.com">Colin Britton</a>
48  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
49  * @version CVS $Id: HostSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
50  */

51
52 public class HostSelector extends NamedPatternsSelector {
53
54     public void configure(Configuration conf) throws ConfigurationException {
55         configure(conf, "host", "name", "value");
56     }
57
58     public boolean select(String JavaDoc expression, Map JavaDoc objectModel, Parameters parameters) {
59         // Inform proxies that response varies with the Host header
60
ObjectModelHelper.getResponse(objectModel).addHeader("Vary", "Host");
61
62         // Get the host request header
63
String JavaDoc host = ObjectModelHelper.getRequest(objectModel).getHeader("Host");
64         if (host == null) {
65             getLogger().debug("No Host header -- failing.");
66             return false;
67         }
68
69         return checkPatterns(expression, host);
70     }
71 }
72
Popular Tags