001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.selection;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.environment.ObjectModelHelper;
025:
026: /**
027: * <p>The {@link RegexpHeaderSelector} class defines a selector matching
028: * specific headers to configured regular-expression patterns.</p>
029: *
030: * <p>The configuration of an {@link RegexpHeaderSelector} follows exactly
031: * what has been outlined in {@link AbstractRegexpSelector} regarding regular
032: * expression patterns, and additionally it requires an extra configuration element
033: * specifying the header whose value needs to be matched:</p>
034: *
035: * <pre>
036: * <map:components>
037: * ...
038: * <map:selectors default="...">
039: * <map:selector name="..." src="org.apache.cocoon.selection....">
040: * <pattern name="empty">^$</pattern>
041: * <pattern name="number">^[0-9]+$</pattern>
042: * <pattern name="string">^.+$</pattern>
043: * <header-name>...</header-name>
044: * </map:selector>
045: * </map:selectors>
046: * </map:components>
047: * </pre>
048: *
049: * <p>If not configured, or if it needs to be overriddent, the header name can
050: * also be specified as a <code><map:parameter .../></code> inside the
051: * pipeline itself.</p>
052: *
053: * @version CVS $Id: RegexpHeaderSelector.java 433543 2006-08-22 06:22:54Z crossley $
054: */
055: public class RegexpHeaderSelector extends AbstractRegexpSelector {
056:
057: /** <p>The name of the header to work on.</p> */
058: protected String headerName;
059:
060: /**
061: * <p>Create a new {@link RegexpHeaderSelector} instance.</p>
062: */
063: public RegexpHeaderSelector() {
064: super ();
065: }
066:
067: /**
068: * <p>Configure this instance parsing all regular expression patterns and
069: * storing the header name upon which selection occurs.</p>
070: *
071: * @param configuration the {@link Configuration} instance where configured
072: * patterns are defined.
073: * @throws ConfigurationException if one of the regular-expression to configure
074: * could not be compiled.
075: */
076: public void configure(Configuration configuration)
077: throws ConfigurationException {
078: super .configure(configuration);
079: this .headerName = configuration.getChild("header-name")
080: .getValue(null);
081: }
082:
083: /**
084: * <p>Return the value of the header identified by the configured header
085: * name, if any.</p>
086: *
087: * @param objectModel the Cocoon object model.
088: * @param parameters the {@link Parameters} associated with the pipeline.
089: * @return the value of the configured request parameter or <b>null</b>.
090: */
091: public Object getSelectorContext(Map objectModel,
092: Parameters parameters) {
093: String name = parameters.getParameter("header-name",
094: this .headerName);
095: if (name == null) {
096: this .getLogger().warn("No header name given -- failing.");
097: return null;
098: }
099: return ObjectModelHelper.getRequest(objectModel)
100: .getHeader(name);
101: }
102:
103: /**
104: * Selectors test pattern against some objects in a <code>Map</code>
105: * model and signals success with the returned boolean value
106: * @param expr The expression to test.
107: * @return Signals successful test.
108: */
109: public boolean select(String expr, Map objectModel,
110: Parameters params) {
111: // Inform proxies that response varies with the selector header
112: String name = params.getParameter("header-name",
113: this .headerName);
114: if (name != null)
115: ObjectModelHelper.getResponse(objectModel).addHeader(
116: "Vary", name);
117: return select(expr, getSelectorContext(objectModel, params));
118: }
119:
120: }
|