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.Configuration;
20: import org.apache.avalon.framework.configuration.ConfigurationException;
21: import org.apache.avalon.framework.parameters.Parameters;
22: import org.apache.cocoon.environment.ObjectModelHelper;
23:
24: import java.util.Map;
25:
26: /**
27: * A <code>Selector</code> that matches a string from within the host parameter
28: * of the HTTP request.
29: *
30: * <p>Configuration:
31: * <pre>
32: * <map:selector name="host" src="org.apache.cocoon.selection.HostSelector">
33: * <host name="uk-site" value="www.foo.co.uk"/>
34: * </map:selector>
35: * </pre>
36: * <p>Usage:
37: * <pre>
38: * <map:select type="host">
39: * <map:when test="uk-site">
40: * <map:transform src="stylesheets/page/uk.xsl"/>
41: * </map:when>
42: * <map:otherwise>
43: * <map:transform src="stylesheets/page/us.xsl"/>
44: * </map:otherwise>
45: * </map:select>
46: * </pre>
47: *
48: * @author <a href="mailto:cbritton@centervilletech.com">Colin Britton</a>
49: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
50: * @version CVS $Id: HostSelector.java 433543 2006-08-22 06:22:54Z crossley $
51: */
52:
53: public class HostSelector extends NamedPatternsSelector {
54:
55: public void configure(Configuration conf)
56: throws ConfigurationException {
57: configure(conf, "host", "name", "value");
58: }
59:
60: public boolean select(String expression, Map objectModel,
61: Parameters parameters) {
62: // Inform proxies that response varies with the Host header
63: ObjectModelHelper.getResponse(objectModel).addHeader("Vary",
64: "Host");
65:
66: // Get the host request header
67: String host = ObjectModelHelper.getRequest(objectModel)
68: .getHeader("Host");
69: if (host == null) {
70: getLogger().debug("No Host header -- failing.");
71: return false;
72: }
73:
74: return checkPatterns(expression, host);
75: }
76: }
|