Example usage for javax.mail Part isMimeType

List of usage examples for javax.mail Part isMimeType

Introduction

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

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

Usage

From source file:mitm.common.mail.filter.UnsupportedFormatStripper.java

private void scanPart(Part part, PartsContext context, int depth) throws MessagingException, IOException {
    if (depth > MAX_DEPTH) {
        return;/*from  www  .  j av  a  2s.  c  o m*/
    }

    if (part.isMimeType("multipart/mixed")) {
        MimeMultipart parts = (MimeMultipart) part.getContent();

        int partCount = parts.getCount();

        for (int i = 0; i < partCount; i++) {
            Part child = parts.getBodyPart(i);

            scanPart(child, context, ++depth);
        }
    } else if (part.isMimeType("multipart/*")) {
        /*
         * We will always add any multipart that's not a mixed multipart
         */
        context.getParts().add(part);
    } else if (part.isMimeType("text/plain")) {
        context.getParts().add(part);
    } else if (part.isMimeType("text/html")) {
        context.getParts().add(part);
    } else if (!addSupportedPart(part, context)) {
        skipPart(part, true, context);
    }
}

From source file:com.ikon.util.MailUtils.java

/**
 * Add attachments to an imported mail./*  w  w  w  . j a v  a2s. c  om*/
 */
public static void addAttachments(String token, com.ikon.bean.Mail mail, Part p, String userId)
        throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException,
        UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();

        for (int i = 1; i < count; i++) {
            BodyPart bp = mp.getBodyPart(i);

            if (bp.getFileName() != null) {
                String name = MimeUtility.decodeText(bp.getFileName());
                String fileName = FileUtils.getFileName(name);
                String fileExtension = FileUtils.getFileExtension(name);
                String testName = name;

                // Test if already exists a document with the same name in the mail
                for (int j = 1; OKMRepository.getInstance().hasNode(token,
                        mail.getPath() + "/" + testName); j++) {
                    // log.info("Trying with: {}", testName);
                    testName = fileName + " (" + j + ")." + fileExtension;
                }

                Document attachment = new Document();
                String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase());
                attachment.setMimeType(mimeType);
                attachment.setPath(mail.getPath() + "/" + testName);
                InputStream is = bp.getInputStream();

                if (Config.REPOSITORY_NATIVE) {
                    new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId);
                } else {
                    new JcrDocumentModule().create(token, attachment, is, userId);
                }

                is.close();
            }
        }
    }
}

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

private void extractBody(Part Mess, cfArrayData AD, String attachURI, String attachDIR) throws Exception {

    if (Mess.isMimeType("multipart/*")) {

        Multipart mp = (Multipart) Mess.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            extractBody(mp.getBodyPart(i), AD, attachURI, attachDIR);

    } else {//from   w  w w .j ava 2 s  .c o  m

        cfStructData sd = new cfStructData();

        String tmp = Mess.getContentType();
        if (tmp.indexOf(";") != -1)
            tmp = tmp.substring(0, tmp.indexOf(";"));

        sd.setData("mimetype", new cfStringData(tmp));

        String filename = getFilename(Mess);
        String dispos = getDisposition(Mess);

        MimeType messtype = new MimeType(tmp);
        // Note that we can't use Mess.isMimeType() here due to bug #2080
        if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && messtype.match("text/*")) {

            Object content;
            String contentType = Mess.getContentType().toLowerCase();
            // support aliases of UTF-7 - UTF7, UNICODE-1-1-UTF-7, csUnicode11UTF7, UNICODE-2-0-UTF-7
            if (contentType.indexOf("utf-7") != -1 || contentType.indexOf("utf7") != -1) {
                InputStream ins = Mess.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy(ins, bos);
                content = new String(UTF7Converter.convert(bos.toByteArray()));
            } else {
                try {
                    content = Mess.getContent();
                } catch (UnsupportedEncodingException e) {
                    content = Mess.getInputStream();
                } catch (IOException ioe) {
                    // This will happen on BD/Java when the attachment has no content
                    // NOTE: this is the fix for bug NA#3198.
                    content = "";
                }
            }

            if (content instanceof InputStream) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy((InputStream) content, bos);
                sd.setData("content", new cfStringData(new String(bos.toByteArray())));
            } else {
                sd.setData("content", new cfStringData(content.toString()));
            }

            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("filename", new cfStringData(filename == null ? "" : filename));

        } else if (attachDIR != null) {

            sd.setData("content", new cfStringData(""));

            if (filename == null || filename.length() == 0)
                filename = "unknownfile";

            filename = getAttachedFilename(attachDIR, filename);

            //--[ An attachment, save it out to disk
            try {
                BufferedInputStream in = new BufferedInputStream(Mess.getInputStream());
                BufferedOutputStream out = new BufferedOutputStream(
                        cfEngine.thisPlatform.getFileIO().getFileOutputStream(new File(attachDIR + filename)));
                IOUtils.copy(in, out);

                out.flush();
                out.close();
                in.close();

                sd.setData("file", cfBooleanData.TRUE);
                sd.setData("filename", new cfStringData(filename));
                if (attachURI.charAt(attachURI.length() - 1) != '/')
                    sd.setData("url", new cfStringData(attachURI + '/' + filename));
                else
                    sd.setData("url", new cfStringData(attachURI + filename));
                sd.setData("size", new cfNumberData((int) new File(attachDIR + filename).length()));

            } catch (Exception ignoreException) {
                // NOTE: this could happen when we don't have permission to write to the specified directory
                //       so let's log an error message to make this easier to debug.
                cfEngine.log("-] Failed to save attachment to " + attachDIR + filename + ", exception=["
                        + ignoreException.toString() + "]");
            }

        } else {
            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("content", new cfStringData(""));
        }

        AD.addElement(sd);
    }
}

From source file:com.riq.MailHandlerServlet.java

private String getText(Part p) throws MessagingException, IOException {
    //    log.info("Message S Zero");
    //    log.info("Inside MailServlet getText");

    if (p.isMimeType("text/*")) {
        String s = (String) p.getContent();
        log.info("Message S1: " + s);
        boolean textIsHtml = p.isMimeType("text/html");
        return s;
    }/*  w w  w.  j  ava 2 s.c o m*/

    if (p.isMimeType("multipart/alternative")) {
        Multipart mp = (Multipart) p.getContent();
        String text = null;
        for (int i = 0; i < mp.getCount(); i++) {
            Part bp = mp.getBodyPart(i);
            if (bp.isMimeType("text/plain")) {
                if (text == null)
                    text = getText(bp);
                log.info("Message S2: " + text);
                continue;
            } else if (bp.isMimeType("text/html")) {
                String s = getText(bp);
                if (s != null)
                    log.info("Message S3: " + s);
                return s;
            } else if (bp.isMimeType("message/rfc822")) {
                Object nestedObject = "";
                nestedObject = bp.getContent();
                Multipart nestedPart = (Multipart) nestedObject;
                BodyPart nestedBodyPart = nestedPart.getBodyPart(0);
                String s = getText(nestedBodyPart);
                if (s != null)
                    log.info("Message Nested: " + s);
                return s;
            } else {
                log.info("Message nada");
                return getText(bp);
            }
        }
        return text;

    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            String s = getText(mp.getBodyPart(i));
            if (s != null) {
                log.info("Message S4: " + s);
                return s;
            }
        }
    }
    return null;
}

From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java

public void buildMd5CheckSums(List<String> checksums, Part part) throws MessagingException, IOException {
    if (part.isMimeType(MULTIPART_TYPE)) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            buildMd5CheckSums(checksums, mp.getBodyPart(i));
        }//from   w w w  .  j a  v a  2 s  . c  om
    } else if (isAttach(part)) {
        checksums.add(getCheckSum(part));
    }
}

From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java

public boolean hasAttach(Part part) throws MessagingException, IOException {
    if (part.isMimeType(MULTIPART_TYPE)) {
        Multipart mp = (Multipart) part.getContent();
        boolean has = false;
        for (int i = 0; i < mp.getCount(); i++) {
            if (hasAttach(mp.getBodyPart(i))) {
                has = true;/*from  w w w.j av a2s.  co  m*/
                break;
            }
        }
        return has;
    } else if (isAttach(part)) {
        return true;
    } else {
        return false;
    }
}

From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java

private boolean isMailAttached(Part part) throws MessagingException {
    if (isMsgToBeEvaluateAsAttach()) {
        return part.isMimeType(MESSAGE_TYPE);
    }/*from w  ww .  ja v a2 s.  c  o m*/
    return false;
}

From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java

public void buildAttachments(List<Attach> attachs, Part part, String baseStorePath)
        throws MessagingException, IOException {
    if (part.isMimeType(MULTIPART_TYPE)) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            buildAttachments(attachs, mp.getBodyPart(i), baseStorePath);
        }/*  w  w w .j a  v a 2  s . c o  m*/
    } else if (isAttach(part)) {
        attachs.add(getAttach(part, baseStorePath));
    }
}

From source file:com.openkm.util.MailUtils.java

/**
 * Add attachments to an imported mail.//from   w w w.  j  a  v a  2 s  . co m
 */
public static void addAttachments(String token, com.openkm.bean.Mail mail, Part p, String userId)
        throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException,
        UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();

        for (int i = 1; i < count; i++) {
            BodyPart bp = mp.getBodyPart(i);

            if (bp.getFileName() != null) {
                String name = MimeUtility.decodeText(bp.getFileName());
                String fileName = FileUtils.getFileName(name);
                String fileExtension = FileUtils.getFileExtension(name);
                String testName = name;

                // Test if already exists a document with the same name in the mail
                for (int j = 1; OKMRepository.getInstance().hasNode(token,
                        mail.getPath() + "/" + testName); j++) {
                    // log.info("Trying with: {}", testName);
                    testName = fileName + " (" + j + ")." + fileExtension;
                }

                Document attachment = new Document();
                String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase());
                attachment.setMimeType(mimeType);
                attachment.setPath(mail.getPath() + "/" + testName);
                InputStream is = bp.getInputStream();

                if (Config.REPOSITORY_NATIVE) {
                    new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId);
                } else {
                    new JcrDocumentModule().create(token, attachment, is, userId);
                }

                is.close();
            }
        }
    }
}

From source file:mitm.common.dlp.impl.MimeMessageTextExtractorImpl.java

private boolean onPart(Part parent, Part part, Object context) throws PartException {
    try {/*from   w  ww  .j  av a  2 s . c o  m*/
        PartContext partContext = (PartContext) context;

        /*
         * If the part is a RFC822 message (ie. an attached MIME message we need to extract the message and
         * parse the message
         */
        if (part.isMimeType(MimeTypes.MESSAGE_RFC822)) {
            handleRFC822(part, partContext);
        } else {
            handlePart(part, partContext);
        }
    } catch (Exception e) {
        if (isFailOnException()) {
            if (e instanceof PartException) {
                throw (PartException) e;
            }

            throw new PartException(e);
        } else {
            logException("Exception while handling part. Part will be skipped.", e);
        }
    }

    return true;
}