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    private static final long serialVersionUID = 2L;
36  
37    private static final Logger LOG = LoggerFactory.getLogger(UserAgent.class);
38  
39    public static final String DEFAULT_NAME = "standard";
40  
41    public static final UserAgent DEFAULT = new UserAgent(null, null);
42  
43    public static final UserAgent MSIE = new UserAgent("msie", null);
44  
45    /**
46     * @deprecated no longer supported, since Tobago 1.5
47     */
48    @Deprecated
49    public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0");
50  
51    /**
52     * @deprecated no longer supported, since Tobago 1.5
53     */
54    @Deprecated
55    public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5");
56  
57    public static final UserAgent MSIE_6_0 = new UserAgent(
58        "msie", "6_0", EnumSet.noneOf(Capability.class), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
59  
60    public static final UserAgent MSIE_7_0 = new UserAgent(
61        "msie", "7_0", EnumSet.noneOf(Capability.class), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
62  
63    public static final UserAgent MSIE_7_0_COMPAT = new UserAgent(
64        "msie", "7_0", EnumSet.of(Capability.IE_COMPATIBILITY_MODE), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
65  
66    /**
67     * @deprecated no longer supported, since Tobago 1.5. Misspelled. Use {@link #MSIE_7_0}
68     */
69    @Deprecated
70    public static final UserAgent MSIE_7_O = MSIE_7_0;
71  
72    public static final UserAgent MSIE_8_0 = new UserAgent(
73        "msie", "8_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
74  
75    public static final UserAgent MSIE_9_0 = new UserAgent(
76        "msie", "9_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
77  
78    // CSP is not fully supported, only sandboxing
79    public static final UserAgent MSIE_10_0 = new UserAgent(
80        "msie", "10_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
81  
82    // CSP is not fully supported, only sandboxing
83    public static final UserAgent MSIE_11_0 = new UserAgent(
84        "msie", "11_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
85  
86    /**
87     * @deprecated no longer supported, since Tobago 1.5
88     */
89    @Deprecated
90    public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac");
91  
92    /**
93     * @deprecated no longer supported, since Tobago 1.5
94     */
95    @Deprecated
96    public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac");
97  
98  
99    /**
100    * e. g. Opera 10
101    */
102   public static final UserAgent PRESTO = new UserAgent("presto", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML));
103 
104   /**
105    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
106    */
107   public static final UserAgent OPERA = new UserAgent("opera", null);
108 
109   /**
110    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
111    */
112   @Deprecated
113   public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0");
114 
115   /**
116    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
117    */
118   @Deprecated
119   public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0");
120 
121   /**
122    * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
123    */
124   @Deprecated
125   public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11");
126 
127   /**
128    * @deprecated no longer supported, since Tobago 1.5
129    */
130   @Deprecated
131   public static final UserAgent MOZILLA = new UserAgent("mozilla", null);
132 
133   /**
134    * @deprecated no longer supported, since Tobago 1.5
135    */
136   @Deprecated
137   public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7");
138 
139   /**
140    * @deprecated no longer supported, since Tobago 1.5
141    */
142   @Deprecated
143   public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0");
144 
145   /**
146    * @deprecated no longer supported, since Tobago 1.5
147    */
148   @Deprecated
149   public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6");
150 
151   /**
152    * e. g. Firefox
153    */
154   public static final UserAgent GECKO = new UserAgent("gecko", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML));
155 
156   /**
157    * e. g. Firefox 2.0
158    */
159   public static final UserAgent GECKO_1_8 = new UserAgent("gecko", "1_8", EnumSet.of(Capability.CONTENT_TYPE_XHTML),
160       CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
161 
162   /**
163    * e. g. Firefox 3.0, 3.5, 3.6
164    */
165   public static final UserAgent GECKO_1_9 = new UserAgent("gecko", "1_9", EnumSet.of(Capability.CONTENT_TYPE_XHTML),
166       CspHeader.NOT_SUPPORTED, CsproHeader.NOT_SUPPORTED);
167 
168   /**
169    * e. g. Firefox 4 to 22
170    */
171   public static final UserAgent GECKO_2_0
172       = new UserAgent("gecko", null,
173       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.X, CsproHeader.X);
174 
175   /**
176    * e. g. Firefox 23 or higher
177    */
178   public static final UserAgent GECKO_23_0
179       = new UserAgent("gecko", null,
180       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.STANDARD, CsproHeader.STANDARD);
181 
182   /**
183    * e. g. Safari 4, Safari 5, Chrome
184    */
185   public static final UserAgent WEBKIT
186       = new UserAgent("webkit", null,
187       EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML), CspHeader.WEBKIT, CsproHeader.WEBKIT);
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 static 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     private CspHeader(final String[] names) {
365       this.names = names;
366     }
367 
368     public String[] getNames() {
369       return names;
370     }
371   }
372 
373   private static 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     private CsproHeader(final String[] names) {
383       this.names = names;
384     }
385 
386     public String[] getNames() {
387       return names;
388     }
389   }
390 }