Coverage Report - net.fckeditor.handlers.ExtensionsHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtensionsHandler
91%
29/32
64%
9/14
0
 
 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 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 3840 2009-07-08 20:29:46Z mosipov $
 41  
  */
 42  0
 public class ExtensionsHandler {
 43  
 
 44  1
         private static Map<ResourceTypeHandler, Set<String>> extensionsAllowed = new HashMap<ResourceTypeHandler, Set<String>>();
 45  1
         private static Map<ResourceTypeHandler, Set<String>> extensionsDenied = new HashMap<ResourceTypeHandler, Set<String>>();
 46  
 
 47  
         static {
 48  
                 // load defaults
 49  1
                 extensionsAllowed.put(ResourceTypeHandler.FILE, Utils.getSet(PropertiesLoader
 50  
                     .getProperty("connector.resourceType.file.extensions.allowed")));
 51  1
                 extensionsDenied.put(ResourceTypeHandler.FILE, Utils.getSet(PropertiesLoader
 52  
                     .getProperty("connector.resourceType.file.extensions.denied")));
 53  1
                 extensionsAllowed.put(ResourceTypeHandler.MEDIA, Utils.getSet(PropertiesLoader
 54  
                     .getProperty("connector.resourceType.media.extensions.allowed")));
 55  1
                 extensionsDenied.put(ResourceTypeHandler.MEDIA, Utils.getSet(PropertiesLoader
 56  
                     .getProperty("connector.resourceType.media.extensions.denied")));
 57  1
                 extensionsAllowed.put(ResourceTypeHandler.IMAGE, Utils.getSet(PropertiesLoader
 58  
                     .getProperty("connector.resourceType.image.extensions.allowed")));
 59  1
                 extensionsDenied.put(ResourceTypeHandler.IMAGE, Utils.getSet(PropertiesLoader
 60  
                     .getProperty("connector.resourceType.image.extensions.denied")));
 61  1
                 extensionsAllowed.put(ResourceTypeHandler.FLASH, Utils.getSet(PropertiesLoader
 62  
                     .getProperty("connector.resourceType.flash.extensions.allowed")));
 63  1
                 extensionsDenied.put(ResourceTypeHandler.FLASH, Utils.getSet(PropertiesLoader
 64  
                     .getProperty("connector.resourceType.flash.extensions.denied")));
 65  1
         }
 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  1
                 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  2
                 if (extensionsList != null) {
 91  2
                         extensionsAllowed.put(type, Utils.getSet(extensionsList));
 92  2
                         extensionsDenied.get(type).clear();
 93  
                 }
 94  2
         }
 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  1
                 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  1
                 if (extensionsList != null) {
 120  1
                         extensionsDenied.put(type, Utils.getSet(extensionsList));
 121  1
                         extensionsAllowed.get(type).clear();
 122  
                 }
 123  1
         }
 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  7
                 if (type == null || extension == null)
 138  0
                         return false;
 139  7
                 String ext = extension.toLowerCase();
 140  7
                 Set<String> allowed = extensionsAllowed.get(type);
 141  7
                 Set<String> denied = extensionsDenied.get(type);
 142  7
                 if (allowed.isEmpty())
 143  3
                         return !denied.contains(ext);
 144  4
                 if (denied.isEmpty())
 145  4
                         return allowed.contains(ext);
 146  0
                 return false;
 147  
         }
 148  
 }