001    package org.apache.myfaces.tobago.context;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.internal.util.Deprecation;
021    
022    import java.io.Serializable;
023    import java.util.ArrayList;
024    import java.util.Arrays;
025    import java.util.Collections;
026    import java.util.HashSet;
027    import java.util.List;
028    import java.util.Set;
029    
030    public class UserAgent implements Serializable {
031    
032      private static final long serialVersionUID = 2L;
033    
034      public static final String DEFAULT_NAME = "standard";
035    
036      public static final UserAgent DEFAULT = new UserAgent(null, null);
037    
038    
039      public static final UserAgent MSIE = new UserAgent("msie", null);
040    
041      /**
042       * @deprecated no longer supported, since Tobago 1.5
043       */
044      @Deprecated
045      public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0");
046    
047      /**
048       * @deprecated no longer supported, since Tobago 1.5
049       */
050      @Deprecated
051      public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5");
052    
053      public static final UserAgent MSIE_6_0 = new UserAgent("msie", "6_0");
054    
055      public static final UserAgent MSIE_7_0 = new UserAgent("msie", "7_0");
056    
057      /**
058       * @deprecated no longer supported, since Tobago 1.5. Misspelled. Use {@link #MSIE_7_0}
059       */
060      @Deprecated
061      public static final UserAgent MSIE_7_O = MSIE_7_0;
062    
063      public static final UserAgent MSIE_8_0 = new UserAgent("msie", "8_0");
064    
065      public static final UserAgent MSIE_9_0 = new UserAgent("msie", "9_0");
066    
067      /**
068       * @deprecated no longer supported, since Tobago 1.5
069       */
070      @Deprecated
071      public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac");
072    
073      /**
074       * @deprecated no longer supported, since Tobago 1.5
075       */
076      @Deprecated
077      public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac");
078    
079    
080      /**
081       * e. g. Opera 10
082       */
083      public static final UserAgent PRESTO = new UserAgent("presto", null);
084    
085      /**
086       * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
087       */
088      public static final UserAgent OPERA = new UserAgent("opera", null);
089    
090      /**
091       * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
092       */
093      @Deprecated
094      public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0");
095    
096      /**
097       * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
098       */
099      @Deprecated
100      public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0");
101    
102      /**
103       * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}.
104       */
105      @Deprecated
106      public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11");
107    
108      /**
109       * @deprecated no longer supported, since Tobago 1.5
110       */
111      @Deprecated
112      public static final UserAgent MOZILLA = new UserAgent("mozilla", null);
113    
114      /**
115       * @deprecated no longer supported, since Tobago 1.5
116       */
117      @Deprecated
118      public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7");
119    
120      /**
121       * @deprecated no longer supported, since Tobago 1.5
122       */
123      @Deprecated
124      public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0");
125    
126      /**
127       * @deprecated no longer supported, since Tobago 1.5
128       */
129      @Deprecated
130      public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6");
131    
132      /**
133       * e. g. Firefox
134       */
135      public static final UserAgent GECKO = new UserAgent("gecko", null);
136    
137      /**
138       * e. g. Firefox 2.0
139       */
140      public static final UserAgent GECKO_1_8 = new UserAgent("gecko", "1.8");
141    
142      /**
143       * e. g. Firefox 3.0, 3.5, 3.6
144       */
145      public static final UserAgent GECKO_1_9 = new UserAgent("gecko", "1.9");
146    
147      /**
148       * e. g. Firefox 4.0
149       */
150      public static final UserAgent GECKO_2_0 = new UserAgent("gecko", "2.0", Capability.PLACEHOLDER);
151    
152      /**
153       * e. g. Safari 4, Safari 5, Chrome
154       */
155      public static final UserAgent WEBKIT = new UserAgent("webkit", null, Capability.PLACEHOLDER);
156    
157      private String name;
158    
159      private String version;
160    
161      private Set<Capability> capabilities = new HashSet<Capability>();
162    
163      private UserAgent(String name, String version, Capability... capabilities) {
164        this.name = name;
165        this.version = version;
166        this.capabilities.addAll(Arrays.asList(capabilities));
167      }
168    
169      public boolean hasCapability(Capability capability) {
170        return capabilities.contains(capability);
171      }
172    
173      public boolean isMsie() {
174        return MSIE.name.equals(name);
175      }
176    
177      public boolean isMsie6() {
178        return MSIE_6_0.name.equals(name) && MSIE_6_0.version.equals(version);
179      }
180    
181      /**
182       * @deprecated no longer supported, since Tobago 1.5
183       */
184      @Deprecated
185      public boolean isMozilla() {
186        return MOZILLA.name.equals(name);
187      }
188    
189      public List<String> getFallbackList() {
190        return getFallbackList(false);
191      }
192    
193      private List<String> getFallbackList(boolean reverseOrder) {
194        List<String> list = new ArrayList<String>(3);
195        if (version != null) {
196          list.add(name + '_' + version);
197        }
198        if (name != null) {
199          list.add(name);
200        }
201        list.add(DEFAULT_NAME);
202        if (reverseOrder) {
203          Collections.reverse(list);
204        }
205        return list;
206      }
207    
208      public static UserAgent getInstance(String header) {
209        if (header == null) {
210          return DEFAULT;
211        }
212    
213        if (header.contains("MSIE")) {
214          if (header.contains("MSIE 6.0")) {
215            return MSIE_6_0;
216          } else if (header.contains("MSIE 7.0")) {
217            return MSIE_7_0;
218          } else if (header.contains("MSIE 8.0")) {
219            return MSIE_8_0;
220          } else if (header.contains("MSIE 9.0")) {
221            return MSIE_9_0;
222          } else {
223            return MSIE;
224          }
225        } else if (header.contains("AppleWebKit")) {
226          return WEBKIT;
227        } else if (header.contains("Gecko")) {
228          if (header.contains("rv:1.8")) {
229            return GECKO_1_8;
230          } else if (header.contains("rv:1.9")) {
231            return GECKO_1_9;
232          } else if (header.contains("rv:2.0")) {
233            return GECKO_2_0;
234          } else {
235            return GECKO;
236          }
237        } else if (header.contains("Presto")) {
238          return PRESTO;
239        }
240    
241        return DEFAULT;
242      }
243    
244      /**
245       * @deprecated no longer supported, since Tobago 1.5
246       */
247      @Deprecated
248      public static UserAgent getInstanceForId(String id) {
249        Deprecation.LOG.error("Getting the user agent from its id is no longer supported! id='" + id + "'");
250        return DEFAULT;
251      }
252    
253      /**
254       * @deprecated don't use toString() functionality, but for logging!
255       */
256      @Deprecated
257      public String toString() {
258        return version != null
259            ? name + '_' + version
260            : name;
261      }
262    }