Example usage for javax.mail.internet MimeBodyPart writeTo

List of usage examples for javax.mail.internet MimeBodyPart writeTo

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart writeTo.

Prototype

@Override
public void writeTo(OutputStream os) throws IOException, MessagingException 

Source Link

Document

Output the body part as an RFC 822 format stream.

Usage

From source file:gov.nih.nci.cacis.nav.SendEncryptedMail.java

private MimeMessage encryptMessage(MimeMessage message, Session session, Certificate cert)
        throws NoSuchAlgorithmException, NoSuchProviderException, SMIMEException, MessagingException,
        IOException {/*ww  w. j av  a2 s .  co  m*/
    /* Create the encrypter */
    final SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
    encrypter.addKeyTransRecipient((X509Certificate) cert);

    /* Encrypt the message */
    final MimeBodyPart encryptedPart = encrypter.generate(message, SMIMEEnvelopedGenerator.AES256_CBC,
            PROVIDER_TYPE);

    /*
     * Create a new MimeMessage that contains the encrypted and signed content
     */
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    encryptedPart.writeTo(out);

    final MimeMessage encryptedMessage = new MimeMessage(session, new ByteArrayInputStream(out.toByteArray()));

    /* Set all original MIME headers in the encrypted message */
    final Enumeration headers = message.getAllHeaderLines();
    while (headers.hasMoreElements()) {
        final String headerLine = (String) headers.nextElement();
        /*
         * Make sure not to override any content-* headers from the original message
         */
        if (!Strings.toLowerCase(headerLine).startsWith("content-")) {
            encryptedMessage.addHeaderLine(headerLine);
        }
    }

    return encryptedMessage;
}

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

/**Prepares the message if it contains no MIME structure*/
public AS2Message createMessageNoMIME(AS2Message message, Partner receiver) throws Exception {
    AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info();
    BCCryptoHelper cryptoHelper = new BCCryptoHelper();
    //payload content type.
    message.setContentType(receiver.getContentType());
    message.setRawData(message.getPayload(0).getData());
    message.setDecryptedRawData(message.getPayload(0).getData());
    if (this.logger != null) {
        this.logger.log(Level.INFO,
                this.rb.getResourceString("message.notsigned", new Object[] { info.getMessageId() }), info);
    }//from   w  ww .jav a 2  s .  c  o  m
    //compute content mic. Use sha1 as hash alg.
    String digestOID = cryptoHelper.convertAlgorithmNameToOID(BCCryptoHelper.ALGORITHM_SHA1);
    String mic = cryptoHelper.calculateMIC(message.getPayload(0).getData(), digestOID);
    info.setReceivedContentMIC(mic + ", sha1");
    //add compression
    if (receiver.getCompressionType() == AS2Message.COMPRESSION_ZLIB) {
        info.setCompressionType(AS2Message.COMPRESSION_ZLIB);
        int uncompressedSize = message.getDecryptedRawData().length;
        MimeBodyPart bodyPart = this.compressPayload(receiver, message.getDecryptedRawData(),
                receiver.getContentType());
        int compressedSize = bodyPart.getSize();
        //sometimes size() is unable to determine the size of the compressed body part and will return -1. Dont log the
        //compression ratio in this case.
        if (compressedSize == -1) {
            if (this.logger != null) {
                this.logger.log(Level.INFO, this.rb.getResourceString("message.compressed.unknownratio",
                        new Object[] { info.getMessageId() }), info);
            }
        } else {
            if (this.logger != null) {
                this.logger.log(Level.INFO,
                        this.rb.getResourceString("message.compressed",
                                new Object[] { info.getMessageId(),
                                        AS2Tools.getDataSizeDisplay(uncompressedSize),
                                        AS2Tools.getDataSizeDisplay(compressedSize) }),
                        info);
            }
        }
        //write compressed data into the message array
        ByteArrayOutputStream bodyOutStream = new ByteArrayOutputStream();
        bodyPart.writeTo(bodyOutStream);
        bodyOutStream.flush();
        bodyOutStream.close();
        message.setDecryptedRawData(bodyOutStream.toByteArray());
    }
    //no encryption
    if (info.getEncryptionType() == AS2Message.ENCRYPTION_NONE) {
        if (this.logger != null) {
            this.logger.log(Level.INFO,
                    this.rb.getResourceString("message.notencrypted", new Object[] { info.getMessageId() }),
                    info);
        }
        message.setRawData(message.getDecryptedRawData());
    } else {
        //encrypt the message raw data
        String cryptAlias = this.encryptionCertManager
                .getAliasByFingerprint(receiver.getCryptFingerprintSHA1());
        this.encryptDataToMessage(message, cryptAlias, info.getEncryptionType(), receiver);
    }
    return (message);
}

From source file:org.apache.axiom.om.impl.MIMEOutputUtils.java

/**
 * @deprecated Use {@link OMMultipartWriter} instead.
 *///from  ww w.j a va2  s.c om
public static void writeBodyPart(OutputStream outStream, MimeBodyPart part, String boundary)
        throws IOException, MessagingException {
    if (isDebugEnabled) {
        log.debug("Start writeMimeBodyPart for " + part.getContentID());
    }
    outStream.write(CRLF);
    part.writeTo(outStream);
    outStream.write(CRLF);
    writeMimeBoundary(outStream, boundary);
    outStream.flush();
    if (isDebugEnabled) {
        log.debug("End writeMimeBodyPart");
    }
}

From source file:org.apache.axis.attachments.MimeUtils.java

/**
 * Gets the header length for any part./*w w w  .  jav a 2s.c  om*/
 * @param bp the part to determine the header length for.
 * @return the length in bytes.
 *
 * @throws javax.mail.MessagingException
 * @throws java.io.IOException
 */
private static long getHeaderLength(javax.mail.internet.MimeBodyPart bp)
        throws javax.mail.MessagingException, java.io.IOException {

    javax.mail.internet.MimeBodyPart headersOnly = new javax.mail.internet.MimeBodyPart(
            new javax.mail.internet.InternetHeaders(), new byte[0]);

    for (java.util.Enumeration en = bp.getAllHeaders(); en.hasMoreElements();) {
        javax.mail.Header header = (javax.mail.Header) en.nextElement();

        headersOnly.addHeader(header.getName(), header.getValue());
    }

    java.io.ByteArrayOutputStream bas = new java.io.ByteArrayOutputStream(1024 * 16);

    headersOnly.writeTo(bas);
    bas.close();

    return (long) bas.size(); // This has header length plus the crlf part that seperates the data
}