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