Example usage for org.bouncycastle.mail.smime SMIMECompressedGenerator generate

List of usage examples for org.bouncycastle.mail.smime SMIMECompressedGenerator generate

Introduction

In this page you can find the example usage for org.bouncycastle.mail.smime SMIMECompressedGenerator generate.

Prototype

public MimeBodyPart generate(MimeMessage message, OutputCompressor compressor) throws SMIMEException 

Source Link

Document

generate an compressed object that contains an SMIME Compressed object using the given provider from the contents of the passed in message

Usage

From source file:chapter9.CompressedMailExample.java

/**
 *
 * @param args//  w  w  w  .j  a  v a2s  .c o  m
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    //1.- Create the message we want compressed
    MimeBodyPart dataPart = new MimeBodyPart();

    dataPart.setText("Hello World!!");

    //2.- Set up the generator
    SMIMECompressedGenerator gen = new SMIMECompressedGenerator();

    //3.- Generate the compressed message
    MimeBodyPart comPart = gen.generate(dataPart, SMIMECompressedGenerator.ZLIB);

    //4-. Create the mail message
    MimeMessage mail = Utils.createMimeMessage("example compressed message", comPart.getContent(),
            comPart.getContentType());

    //5.- Create the enveloped object from the mail message
    SMIMECompressed compressed = new SMIMECompressed(mail);

    //6.- Uncompression step
    MimeBodyPart recoveredPart = SMIMEUtil.toMimeBodyPart(compressed.getContent());

    //7.- Content display step
    System.out.print("Content: ");
    System.out.println(recoveredPart.getContent());
}

From source file:com.zotoh.crypto.CryptoUte.java

License:Open Source License

/**
 * @param contentType/*from www  .  j  a  va  2 s.  c  om*/
 * @param msg
 * @return
 * @throws IOException
 * @throws MessagingException
 * @throws GeneralSecurityException
 */
public static MimeBodyPart compressContent(String contentType, StreamData msg)
        throws IOException, MessagingException, GeneralSecurityException {

    tstEStrArg("content-type", contentType);
    tstObjArg("input-content", msg);

    SMIMECompressedGenerator gen = new SMIMECompressedGenerator();
    MimeBodyPart bp = new MimeBodyPart();
    SmDataSource ds;

    if (msg.isDiskFile()) {
        ds = new SmDataSource(msg.getFileRef(), contentType);
    } else {
        ds = new SmDataSource(msg.getBytes(), contentType);
    }

    bp.setDataHandler(new DataHandler(ds));
    try {
        return gen.generate(bp, SMIMECompressedGenerator.ZLIB);
    } catch (SMIMEException e) {
        throw new GeneralSecurityException(e);
    }
}

From source file:com.zotoh.crypto.CryptoUte.java

License:Open Source License

/**
 * @param cType//from w  w w .  ja v  a2s.c o  m
 * @param cte
 * @param contentLoc
 * @param cid
 * @param msg
 * @return
 * @throws MessagingException
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static MimeBodyPart compressContent(String cType, String cte, String contentLoc, String cid,
        StreamData msg) throws MessagingException, IOException, GeneralSecurityException {

    tstEStrArg("content-type", cType);
    tstEStrArg("content-id", cid);
    tstObjArg("input-content", msg);

    SMIMECompressedGenerator gen = new SMIMECompressedGenerator();
    MimeBodyPart bp = new MimeBodyPart();
    SmDataSource ds;

    if (msg.isDiskFile()) {
        ds = new SmDataSource(msg.getFileRef(), cType);
    } else {
        ds = new SmDataSource(msg.getBytes(), cType);
    }

    if (!isEmpty(contentLoc)) {
        bp.setHeader("content-location", contentLoc);
    }

    try {
        bp.setHeader("content-id", cid);
        bp.setDataHandler(new DataHandler(ds));
        bp = gen.generate(bp, SMIMECompressedGenerator.ZLIB);
    } catch (SMIMEException e) {
        throw new GeneralSecurityException(e);
    }

    if (true) {
        int pos = cid.lastIndexOf(">");
        if (pos >= 0) {
            cid = cid.substring(0, pos) + "--z>";
        } else {
            cid = cid + "--z";
        }
    }

    if (!isEmpty(contentLoc)) {
        bp.setHeader("content-location", contentLoc);
    }
    bp.setHeader("content-id", cid);

    // always base64
    cte = "base64";

    if (!isEmpty(cte)) {
        bp.setHeader("content-transfer-encoding", cte);
    }

    return bp;
}

From source file:de.mendelson.comm.as2.message.AS2MessageCreation.java

/**
 * Compresses the payload using the ZLIB algorithm
 *///from  w  w  w  . j a  v  a  2s  .  c  om
private MimeBodyPart compressPayload(Partner receiver, InputStream dataStream, String contentType)
        throws Exception {
    MimeBodyPart bodyPart = new MimeBodyPart();
    bodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(dataStream, contentType)));
    bodyPart.addHeader("Content-Type", contentType);
    if (receiver.getContentTransferEncoding() == AS2Message.CONTENT_TRANSFER_ENCODING_BASE64) {
        bodyPart.addHeader("Content-Transfer-Encoding", "base64");
    } else {
        bodyPart.addHeader("Content-Transfer-Encoding", "binary");
    }
    SMIMECompressedGenerator generator = new SMIMECompressedGenerator();
    if (receiver.getContentTransferEncoding() == AS2Message.CONTENT_TRANSFER_ENCODING_BASE64) {
        generator.setContentTransferEncoding("base64");
    } else {
        generator.setContentTransferEncoding("binary");
    }
    return (generator.generate(bodyPart, new ZlibCompressor()));
}

From source file:de.mendelson.comm.as2.message.AS2MessageCreation.java

/**
 * Compresses the payload using the ZLIB algorithm
 *//*from www . jav  a2 s .  com*/
private MimeBodyPart compressPayload(Partner receiver, Part contentPart) throws SMIMEException {
    SMIMECompressedGenerator generator = new SMIMECompressedGenerator();
    if (receiver.getContentTransferEncoding() == AS2Message.CONTENT_TRANSFER_ENCODING_BASE64) {
        generator.setContentTransferEncoding("base64");
    } else {
        generator.setContentTransferEncoding("binary");
    }
    if (contentPart instanceof MimeBodyPart) {
        return (generator.generate((MimeBodyPart) contentPart, new ZlibCompressor()));
    } else if (contentPart instanceof MimeMessage) {
        return (generator.generate((MimeMessage) contentPart, new ZlibCompressor()));
    } else {
        throw new IllegalArgumentException(
                "compressPayload: Unable to compress a Part of class " + contentPart.getClass().getName());
    }
}

From source file:hk.hku.cecid.piazza.commons.security.SMimeMessage.java

License:Open Source License

/**
 * Compresses the encapsulated MIME body part.
 * //  w  w w .  j  a va  2 s  .  co m
 * @return an S/MIME message encapsulating the compressed MIME body part. 
 * @throws SMimeException if unable to compress the body part.
 */
public SMimeMessage compress() throws SMimeException {
    try {
        try {
            setDefaults();

            /* Create the generator for creating an smime/compressed body part */
            SMIMECompressedGenerator compressor = new SMIMECompressedGenerator();
            compressor.setContentTransferEncoding(getContentTransferEncoding());

            /* compress the body part */
            MimeBodyPart compressedPart = compressor.generate(bodyPart, SMIMECompressedGenerator.ZLIB);
            return new SMimeMessage(compressedPart, this);
        } catch (org.bouncycastle.mail.smime.SMIMEException ex) {
            throw new SMimeException(ex.getMessage(), ex.getUnderlyingException());
        }
    } catch (Exception e) {
        throw new SMimeException("Unable to compress body part", e);
    }
}

From source file:io.aos.crypto.spl09.CompressedMailExample.java

License:Apache License

public static void main(String args[]) throws Exception {
    // create the message we want compressed
    MimeBodyPart dataPart = new MimeBodyPart();

    dataPart.setText("Hello world!");

    // set up the generator
    SMIMECompressedGenerator gen = new SMIMECompressedGenerator();

    // generate the compressed message
    MimeBodyPart comPart = gen.generate(dataPart, SMIMECompressedGenerator.ZLIB);

    // create the mail message
    MimeMessage mail = Utils.createMimeMessage("example compressed message", comPart.getContent(),
            comPart.getContentType());/*from  w  ww .j av a2  s  . c  o m*/

    // create the enveloped object from the mail message
    SMIMECompressed compressed = new SMIMECompressed(mail);

    // uncompression step
    MimeBodyPart recoveredPart = SMIMEUtil.toMimeBodyPart(compressed.getContent());

    // content display step
    System.out.print("Content: ");
    System.out.println(recoveredPart.getContent());
}