##### Coverage Report - org.apache.turbine.util.BrowserDetector

Classes in this File | Line Coverage | Branch Coverage | Complexity
BrowserDetector |
85% | 24/28
50% | 1/2
| 1.1111111111111112;1,111

1 |  |
     package org.apache.turbine.util;
2 |  |
3 |  |
     import java.util.HashMap;
4 |  |
     import java.util.Map;
5 |  |
6 |  |
     import net.sf.uadetector.OperatingSystem;
7 |  |
     import net.sf.uadetector.ReadableUserAgent;
8 |  |
     import net.sf.uadetector.UserAgentStringParser;
9 |  |
     import net.sf.uadetector.VersionNumber;
10 |  |
     import net.sf.uadetector.service.UADetectorServiceFactory;
11 |  |
12 |  |
     /*
13 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
14 |  |
      * or more contributor license agreements.  See the NOTICE file
15 |  |
      * distributed with this work for additional information
16 |  |
      * regarding copyright ownership.  The ASF licenses this file
17 |  |
      * to you under the Apache License, Version 2.0 (the
18 |  |
      * "License"); you may not use this file except in compliance
19 |  |
      * with the License.  You may obtain a copy of the License at
20 |  |
      *
21 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
22 |  |
      *
23 |  |
      * Unless required by applicable law or agreed to in writing,
24 |  |
      * software distributed under the License is distributed on an
25 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26 |  |
      * KIND, either express or implied.  See the License for the
27 |  |
      * specific language governing permissions and limitations
28 |  |
      * under the License.
29 |  |
      */
30 |  |
31 |  |
     /**
32 |  |
      * This class parses the user agent string and provides getters for
33 |  |
      * its parts. It uses UADetector (http://uadetector.sourceforge.net/)
34 |  |
      *
35 |  |
      * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
36 |  |
      * @author <a href="mailto:leon@clearink.com">Leon Atkisnon</a>
37 |  |
      * @author <a href="mailto:mospaw@polk-county.com">Chris Mospaw</a>
38 |  |
      * @author <a href="mailto:bgriffin@cddb.com">Benjamin Elijah Griffin</a>
39 |  |
      * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
40 |  |
      */
41 |  |
     public class BrowserDetector
42 |  |
     {
43 |  |
         /** The user agent string. */
44 | 5 |
         private String userAgentString = "";
45 |  |
46 |  |
         /** The user agent cache. */
47 | 1 |
         private static volatile Map<String, ReadableUserAgent> userAgentCache =
48 |  |
                 new HashMap<String, ReadableUserAgent>();
49 |  |
50 |  |
         /** The user agent parser */
51 | 1 |
         private static UserAgentStringParser parser =
52 |  |
                 UADetectorServiceFactory.getCachingAndUpdatingParser();
53 |  |
54 |  |
         /** The browser name specified in the user agent string. */
55 | 5 |
         private String browserName = "";
56 |  |
57 |  |
         /**
58 |  |
          * The browser version specified in the user agent string.  If we
59 |  |
          * can't parse the version just assume an old browser.
60 |  |
          */
61 | 5 |
         private float browserVersion = (float) 1.0;
62 |  |
63 |  |
         /**
64 |  |
          * The browser platform specified in the user agent string.
65 |  |
          */
66 | 5 |
         private String browserPlatform = "unknown";
67 |  |
68 |  |
         /**
69 |  |
          * Constructor used to initialize this class.
70 |  |
          *
71 |  |
          * @param userAgentString A String with the user agent field.
72 |  |
          */
73 |  |
         public BrowserDetector(String userAgentString)
74 | 5 |
         {
75 | 5 |
             this.userAgentString = userAgentString;
76 | 5 |
             parse();
77 | 5 |
         }
78 |  |
79 |  |
         /**
80 |  |
          * Constructor used to initialize this class.
81 |  |
          *
82 |  |
          * @param data The Turbine RunData object.
83 |  |
          */
84 |  |
         public BrowserDetector(RunData data)
85 |  |
         {
86 | 0 |
             this(data.getUserAgent());
87 | 0 |
         }
88 |  |
89 |  |
         /**
90 |  |
          * The browser name specified in the user agent string.
91 |  |
          *
92 |  |
          * @return A String with the browser name.
93 |  |
          */
94 |  |
         public String getBrowserName()
95 |  |
         {
96 | 5 |
             return browserName;
97 |  |
         }
98 |  |
99 |  |
         /**
100 |  |
          * The browser platform specified in the user agent string.
101 |  |
          *
102 |  |
          * @return A String with the browser platform.
103 |  |
          */
104 |  |
         public String getBrowserPlatform()
105 |  |
         {
106 | 5 |
             return browserPlatform;
107 |  |
         }
108 |  |
109 |  |
         /**
110 |  |
          * The browser version specified in the user agent string.
111 |  |
          *
112 |  |
          * @return A String with the browser version.
113 |  |
          */
114 |  |
         public float getBrowserVersion()
115 |  |
         {
116 | 5 |
             return browserVersion;
117 |  |
         }
118 |  |
119 |  |
         /**
120 |  |
          * The user agent string for this class.
121 |  |
          *
122 |  |
          * @return A String with the user agent.
123 |  |
          */
124 |  |
         public String getUserAgentString()
125 |  |
         {
126 | 0 |
             return userAgentString;
127 |  |
         }
128 |  |
129 |  |
         /**
130 |  |
          * The user agent for this class.
131 |  |
          *
132 |  |
          * @return A user agent.
133 |  |
          */
134 |  |
         public ReadableUserAgent getUserAgent()
135 |  |
         {
136 | 0 |
             return userAgentCache.get(userAgentString);
137 |  |
         }
138 |  |
139 |  |
         /**
140 |  |
          * Helper method to initialize this class.
141 |  |
          */
142 |  |
         private void parse()
143 |  |
         {
144 | 5 |
             ReadableUserAgent userAgent = userAgentCache.get(userAgentString);
145 |  |
146 | 5 |
             if (userAgent == null)
147 |  |
             {
148 | 5 |
                 userAgent = parser.parse(userAgentString);
149 | 5 |
                 userAgentCache.put(userAgentString, userAgent);
150 |  |
             }
151 |  |
152 |  |
             // Get the browser name and version.
153 | 5 |
             browserName = userAgent.getName();
154 | 5 |
             VersionNumber version = userAgent.getVersionNumber();
155 | 5 |
             browserVersion = toFloat(version.toVersionString());
156 |  |
157 |  |
             // Try to figure out what platform.
158 | 5 |
             OperatingSystem os = userAgent.getOperatingSystem();
159 | 5 |
             browserPlatform = os.getFamilyName();
160 | 5 |
         }
161 |  |
162 |  |
         /**
163 |  |
          * Helper method to convert String to a float.
164 |  |
          *
165 |  |
          * @param s A String.
166 |  |
          * @return The String converted to float.
167 |  |
          */
168 |  |
         private static final float toFloat(String s)
169 |  |
         {
170 | 5 |
             return Float.parseFloat(s);
171 |  |
         }
172 |  |
173 |  |
     }