KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > encryption > PDEncryptionManager


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.encryption;
32
33 import org.pdfbox.cos.COSDictionary;
34 import org.pdfbox.cos.COSName;
35 import java.io.IOException JavaDoc;
36
37 import java.lang.reflect.Constructor JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39
40 import java.util.Collections JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * This class will handle loading of the different security handlers.
46  *
47  * See PDF Reference 1.4 section "3.5 Encryption"
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.7 $
51  * @deprecated Made deprecated by the new security layer of PDFBox. Use SecurityHandlers instead.
52  */

53
54 public class PDEncryptionManager
55 {
56     private static Map JavaDoc handlerMap = Collections.synchronizedMap( new HashMap JavaDoc() );
57
58     static
59     {
60         registerSecurityHandler( PDStandardEncryption.FILTER_NAME, PDStandardEncryption.class );
61     }
62
63     private PDEncryptionManager()
64     {
65     }
66
67     /**
68      * This will allow the user to register new security handlers when unencrypting a
69      * document.
70      *
71      * @param filterName As described in the encryption dictionary.
72      * @param handlerClass A subclass of PDEncryptionDictionary that has a constructor that takes
73      * a COSDictionary.
74      */

75     public static void registerSecurityHandler( String JavaDoc filterName, Class JavaDoc handlerClass )
76     {
77         handlerMap.put( COSName.getPDFName( filterName ), handlerClass );
78     }
79
80     /**
81      * This will get the correct security handler for the encryption dictionary.
82      *
83      * @param dictionary The encryption dictionary.
84      *
85      * @return An implementation of PDEncryptionDictionary(PDStandardEncryption for most cases).
86      *
87      * @throws IOException If a security handler could not be found.
88      */

89     public static PDEncryptionDictionary getEncryptionDictionary( COSDictionary dictionary )
90         throws IOException JavaDoc
91     {
92         Object JavaDoc retval = null;
93         if( dictionary != null )
94         {
95             COSName filter = (COSName)dictionary.getDictionaryObject( COSName.FILTER );
96             Class JavaDoc handlerClass = (Class JavaDoc)handlerMap.get( filter );
97             if( handlerClass == null )
98             {
99                 throw new IOException JavaDoc( "No handler for security handler '" + filter.getName() + "'" );
100             }
101             else
102             {
103                 try
104                 {
105                     Constructor JavaDoc ctor = handlerClass.getConstructor( new Class JavaDoc[] {
106                         COSDictionary.class
107                     } );
108                     retval = ctor.newInstance( new Object JavaDoc[] {
109                         dictionary
110                     } );
111                 }
112                 catch( NoSuchMethodException JavaDoc e )
113                 {
114                     throw new IOException JavaDoc( e.getMessage() );
115                 }
116                 catch( InstantiationException JavaDoc e )
117                 {
118                     throw new IOException JavaDoc( e.getMessage() );
119                 }
120                 catch( IllegalAccessException JavaDoc e )
121                 {
122                     throw new IOException JavaDoc( e.getMessage() );
123                 }
124                 catch( InvocationTargetException JavaDoc e )
125                 {
126                     throw new IOException JavaDoc( e.getMessage() );
127                 }
128             }
129         }
130         return (PDEncryptionDictionary)retval;
131
132     }
133 }
Popular Tags