Coverage Report - org.apache.commons.openpgp.ant.OpenPgpSignerTask
 
Classes in this File Line Coverage Branch Coverage Complexity
OpenPgpSignerTask
0%
0/87
0%
0/36
3,769
 
 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.commons.openpgp.ant;
 18  
 
 19  
 import org.apache.tools.ant.Task;
 20  
 import org.apache.tools.ant.BuildException;
 21  
 import org.apache.tools.ant.DirectoryScanner;
 22  
 import org.apache.tools.ant.util.FileNameMapper;
 23  
 import org.apache.tools.ant.util.GlobPatternMapper;
 24  
 import org.apache.tools.ant.util.FileUtils;
 25  
 import org.apache.tools.ant.types.FileSet;
 26  
 import org.apache.tools.ant.types.Mapper;
 27  
 import org.apache.commons.openpgp.*;
 28  
 import org.bouncycastle.openpgp.PGPException;
 29  
 
 30  
 import java.io.*;
 31  
 import java.util.Collection;
 32  
 import java.util.ArrayList;
 33  
 import java.util.Iterator;
 34  
 
 35  
 /**
 36  
  */
 37  0
 public class OpenPgpSignerTask extends Task {
 38  
     private File secring;
 39  
     private File pubring;
 40  
     private String password;
 41  
     private String keyId;
 42  0
     private Collection<FileSet> tosign = new ArrayList<FileSet>();
 43  
     private File artefact;
 44  0
     private boolean asciiarmor = true;
 45  
     private Mapper mapperElement;
 46  
 
 47  
     /**
 48  
      * set the secret keyring
 49  
      * @param secring secret keyring file
 50  
      */
 51  
     public void setSecring(File secring) {
 52  0
         this.secring = secring;
 53  0
     }
 54  
 
 55  
     /**
 56  
      * set the public keyring
 57  
      * @param pubring public keyring file
 58  
      */
 59  
     public void setPubring(File pubring) {
 60  0
         this.pubring = pubring;
 61  0
     }
 62  
 
 63  
     /**
 64  
      * set the key id
 65  
      * @param keyId
 66  
      */
 67  
     public void setKeyId(String keyId) {
 68  0
         this.keyId = keyId;
 69  0
     }
 70  
 
 71  
     /**
 72  
      * asciiarmor the signature ?
 73  
      * @param asciiarmor ascii armored signature ?
 74  
      */
 75  
     public void setAsciiarmor(boolean asciiarmor) {
 76  0
         this.asciiarmor = asciiarmor;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * set the value of the password
 81  
      * @param password value of the password
 82  
      */
 83  
     public void setPassword(String password) {
 84  0
         this.password = password;
 85  0
     }
 86  
 
 87  
     /**
 88  
      * artefact to be signed
 89  
      * @param artefact artefact to be signed
 90  
      */
 91  
     public void setArtefact(File artefact) {
 92  0
         this.artefact = artefact;
 93  0
     }
 94  
 
 95  
 
 96  
     public void add(FileSet fs) {
 97  0
         tosign.add(fs);
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Define the mapper to map source to destination files.
 102  
      * @return a mapper to be configured.
 103  
      * @exception org.apache.tools.ant.BuildException if more than one mapper is defined.
 104  
      */
 105  
     public Mapper createMapper() throws BuildException {
 106  0
         if (mapperElement != null) {
 107  0
             throw new BuildException("Cannot define more than one mapper",
 108  
                     getLocation());
 109  
         }
 110  0
         mapperElement = new Mapper(getProject());
 111  0
         return mapperElement;
 112  
     }
 113  
 
 114  
     public void execute() {
 115  0
         if (secring == null) {
 116  0
             throw new BuildException("secring attribute compulsory");
 117  
         }
 118  0
         if (pubring == null) {
 119  0
             throw new BuildException("pubring attribute compulsory");
 120  
         }
 121  0
         if (password == null) {
 122  0
             throw new BuildException("password attribute compulsory");
 123  
         }
 124  0
         if (tosign.size() == 0 && artefact == null) {
 125  0
             throw new BuildException("supply the attribute 'artefact' or one nested fileset");
 126  
         }
 127  0
         if (!secring.exists() || !secring.canRead()) {
 128  0
             throw new  BuildException("secret keyring file '" + secring.getAbsolutePath() + "' does not exist or is not readable");
 129  
         }
 130  0
         if (!pubring.exists() || !pubring.canRead()) {
 131  0
             throw new  BuildException("public keyring file '" + pubring.getAbsolutePath() + "' does not exist or is not readable");
 132  
         }
 133  
         FileInputStream secStream;
 134  
         FileInputStream pubStream;
 135  0
         KeyRing keyRing = null;
 136  
         try {
 137  0
             secStream = new FileInputStream(secring);
 138  0
             pubStream = new FileInputStream(pubring);
 139  0
             keyRing = new BouncyCastleKeyRing(secStream,
 140  
                     pubStream, password.toCharArray() );
 141  0
         } catch (IOException ioe) {
 142  0
             throw new BuildException(ioe);
 143  0
         } catch (PGPException pgpe) {
 144  0
             throw new BuildException(pgpe);
 145  0
         }
 146  0
         if (artefact != null) {
 147  0
             dosign(keyRing, artefact);
 148  
         }
 149  0
         if (tosign.size() != 0) {
 150  0
             for (FileSet fileset : tosign) {
 151  0
                 dosign(keyRing, fileset);
 152  
             }
 153  
         }
 154  0
         FileUtils.close(secStream);
 155  0
         FileUtils.close(pubStream);
 156  0
     }
 157  
     private void dosign(KeyRing keyRing, FileSet fs) {
 158  0
         DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 159  0
         String[] artefacts = ds.getIncludedFiles();
 160  0
         for (String artefact : artefacts) {
 161  0
             dosign(keyRing, new File(fs.getDir(getProject()), artefact), fs.getDir(getProject()), artefact);
 162  
         }
 163  0
     }
 164  
     private void dosign(KeyRing keyRing, File oneartefact) {
 165  0
         dosign(keyRing, oneartefact, oneartefact.getParentFile(), oneartefact.getName());
 166  0
     }
 167  
     private void dosign(KeyRing keyRing, File oneartefact, File basedir, String relpath) {
 168  0
         FileInputStream fis = null;
 169  0
         FileOutputStream fos = null;
 170  
         File signature;
 171  
 
 172  
         try {
 173  0
             fis = new FileInputStream(oneartefact);
 174  0
             FileNameMapper mapper = getMapper();
 175  0
             String [] mappedFiles = mapper.mapFileName(relpath);
 176  0
             if (mappedFiles == null || mappedFiles.length != 1) {
 177  0
                 throw new BuildException("mapper returned more or less than one output");
 178  
             }
 179  0
             signature = new File(basedir, mappedFiles[0]);
 180  0
             fos = new FileOutputStream(signature);
 181  0
             OpenPgpSigner signer = new BouncyCastleOpenPgpSigner();
 182  0
             signer.detachedSign(fis, fos, keyId, keyRing, asciiarmor);
 183  0
         } catch (FileNotFoundException fnfe) {
 184  0
             throw new BuildException(fnfe);
 185  0
         } catch (IOException ioe) {
 186  0
             throw new BuildException(ioe);
 187  0
         } catch (OpenPgpException opgpe) {
 188  0
             throw new BuildException(opgpe);
 189  0
         }
 190  0
         FileUtils.close(fos);
 191  0
         FileUtils.close(fis);
 192  
 
 193  0
     }
 194  
     /**
 195  
      * returns the mapper to use based on nested elements or the
 196  
      */
 197  
     private FileNameMapper getMapper() {
 198  0
         FileNameMapper mapper = null;
 199  0
         if (mapperElement != null) {
 200  0
             mapper = mapperElement.getImplementation();
 201  
         } else {
 202  0
             mapper = new GlobPatternMapper();
 203  0
             mapper.setFrom("*");
 204  0
             if (asciiarmor) {
 205  0
                 mapper.setTo("*.asc");
 206  
             } else {
 207  0
                 mapper.setTo("*.sig");
 208  
             }
 209  
         }
 210  0
         return mapper;
 211  
     }
 212  
 
 213  
 }