You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFEncryptionManager.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.security.Provider;
  21. import java.security.Security;
  22. import org.apache.avalon.framework.logger.Logger;
  23. import org.apache.fop.apps.FOUserAgent;
  24. /**
  25. * This class acts as a factory for PDF encryption support. It enables the
  26. * feature to be optional to FOP depending on the availability of JCE.
  27. */
  28. public class PDFEncryptionManager {
  29. /**
  30. * Indicates whether JCE is available.
  31. * @return boolean true if JCE is present
  32. */
  33. public static boolean isJCEAvailable() {
  34. try {
  35. Class clazz = Class.forName("javax.crypto.Cipher");
  36. return true;
  37. } catch (ClassNotFoundException e) {
  38. return false;
  39. }
  40. }
  41. /**
  42. * Checks whether the necessary algorithms are available.
  43. * @return boolean True if all necessary algorithms are present
  44. */
  45. public static boolean checkAvailableAlgorithms() {
  46. if (!isJCEAvailable()) {
  47. return false;
  48. } else {
  49. Provider[] providers;
  50. providers = Security.getProviders("Cipher.RC4");
  51. if (providers == null) {
  52. return false;
  53. }
  54. providers = Security.getProviders("MessageDigest.MD5");
  55. if (providers == null) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. }
  61. /**
  62. * Sets up PDF encryption if PDF encryption is requested by registering
  63. * a <code>PDFEncryptionParams</code> object with the user agent and if
  64. * the necessary cryptographic support is available.
  65. * @param userAgent the user agent
  66. * @param pdf the PDF document to setup encryption for
  67. * @param log the logger to send warnings to
  68. */
  69. public static void setupPDFEncryption(FOUserAgent userAgent,
  70. PDFDocument pdf,
  71. Logger log) {
  72. if (userAgent == null) {
  73. throw new NullPointerException("User agent must not be null");
  74. }
  75. if (pdf == null) {
  76. throw new NullPointerException("PDF document must not be null");
  77. }
  78. if (userAgent.getPDFEncryptionParams() != null) {
  79. if (!checkAvailableAlgorithms()) {
  80. if (isJCEAvailable()) {
  81. log.warn("PDF encryption has been requested, JCE is "
  82. + "available but there's no "
  83. + "JCE provider available that provides the "
  84. + "necessary algorithms. The PDF won't be "
  85. + "encrypted.");
  86. } else {
  87. log.warn("PDF encryption has been requested but JCE is "
  88. + "unavailable! The PDF won't be encrypted.");
  89. }
  90. }
  91. pdf.setEncryption(userAgent.getPDFEncryptionParams());
  92. }
  93. }
  94. /**
  95. * Creates a new PDFEncryption instance if PDF encryption is available.
  96. * @param objnum PDF object number
  97. * @param params PDF encryption parameters
  98. * @return PDFEncryption the newly created instance, null if PDF encryption
  99. * is unavailable.
  100. */
  101. public static PDFEncryption newInstance(int objnum, PDFEncryptionParams params) {
  102. try {
  103. Class clazz = Class.forName("org.apache.fop.pdf.PDFEncryptionJCE");
  104. Method makeMethod = clazz.getMethod("make",
  105. new Class[] {int.class, PDFEncryptionParams.class});
  106. Object obj = makeMethod.invoke(null,
  107. new Object[] {new Integer(objnum), params});
  108. return (PDFEncryption)obj;
  109. } catch (ClassNotFoundException e) {
  110. if (checkAvailableAlgorithms()) {
  111. System.out.println("JCE and algorithms available, but the "
  112. + "implementation class unavailable. Please do a full "
  113. + "rebuild.");
  114. }
  115. return null;
  116. } catch (NoSuchMethodException e) {
  117. e.printStackTrace();
  118. return null;
  119. } catch (IllegalAccessException e) {
  120. e.printStackTrace();
  121. return null;
  122. } catch (InvocationTargetException e) {
  123. e.printStackTrace();
  124. return null;
  125. }
  126. }
  127. }