1 31 32 package org.pdfbox.pdmodel.encryption; 33 34 import java.lang.reflect.Constructor ; 35 import java.security.Security ; 36 import java.util.Hashtable ; 37 38 import org.bouncycastle.jce.provider.BouncyCastleProvider; 39 40 50 public class SecurityHandlersManager 51 { 52 53 56 private static SecurityHandlersManager instance; 57 58 64 private Hashtable handlerNames = null; 65 66 71 private Hashtable handlerPolicyClasses = null; 72 73 76 private SecurityHandlersManager() 77 { 78 handlerNames = new Hashtable (); 79 handlerPolicyClasses = new Hashtable (); 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 e) 92 { 93 System.err.println("SecurityHandlersManager strange error with builtin handlers: " + e.getMessage()); 94 System.exit(1); 95 } 96 } 97 98 111 public void registerHandler(String filterName, Class securityHandlerClass, Class 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 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 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 175 public SecurityHandler getSecurityHandler(ProtectionPolicy policy) throws BadSecurityHandlerException 176 { 177 178 Object 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 handlerclass = (Class ) found; 185 Class [] argsClasses = {policy.getClass()}; 186 Object [] args = {policy}; 187 try 188 { 189 Constructor c = handlerclass.getDeclaredConstructor(argsClasses); 190 SecurityHandler handler = (SecurityHandler)c.newInstance(args); 191 return handler; 192 } 193 catch(Exception 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 214 public SecurityHandler getSecurityHandler(String filterName) throws BadSecurityHandlerException 215 { 216 Object found = handlerNames.get(filterName); 217 if(found == null) 218 { 219 throw new BadSecurityHandlerException("Cannot find an appropriate security handler for " + filterName); 220 } 221 Class handlerclass = (Class ) found; 222 Class [] argsClasses = {}; 223 Object [] args = {}; 224 try 225 { 226 Constructor c = handlerclass.getDeclaredConstructor(argsClasses); 227 SecurityHandler handler = (SecurityHandler)c.newInstance(args); 228 return handler; 229 } 230 catch(Exception 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 |