KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFEncryptionManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: PDFEncryptionManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.security.Provider JavaDoc;
25 import java.security.Security JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * This class acts as a factory for PDF encryption support. It enables the
32  * feature to be optional to FOP depending on the availability of JCE.
33  */

34 public class PDFEncryptionManager {
35
36     /** logging instance */
37     protected static Log log = LogFactory.getLog(PDFEncryptionManager.class);
38
39     /**
40      * Indicates whether JCE is available.
41      * @return boolean true if JCE is present
42      */

43     public static boolean isJCEAvailable() {
44         try {
45             /*Class clazz =*/ Class.forName("javax.crypto.Cipher");
46             return true;
47         } catch (ClassNotFoundException JavaDoc e) {
48             return false;
49         }
50     }
51     
52     /**
53      * Checks whether the necessary algorithms are available.
54      * @return boolean True if all necessary algorithms are present
55      */

56     public static boolean checkAvailableAlgorithms() {
57         if (!isJCEAvailable()) {
58             return false;
59         } else {
60             Provider JavaDoc[] 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     /**
77      * Sets up PDF encryption if PDF encryption is requested by registering
78      * a <code>PDFEncryptionParams</code> object with the user agent and if
79      * the necessary cryptographic support is available.
80      * @param params the PDF encryption params or null to disable encryption
81      * @param pdf the PDF document to setup encryption for
82      */

83     public static void setupPDFEncryption(PDFEncryptionParams params,
84                                           PDFDocument pdf) {
85         if (pdf == null) {
86             throw new NullPointerException JavaDoc("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     /**
106      * Creates a new PDFEncryption instance if PDF encryption is available.
107      * @param objnum PDF object number
108      * @param params PDF encryption parameters
109      * @return PDFEncryption the newly created instance, null if PDF encryption
110      * is unavailable.
111      */

112     public static PDFEncryption newInstance(int objnum, PDFEncryptionParams params) {
113         try {
114             Class JavaDoc clazz = Class.forName("org.apache.fop.pdf.PDFEncryptionJCE");
115             Method JavaDoc makeMethod = clazz.getMethod("make",
116                         new Class JavaDoc[] {int.class, PDFEncryptionParams.class});
117             Object JavaDoc obj = makeMethod.invoke(null,
118                         new Object JavaDoc[] {new Integer JavaDoc(objnum), params});
119             return (PDFEncryption)obj;
120         } catch (ClassNotFoundException JavaDoc 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 JavaDoc e) {
128             log.error(e);
129             return null;
130         } catch (IllegalAccessException JavaDoc e) {
131             log.error(e);
132             return null;
133         } catch (InvocationTargetException JavaDoc e) {
134             log.error(e);
135             return null;
136         }
137     }
138     
139 }
140
Popular Tags