Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.encryption » [javadoc | source]

    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   package org.apache.pdfbox.pdmodel.encryption;
   18   
   19   import org.apache.pdfbox.cos.COSDictionary;
   20   import org.apache.pdfbox.cos.COSName;
   21   import java.io.IOException;
   22   
   23   import java.lang.reflect.Constructor;
   24   import java.lang.reflect.InvocationTargetException;
   25   
   26   import java.util.Collections;
   27   import java.util.HashMap;
   28   import java.util.Map;
   29   
   30   /**
   31    * This class will handle loading of the different security handlers.
   32    *
   33    * See PDF Reference 1.4 section "3.5 Encryption"
   34    *
   35    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   36    * @version $Revision: 1.7 $
   37    * @deprecated Made deprecated by the new security layer of PDFBox. Use SecurityHandlers instead.
   38    */
   39   
   40   public class PDEncryptionManager
   41   {
   42       private static Map handlerMap = Collections.synchronizedMap( new HashMap() );
   43   
   44       static
   45       {
   46           registerSecurityHandler( PDStandardEncryption.FILTER_NAME, PDStandardEncryption.class );
   47       }
   48   
   49       private PDEncryptionManager()
   50       {
   51       }
   52   
   53       /**
   54        * This will allow the user to register new security handlers when unencrypting a
   55        * document.
   56        *
   57        * @param filterName As described in the encryption dictionary.
   58        * @param handlerClass A subclass of PDEncryptionDictionary that has a constructor that takes
   59        *        a COSDictionary.
   60        */
   61       public static void registerSecurityHandler( String filterName, Class handlerClass )
   62       {
   63           handlerMap.put( COSName.getPDFName( filterName ), handlerClass );
   64       }
   65   
   66       /**
   67        * This will get the correct security handler for the encryption dictionary.
   68        *
   69        * @param dictionary The encryption dictionary.
   70        *
   71        * @return An implementation of PDEncryptionDictionary(PDStandardEncryption for most cases).
   72        *
   73        * @throws IOException If a security handler could not be found.
   74        */
   75       public static PDEncryptionDictionary getEncryptionDictionary( COSDictionary dictionary )
   76           throws IOException
   77       {
   78           Object retval = null;
   79           if( dictionary != null )
   80           {
   81               COSName filter = (COSName)dictionary.getDictionaryObject( COSName.FILTER );
   82               Class handlerClass = (Class)handlerMap.get( filter );
   83               if( handlerClass == null )
   84               {
   85                   throw new IOException( "No handler for security handler '" + filter.getName() + "'" );
   86               }
   87               else
   88               {
   89                   try
   90                   {
   91                       Constructor ctor = handlerClass.getConstructor( new Class[] {
   92                           COSDictionary.class
   93                       } );
   94                       retval = ctor.newInstance( new Object[] {
   95                           dictionary
   96                       } );
   97                   }
   98                   catch( NoSuchMethodException e )
   99                   {
  100                       throw new IOException( e.getMessage() );
  101                   }
  102                   catch( InstantiationException e )
  103                   {
  104                       throw new IOException( e.getMessage() );
  105                   }
  106                   catch( IllegalAccessException e )
  107                   {
  108                       throw new IOException( e.getMessage() );
  109                   }
  110                   catch( InvocationTargetException e )
  111                   {
  112                       throw new IOException( e.getMessage() );
  113                   }
  114               }
  115           }
  116           return (PDEncryptionDictionary)retval;
  117   
  118       }
  119   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.encryption » [javadoc | source]