1 /* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2004-2009 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 */ 21 package net.fckeditor.handlers; 22 23 import java.util.Collections; 24 import java.util.HashMap; 25 import java.util.Map; 26 import java.util.Set; 27 28 import net.fckeditor.tool.Utils; 29 30 /** 31 * File Browser resource types. The File Browser provides a specific resource 32 * type for each and every request. This class is intended to reflect these in 33 * an Enum-like manner. 34 * <p> 35 * The resource types are: 36 * <ul> 37 * <li>File</li> 38 * <li>Flash</li> 39 * <li>Image</li> 40 * <li>Media</li> 41 * </ul> 42 * 43 * @version $Id: ResourceType.java 3759 2009-06-22 20:02:26Z mosipov $ 44 */ 45 public class ResourceType { 46 47 /** The name of the resource type */ 48 private String name; 49 /** The absolute path of the resource type */ 50 private String path; 51 /** The allowed extensions */ 52 private Set<String> allowedEextensions; 53 /** The denied extensions */ 54 private Set<String> deniedExtensions; 55 56 /** Map holding a String to ResourceType reference */ 57 private static Map<String, ResourceType> types = new HashMap<String, ResourceType>( 58 4); 59 60 /** Resource type <code>File</code> */ 61 public static final ResourceType FILE = new ResourceType("File", 62 PropertiesLoader.getFileResourceTypePath(), Utils 63 .getSet(PropertiesLoader 64 .getFileResourceTypeAllowedExtensions()), Utils 65 .getSet(PropertiesLoader 66 .getFileResourceTypeDeniedExtensions())); 67 /** Resource type <code>Flash</code> */ 68 public static final ResourceType FLASH = new ResourceType("Flash", 69 PropertiesLoader.getFlashResourceTypePath(), Utils 70 .getSet(PropertiesLoader 71 .getFlashResourceTypeAllowedExtensions()), Utils 72 .getSet(PropertiesLoader 73 .getFlashResourceTypeDeniedExtensions())); 74 /** Resource type <code>Image</code> */ 75 public static final ResourceType IMAGE = new ResourceType("Image", 76 PropertiesLoader.getImageResourceTypePath(), Utils 77 .getSet(PropertiesLoader 78 .getImageResourceTypeAllowedExtensions()), Utils 79 .getSet(PropertiesLoader 80 .getImageResourceTypeDeniedExtensions())); 81 /** Resource type <code>Media</code> */ 82 public static final ResourceType MEDIA = new ResourceType("Media", 83 PropertiesLoader.getMediaResourceTypePath(), Utils 84 .getSet(PropertiesLoader 85 .getMediaResourceTypeAllowedExtensions()), Utils 86 .getSet(PropertiesLoader 87 .getMediaResourceTypeDeniedExtensions())); 88 89 static { 90 types.put(FILE.getName(), FILE); 91 types.put(FLASH.getName(), FLASH); 92 types.put(IMAGE.getName(), IMAGE); 93 types.put(MEDIA.getName(), MEDIA); 94 } 95 96 /** 97 * This constructor has been made intentionally made private to provide 98 * pre-defined types only. 99 * 100 * @param name 101 * the name of the new resource type 102 * @param path 103 * the absolute path of the new resource type 104 * @param allowedEextensions 105 * the allowed extensions set of the new resource type 106 * @param deniedExtensions 107 * the denied extensions set of the new resource type 108 * @throws IllegalArgumentException 109 * if both sets are empty 110 * @throws IllegalArgumentException 111 * if both sets contain extensions 112 */ 113 private ResourceType(final String name, final String path, 114 final Set<String> allowedEextensions, 115 final Set<String> deniedExtensions) { 116 this.name = name; 117 this.path = path; 118 119 if (allowedEextensions.isEmpty() && deniedExtensions.isEmpty()) 120 throw new IllegalArgumentException( 121 "Both sets are empty, one has always to be filled"); 122 123 if (!allowedEextensions.isEmpty() && !deniedExtensions.isEmpty()) 124 throw new IllegalArgumentException( 125 "Both sets contain extensions, only one can be filled at the same time"); 126 127 this.allowedEextensions = allowedEextensions; 128 this.deniedExtensions = deniedExtensions; 129 } 130 131 /** 132 * Returns the name of this resource type. 133 * 134 * @return the name of this resource type 135 */ 136 public String getName() { 137 return name; 138 } 139 140 /** 141 * Returns the absolute path of this resource type. This path is absolute to 142 * the userfiles path. To set this path, see the <a 143 * href="http://java.fckeditor.net/properties.html">configuration</a>. 144 * 145 * @return the absolute path of this resource type 146 */ 147 public String getPath() { 148 return path; 149 } 150 151 /** 152 * Returns a read-only reference to the allowed extensions set. 153 * 154 * @return the read-only allowed extensions set of this resource type 155 */ 156 public Set<String> getAllowedEextensions() { 157 return Collections.unmodifiableSet(allowedEextensions); 158 } 159 160 /** 161 * Returns a read-only reference to the denied extensions set. 162 * 163 * @return the read-only denied extensions set of this resource type 164 */ 165 public Set<String> getDeniedExtensions() { 166 return Collections.unmodifiableSet(deniedExtensions); 167 } 168 169 /** 170 * Returns the resource type constant with the specified name. 171 * 172 * @param name 173 * the name of the constant to return 174 * @return the resource type constant with the specified name 175 * @throws IllegalArgumentException 176 * if this class has no constant with the specified name 177 * @throws NullPointerException 178 * if <code>name</code> is null or empty 179 */ 180 public static ResourceType valueOf(final String name) { 181 if (Utils.isEmpty(name)) 182 throw new NullPointerException("Name is null or empty"); 183 184 ResourceType rt = types.get(name); 185 if (rt == null) 186 throw new IllegalArgumentException("No resource type const " + name); 187 return rt; 188 } 189 190 /** 191 * Returns <code>true</code> if name represents a valid resource type 192 * constant. 193 * 194 * @param name 195 * the resource type to check 196 * @return <code>true</code> if name represents a valid resource type, else 197 * <code>false</code> 198 */ 199 public static boolean isValidType(final String name) { 200 // HashMap permits null keys, so it will return false in that case 201 return types.containsKey(name); 202 } 203 204 /** 205 * Returns the resource type constant with the specified name. In contrast 206 * to {@link #valueOf(String)} it returns a null instead of throwing an 207 * exception if a resource type constant was not found. 208 * 209 * @param name 210 * the name of the constant to return 211 * @return the resource type constant with the specified name, else 212 * <code>null</code> 213 */ 214 public static ResourceType getResourceType(final String name) { 215 try { 216 return ResourceType.valueOf(name); 217 } catch (Exception e) { 218 return null; 219 } 220 } 221 222 /** 223 * Returns the resource type constant with the specified name. In contrast 224 * to {@link #getResourceType(String)} it returns {@link #FILE} instead of 225 * returning <code>null</code>. 226 * 227 * @param name 228 * the name of the constant to return 229 * @return the resource type constant with the specified name, else 230 * <code>FILE</code> 231 */ 232 public static ResourceType getDefaultResourceType(final String name) { 233 ResourceType rt = getResourceType(name); 234 return rt == null ? FILE : rt; 235 } 236 237 /** 238 * Returns <code>true</code> if extension is allowed. Denied extensions set 239 * takes precedence over allowed extensions set, in other words a negative 240 * check is made against denied set and if this fails, allowed set is 241 * checked. 242 * 243 * @param extension 244 * the extension to check, empty will fail 245 * @return <code>true</code> if extension is allowed, else 246 * <code>false</code> 247 */ 248 public boolean isAllowedExtension(final String extension) { 249 if (Utils.isEmpty(extension)) 250 return false; 251 String ext = extension.toLowerCase(); 252 if (allowedEextensions.isEmpty()) 253 return !deniedExtensions.contains(ext); 254 if (deniedExtensions.isEmpty()) 255 return allowedEextensions.contains(ext); 256 return false; 257 } 258 259 /** 260 * This method wraps {@link #isAllowedExtension(String)}. It simply negates 261 * the return value. 262 * 263 * @deprecated Method will be removed in FCKeditor.Java 2.6, use 264 * {@link #isDeniedExtension(String)}. 265 * @see #isDeniedExtension(String) 266 * @param extension 267 * Extension string. 268 * @return <code>true</code> if extension is not fails, else 269 * <code>false</code>. 270 */ 271 @Deprecated 272 public boolean isNotAllowedExtension(final String extension) { 273 return !isAllowedExtension(extension); 274 } 275 276 /** 277 * Returns <code>true</code> if extension is denied. This method simply 278 * negates {@link #isAllowedExtension(String)}. 279 * 280 * @param extension 281 * the extension to check, empty will fail 282 * @return <code>true</code> if extension is denied, else <code>false</code> 283 */ 284 public boolean isDeniedExtension(final String extension) { 285 return !isAllowedExtension(extension); 286 } 287 288 /** 289 * Compares the specified object with this resource type for equality. The 290 * comparison is based on class and name only. 291 * 292 * @param obj 293 * Object to be compared with this resource type. 294 * @return <code>true</code> if the specified object is equal to this 295 * resource type 296 */ 297 @Override 298 public boolean equals(Object obj) { 299 if (this == obj) 300 return true; 301 302 if (obj == null || this.getClass() != obj.getClass()) 303 return false; 304 305 final ResourceType rt = (ResourceType) obj; 306 return name.equals(rt.getName()); 307 } 308 309 /** 310 * Returns the hash code value for this resource type. The hash code equals 311 * the hash code of the name field. 312 * 313 * @return the hash code value for this resource type 314 */ 315 @Override 316 public int hashCode() { 317 return name.hashCode(); 318 } 319 }