1 31 package org.pdfbox.pdmodel.encryption; 32 33 import org.pdfbox.cos.COSDictionary; 34 import org.pdfbox.cos.COSName; 35 import java.io.IOException ; 36 37 import java.lang.reflect.Constructor ; 38 import java.lang.reflect.InvocationTargetException ; 39 40 import java.util.Collections ; 41 import java.util.HashMap ; 42 import java.util.Map ; 43 44 53 54 public class PDEncryptionManager 55 { 56 private static Map handlerMap = Collections.synchronizedMap( new HashMap () ); 57 58 static 59 { 60 registerSecurityHandler( PDStandardEncryption.FILTER_NAME, PDStandardEncryption.class ); 61 } 62 63 private PDEncryptionManager() 64 { 65 } 66 67 75 public static void registerSecurityHandler( String filterName, Class handlerClass ) 76 { 77 handlerMap.put( COSName.getPDFName( filterName ), handlerClass ); 78 } 79 80 89 public static PDEncryptionDictionary getEncryptionDictionary( COSDictionary dictionary ) 90 throws IOException 91 { 92 Object retval = null; 93 if( dictionary != null ) 94 { 95 COSName filter = (COSName)dictionary.getDictionaryObject( COSName.FILTER ); 96 Class handlerClass = (Class )handlerMap.get( filter ); 97 if( handlerClass == null ) 98 { 99 throw new IOException ( "No handler for security handler '" + filter.getName() + "'" ); 100 } 101 else 102 { 103 try 104 { 105 Constructor ctor = handlerClass.getConstructor( new Class [] { 106 COSDictionary.class 107 } ); 108 retval = ctor.newInstance( new Object [] { 109 dictionary 110 } ); 111 } 112 catch( NoSuchMethodException e ) 113 { 114 throw new IOException ( e.getMessage() ); 115 } 116 catch( InstantiationException e ) 117 { 118 throw new IOException ( e.getMessage() ); 119 } 120 catch( IllegalAccessException e ) 121 { 122 throw new IOException ( e.getMessage() ); 123 } 124 catch( InvocationTargetException e ) 125 { 126 throw new IOException ( e.getMessage() ); 127 } 128 } 129 } 130 return (PDEncryptionDictionary)retval; 131 132 } 133 } | Popular Tags |