Example usage for javax.mail Part getDisposition

List of usage examples for javax.mail Part getDisposition

Introduction

In this page you can find the example usage for javax.mail Part getDisposition.

Prototype

public String getDisposition() throws MessagingException;

Source Link

Document

Return the disposition of this part.

Usage

From source file:com.canoo.webtest.plugins.emailtest.EmailMessageStructureFilter.java

private static String processMessagePart(final Part part) throws MessagingException {
    final String disp = part.getDisposition();
    final String contentType = part.getContentType();
    if (Part.ATTACHMENT.equals(disp)) {
        return "    <part type=\"attachment\" filename=\"" + part.getFileName() + "\" contentType=\""
                + extractBaseContentType(contentType) + "\"/>" + LS;
    }//from   w w w. j  a va2  s .c o  m
    return "    <part type=\"inline\" contentType=\"" + extractBaseContentType(contentType) + "\"/>" + LS;
}

From source file:com.naryx.tagfusion.cfm.mail.cfMailMessageData.java

public static String getDisposition(Part _mess) throws MessagingException {
    String dispos = _mess.getDisposition();
    // getDisposition isn't 100% reliable 
    if (dispos == null) {
        String[] disposHdr = _mess.getHeader("Content-Disposition");
        if (disposHdr != null && disposHdr.length > 0 && disposHdr[0].startsWith(Part.ATTACHMENT)) {
            return Part.ATTACHMENT;
        }//from ww w  . j  a v  a  2  s.  c o  m
    }

    return dispos;
}

From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java

@SuppressWarnings("unchecked")
protected static void getAttachmentParts(Part p, String defaultFilename, MimetypeRegistry mimeService,
        ExecutionContext context) throws MessagingException, IOException {
    String filename = getFilename(p, defaultFilename);
    List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY);

    if (!p.isMimeType("multipart/*")) {
        String disp = p.getDisposition();
        // no disposition => consider it may be body
        if (disp == null && !context.containsKey(BODY_KEY)) {
            // this will need to be parsed
            context.put(BODY_KEY, p.getContent());
        } else if (disp != null
                && (disp.equalsIgnoreCase(Part.ATTACHMENT) || (disp.equalsIgnoreCase(Part.INLINE)
                        && !(p.isMimeType("text/*") || p.isMimeType("image/*"))))) {
            log.debug("Saving attachment to file " + filename);
            Blob blob = Blobs.createBlob(p.getInputStream());
            String mime = DEFAULT_BINARY_MIMETYPE;
            try {
                if (mimeService != null) {
                    ContentType contentType = new ContentType(p.getContentType());
                    mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob,
                            contentType.getBaseType());
                }//from   w  ww . j a v a 2  s  .  co m
            } catch (MimetypeDetectionException e) {
                log.error(e);
            }
            blob.setMimeType(mime);

            blob.setFilename(filename);

            blobs.add(blob);

            // for debug
            // File f = new File(filename);
            // ((MimeBodyPart) p).saveFile(f);

            log.debug("---------------------------");

        } else {
            log.debug(String.format("Ignoring part with type %s", p.getContentType()));
        }
    }

    if (p.isMimeType("multipart/*")) {
        log.debug("This is a Multipart");
        log.debug("---------------------------");
        Multipart mp = (Multipart) p.getContent();

        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context);
        }
    } else if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) {
        log.debug("This is a Nested Message");
        log.debug("---------------------------");
        getAttachmentParts((Part) p.getContent(), defaultFilename, mimeService, context);
    }

}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

public static void handlePart(String foldername, Part part) throws MessagingException, IOException {
    String disposition = part.getDisposition();
    // String contentType = part.getContentType();

    if ((disposition != null)
            && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
        saveFile(foldername, MimeUtility.decodeText(part.getFileName()), part.getInputStream());
    }/*from w  ww .jav  a2 s. c  o  m*/
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

/**
 * Processes part of a message/*from ww w  .  j a  v a 2s.co  m*/
 */
private static String handleSimplePart(Part part, List<EmailAttachment> attachments, boolean ignoreAttachments)
        throws MessagingException, IOException {
    // Check for content disposition and content type
    String disposition = part.getDisposition();
    String contentType = part.getContentType();
    LOGGER.debug("disposition=" + disposition);
    LOGGER.debug("Body type is: " + contentType);

    // tread if the part is a message
    boolean inlineMessage = part.isMimeType("message/rfc822");
    if (inlineMessage) {
        LOGGER.debug("Inner message:" + part.getFileName());
        Message subMessage = (Message) part.getContent();
        StringBuffer s = handlePart(subMessage, attachments, ignoreAttachments);
        System.out.println();
        return s.toString();
    }
    if (disposition == null) {
        if ("image/BMP".equals(contentType)) {
            // BMP image add as attachment
            if (ignoreAttachments == false) {
                try {
                    attachments.add(createEmailAttachment(part, "image.bmp"));
                } catch (Exception e) {
                    // just ignore
                }
            }
            return null;
        } else if (part.isMimeType("text/*")) {
            return getText(part);
        } else {
            if (ignoreAttachments == false) {
                handleAttachment(part, attachments);
            }
            return null;
        }
    }
    if (disposition.equalsIgnoreCase(Part.INLINE)) {
        return handleInline(part, attachments, ignoreAttachments);
    }
    if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
        if (ignoreAttachments == false) {
            handleAttachment(part, attachments);
        }
        return null;
    }
    LOGGER.debug("Unknown disposition:" + disposition + "Threat as attachment");
    if (ignoreAttachments == false) {
        handleAttachment(part, attachments);
    }
    return null;
}

From source file:com.stimulus.archiva.extraction.MessageExtraction.java

private static void dumpPart(Part p, Hashtable<String, Part> attachments, Hashtable<String, String> inlines,
        Hashtable<String, String> images, Hashtable<String, String> nonImages, ArrayList<String> mimeTypes,
        String subject) throws Exception {
    mimeTypes.add(p.getContentType());/* www .j a v a2s  .c  o m*/
    if (!p.isMimeType("multipart/*")) {

        String disp = p.getDisposition();
        String fname = p.getFileName();
        if (fname != null) {
            try {
                fname = MimeUtility.decodeText(fname);
            } catch (Exception e) {
                logger.debug("cannot decode filename:" + e.getMessage());
            }
        }
        if ((disp != null && Compare.equalsIgnoreCase(disp, Part.ATTACHMENT)) || fname != null) {
            String filename = getFilename(subject, p);
            if (!filename.equalsIgnoreCase("winmail.dat")) {
                attachments.put(filename, p);
            }
            /*if (p.isMimeType("image/*")) {
               String str[] = p.getHeader("Content-ID");
               if (str != null) images.put(filename,str[0]);
               else images.put(filename, filename);
            }
            return;*/
        }
    }
    if (p.isMimeType("text/plain")) {
        String str = "";
        if (inlines.containsKey("text/plain"))
            str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n";
        inlines.put("text/plain", str + getTextContent(p));
    } else if (p.isMimeType("text/html")) {
        inlines.put("text/html", getTextContent(p));
    } else if (p.isMimeType("text/xml")) {
        attachments.put(getFilename(subject, p), p);
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); i++)
            dumpPart(mp.getBodyPart(i), attachments, inlines, images, nonImages, mimeTypes, subject);
    } else if (p.isMimeType("message/rfc822")) {
        dumpPart((Part) p.getContent(), attachments, inlines, images, nonImages, mimeTypes, subject);
    } else if (p.isMimeType("application/ms-tnef")) {
        Part tnefpart = TNEFMime.convert(null, p, false);
        if (tnefpart != null) {
            dumpPart((Part) tnefpart, attachments, inlines, images, nonImages, mimeTypes, subject);
        }
    } else if (p.isMimeType("application/*")) {
        String filename = getFilename("application", p);
        attachments.put(filename, p);
        String str[] = p.getHeader("Content-ID");
        if (str != null)
            nonImages.put(filename, str[0]);
    } else if (p.isMimeType("image/*")) {
        String fileName = getFilename("image", p);
        attachments.put(fileName, p);
        String str[] = p.getHeader("Content-ID");
        if (str != null)
            images.put(fileName, str[0]);
        else
            images.put(fileName, fileName);
    } else {
        String contentType = p.getContentType();
        Object o = p.getContent();
        if (o instanceof String) {
            String str = "";
            if (inlines.containsKey("text/plain"))
                str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n";
            inlines.put(contentType, str + (String) o);
        } else {
            String fileName = getFilenameFromContentType("attach", contentType);
            attachments.put(fileName, p);
        }
    }
}

From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java

private boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {/* w  ww. jav a 2s. c o m*/
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            SilverLogger.getLogger(this).error(e);
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            SilverLogger.getLogger(this).error(e);
        }
    }
    return false;
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {/*from   w  w  w.  j a v  a 2 s .  co m*/
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    return false;
}

From source file:org.silverpeas.components.mailinglist.service.job.MailProcessor.java

/**
 * Analyze the part to check if it is an attachment, a base64 encoded file or some text.
 * @param part the part to be analyzed./*from   w ww .ja  v  a 2  s  . c  o  m*/
 * @return true if it is some text - false otherwise.
 * @throws MessagingException
 */
protected boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            logger.error(e.getMessage(), e);
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}

From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java

/**
 * Analyze the part to check if it is an attachment, a base64 encoded file or some text.
 *
 * @param part the part to be analyzed./*  w ww. j a  v a  2  s  . c  o  m*/
 * @return true if it is some text - false otherwise.
 * @throws MessagingException
 */
protected boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return false;
}