View Javadoc

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