001: /**
002: * Copyright (c) 2003-2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.encryption;
031:
032: import org.pdfbox.cos.COSDictionary;
033: import org.pdfbox.cos.COSName;
034: import java.io.IOException;
035:
036: import java.lang.reflect.Constructor;
037: import java.lang.reflect.InvocationTargetException;
038:
039: import java.util.Collections;
040: import java.util.HashMap;
041: import java.util.Map;
042:
043: /**
044: * This class will handle loading of the different security handlers.
045: *
046: * See PDF Reference 1.4 section "3.5 Encryption"
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @version $Revision: 1.7 $
050: * @deprecated Made deprecated by the new security layer of PDFBox. Use SecurityHandlers instead.
051: */
052:
053: public class PDEncryptionManager {
054: private static Map handlerMap = Collections
055: .synchronizedMap(new HashMap());
056:
057: static {
058: registerSecurityHandler(PDStandardEncryption.FILTER_NAME,
059: PDStandardEncryption.class);
060: }
061:
062: private PDEncryptionManager() {
063: }
064:
065: /**
066: * This will allow the user to register new security handlers when unencrypting a
067: * document.
068: *
069: * @param filterName As described in the encryption dictionary.
070: * @param handlerClass A subclass of PDEncryptionDictionary that has a constructor that takes
071: * a COSDictionary.
072: */
073: public static void registerSecurityHandler(String filterName,
074: Class handlerClass) {
075: handlerMap.put(COSName.getPDFName(filterName), handlerClass);
076: }
077:
078: /**
079: * This will get the correct security handler for the encryption dictionary.
080: *
081: * @param dictionary The encryption dictionary.
082: *
083: * @return An implementation of PDEncryptionDictionary(PDStandardEncryption for most cases).
084: *
085: * @throws IOException If a security handler could not be found.
086: */
087: public static PDEncryptionDictionary getEncryptionDictionary(
088: COSDictionary dictionary) throws IOException {
089: Object retval = null;
090: if (dictionary != null) {
091: COSName filter = (COSName) dictionary
092: .getDictionaryObject(COSName.FILTER);
093: Class handlerClass = (Class) handlerMap.get(filter);
094: if (handlerClass == null) {
095: throw new IOException(
096: "No handler for security handler '"
097: + filter.getName() + "'");
098: } else {
099: try {
100: Constructor ctor = handlerClass
101: .getConstructor(new Class[] { COSDictionary.class });
102: retval = ctor
103: .newInstance(new Object[] { dictionary });
104: } catch (NoSuchMethodException e) {
105: throw new IOException(e.getMessage());
106: } catch (InstantiationException e) {
107: throw new IOException(e.getMessage());
108: } catch (IllegalAccessException e) {
109: throw new IOException(e.getMessage());
110: } catch (InvocationTargetException e) {
111: throw new IOException(e.getMessage());
112: }
113: }
114: }
115: return (PDEncryptionDictionary) retval;
116:
117: }
118: }
|