[jboss-cvs] jboss-seam/src/pdf/org/jboss/seam/pdf/ui ...

Norman Richards norman.richards at jboss.com
Sun Jan 28 22:01:33 EST 2007


  User: nrichards
  Date: 07/01/28 22:01:33

  Modified:    src/pdf/org/jboss/seam/pdf/ui   UIDocument.java
                        UISignature.java
  Log:
  more signature support
  
  Revision  Changes    Path
  1.13      +16 -1     jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIDocument.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UIDocument.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UIDocument.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -b -r1.12 -r1.13
  --- UIDocument.java	25 Jan 2007 06:17:23 -0000	1.12
  +++ UIDocument.java	29 Jan 2007 03:01:33 -0000	1.13
  @@ -37,6 +37,8 @@
       String margins;
       Boolean marginMirroring;
    
  +    UISignature signatureField;
  +    
       public void setType(String type) {
           this.type = type;
       }
  @@ -77,6 +79,7 @@
           this.orientation = orientation;
       }
       
  +    
       public Object getITextObject() {
           return document;
       }
  @@ -166,6 +169,10 @@
           }
       }
   
  +    public void addSignature(UISignature signatureField) {
  +        this.signatureField = signatureField;
  +    }
  +
       @Override
       public void encodeBegin(FacesContext context) 
           throws IOException
  @@ -236,10 +243,16 @@
       {
           document.close();
   
  +        byte[] bytes = stream.toByteArray();
  +
  +        if (signatureField != null) {
  +            bytes = signatureField.sign(bytes);
  +        }
  +        
           DocumentStore.instance().saveData(id,
                                             baseName,
                                             docType,
  -                                          stream.toByteArray());        
  +                                          bytes);        
   
           ResponseWriter response = context.getResponseWriter();
           response.endElement("body");
  @@ -250,6 +263,8 @@
           Manager.instance().beforeRedirect();
       }
   
  +       
  +
       public DocWriter getWriter() {
           return writer;
       }    
  
  
  
  1.2       +76 -0     jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UISignature.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UISignature.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/pdf/org/jboss/seam/pdf/ui/UISignature.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- UISignature.java	25 Jan 2007 06:17:23 -0000	1.1
  +++ UISignature.java	29 Jan 2007 03:01:33 -0000	1.2
  @@ -1,20 +1,38 @@
   package org.jboss.seam.pdf.ui;
   
  +import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.io.InputStream;
  +import java.security.KeyStore;
  +import java.security.PrivateKey;
  +import java.security.cert.Certificate;
   
   import javax.faces.context.FacesContext;
   
   import org.jboss.seam.pdf.ITextUtils;
  +import org.jboss.seam.util.Resources;
   
   import com.lowagie.text.DocWriter;
   import com.lowagie.text.pdf.PdfAcroForm;
  +import com.lowagie.text.pdf.PdfReader;
  +import com.lowagie.text.pdf.PdfSignatureAppearance;
  +import com.lowagie.text.pdf.PdfStamper;
   import com.lowagie.text.pdf.PdfWriter;
   
   public class UISignature 
       extends ITextComponent 
   {
  +    // signature box
       String field;
       String size;
  +    String reason;
  +    String location;
  +    
  +    // keystore
  +    String keyStore         = null;
  +    String keyStorePassword = null;
  +    String keyPassword      = null;
  +    String keyAlias         = null;   
       
       public void setField(String field) {
           this.field = field;
  @@ -23,6 +41,28 @@
       public void setSize(String size) {
           this.size = size;
       }
  +    public void setReason(String reason) {
  +        this.reason = reason;
  +    }
  +    public void setLocation(String location) {
  +        this.location = location;
  +    }
  +       
  +    public void setKeyAlias(String keyAlias) {
  +        this.keyAlias = keyAlias;
  +    }
  +
  +    public void setKeyPassword(String keyPassword) {
  +        this.keyPassword = keyPassword;
  +    }
  +
  +    public void setKeyStore(String keyStore) {
  +        this.keyStore = keyStore;
  +    }
  +
  +    public void setKeyStorePassword(String keyStorePassword) {
  +        this.keyStorePassword = keyStorePassword;
  +    }
       
       @Override
       public void createITextObject(FacesContext context) {}
  @@ -64,6 +104,9 @@
           }
           form.addSignature(field, rect[0], rect[1], rect[2], rect[3]);
           
  +        UIDocument doc = (UIDocument) findITextParent(this, UIDocument.class);
  +        doc.addSignature(this);
  +        
           super.encodeEnd(context);
       }
   
  @@ -79,4 +122,37 @@
           return null;
       }    
   
  +    public byte[] sign(byte[] originalBytes) {         
  +        try {                        
  +            InputStream is = Resources.getResourceAsStream(keyStore);
  +            
  +            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
  +            ks.load(is, keyStorePassword.toCharArray());
  +
  +            PrivateKey key = (PrivateKey) ks.getKey(keyAlias, keyPassword.toCharArray());
  +            Certificate[] chain =  ks.getCertificateChain(keyAlias); 
  +
  +            PdfReader reader = new PdfReader(originalBytes); 
  +            ByteArrayOutputStream os = new ByteArrayOutputStream();                 
  +
  +            PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); 
  +            PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); 
  +            appearance.setCrypto(key, chain, null, 
  +                                 PdfSignatureAppearance.SELF_SIGNED); 
  +            
  +            appearance.setReason(reason); 
  +            appearance.setLocation(location); 
  +            
  +            appearance.setVisibleSignature(field); 
  +            stamper.close();      
  +            
  +            return os.toByteArray();
  +        } catch (Exception e) {
  +            throw new RuntimeException(e);
  +        }
  +
  +    }
  +
  +
  +    
   }
  
  
  



More information about the jboss-cvs-commits mailing list