KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tests a specific browser pattern against the requesting user-agent.
27  *
28  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
29  * @author <a HREF="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
30  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
31  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
32  * @version CVS $Id: BrowserSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34
35 public class BrowserSelector extends NamedPatternsSelector {
36
37     public void configure(Configuration conf) throws ConfigurationException {
38         configure(conf, "browser", "name", "useragent");
39     }
40
41     public boolean select(String JavaDoc expression, Map JavaDoc objectModel, Parameters parameters) {
42         // Inform proxies that response varies with the user-agent header
43
ObjectModelHelper.getResponse(objectModel).addHeader("Vary", "User-Agent");
44
45         // Get the user-agent request header
46
String JavaDoc userAgent = ObjectModelHelper.getRequest(objectModel).getHeader("User-Agent");
47         if (userAgent == null) {
48             getLogger().debug("No User-Agent header -- failing.");
49             return false;
50         }
51
52         return checkPatterns(expression, userAgent);
53     }
54 }
55
Popular Tags