Package org.pdfsam.console.business.dto.commands

Examples of org.pdfsam.console.business.dto.commands.EncryptParsedCommand


  public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {

    if ((parsedCommand != null) && (parsedCommand instanceof EncryptParsedCommand)) {

      EncryptParsedCommand inputCommand = (EncryptParsedCommand) parsedCommand;
      setPercentageOfWorkDone(0);
      int encType = PdfWriter.STANDARD_ENCRYPTION_40;
      PrefixParser prefixParser;
      try {
        PdfFile[] fileList = arraysConcat(inputCommand.getInputFileList(), getPdfFiles(inputCommand.getInputDirectory()));
        // check if empty
        if (fileList == null || !(fileList.length > 0)) {
          throw new EncryptException(EncryptException.CMD_NO_INPUT_FILE);
        }
        for (int i = 0; i < fileList.length; i++) {
          try {
            // set the encryption type
            if (EncryptParsedCommand.E_AES_128.equals(inputCommand.getEncryptionType())) {
              encType = PdfWriter.ENCRYPTION_AES_128;
            } else if (EncryptParsedCommand.E_RC4_128.equals(inputCommand.getEncryptionType())) {
              encType = PdfWriter.STANDARD_ENCRYPTION_128;
            }

            prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(), fileList[i].getFile().getName());
            File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
            pdfReader = new PdfReader(new RandomAccessFileOrArray(fileList[i].getFile().getAbsolutePath()), fileList[i]
                .getPasswordBytes());
            pdfReader.removeUnusedObjects();
            pdfReader.consolidateNamedDestinations();

            // version
            LOG.debug("Creating a new document.");
            Character pdfVersion = inputCommand.getOutputPdfVersion();
            if (pdfVersion != null) {
              pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile), inputCommand.getOutputPdfVersion().charValue());
            } else {
              pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile), pdfReader.getPdfVersion());
            }

            HashMap meta = pdfReader.getInfo();
            meta.put("Creator", ConsoleServicesFacade.CREATOR);

            setCompressionSettingOnStamper(inputCommand, pdfStamper);

            pdfStamper.setMoreInfo(meta);
            pdfStamper.setEncryption(encType, inputCommand.getUserPwd(), inputCommand.getOwnerPwd(), inputCommand.getPermissions());
            pdfStamper.close();
            pdfReader.close();
            File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName());
            FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite());
            LOG.debug("Encrypted file " + outFile.getCanonicalPath() + " created.");
            setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length);
          } catch (Exception e) {
            LOG.error("Error encrypting file " + fileList[i].getFile().getName(), e);
          }
        }
        LOG.info("Pdf files encrypted in " + inputCommand.getOutputFile().getAbsolutePath() + ".");
        LOG.info("Permissions: " + PdfEncryptor.getPermissionsVerbose(inputCommand.getPermissions()) + ".");
      } catch (Exception e) {
        throw new EncryptException(e);
      } finally {
        setWorkCompleted();
      }
View Full Code Here


*/
public class EncryptCmdValidator extends AbstractCmdValidator {

  public AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler) throws ConsoleException {
   
    EncryptParsedCommand parsedCommandDTO = new EncryptParsedCommand();
   
    if(cmdLineHandler != null){
      //-o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(EncryptParsedCommand.O_ARG);
      if ((oOption.isSet())){
              File outFile = oOption.getFile();
              ValidationUtility.assertValidDirectory(outFile);
              parsedCommandDTO.setOutputFile(outFile);           
          }else{
            throw new ParseException(ParseException.ERR_NO_O);
          }
     
      //-p
          StringParam pOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.P_ARG);
          if(pOption.isSet()){
            parsedCommandDTO.setOutputFilesPrefix(pOption.getValue());
          }
         
          //-apwd
          StringParam apwdOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.APWD_ARG);
          if(apwdOption.isSet()){
            parsedCommandDTO.setOwnerPwd(apwdOption.getValue());
          }
         
          //-upwd
          StringParam upwdOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.UPWD_ARG);
          if(upwdOption.isSet()){
            parsedCommandDTO.setUserPwd(upwdOption.getValue());
          }
         
          //-etype
          StringParam etypeOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.ETYPE_ARG);
          if(etypeOption.isSet()){
            parsedCommandDTO.setEncryptionType(etypeOption.getValue());
          }
         
          //-f - d
      PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(EncryptParsedCommand.F_ARG);
      FileParam dOption = (FileParam) cmdLineHandler.getOption(EncryptParsedCommand.D_ARG);
      if(fOption.isSet() || dOption.isSet()){
        //-f
            if(fOption.isSet()){
          //validate file extensions
              for(Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext();){
                PdfFile currentFile = (PdfFile) fIterator.next();
                ValidationUtility.assertValidPdfExtension(currentFile.getFile().getName());             
              }
              parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
            }
            //-d
        if ((dOption.isSet())){
                File inputDir = dOption.getFile();
                ValidationUtility.assertValidDirectory(inputDir);
                parsedCommandDTO.setInputDirectory(inputDir)
            }
      }else{
        throw new ParseException(ParseException.ERR_NO_F_OR_D);
      }
         
          //-allow
          StringParam allowOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.ALLOW_ARG);
          if(allowOption.isSet()){
            Hashtable permissionsMap = getPermissionsMap(parsedCommandDTO.getEncryptionType());
            int permissions = 0;
            if(!permissionsMap.isEmpty()){
              for(Iterator permIterator = allowOption.getValues().iterator(); permIterator.hasNext();){
                String currentPermission = (String) permIterator.next();
                Object value = permissionsMap.get(currentPermission);
                if(value != null){
                  permissions |= ((Integer)value).intValue();
              }
              }
            }
            permissionsMap = null;
            parsedCommandDTO.setPermissions(permissions);
          }
    }else{
      throw new ConsoleException(ConsoleException.CMD_LINE_HANDLER_NULL);
    }
    return parsedCommandDTO; 
View Full Code Here

TOP

Related Classes of org.pdfsam.console.business.dto.commands.EncryptParsedCommand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.