Example usage for com.lowagie.text ExceptionConverter ExceptionConverter

List of usage examples for com.lowagie.text ExceptionConverter ExceptionConverter

Introduction

In this page you can find the example usage for com.lowagie.text ExceptionConverter ExceptionConverter.

Prototype

public ExceptionConverter(Exception ex) 

Source Link

Document

Construct a RuntimeException based on another Exception

Usage

From source file:es.uji.security.crypto.pdf.PdfPKCS7TSA.java

License:Mozilla Public License

/**                                                                             
 * Gets the bytes for the PKCS#1 object.                                        
 * @return a byte array                                                         
 *//* w ww.  jav a 2  s. c o m*/
public byte[] getEncodedPKCS1() {
    try {
        if (externalDigest != null)
            digest = externalDigest;
        else
            digest = sig.sign();
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        ASN1OutputStream dout = new ASN1OutputStream(bOut);
        dout.writeObject(new DEROctetString(digest));
        dout.close();

        return bOut.toByteArray();
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:es.uji.security.crypto.pdf.PdfPKCS7TSA.java

License:Mozilla Public License

/**                                                                             
 * Sets the digest/signature to an external calculated value.                   
 * @param digest the digest. This is the actual signature                       
 * @param RSAdata the extra data that goes into the data tag in PKCS#7          
 * @param digestEncryptionAlgorithm the encryption algorithm. It may must be <CODE>null</CODE> if the <CODE>digest</CODE>
 * is also <CODE>null</CODE>. If the <CODE>digest</CODE> is not <CODE>null</CODE>                                        
 * then it may be "RSA" or "DSA"                                                                                         
 *//*from   w  ww .  j a  v  a  2s .c o m*/
public void setExternalDigest(byte digest[], byte RSAdata[], String digestEncryptionAlgorithm) {
    externalDigest = digest;
    externalRSAdata = RSAdata;
    if (digestEncryptionAlgorithm != null) {
        if (digestEncryptionAlgorithm.equals("RSA")) {
            this.digestEncryptionAlgorithm = ID_RSA;
        } else if (digestEncryptionAlgorithm.equals("DSA")) {
            this.digestEncryptionAlgorithm = ID_DSA;
        } else
            throw new ExceptionConverter(
                    new NoSuchAlgorithmException("Unknown Key Algorithm " + digestEncryptionAlgorithm));
    }
}

From source file:es.uji.security.crypto.pdf.PdfPKCS7TSA.java

License:Mozilla Public License

/**
 * Gets the bytes for the PKCS7SignedData object. Optionally the authenticatedAttributes
 * in the signerInfo can also be set, OR a time-stamp-authority client                  
 * may be provided.                                                                     
 * @param secondDigest the digest in the authenticatedAttributes                        
 * @param signingTime the signing time in the authenticatedAttributes                   
 * @param tsaUrl TSAClient - null or an optional time stamp authority client
 * @return byte[] the bytes for the PKCS7SignedData object                              
 * @since   2.1.6                                                                       
 *//* ww w .  j a va2 s . c  om*/
public byte[] getEncodedPKCS7(byte secondDigest[], Calendar signingTime, String tsaUrl, byte[] ocsp) {
    try {
        if (externalDigest != null) {
            digest = externalDigest;
            if (RSAdata != null)
                RSAdata = externalRSAdata;
        } else if (externalRSAdata != null && RSAdata != null) {
            RSAdata = externalRSAdata;
            sig.update(RSAdata);
            digest = sig.sign();
        } else {
            if (RSAdata != null) {
                RSAdata = messageDigest.digest();
                sig.update(RSAdata);
            }
            digest = sig.sign();
        }

        // Create the set of Hash algorithms                                                                
        ASN1EncodableVector digestAlgorithms = new ASN1EncodableVector();
        for (Iterator it = digestalgos.iterator(); it.hasNext();) {
            ASN1EncodableVector algos = new ASN1EncodableVector();
            algos.add(new DERObjectIdentifier((String) it.next()));
            algos.add(DERNull.INSTANCE);
            digestAlgorithms.add(new DERSequence(algos));
        }

        // Create the contentInfo.                                                                          
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(ID_PKCS7_DATA));
        if (RSAdata != null)
            v.add(new DERTaggedObject(0, new DEROctetString(RSAdata)));
        DERSequence contentinfo = new DERSequence(v);

        // Get all the certificates                                                                         
        //                                                                                                  
        v = new ASN1EncodableVector();
        for (Iterator i = certs.iterator(); i.hasNext();) {
            ASN1InputStream tempstream = new ASN1InputStream(
                    new ByteArrayInputStream(((X509Certificate) i.next()).getEncoded()));
            v.add(tempstream.readObject());
        }

        DERSet dercertificates = new DERSet(v);

        // Create signerinfo structure.                                                                                    
        //                                                                                                                 
        ASN1EncodableVector signerinfo = new ASN1EncodableVector();

        // Add the signerInfo version                                                                                      
        //                                                                                                                 
        signerinfo.add(new DERInteger(signerversion));

        v = new ASN1EncodableVector();
        v.add(getIssuer(signCert.getTBSCertificate()));
        v.add(new DERInteger(signCert.getSerialNumber()));
        signerinfo.add(new DERSequence(v));

        // Add the digestAlgorithm                                                                                         
        v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(digestAlgorithm));
        v.add(new DERNull());
        signerinfo.add(new DERSequence(v));

        // add the authenticated attribute if present                                                                      
        if (secondDigest != null && signingTime != null) {
            signerinfo.add(new DERTaggedObject(false, 0,
                    getAuthenticatedAttributeSet(secondDigest, signingTime, ocsp)));
        }
        // Add the digestEncryptionAlgorithm                                                                               
        v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(digestEncryptionAlgorithm));
        v.add(new DERNull());
        signerinfo.add(new DERSequence(v));

        // Add the digest                                                                                                  
        signerinfo.add(new DEROctetString(digest));

        // When requested, go get and add the timestamp. May throw an exception.                                           
        // Added by Martin Brunecky, 07/12/2007 folowing Aiken Sam, 2006-11-15                                             
        // Sam found Adobe expects time-stamped SHA1-1 of the encrypted digest                                             
        if (tsaUrl != null) {
            byte[] tsImprint = MessageDigest.getInstance("SHA-1").digest(digest);

            TSResponse response = TimeStampFactory.getTimeStampResponse(tsaUrl, tsImprint, false);
            byte[] tsToken = response.getEncodedToken();

            //Strip the status code out of the response, the adobe validator requieres it. 
            //TODO: Research about this.
            byte[] status = { 0x30, (byte) 0x82, 0x03, (byte) 0xA7, 0x30, 0x03, 0x02, 0x01, 0x00 };
            byte[] modTsToken = new byte[tsToken.length - status.length];
            System.arraycopy(tsToken, status.length, modTsToken, 0, tsToken.length - status.length);

            if (modTsToken != null) {
                ASN1EncodableVector unauthAttributes = buildUnauthenticatedAttributes(modTsToken);
                if (unauthAttributes != null) {
                    signerinfo.add(new DERTaggedObject(false, 1, new DERSet(unauthAttributes)));
                }
            }
        }

        // Finally build the body out of all the components above                                                          
        ASN1EncodableVector body = new ASN1EncodableVector();
        body.add(new DERInteger(version));
        body.add(new DERSet(digestAlgorithms));
        body.add(contentinfo);
        body.add(new DERTaggedObject(false, 0, dercertificates));

        if (!crls.isEmpty()) {
            v = new ASN1EncodableVector();
            for (Iterator i = crls.iterator(); i.hasNext();) {
                ASN1InputStream t = new ASN1InputStream(
                        new ByteArrayInputStream(((X509CRL) i.next()).getEncoded()));
                v.add(t.readObject());
            }
            DERSet dercrls = new DERSet(v);
            body.add(new DERTaggedObject(false, 1, dercrls));
        }

        // Only allow one signerInfo                                                                                       
        body.add(new DERSet(new DERSequence(signerinfo)));

        // Now we have the body, wrap it in it's PKCS7Signed shell                                                         
        // and return it                                                                                                   
        //                                                                                                                 
        ASN1EncodableVector whole = new ASN1EncodableVector();
        whole.add(new DERObjectIdentifier(ID_PKCS7_SIGNED_DATA));
        whole.add(new DERTaggedObject(0, new DERSequence(body)));

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        ASN1OutputStream dout = new ASN1OutputStream(bOut);
        dout.writeObject(new DERSequence(whole));
        dout.close();

        return bOut.toByteArray();
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:es.uji.security.crypto.pdf.PdfPKCS7TSA.java

License:Mozilla Public License

/**
 * When using authenticatedAttributes the authentication process is different.
 * The document digest is generated and put inside the attribute. The signing is done over the DER encoded
 * authenticatedAttributes. This method provides that encoding and the parameters must be                 
 * exactly the same as in {@link #getEncodedPKCS7(byte[],Calendar)}.                                      
 * <p>                                                                                                    
 * A simple example:                                                                                      
 * <p>                                                                                                    
 * <pre>                                                                                                  
 * Calendar cal = Calendar.getInstance();                                                                 
 * PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false);                                    
 * MessageDigest messageDigest = MessageDigest.getInstance("SHA1");                                       
 * byte buf[] = new byte[8192];                                                                           
 * int n;                                                                                                 
 * InputStream inp = sap.getRangeStream();                                                                
 * while ((n = inp.read(buf)) &gt; 0) {                                                                   
 *    messageDigest.update(buf, 0, n);                                                                    
 * }                                                                                                      
 * byte hash[] = messageDigest.digest();                                                                  
 * byte sh[] = pk7.getAuthenticatedAttributeBytes(hash, cal);                                             
 * pk7.update(sh, 0, sh.length);                                                                          
 * byte sg[] = pk7.getEncodedPKCS7(hash, cal);                                                            
 * </pre>                                                                                                 
 * @param secondDigest the content digest                                                                 
 * @param signingTime the signing time                                                                    
 * @return the byte array representation of the authenticatedAttributes ready to be signed                
 *///from w  ww  . j  ava2 s  .  com
public byte[] getAuthenticatedAttributeBytes(byte secondDigest[], Calendar signingTime, byte[] ocsp) {
    try {
        return getAuthenticatedAttributeSet(secondDigest, signingTime, ocsp).getEncoded(ASN1Encodable.DER);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:es.uji.security.crypto.pdf.PdfPKCS7TSA.java

License:Mozilla Public License

private DERSet getAuthenticatedAttributeSet(byte secondDigest[], Calendar signingTime, byte[] ocsp) {
    try {/*ww  w.ja  v  a  2 s .com*/
        ASN1EncodableVector attribute = new ASN1EncodableVector();
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(ID_CONTENT_TYPE));
        v.add(new DERSet(new DERObjectIdentifier(ID_PKCS7_DATA)));
        attribute.add(new DERSequence(v));
        v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(ID_SIGNING_TIME));
        v.add(new DERSet(new DERUTCTime(signingTime.getTime())));
        attribute.add(new DERSequence(v));
        v = new ASN1EncodableVector();
        v.add(new DERObjectIdentifier(ID_MESSAGE_DIGEST));
        v.add(new DERSet(new DEROctetString(secondDigest)));
        attribute.add(new DERSequence(v));
        if (ocsp != null) {
            v = new ASN1EncodableVector();
            v.add(new DERObjectIdentifier(ID_ADBE_REVOCATION));
            DEROctetString doctet = new DEROctetString(ocsp);
            ASN1EncodableVector vo1 = new ASN1EncodableVector();
            ASN1EncodableVector v2 = new ASN1EncodableVector();
            v2.add(OCSPObjectIdentifiers.id_pkix_ocsp_basic);
            v2.add(doctet);
            DEREnumerated den = new DEREnumerated(0);
            ASN1EncodableVector v3 = new ASN1EncodableVector();
            v3.add(den);
            v3.add(new DERTaggedObject(true, 0, new DERSequence(v2)));
            vo1.add(new DERSequence(v3));
            v.add(new DERSet(new DERSequence(new DERTaggedObject(true, 1, new DERSequence(vo1)))));
            attribute.add(new DERSequence(v));
        } else if (!crls.isEmpty()) {
            v = new ASN1EncodableVector();
            v.add(new DERObjectIdentifier(ID_ADBE_REVOCATION));
            ASN1EncodableVector v2 = new ASN1EncodableVector();
            for (Iterator i = crls.iterator(); i.hasNext();) {
                ASN1InputStream t = new ASN1InputStream(
                        new ByteArrayInputStream(((X509CRL) i.next()).getEncoded()));
                v2.add(t.readObject());
            }
            v.add(new DERSet(new DERSequence(new DERTaggedObject(true, 0, new DERSequence(v2)))));
            attribute.add(new DERSequence(v));
        }
        return new DERSet(attribute);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java

License:GNU General Public License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)//w  w  w .  jav  a 2 s  .  c o m
 */
public void onOpenDocument(PdfWriter writer, Document document) {
    try {
        headerImage = Image.getInstance(getLogoUrl());
        table = new PdfPTable(new float[] { 1f, 2f });
        Phrase p = new Phrase();
        Chunk ck = new Chunk(cr.getTitle(), new Font(Font.HELVETICA, 16, Font.BOLD));
        p.add(ck);
        p.add(Chunk.NEWLINE);
        ck = new Chunk(ConversationExporter.formatName(account), new Font(Font.HELVETICA, 12, Font.BOLDITALIC));
        p.add(ck);
        p.add(Chunk.NEWLINE);
        ck = new Chunk(cm.length + " messages", new Font(Font.HELVETICA, 10));
        p.add(ck);
        table.getDefaultCell().setBorder(0);
        table.addCell(new Phrase(new Chunk(headerImage, 0, 0)));
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(p);
        // initialization of the template
        tpl = writer.getDirectContent().createTemplate(100, 100);
        tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));
        // initialization of the font
        helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:jdbreport.model.io.pdf.itext2.ReportFontMapper.java

License:Apache License

public com.lowagie.text.Font styleToPdf(CellStyle style) {
    try {/* ww w .  j a v  a  2  s  . com*/
        java.awt.Font font = new java.awt.Font(style.getFamily(), style.getStyle(), style.getSize());
        BaseFont baseFont = awtToPdf(font);
        if (baseFont != null) {
            return new com.lowagie.text.Font(baseFont, 0.0f + (float) style.getSize(), style.getStyle(),
                    style.getForegroundColor());
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }

    return null;
}

From source file:jdbreport.model.io.pdf.itext2.ReportFontMapper.java

License:Apache License

public BaseFont awtToPdf(String fontName) {
    try {//from   w w  w .ja v a  2 s. co  m
        BaseFontParameters p = getBaseFontParameters(fontName);
        if (p != null) {
            return BaseFont.createFont(p.fontName, BaseFont.IDENTITY_H, p.embedded, p.cached, p.ttfAfm, p.pfb);
        }
    } catch (DocumentException | IOException e) {
        throw new ExceptionConverter(e);
    }
    return null;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static void setEncabezado(PdfWriter writer, Document document, String texto) {
    if (writer.getPageNumber() > 1) {
        try {//from w w w  . j a  v  a  2  s  .c  o  m
            PdfPTable encabezado = new PdfPTable(1);
            encabezado.setTotalWidth(document.right() - document.left() - 120);
            encabezado.addCell(Addons.setCeldaPDF(texto, Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_LEFT, 0));
            encabezado.writeSelectedRows(0, -1, 60, document.top() + 25, writer.getDirectContent());

            PdfContentByte cb = writer.getDirectContent();
            cb.setLineWidth(2);
            cb.moveTo(60, document.top() + 10);
            cb.lineTo(document.right() - document.left() - 58, document.top() + 10);
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static void setPie(PdfWriter writer, Document document, String rep_pie) {
    try {//from   ww w  .  j  a  v a2s.  com
        PdfContentByte cb = writer.getDirectContent();
        /*cb.setLineWidth(2);
        cb.moveTo(60, document.bottomMargin()-5);
        cb.lineTo(document.right() - document.left()-70, document.bottomMargin()-5);
        */
        PdfPTable pie = new PdfPTable(1);
        pie.setTotalWidth(document.right() - document.left() - 120);
        pie.addCell(Addons.setCeldaPDF(rep_pie, Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_CENTER, 0));
        pie.addCell(Addons.setCeldaPDF("Pg " + String.valueOf(writer.getPageNumber()), Font.HELVETICA, 9,
                Font.BOLD, Element.ALIGN_RIGHT, 0));
        pie.addCell(Addons.setCeldaPDF(
                "Reporte diseado por: Jorge Mueses Cevallos.      Mvil: 095204832     mail:jorge_mueses@yahoo.com",
                Font.HELVETICA, 5, Font.BOLD, Element.ALIGN_LEFT, 0));
        pie.writeSelectedRows(0, -1, 60, document.bottomMargin() - 10, cb);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}