001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.context;
021
022 import java.io.Serializable;
023 import java.util.ArrayList;
024 import java.util.Collections;
025 import java.util.List;
026 import java.util.Locale;
027
028 public class UserAgent implements Serializable {
029
030 private static final long serialVersionUID = -3138810465122379395L;
031
032 public static final String DEFAULT_NAME = "standard";
033
034 public static final UserAgent DEFAULT = new UserAgent(null, null);
035
036
037 public static final UserAgent MSIE = new UserAgent("msie", null);
038
039 public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0");
040
041 public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5");
042
043 public static final UserAgent MSIE_6_0 = new UserAgent("msie", "6_0");
044
045 public static final UserAgent MSIE_7_0 = new UserAgent("msie", "7_0");
046
047 /** @deprecated spell error, use {@link #MSIE_7_0} */
048 @Deprecated
049 public static final UserAgent MSIE_7_O = MSIE_7_0;
050
051 public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac");
052
053 public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac");
054
055
056 public static final UserAgent OPERA = new UserAgent("opera", null);
057
058 public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0");
059
060 public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0");
061
062 public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11");
063
064
065 public static final UserAgent MOZILLA = new UserAgent("mozilla", null);
066
067 public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7");
068
069 public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0");
070
071 public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6");
072
073 /** Firefox 2 */
074 public static final UserAgent MOZILLA_5_0_FF_2 = new UserAgent("mozilla", "5_0_ff_2");
075
076 private String name;
077
078 private String version;
079
080
081 private UserAgent(String name, String version) {
082 this.name = name;
083 this.version = version;
084 }
085
086 public boolean isMsie() {
087 return MSIE.name.equals(name);
088 }
089
090 public boolean isMsie6() {
091 return MSIE_6_0.name.equals(name) && MSIE_6_0.version.equals(version);
092 }
093
094 public boolean isMozilla() {
095 return MOZILLA.name.equals(name);
096 }
097
098 public List<String> getFallbackList() {
099 return getFallbackList(false);
100 }
101
102 private List<String> getFallbackList(boolean reverseOrder) {
103 List<String> list = new ArrayList<String>(3);
104 if (version != null) {
105 list.add(name + '_' + version);
106 }
107 if (name != null) {
108 list.add(name);
109 }
110 list.add(DEFAULT_NAME);
111 if (reverseOrder) {
112 Collections.reverse(list);
113 }
114 return list;
115 }
116
117 public static UserAgent getInstance(String header) {
118 if (header == null) {
119 return DEFAULT;
120 }
121
122 header = header.toLowerCase(Locale.ENGLISH).replace('/', ' ');
123 if (header.indexOf("opera") > -1) {
124 if (header.indexOf("opera 5.0") > -1) {
125 return OPERA_5_0;
126 } else if (header.indexOf("opera 6.0") > -1) {
127 return OPERA_6_0;
128 } else if (header.indexOf("opera 7.11") > -1) {
129 return OPERA_7_11;
130 } else {
131 return OPERA;
132 }
133 } else if (header.indexOf("msie") > -1 || header.indexOf("trident") > -1) {
134 if (header.indexOf("msie 5.0") > -1) {
135 if (header.indexOf("mac") > -1) {
136 return MSIE_5_0_MAC;
137 } else {
138 return MSIE_5_0;
139 }
140 } else if (header.indexOf("msie 5.5") > -1) {
141 return MSIE_5_5;
142 } else if (header.indexOf("msie 6.0") > -1) {
143 if (header.indexOf("mac") > -1) {
144 return MSIE_6_0_MAC;
145 } else {
146 return MSIE_6_0;
147 }
148 } else if (header.indexOf("msie 7.0") > -1) {
149 return MSIE_7_O;
150 } else if (header.indexOf("rv:11") > -1) {
151 return MOZILLA; // IE11 - Mozilla is here the best matching browser
152 } else {
153 return MSIE;
154 }
155 } else if (header.indexOf("mozilla") > -1) {
156 if (header.indexOf("mozilla 4.7") > -1) {
157 return MOZILLA_4_7;
158 } else if (header.indexOf("mozilla 5.0") > -1) {
159 if (header.indexOf("rv:1.6") > -1) {
160 return MOZILLA_5_0_R1_6;
161 } else if (header.indexOf("firefox 2") > -1) {
162 return MOZILLA_5_0_FF_2;
163 } else {
164 return MOZILLA_5_0;
165 }
166 } else {
167 return MOZILLA;
168 }
169 }
170
171 return DEFAULT;
172 }
173
174 public static UserAgent getInstanceForId(String id) {
175 if (id == null) {
176 return DEFAULT;
177 }
178
179 if (id.indexOf("opera") == 0) {
180 if (id.equals("opera_5_0")) {
181 return OPERA_5_0;
182 } else if (id.equals("opera_6_0")) {
183 return OPERA_6_0;
184 } else if (id.equals("opera_7_11")) {
185 return OPERA_7_11;
186 } else {
187 return OPERA;
188 }
189 } else if (id.indexOf("msie") == 0) {
190 if (id.equals("msie_5_0")) {
191 return MSIE_5_0;
192 } else if (id.equals("msie_5_0_mac")) {
193 return MSIE_5_0_MAC;
194 } else if (id.equals("msie_5_5")) {
195 return MSIE_5_5;
196 } else if (id.equals("msie_6_0")) {
197 return MSIE_6_0;
198 } else if (id.equals("msie_6_0_mac")) {
199 return MSIE_6_0_MAC;
200 } else if (id.equals("msie_7_0")) {
201 return MSIE_7_O;
202 } else {
203 return MSIE;
204 }
205 } else if (id.indexOf("mozilla") == 0) {
206 if (id.equals("mozilla_4_7")) {
207 return MOZILLA_4_7;
208 } else if (id.equals("mozilla_5_0")) {
209 return MOZILLA_5_0;
210 } else if (id.equals("mozilla_5_0_r1_6")) {
211 return MOZILLA_5_0_R1_6;
212 } else if (id.equals("mozilla_5_0_ff_2")) {
213 return MOZILLA_5_0_FF_2;
214 } else {
215 return MOZILLA;
216 }
217 }
218
219 return DEFAULT;
220 }
221
222
223 /**
224 * @deprecated don't use toString() functionallity!
225 */
226 public String toString() {
227 return version != null
228 ? name + '_' + version
229 : name;
230 }
231 }
232