1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.security.Provider ; 25 import java.security.Security ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 34 public class PDFEncryptionManager { 35 36 37 protected static Log log = LogFactory.getLog(PDFEncryptionManager.class); 38 39 43 public static boolean isJCEAvailable() { 44 try { 45 Class.forName("javax.crypto.Cipher"); 46 return true; 47 } catch (ClassNotFoundException e) { 48 return false; 49 } 50 } 51 52 56 public static boolean checkAvailableAlgorithms() { 57 if (!isJCEAvailable()) { 58 return false; 59 } else { 60 Provider [] providers; 61 providers = Security.getProviders("Cipher.RC4"); 62 if (providers == null) { 63 log.warn("Cipher provider for RC4 not available."); 64 return false; 65 } 66 providers = Security.getProviders("MessageDigest.MD5"); 67 if (providers == null) { 68 log.warn("MessageDigest provider for MD5 not available."); 69 return false; 70 } 71 return true; 72 } 73 } 74 75 76 83 public static void setupPDFEncryption(PDFEncryptionParams params, 84 PDFDocument pdf) { 85 if (pdf == null) { 86 throw new NullPointerException ("PDF document must not be null"); 87 } 88 if (params != null) { 89 if (!checkAvailableAlgorithms()) { 90 if (isJCEAvailable()) { 91 log.warn("PDF encryption has been requested, JCE is " 92 + "available but there's no " 93 + "JCE provider available that provides the " 94 + "necessary algorithms. The PDF won't be " 95 + "encrypted."); 96 } else { 97 log.warn("PDF encryption has been requested but JCE is " 98 + "unavailable! The PDF won't be encrypted."); 99 } 100 } 101 pdf.setEncryption(params); 102 } 103 } 104 105 112 public static PDFEncryption newInstance(int objnum, PDFEncryptionParams params) { 113 try { 114 Class clazz = Class.forName("org.apache.fop.pdf.PDFEncryptionJCE"); 115 Method makeMethod = clazz.getMethod("make", 116 new Class [] {int.class, PDFEncryptionParams.class}); 117 Object obj = makeMethod.invoke(null, 118 new Object [] {new Integer (objnum), params}); 119 return (PDFEncryption)obj; 120 } catch (ClassNotFoundException e) { 121 if (checkAvailableAlgorithms()) { 122 log.warn("JCE and algorithms available, but the " 123 + "implementation class unavailable. Please do a full " 124 + "rebuild."); 125 } 126 return null; 127 } catch (NoSuchMethodException e) { 128 log.error(e); 129 return null; 130 } catch (IllegalAccessException e) { 131 log.error(e); 132 return null; 133 } catch (InvocationTargetException e) { 134 log.error(e); 135 return null; 136 } 137 } 138 139 } 140 | Popular Tags |