View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.context;
21  
22  import org.apache.myfaces.tobago.internal.util.Deprecation;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.io.Serializable;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.EnumSet;
30  import java.util.List;
31  import java.util.StringTokenizer;
32  
33  public final class UserAgent implements Serializable {
34  
35    public static final String DEFAULT_NAME = "standard";
36  
37    public static final UserAgent DEFAULT = new UserAgent(null, null);
38  
39    public static final UserAgent MSIE = new UserAgent("msie", null);
40  
41    /**
42     * @deprecated no longer supported, since Tobago 1.5
43     */
44    @Deprecated
45    public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0");
46  
47    /**
48     * @deprecated no longer supported, since Tobago 1.5
49     */
50    @Deprecated
51    public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5");
52  
53    public static final UserAgent MSIE_6_0 = new UserAgent(
54        "msie", "6_0", EnumSet.noneOf(Capability.class), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
55  
56    public static final UserAgent MSIE_7_0 = new UserAgent(
57        "msie", "7_0", EnumSet.noneOf(Capability.class), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
58  
59    public static final UserAgent MSIE_7_0_COMPAT = new UserAgent(
60        "msie", "7_0", EnumSet.of(Capability.IE_COMPATIBILITY_MODE), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
61  
62    /**
63     * @deprecated no longer supported, since Tobago 1.5. Misspelled. Use {@link #MSIE_7_0}
64     */
65    @Deprecated
66    public static final UserAgent MSIE_7_O = MSIE_7_0;
67  
68    public static final UserAgent MSIE_8_0 = new UserAgent(
69        "msie", "8_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
70  
71    public static final UserAgent MSIE_9_0 = new UserAgent(
72        "msie", "9_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
73  
74    // CSP is not fully supported, only sandboxing
75    public static final UserAgent MSIE_10_0 = new UserAgent(
76        "msie", "10_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
77  
78    // CSP is not fully supported, only sandboxing
79    public static final UserAgent MSIE_11_0 = new UserAgent(
80        "msie", "11_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
81  
82    /**
83     * @deprecated no longer supported, since Tobago 1.5
84     */
85    @Deprecated
86    public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac");
87  
88    /**
89     * @deprecated no longer supported, since Tobago 1.5
90     */
91    @Deprecated
92    public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac");
93  
94  
95    /**
96     * e. g. Opera 10
97     */
98    public static final UserAgent PRESTO = new UserAgent("presto", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML));
99  
100   /**
101    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
102    */
103   public static final UserAgent OPERA = new UserAgent("opera", null);
104 
105   /**
106    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
107    */
108   @Deprecated
109   public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0");
110 
111   /**
112    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
113    */
114   @Deprecated
115   public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0");
116 
117   /**
118    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
119    */
120   @Deprecated
121   public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11");
122 
123   /**
124    * @deprecated no longer supported, since Tobago 1.5
125    */
126   @Deprecated
127   public static final UserAgent MOZILLA = new UserAgent("mozilla", null);
128 
129   /**
130    * @deprecated no longer supported, since Tobago 1.5
131    */
132   @Deprecated
133   public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7");
134 
135   /**
136    * @deprecated no longer supported, since Tobago 1.5
137    */
138   @Deprecated
139   public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0");
140 
141   /**
142    * @deprecated no longer supported, since Tobago 1.5
143    */
144   @Deprecated
145   public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6");
146 
147   /**
148    * e. g. Firefox
149    */
150   public static final UserAgent GECKO = new UserAgent("gecko", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML));
151 
152   /**
153    * e. g. Firefox 2.0
154    */
155   public static final UserAgent GECKO_1_8 = new UserAgent("gecko", "1_8", EnumSet.of(Capability.CONTENT_TYPE_XHTML),
156       CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
157 
158   /**
159    * e. g. Firefox 3.0, 3.5, 3.6
160    */
161   public static final UserAgent GECKO_1_9 = new UserAgent("gecko", "1_9", EnumSet.of(Capability.CONTENT_TYPE_XHTML),
162       CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
163 
164   /**
165    * e. g. Firefox 4 to 22
166    */
167   public static final UserAgent GECKO_2_0
168       = new UserAgent("gecko", null,
169       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
170 
171   /**
172    * e. g. Firefox 23 or higher
173    */
174   public static final UserAgent GECKO_23_0
175       = new UserAgent("gecko", null,
176       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.STANDARD, CsproHeader.STANDARD);
177 
178   /**
179    * e. g. Safari 4, Safari 5, Chrome
180    */
181   public static final UserAgent WEBKIT
182       = new UserAgent("webkit", null,
183       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.WEBKIT, CsproHeader.WEBKIT);
184 
185   private static final long serialVersionUID = 2L;
186 
187   private static final Logger LOG = LoggerFactory.getLogger(UserAgent.class);
188 
189   private final String name;
190 
191   /**
192    * @deprecated Version shouldn't be used in the future. Use capability instead, even better
193    * use same code on the server when possible and use capability via JavaScript.
194    */
195   @Deprecated
196   private final String version;
197 
198   private final EnumSet<Capability> capabilities;
199 
200   private final CspHeader cspHeader;
201 
202   private final CsproHeader csproHeader;
203 
204   private UserAgent(final String name, final String version) {
205     this(name, version, EnumSet.of(Capability.CONTENT_TYPE_XHTML));
206   }
207 
208   private UserAgent(final String name, final String version, final EnumSet<Capability> capabilities) {
209     this(name, version, capabilities, CspHeader.STANDARD, CsproHeader.STANDARD);
210   }
211 
212   private UserAgent(
213       final String name, final String version, final EnumSet<Capability> capabilities, final CspHeader cspHeader,
214       final CsproHeader csproHeader) {
215     this.name = name;
216     this.version = version;
217     this.capabilities = capabilities;
218     this.cspHeader = cspHeader;
219     this.csproHeader = csproHeader;
220   }
221 
222   public boolean hasCapability(final Capability capability) {
223     return capabilities.contains(capability);
224   }
225 
226   public boolean isMsie() {
227     return MSIE.name.equals(name);
228   }
229 
230   /**
231    * @deprecated no longer supported, since Tobago 3.0
232    */
233   @Deprecated
234   public boolean isMsie6() {
235     return MSIE_6_0.name.equals(name) && MSIE_6_0.version.equals(version);
236   }
237 
238   /**
239    * @deprecated no longer supported, since Tobago 1.5
240    */
241   @Deprecated
242   public boolean isMozilla() {
243     return MOZILLA.name.equals(name);
244   }
245 
246   public List<String> getFallbackList() {
247     return getFallbackList(false);
248   }
249 
250   private List<String> getFallbackList(final boolean reverseOrder) {
251     final List<String> list = new ArrayList<String>(3);
252     if (version != null) {
253       list.add(name + '_' + version);
254     }
255     if (name != null) {
256       list.add(name);
257     }
258     list.add(DEFAULT_NAME);
259     if (reverseOrder) {
260       Collections.reverse(list);
261     }
262     return list;
263   }
264 
265   /**
266    * @return The HTTP header names for Content-Security-Policy.
267    */
268   public String[] getCspHeaders() {
269     return cspHeader.getNames();
270   }
271 
272   /**
273    * @return The HTTP header name for Content-Security-Policy-Report-Only.
274    */
275   public String[] getCspReportOnlyHeaders() {
276     return csproHeader.getNames();
277   }
278 
279   public static UserAgent getInstance(final String header) {
280     if (header == null) {
281       return DEFAULT;
282     }
283 
284     if (header.contains("MSIE") || header.contains("Trident")) {
285       if (header.contains("MSIE 6.0")) {
286         return MSIE_6_0;
287       } else if (header.contains("MSIE 7.0")) {
288         if (header.contains("Trident")) {
289           return MSIE_7_0_COMPAT;
290         } else {
291           return MSIE_7_0;
292         }
293       } else if (header.contains("MSIE 8.0")) {
294         return MSIE_8_0;
295       } else if (header.contains("MSIE 9.0")) {
296         return MSIE_9_0;
297       } else if (header.contains("MSIE 10.0")) {
298         return MSIE_10_0;
299       } else if (header.contains("rv:11")) {
300         return MSIE_11_0;
301       } else {
302         return MSIE;
303       }
304     } else if (header.contains("AppleWebKit")) {
305       return WEBKIT;
306     } else if (header.contains("Gecko")) {
307       if (header.contains("rv:1.8")) {
308         return GECKO_1_8;
309       } else if (header.contains("rv:1.9")) {
310         return GECKO_1_9;
311       } else {
312         final int index = header.indexOf("rv:");
313         final StringTokenizer tokenizer = new StringTokenizer(header.substring(index + 3), " .");
314         final String versionString = tokenizer.nextToken();
315         try {
316           final int version = Integer.parseInt(versionString);
317           if (version >= 23) {
318             return GECKO_23_0;
319           } else if (version >= 2) {
320             return GECKO_2_0;
321           }
322         } catch (final NumberFormatException e) {
323           if (LOG.isDebugEnabled()) {
324             LOG.debug(header, e);
325           }
326         }
327         return GECKO;
328       }
329     } else if (header.contains("Presto")) {
330       return PRESTO;
331     }
332 
333     return DEFAULT;
334   }
335 
336   /**
337    * @deprecated no longer supported, since Tobago 1.5
338    */
339   @Deprecated
340   public static UserAgent getInstanceForId(final String id) {
341     Deprecation.LOG.error("Getting the user agent from its id is no longer supported! id='" + id + "'");
342     return DEFAULT;
343   }
344 
345   /**
346    * @deprecated don't use toString() functionality, but for logging!
347    */
348   @Deprecated
349   public String toString() {
350     return version != null
351         ? name + '_' + version
352         : name;
353   }
354 
355   private enum CspHeader {
356 
357     NOT_SUPPORTED(new String[] {}),
358     X(new String[] {"Content-Security-Policy", "X-Content-Security-Policy"}),
359     WEBKIT(new String[] {"Content-Security-Policy", "X-WebKit-CSP"}),
360     STANDARD(new String[] {"Content-Security-Policy"});
361 
362     private String[] names;
363 
364     CspHeader(final String[] names) {
365       this.names = names;
366     }
367 
368     public String[] getNames() {
369       return names;
370     }
371   }
372 
373   private enum CsproHeader {
374 
375     NOT_SUPPORTED(new String[] {}),
376     X(new String[] {"Content-Security-Policy-Report-Only", "X-Content-Security-Policy-Report-Only"}),
377     WEBKIT(new String[] {"Content-Security-Policy-Report-Only", "X-WebKit-CSP-Report-Only"}),
378     STANDARD(new String[] {"Content-Security-Policy-Report-Only"});
379 
380     private String[] names;
381 
382     CsproHeader(final String[] names) {
383       this.names = names;
384     }
385 
386     public String[] getNames() {
387       return names;
388     }
389   }
390 }