Example usage for javax.mail.internet MimeBodyPart getAllHeaders

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

Introduction

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

Prototype

@Override
public Enumeration<Header> getAllHeaders() throws MessagingException 

Source Link

Document

Return all the headers from this Message as an Enumeration of Header objects.

Usage

From source file:mitm.common.mail.BodyPartUtils.java

/**
 * This is the only way I know of to create a new MimeBodyPart from another MimeBodyPart which is safe
 * for signed email. All other methods break the signature when quoted printable soft breaks are used.
 * //  w ww . j  a  v  a  2s  .c  om
 * example of quoted printable soft breaks:
 * 
 * Content-Transfer-Encoding: quoted-printable
        
 * soft break example =
 * another line =
 *
 * All other methods will re-encode and removes the soft breaks.
 * 
 * @param sourceMessage
 * @param matcher
 * @return
 * @throws IOException
 * @throws MessagingException
 */
@SuppressWarnings("unchecked")
public static MimeBodyPart makeContentBodyPartRawBytes(MimeBodyPart sourceMessage, HeaderMatcher matcher)
        throws IOException, MessagingException {
    /*
     * getRawInputStream() can throw a MessagingException when the source message is a MimeMessage
     * that is created in code (ie. not from a stream)
     */
    InputStream messageStream = sourceMessage.getRawInputStream();

    byte[] rawMessage = IOUtils.toByteArray(messageStream);

    InternetHeaders destinationHeaders = new InternetHeaders();

    Enumeration<Header> sourceHeaders = sourceMessage.getAllHeaders();

    HeaderUtils.copyHeaders(sourceHeaders, destinationHeaders, matcher);

    MimeBodyPart newBodyPart = new MimeBodyPart(destinationHeaders, rawMessage);

    return newBodyPart;
}

From source file:com.adaptris.core.services.splitter.MimePartSplitter.java

private void copyHeaders(MimeBodyPart src, AdaptrisMessage dest) throws MessagingException {
    if (preserveHeaders()) {
        Enumeration e = src.getAllHeaders();
        while (e.hasMoreElements()) {
            Header h = (Header) e.nextElement();
            dest.addMetadata(defaultIfEmpty(getHeaderPrefix(), "") + h.getName(), h.getValue());
        }//from   w w  w  . j  av  a  2s . c  o m
    }
}

From source file:com.adaptris.core.services.mime.MimePartSelector.java

/**
 * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage)
 *//*from  ww w .ja va 2 s  .c o m*/
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    try {
        BodyPartIterator mp = MimeHelper.createBodyPartIterator(msg);
        MimeBodyPart part = selector.select(mp);
        if (part != null) {
            if (preserveHeadersAsMetadata()) {
                addHeadersAsMetadata(mp.getHeaders().getAllHeaders(), headerPrefix(), msg);
            }
            if (preservePartHeadersAsMetadata()) {
                addHeadersAsMetadata(part.getAllHeaders(), partHeaderPrefix(), msg);
            }
            StreamUtil.copyAndClose(part.getInputStream(), msg.getOutputStream());
            if (markAsNonMime()) {
                if (msg.containsKey(CoreConstants.MSG_MIME_ENCODED)) {
                    msg.removeMetadata(msg.getMetadata(CoreConstants.MSG_MIME_ENCODED));
                }
            }
        } else {
            log.warn("Could not select a MimePart for extraction, ignoring");
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}

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

/**
 * Gets the header length for any part.//w ww  . ja  v a  2 s .c  o  m
 * @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
}

From source file:org.apache.olingo.fit.AbstractServices.java

protected Response bodyPartRequest(final MimeBodyPart body, final Map<String, String> references)
        throws Exception {
    @SuppressWarnings("unchecked")
    final Enumeration<Header> en = body.getAllHeaders();

    Header header = en.nextElement();/*from   w w  w . j  av  a 2s. c  o m*/
    final String request = header.getName()
            + (StringUtils.isNotBlank(header.getValue()) ? ":" + header.getValue() : "");

    final Matcher matcher = REQUEST_PATTERN.matcher(request);
    final Matcher matcherRef = BATCH_REQUEST_REF_PATTERN.matcher(request);

    final MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();

    while (en.hasMoreElements()) {
        header = en.nextElement();
        headers.putSingle(header.getName(), header.getValue());
    }

    final Response res;
    final String url;
    final String method;

    if (matcher.find()) {
        url = matcher.group(2);
        method = matcher.group(1);
    } else if (matcherRef.find()) {
        url = references.get(matcherRef.group(2)) + matcherRef.group(3);
        method = matcherRef.group(1);
    } else {
        url = null;
        method = null;
    }

    if (url == null) {
        res = null;
    } else {
        final WebClient client = WebClient.create(url, "odatajclient", "odatajclient", null);
        client.headers(headers);

        if ("DELETE".equals(method)) {
            res = client.delete();
        } else {
            final InputStream is = body.getDataHandler().getInputStream();
            String content = IOUtils.toString(is);
            IOUtils.closeQuietly(is);

            final Matcher refs = REF_PATTERN.matcher(content);

            while (refs.find()) {
                content = content.replace(refs.group(1), references.get(refs.group(1)));
            }

            if ("PATCH".equals(method) || "MERGE".equals(method)) {
                client.header("X-HTTP-METHOD", method);
                res = client.invoke("POST", IOUtils.toInputStream(content));
            } else {
                res = client.invoke(method, IOUtils.toInputStream(content));
            }
        }

        // When updating to CXF 3.0.1, uncomment the following line, see CXF-5865
        //client.close();
    }

    return res;
}

From source file:org.apache.olingo.fit.Services.java

private Response bodyPartRequest(final MimeBodyPart body, final Map<String, String> references)
        throws Exception {
    @SuppressWarnings("unchecked")
    final Enumeration<Header> en = body.getAllHeaders();

    Header header = en.nextElement();// w ww. j a  v a  2 s . co m
    final String request = header.getName()
            + (StringUtils.isNotBlank(header.getValue()) ? ":" + header.getValue() : "");

    final Matcher matcher = REQUEST_PATTERN.matcher(request);
    final Matcher matcherRef = BATCH_REQUEST_REF_PATTERN.matcher(request);

    final MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>();

    while (en.hasMoreElements()) {
        header = en.nextElement();
        headers.putSingle(header.getName(), header.getValue());
    }

    final Response res;
    final String url;
    final String method;

    if (matcher.find()) {
        url = matcher.group(2);
        method = matcher.group(1);
    } else if (matcherRef.find()) {
        url = references.get(matcherRef.group(2)) + matcherRef.group(3);
        method = matcherRef.group(1);
    } else {
        url = null;
        method = null;
    }

    if (url == null) {
        res = null;
    } else {
        final WebClient client = WebClient.create(url, "odatajclient", "odatajclient", null);
        client.headers(headers);

        if ("DELETE".equals(method)) {
            res = client.delete();
        } else {
            final InputStream is = body.getDataHandler().getInputStream();
            String content = IOUtils.toString(is);
            IOUtils.closeQuietly(is);

            final Matcher refs = REF_PATTERN.matcher(content);

            while (refs.find()) {
                content = content.replace(refs.group(1), references.get(refs.group(1)));
            }

            if ("PATCH".equals(method) || "MERGE".equals(method)) {
                client.header("X-HTTP-METHOD", method);
                res = client.invoke("POST", IOUtils.toInputStream(content));
            } else {
                res = client.invoke(method, IOUtils.toInputStream(content));
            }
        }

        // When updating to CXF 3.0.1, uncomment the following line, see CXF-5865
        // client.close();
    }

    return res;
}

From source file:org.xmlactions.email.EMailParser.java

private void handlePart(Part part) throws MessagingException, IOException, DocumentException {

    log.debug("\n\n\nhandlePart ==>>");
    log.debug("part.toString():" + part.toString());
    log.debug(//from  w w  w  .java  2s.c  om
            "part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment"));
    log.debug("part.getContentType():" + part.getContentType());
    log.debug("part.getFilename():" + part.getFileName());
    log.debug("part.isAttachment:" + part.getFileName());
    log.debug("part.isMessage:" + (part.getContent() instanceof Message));
    Object obj = part.getContent();
    if (obj instanceof Multipart) {
        Multipart mmp = (Multipart) obj;
        for (int i = 0; i < mmp.getCount(); i++) {
            Part bodyPart = mmp.getBodyPart(i);
            if (bodyPart instanceof Message) {
                setFirstMessageProcessed(true);// need to mark this when we
                // get a forwarded message
                // so we don't look for case
                // numbers in forwarded
                // emails.
            }
            handlePart(bodyPart);
        }
    } else if (obj instanceof Part) {
        if (obj instanceof Message) {
            setFirstMessageProcessed(true);// need to mark this when we get
            // a forwarded message so we
            // don't look for case numbers
            // in forwarded emails.
        }
        handlePart((Part) obj);
    } else {
        if (part instanceof MimeBodyPart) {
            MimeBodyPart p = (MimeBodyPart) part;
            Enumeration enumeration = p.getAllHeaders();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            Object content = p.getContent();
            enumeration = p.getAllHeaderLines();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            DataHandler dh = p.getDataHandler();
            if (dh == null)
                dh = null;
        }
        addPart(part);
        log.debug("=== Add Part ===");
        log.debug((String) (part.getFileName() != null ? "isAttachment" : part.getContent()));
        // log.info("not recognised class:" + obj.getClass().getName() +
        // "\n" + obj);
    }
    log.debug("<<== handlePart");
}