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.Collections; 025 import java.util.EnumSet; 026 import java.util.List; 027 028 public class UserAgent implements Serializable { 029 030 private static final long serialVersionUID = 2L; 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 /** 040 * @deprecated no longer supported, since Tobago 1.5 041 */ 042 @Deprecated 043 public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0"); 044 045 /** 046 * @deprecated no longer supported, since Tobago 1.5 047 */ 048 @Deprecated 049 public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5"); 050 051 public static final UserAgent MSIE_6_0 = new UserAgent("msie", "6_0"); 052 053 public static final UserAgent MSIE_7_0 = new UserAgent("msie", "7_0"); 054 055 /** 056 * @deprecated no longer supported, since Tobago 1.5. Misspelled. Use {@link #MSIE_7_0} 057 */ 058 @Deprecated 059 public static final UserAgent MSIE_7_O = MSIE_7_0; 060 061 public static final UserAgent MSIE_8_0 = new UserAgent("msie", "8_0"); 062 063 public static final UserAgent MSIE_9_0 = new UserAgent("msie", "9_0", EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 064 065 /** 066 * @deprecated no longer supported, since Tobago 1.5 067 */ 068 @Deprecated 069 public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac"); 070 071 /** 072 * @deprecated no longer supported, since Tobago 1.5 073 */ 074 @Deprecated 075 public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac"); 076 077 078 /** 079 * e. g. Opera 10 080 */ 081 public static final UserAgent PRESTO = new UserAgent("presto", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 082 083 /** 084 * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}. 085 */ 086 public static final UserAgent OPERA = new UserAgent("opera", null); 087 088 /** 089 * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}. 090 */ 091 @Deprecated 092 public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0"); 093 094 /** 095 * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}. 096 */ 097 @Deprecated 098 public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0"); 099 100 /** 101 * @deprecated no longer supported, since Tobago 1.5. Please use {@link #PRESTO}. 102 */ 103 @Deprecated 104 public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11"); 105 106 /** 107 * @deprecated no longer supported, since Tobago 1.5 108 */ 109 @Deprecated 110 public static final UserAgent MOZILLA = new UserAgent("mozilla", null); 111 112 /** 113 * @deprecated no longer supported, since Tobago 1.5 114 */ 115 @Deprecated 116 public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7"); 117 118 /** 119 * @deprecated no longer supported, since Tobago 1.5 120 */ 121 @Deprecated 122 public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0"); 123 124 /** 125 * @deprecated no longer supported, since Tobago 1.5 126 */ 127 @Deprecated 128 public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6"); 129 130 /** 131 * e. g. Firefox 132 */ 133 public static final UserAgent GECKO = new UserAgent("gecko", null, EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 134 135 /** 136 * e. g. Firefox 2.0 137 */ 138 public static final UserAgent GECKO_1_8 = new UserAgent("gecko", "1_8", EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 139 140 /** 141 * e. g. Firefox 3.0, 3.5, 3.6 142 */ 143 public static final UserAgent GECKO_1_9 = new UserAgent("gecko", "1_9", EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 144 145 /** 146 * e. g. Firefox 4.0 147 */ 148 public static final UserAgent GECKO_2_0 149 = new UserAgent("gecko", "2_0", EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML)); 150 151 /** 152 * e. g. Firefox 5.0 153 */ 154 public static final UserAgent GECKO_5_0 155 = new UserAgent("gecko", "5_0", EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML)); 156 157 /** 158 * e. g. Safari 4, Safari 5, Chrome 159 */ 160 public static final UserAgent WEBKIT 161 = new UserAgent("webkit", null, EnumSet.of(Capability.PLACEHOLDER, Capability.CONTENT_TYPE_XHTML)); 162 163 private final String name; 164 165 private final String version; 166 167 private final EnumSet<Capability> capabilities; 168 169 private UserAgent(String name, String version) { 170 this(name , version, EnumSet.of(Capability.CONTENT_TYPE_XHTML)); 171 } 172 173 private UserAgent(String name, String version, EnumSet<Capability> capabilities) { 174 this.name = name; 175 this.version = version; 176 this.capabilities = capabilities; 177 } 178 179 public boolean hasCapability(Capability capability) { 180 return capabilities.contains(capability); 181 } 182 183 public boolean isMsie() { 184 return MSIE.name.equals(name); 185 } 186 187 public boolean isMsie6() { 188 return MSIE_6_0.name.equals(name) && MSIE_6_0.version.equals(version); 189 } 190 191 /** 192 * @deprecated no longer supported, since Tobago 1.5 193 */ 194 @Deprecated 195 public boolean isMozilla() { 196 return MOZILLA.name.equals(name); 197 } 198 199 public List<String> getFallbackList() { 200 return getFallbackList(false); 201 } 202 203 private List<String> getFallbackList(boolean reverseOrder) { 204 List<String> list = new ArrayList<String>(3); 205 if (version != null) { 206 list.add(name + '_' + version); 207 } 208 if (name != null) { 209 list.add(name); 210 } 211 list.add(DEFAULT_NAME); 212 if (reverseOrder) { 213 Collections.reverse(list); 214 } 215 return list; 216 } 217 218 public static UserAgent getInstance(String header) { 219 if (header == null) { 220 return DEFAULT; 221 } 222 223 if (header.contains("MSIE")) { 224 if (header.contains("MSIE 6.0")) { 225 return MSIE_6_0; 226 } else if (header.contains("MSIE 7.0")) { 227 return MSIE_7_0; 228 } else if (header.contains("MSIE 8.0")) { 229 return MSIE_8_0; 230 } else if (header.contains("MSIE 9.0")) { 231 return MSIE_9_0; 232 } else { 233 return MSIE; 234 } 235 } else if (header.contains("AppleWebKit")) { 236 return WEBKIT; 237 } else if (header.contains("Gecko")) { 238 if (header.contains("rv:1.8")) { 239 return GECKO_1_8; 240 } else if (header.contains("rv:1.9")) { 241 return GECKO_1_9; 242 } else if (header.contains("rv:2.0")) { 243 return GECKO_2_0; 244 } else if (header.contains("rv:5.0")) { 245 return GECKO_5_0; 246 } else { 247 return GECKO; 248 } 249 } else if (header.contains("Presto")) { 250 return PRESTO; 251 } 252 253 return DEFAULT; 254 } 255 256 /** 257 * @deprecated no longer supported, since Tobago 1.5 258 */ 259 @Deprecated 260 public static UserAgent getInstanceForId(String id) { 261 Deprecation.LOG.error("Getting the user agent from its id is no longer supported! id='" + id + "'"); 262 return DEFAULT; 263 } 264 265 /** 266 * @deprecated don't use toString() functionality, but for logging! 267 */ 268 @Deprecated 269 public String toString() { 270 return version != null 271 ? name + '_' + version 272 : name; 273 } 274 }