Example usage for javax.mail.internet MimeBodyPart getSize

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

Introduction

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

Prototype

@Override
public int getSize() throws MessagingException 

Source Link

Document

Return the size of the content of this body part in bytes.

Usage

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 w w.j  a  va 2s  .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:de.mendelson.comm.as2.message.AS2MessageParser.java

/**Uncompresses message data*/
public byte[] decompressData(AS2MessageInfo info, byte[] data, String contentType) throws Exception {
    MimeBodyPart compressedPart = new MimeBodyPart();
    compressedPart.setDataHandler(new DataHandler(new ByteArrayDataSource(data, contentType)));
    compressedPart.setHeader("content-type", contentType);
    return (this.decompressData(info, new SMIMECompressed(compressedPart), compressedPart.getSize()));
}

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

/**
 * Determine the length for the individual part.
 * @param bp is the part to be searched.
 * @return the length in bytes./*ww w . java 2  s . co m*/
 */
protected static long getContentLength(javax.mail.internet.MimeBodyPart bp) {

    long headerLength = -1L;
    long dataSize = -1L;

    try {
        headerLength = getHeaderLength(bp);

        javax.activation.DataHandler dh = bp.getDataHandler();
        javax.activation.DataSource ds = dh.getDataSource();

        // Do files our selfs since this is costly to read in. Ask the file system.
        // This is 90% of the use of attachments.
        if (ds instanceof javax.activation.FileDataSource) {
            javax.activation.FileDataSource fdh = (javax.activation.FileDataSource) ds;
            java.io.File df = fdh.getFile();

            if (!df.exists()) {
                throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath()));
            }

            dataSize = df.length();
        } else {
            dataSize = bp.getSize();

            if (-1 == dataSize) { // Data size is not known so read it the hard way...
                dataSize = 0;

                java.io.InputStream in = ds.getInputStream();
                byte[] readbuf = new byte[64 * 1024];
                int bytesread;

                do {
                    bytesread = in.read(readbuf);

                    if (bytesread > 0) {
                        dataSize += bytesread;
                    }
                } while (bytesread > -1);

                in.close();
            }
        }
    } catch (Exception e) {
        log.error(Messages.getMessage("exception00"), e);
    }

    return dataSize + headerLength;
}