Example usage for javax.mail.internet MimeMessage getFileName

List of usage examples for javax.mail.internet MimeMessage getFileName

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getFileName.

Prototype

@Override
public String getFileName() throws MessagingException 

Source Link

Document

Get the filename associated with this Message.

Usage

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

/**Writes a passed payload data to the passed message object. Could be called from either the MDN
 * processing or the message processing/*  www  .jav a 2  s .  c  o  m*/
 */
public void writePayloadsToMessage(byte[] data, AS2Message message, Properties header) throws Exception {
    ByteArrayOutputStream payloadOut = new ByteArrayOutputStream();
    MimeMessage testMessage = new MimeMessage(Session.getInstance(System.getProperties()),
            new ByteArrayInputStream(data));
    //multiple attachments?
    if (testMessage.isMimeType("multipart/*")) {
        this.writePayloadsToMessage(testMessage, message, header);
        return;
    }
    InputStream payloadIn = null;
    AS2Info info = message.getAS2Info();
    if (info instanceof AS2MessageInfo && info.getSignType() == AS2Message.SIGNATURE_NONE
            && ((AS2MessageInfo) info).getCompressionType() == AS2Message.COMPRESSION_NONE) {
        payloadIn = new ByteArrayInputStream(data);
    } else if (testMessage.getSize() > 0) {
        payloadIn = testMessage.getInputStream();
    } else {
        payloadIn = new ByteArrayInputStream(data);
    }
    this.copyStreams(payloadIn, payloadOut);
    payloadOut.flush();
    payloadOut.close();
    byte[] payloadData = payloadOut.toByteArray();
    AS2Payload as2Payload = new AS2Payload();
    as2Payload.setData(payloadData);
    String contentIdHeader = header.getProperty("content-id");
    if (contentIdHeader != null) {
        as2Payload.setContentId(contentIdHeader);
    }
    String contentTypeHeader = header.getProperty("content-type");
    if (contentTypeHeader != null) {
        as2Payload.setContentType(contentTypeHeader);
    }
    try {
        as2Payload.setOriginalFilename(testMessage.getFileName());
    } catch (MessagingException e) {
        if (this.logger != null) {
            this.logger.log(Level.WARNING, this.rb.getResourceString("filename.extraction.error",
                    new Object[] { info.getMessageId(), e.getMessage(), }), info);
        }
    }
    if (as2Payload.getOriginalFilename() == null) {
        String filenameHeader = header.getProperty("content-disposition");
        if (filenameHeader != null) {
            //test part for convinience: extract file name
            MimeBodyPart filenamePart = new MimeBodyPart();
            filenamePart.setHeader("content-disposition", filenameHeader);
            try {
                as2Payload.setOriginalFilename(filenamePart.getFileName());
            } catch (MessagingException e) {
                if (this.logger != null) {
                    this.logger.log(Level.WARNING, this.rb.getResourceString("filename.extraction.error",
                            new Object[] { info.getMessageId(), e.getMessage(), }), info);
                }
            }
        }
    }
    message.addPayload(as2Payload);
}