View Javadoc

1   /*
2    * Copyright 2000-2001,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  
17  package org.apache.jetspeed.capability;
18  
19  import org.apache.jetspeed.om.registry.ClientEntry;
20  import org.apache.jetspeed.om.registry.ClientRegistry;
21  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
22  import org.apache.jetspeed.services.logging.JetspeedLogger;
23  import org.apache.jetspeed.services.Registry;
24  
25  import org.apache.turbine.util.RunData;
26  
27  
28  /***
29  This class describes various browsers capabilities and provides the
30  ability to query them.
31  
32  FIXME: the implementation should change to be configuration file based and
33  handle more browsers.
34  
35  @author <a href="mailto:raphael@apache.org">RaphaŽl Luta</a>
36  @version $Id: CapabilityMapFactory.java,v 1.15 2004/02/23 02:46:39 jford Exp $
37  */
38  public class CapabilityMapFactory
39  {
40  
41      public static final String DEFAULT_AGENT = "Mozilla/4.0";
42  
43      public static final String AGENT_XML = "agentxml/1.0";
44  
45      /***
46       * Static initialization of the logger for this class
47       */
48      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(CapabilityMapFactory.class.getName());
49      
50      /***
51      Returns the map corresponding to the given RunData.
52      *
53      FIXME: the method will be changed to use a Jetspeed specific request
54      wrapper
55  
56      @param rundata the request RunData
57      @return the map correspondin to the user-agent described in the RunData
58      */
59      public static CapabilityMap getCapabilityMap( RunData rundata )
60      {
61  
62          if (rundata == null)
63          {
64              return getCapabilityMap(DEFAULT_AGENT);
65          }
66  
67          return getCapabilityMap( rundata.getUserAgent() );
68      }
69  
70      /***
71      Returns the map corresponding to the given user-agent
72  
73      @param useragent a user-agent string in the HTTP User-agent format
74      @return the map corresponding to the user-agent
75      */
76      public static CapabilityMap getCapabilityMap( String useragent )
77      {
78          CapabilityMap map = null;
79  
80          if (useragent == null)
81          {
82              useragent = DEFAULT_AGENT;
83          }
84  
85          ClientRegistry registry = (ClientRegistry)Registry.get(Registry.CLIENT);
86          ClientEntry entry = registry.findEntry(useragent);
87  
88          if (entry == null)
89          {
90              if (useragent.equals(DEFAULT_AGENT))
91              {
92                  logger.error("CapabilityMap: Default agent not found in Client Registry !");
93              }
94              else
95              {
96                  if (logger.isDebugEnabled())
97                  {
98                      logger.debug("CapabilityMap: useragent "+ useragent + "unknown, falling back to default");
99                  }
100                 map = getDefaultCapabilityMap();
101             }
102         }
103         else
104         {
105             map = new BaseCapabilityMap(useragent, entry);
106         }
107 
108 
109         if (logger.isDebugEnabled())
110         {
111             logger.debug("CapabilityMap: User-agent: "+useragent+" mapped to "+map);
112         }
113 
114         return map;
115     }
116 
117     /***
118     Returns the map corresponding to the given user-agent
119 
120     @param useragent a user-agent string in the HTTP User-agent format
121     @return the map corresponding to the user-agent
122     */
123     public static CapabilityMap getDefaultCapabilityMap()
124     {
125         return getCapabilityMap(DEFAULT_AGENT);
126     }
127 }