KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-2005, 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
32 package org.pdfbox.pdmodel.encryption;
33
34 import java.lang.reflect.Constructor JavaDoc;
35 import java.security.Security JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 import org.bouncycastle.jce.provider.BouncyCastleProvider;
39
40 /**
41  * This class manages security handlers for the application. It follows the singleton pattern.
42  * To be usable, security managers must be registered in it. Security managers are retrieved by
43  * the application when necessary.
44  *
45  * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
46  *
47  * @version $Revision: 1.3 $
48  *
49  */

50 public class SecurityHandlersManager
51 {
52     
53     /**
54      * The unique instance of this manager.
55      */

56     private static SecurityHandlersManager instance;
57     
58     /**
59      * hashtable used to index handlers regarding their name.
60      * Basically this will be used when opening an encrypted
61      * document to find the appropriate security handler to handle
62      * security features of the document.
63      */

64     private Hashtable JavaDoc handlerNames = null;
65     
66     /**
67      * Hashtable used to index handlers regarding the class of
68      * protection policy they use. Basically this will be used when
69      * encrypting a document.
70      */

71     private Hashtable JavaDoc handlerPolicyClasses = null;
72     
73     /**
74      * private constructor.
75      */

76     private SecurityHandlersManager()
77     {
78         handlerNames = new Hashtable JavaDoc();
79         handlerPolicyClasses = new Hashtable JavaDoc();
80         try
81         {
82             this.registerHandler(
83                 StandardSecurityHandler.FILTER,
84                 StandardSecurityHandler.class,
85                 StandardProtectionPolicy.class);
86             this.registerHandler(
87                 PublicKeySecurityHandler.FILTER,
88                 PublicKeySecurityHandler.class,
89                 PublicKeyProtectionPolicy.class);
90         }
91         catch(Exception JavaDoc e)
92         {
93             System.err.println("SecurityHandlersManager strange error with builtin handlers: " + e.getMessage());
94             System.exit(1);
95         }
96     }
97     
98     /**
99      * register a security handler.
100      *
101      * If the security handler was already registered an exception is thrown.
102      * If another handler was previously registered for the same filter name or
103      * for the same policy name, an exception is thrown
104      *
105      * @param filterName The name of the filter.
106      * @param securityHandlerClass Security Handler class to register.
107      * @param protectionPolicyClass Protection Policy class to register.
108      *
109      * @throws BadSecurityHandlerException If there is an error registering the security handler.
110      */

111     public void registerHandler(String JavaDoc filterName, Class JavaDoc securityHandlerClass, Class JavaDoc protectionPolicyClass)
112         throws BadSecurityHandlerException
113     {
114         if(handlerNames.contains(securityHandlerClass) || handlerPolicyClasses.contains(securityHandlerClass))
115         {
116             throw new BadSecurityHandlerException("the following security handler was already registered: " +
117                 securityHandlerClass.getName());
118         }
119         
120         if(SecurityHandler.class.isAssignableFrom(securityHandlerClass))
121         {
122             try
123             {
124                 if(handlerNames.containsKey(filterName))
125                 {
126                     throw new BadSecurityHandlerException("a security handler was already registered " +
127                         "for the filter name " + filterName);
128                 }
129                 if(handlerPolicyClasses.containsKey(protectionPolicyClass))
130                 {
131                     throw new BadSecurityHandlerException("a security handler was already registered " +
132                         "for the policy class " + protectionPolicyClass.getName());
133                 }
134                 
135                 handlerNames.put(filterName, securityHandlerClass);
136                 handlerPolicyClasses.put(protectionPolicyClass, securityHandlerClass);
137             }
138             catch(Exception JavaDoc e)
139             {
140                 throw new BadSecurityHandlerException(e);
141             }
142         }
143         else
144         {
145             throw new BadSecurityHandlerException("The class is not a super class of SecurityHandler");
146         }
147     }
148     
149     
150     /**
151      * Get the singleton instance.
152      *
153      * @return The SecurityHandlersManager.
154      */

155     public static SecurityHandlersManager getInstance()
156     {
157         if(instance == null)
158         {
159             instance = new SecurityHandlersManager();
160         }
161         Security.addProvider(new BouncyCastleProvider());
162         
163         return instance;
164     }
165     
166     /**
167      * Get the security handler for the protection policy.
168      *
169      * @param policy The policy to get the security handler for.
170      *
171      * @return The appropriate security handler.
172      *
173      * @throws BadSecurityHandlerException If it is unable to create a SecurityHandler.
174      */

175     public SecurityHandler getSecurityHandler(ProtectionPolicy policy) throws BadSecurityHandlerException
176     {
177         
178         Object JavaDoc found = handlerPolicyClasses.get(policy.getClass());
179         if(found == null)
180         {
181             throw new BadSecurityHandlerException(
182                 "Cannot find an appropriate security handler for " + policy.getClass().getName());
183         }
184         Class JavaDoc handlerclass = (Class JavaDoc) found;
185         Class JavaDoc[] argsClasses = {policy.getClass()};
186         Object JavaDoc[] args = {policy};
187         try
188         {
189             Constructor JavaDoc c = handlerclass.getDeclaredConstructor(argsClasses);
190             SecurityHandler handler = (SecurityHandler)c.newInstance(args);
191             return handler;
192         }
193         catch(Exception JavaDoc e)
194         {
195             e.printStackTrace();
196             throw new BadSecurityHandlerException(
197                 "problem while trying to instanciate the security handler "+
198                 handlerclass.getName() + ": " + e.getMessage());
199         }
200     }
201     
202     
203     
204     /**
205      * Retrieve the appropriate SecurityHandler for a the given filter name.
206      * The filter name is an entry of the encryption dictionary of an encrypted document.
207      *
208      * @param filterName The filter name.
209      *
210      * @return The appropriate SecurityHandler if it exists.
211      *
212      * @throws BadSecurityHandlerException If the security handler does not exist.
213      */

214     public SecurityHandler getSecurityHandler(String JavaDoc filterName) throws BadSecurityHandlerException
215     {
216         Object JavaDoc found = handlerNames.get(filterName);
217         if(found == null)
218         {
219             throw new BadSecurityHandlerException("Cannot find an appropriate security handler for " + filterName);
220         }
221         Class JavaDoc handlerclass = (Class JavaDoc) found;
222         Class JavaDoc[] argsClasses = {};
223         Object JavaDoc[] args = {};
224         try
225         {
226             Constructor JavaDoc c = handlerclass.getDeclaredConstructor(argsClasses);
227             SecurityHandler handler = (SecurityHandler)c.newInstance(args);
228             return handler;
229         }
230         catch(Exception JavaDoc e)
231         {
232             e.printStackTrace();
233             throw new BadSecurityHandlerException(
234                 "problem while trying to instanciate the security handler "+
235                 handlerclass.getName() + ": " + e.getMessage());
236         }
237     }
238 }
239
Popular Tags