View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 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.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import net.fckeditor.tool.Utils;
28  
29  /**
30   * This handler manages the allowed and denied extensions for each resource
31   * type. The extensions are preset by the properties managed by
32   * {@link PropertiesLoader}.
33   * <p>
34   * <em>Hint</em>: It's recommend to use either allowed or denied extensions for one file
35   * type. <strong>Never</strong> use both at the same time! That's why denied
36   * extensions of a file type will be deleted if you set the allowed one and vice
37   * versa.
38   * </p>
39   * 
40   * @version $Id: ExtensionsHandler.java 2101 2008-06-22 22:00:48Z mosipov $
41   */
42  public class ExtensionsHandler {
43  
44  	private static Map<ResourceTypeHandler, Set<String>> extensionsAllowed = new HashMap<ResourceTypeHandler, Set<String>>();
45  	private static Map<ResourceTypeHandler, Set<String>> extensionsDenied = new HashMap<ResourceTypeHandler, Set<String>>();
46  
47  	static {
48  		// load defaults
49  		extensionsAllowed.put(ResourceTypeHandler.FILE, Utils.getSet(PropertiesLoader
50  		    .getProperty("connector.resourceType.file.extensions.allowed")));
51  		extensionsDenied.put(ResourceTypeHandler.FILE, Utils.getSet(PropertiesLoader
52  		    .getProperty("connector.resourceType.file.extensions.denied")));
53  		extensionsAllowed.put(ResourceTypeHandler.MEDIA, Utils.getSet(PropertiesLoader
54  		    .getProperty("connector.resourceType.media.extensions.allowed")));
55  		extensionsDenied.put(ResourceTypeHandler.MEDIA, Utils.getSet(PropertiesLoader
56  		    .getProperty("connector.resourceType.media.extensions.denied")));
57  		extensionsAllowed.put(ResourceTypeHandler.IMAGE, Utils.getSet(PropertiesLoader
58  		    .getProperty("connector.resourceType.image.extensions.allowed")));
59  		extensionsDenied.put(ResourceTypeHandler.IMAGE, Utils.getSet(PropertiesLoader
60  		    .getProperty("connector.resourceType.image.extensions.denied")));
61  		extensionsAllowed.put(ResourceTypeHandler.FLASH, Utils.getSet(PropertiesLoader
62  		    .getProperty("connector.resourceType.flash.extensions.allowed")));
63  		extensionsDenied.put(ResourceTypeHandler.FLASH, Utils.getSet(PropertiesLoader
64  		    .getProperty("connector.resourceType.flash.extensions.denied")));
65  	}
66  
67  	/**
68  	 * Getter for the allowed extensions of a file type.
69  	 * 
70  	 * @param type
71  	 *          The file type.
72  	 * @return Set of allowed extensions or an empty set.
73  	 */
74  	public static Set<String> getExtensionsAllowed(final ResourceTypeHandler type) {
75  		return extensionsAllowed.get(type);
76  	}
77  
78  	/**
79  	 * Setter for the allowed extensions of a file type. The denied extensions
80  	 * will be cleared.<br />
81  	 * If <code>extensionsList</code> is <code>null</code>, allowed
82  	 * extensions are kept untouched.
83  	 * 
84  	 * @param type
85  	 *            The file type.
86  	 * @param extensionsList
87  	 *            Required format: <code>ext1&#124;ext2&#124;ext3</code>
88  	 */
89  	public static void setExtensionsAllowed(final ResourceTypeHandler type, final String extensionsList) {
90  		if (extensionsList != null) {
91  			extensionsAllowed.put(type, Utils.getSet(extensionsList));
92  			extensionsDenied.get(type).clear();
93  		}
94  	}
95  
96  	/**
97  	 * Getter for the denied extensions of a file type.
98  	 * 
99  	 * @param type
100 	 *            The file type.
101 	 * @return Set of denied extensions or an empty set.
102 	 */
103 	public static Set<String> getExtensionsDenied(final ResourceTypeHandler type) {
104 		return extensionsDenied.get(type);
105 	}
106 
107 	/**
108 	 * Setter for the denied extensions of a file type. The allowed extensions
109 	 * will be cleared.<br />
110 	 * If <code>extensionsList</code> is <code>null</code>, denied
111 	 * extensions are kept untouched.
112 	 * 
113 	 * @param type
114 	 *            The file type.
115 	 * @param extensionsList
116 	 *            Required format: <code>ext1&#124;ext2&#124;ext3</code>
117 	 */
118 	public static void setExtensionsDenied(final ResourceTypeHandler type, final String extensionsList) {
119 		if (extensionsList != null) {
120 			extensionsDenied.put(type, Utils.getSet(extensionsList));
121 			extensionsAllowed.get(type).clear();
122 		}
123 	}
124 
125 	/**
126 	 * Checks if an extension is allowed for a file type.
127 	 * 
128 	 * @param type
129 	 *            The resource type you want to check.
130 	 * @param extension
131 	 *            The extension you want to check.
132 	 * @return <code>true</code> is extension is allowed else
133 	 *         <code>false</code>. <em>Attention</em>: <code>false</code>
134 	 *         is always returned if 'type' or 'extensions' is <code>null</code>.
135 	 */
136 	public static boolean isAllowed(final ResourceTypeHandler type, final String extension) {
137 		if (type == null || extension == null)
138 			return false;
139 		String ext = extension.toLowerCase();
140 		Set<String> allowed = extensionsAllowed.get(type);
141 		Set<String> denied = extensionsDenied.get(type);
142 		if (allowed.isEmpty())
143 			return !denied.contains(ext);
144 		if (denied.isEmpty())
145 			return allowed.contains(ext);
146 		return false;
147 	}
148 }