net.fckeditor.handlers.ExtensionsHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-core Show documentation
Show all versions of java-core Show documentation
This Java library enables the FCKeditor to be used in a
Servlet/J2EE environment. It provides JSP tags for creating a
FCKeditor instance and a Servlet handling server-side user files
and folders.
/*
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
* Copyright (C) 2004-2009 Frederico Caldeira Knabben
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
* - GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html
*
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
* http://www.gnu.org/licenses/lgpl.html
*
* - Mozilla Public License Version 1.1 or later (the "MPL")
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*/
package net.fckeditor.handlers;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import net.fckeditor.tool.Utils;
/**
* This handler manages the allowed and denied extensions for each resource
* type. The extensions are preset by the properties managed by
* {@link PropertiesLoader}.
*
* Hint: It's recommend to use either allowed or denied extensions for
* one file type. Never use both at the same time! That's why
* denied extensions of a file type will be deleted if you set the allowed one
* and vice versa.
*
*
* @version $Id: ExtensionsHandler.java 3695 2009-06-18 20:18:38Z mosipov $
* @deprecated Class will be removed in FCKeditor.Java 2.6, functionality merged
* into {@link ResourceType}.
* @see ResourceType
*/
@Deprecated
public class ExtensionsHandler {
private static Map> extensionsAllowed = new HashMap>();
private static Map> extensionsDenied = new HashMap>();
static {
// load defaults
extensionsAllowed.put(ResourceType.FILE, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.file.extensions.allowed")));
extensionsDenied.put(ResourceType.FILE, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.file.extensions.denied")));
extensionsAllowed.put(ResourceType.MEDIA, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.media.extensions.allowed")));
extensionsDenied.put(ResourceType.MEDIA, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.media.extensions.denied")));
extensionsAllowed.put(ResourceType.IMAGE, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.image.extensions.allowed")));
extensionsDenied.put(ResourceType.IMAGE, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.image.extensions.denied")));
extensionsAllowed.put(ResourceType.FLASH, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.flash.extensions.allowed")));
extensionsDenied.put(ResourceType.FLASH, Utils.getSet(PropertiesLoader
.getProperty("connector.resourceType.flash.extensions.denied")));
}
/**
* Getter for the allowed extensions of a file type.
*
* @param type
* The file type.
* @return Set of allowed extensions or an empty set.
*/
public static Set getExtensionsAllowed(final ResourceType type) {
return extensionsAllowed.get(type);
}
/**
* Setter for the allowed extensions of a file type. The denied extensions
* will be cleared.
* If extensionsList
is null
, allowed
* extensions are kept untouched.
*
* @param type
* The file type.
* @param extensionsList
* Required format: ext1|ext2|ext3
*/
public static void setExtensionsAllowed(final ResourceType type, final String extensionsList) {
if (extensionsList != null) {
extensionsAllowed.put(type, Utils.getSet(extensionsList));
extensionsDenied.get(type).clear();
}
}
/**
* Getter for the denied extensions of a file type.
*
* @param type
* The file type.
* @return Set of denied extensions or an empty set.
*/
public static Set getExtensionsDenied(final ResourceType type) {
return extensionsDenied.get(type);
}
/**
* Setter for the denied extensions of a file type. The allowed extensions
* will be cleared.
* If extensionsList
is null
, denied
* extensions are kept untouched.
*
* @param type
* The file type.
* @param extensionsList
* Required format: ext1|ext2|ext3
*/
public static void setExtensionsDenied(final ResourceType type, final String extensionsList) {
if (extensionsList != null) {
extensionsDenied.put(type, Utils.getSet(extensionsList));
extensionsAllowed.get(type).clear();
}
}
/**
* Checks if an extension is allowed for a file type.
*
* @param type
* The resource type you want to check.
* @param extension
* The extension you want to check.
* @return true
is extension is allowed else
* false
. Attention: false
* is always returned if 'type' or 'extensions' is null
.
*/
public static boolean isAllowed(final ResourceType type, final String extension) {
if (type == null || extension == null)
return false;
String ext = extension.toLowerCase();
Set allowed = extensionsAllowed.get(type);
Set denied = extensionsDenied.get(type);
if (allowed.isEmpty())
return !denied.contains(ext);
if (denied.isEmpty())
return allowed.contains(ext);
return false;
}
}