Example usage for javax.mail Header getName

List of usage examples for javax.mail Header getName

Introduction

In this page you can find the example usage for javax.mail Header getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of this header.

Usage

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!/*w ww.j  av a2  s . com*/
 *
 * @param message DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MessagingException DOCUMENT ME!
 */
public static InternetHeaders getHeaders(Message message) throws MessagingException {
    Header xheader;
    InternetHeaders xheaders = new InternetHeaders();
    Enumeration xe = message.getAllHeaders();

    for (; xe.hasMoreElements();) {
        xheader = (Header) xe.nextElement();

        if (!xheader.getName().startsWith("From ")) {
            xheaders.addHeader(xheader.getName(), xheader.getValue());
        }
    }

    return xheaders;
}

From source file:com.duroty.utils.mail.MessageUtilities.java

public static InternetHeaders getHeadersWithFrom(Message message) throws MessagingException {
    Header xheader;
    InternetHeaders xheaders = new InternetHeaders();
    Enumeration xe = message.getAllHeaders();

    for (; xe.hasMoreElements();) {
        xheader = (Header) xe.nextElement();
        xheaders.addHeader(xheader.getName(), xheader.getValue());
    }//from w  w w.j a va 2 s .co  m

    return xheaders;
}

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * Given a message that we are replying to, or forwarding,
 *
 * @param part The part to decode.//from w w  w .j a  v a 2 s  . c  o m
 * @param buffer The new message body text buffer.
 * @param dmailParts Vector for new message's attachments.
 *
 * @return The buffer being filled in with the body.
 *
 * @throws MessagingException DOCUMENT ME!
 * @throws IOException
 */
protected static StringBuffer subDecodeContent(Part part, StringBuffer buffer, Vector dmailParts,
        boolean chooseHtml, String breakLine) throws MessagingException, IOException {
    boolean attachIt = true;

    // decode based on content type and disposition
    ContentType xctype = MessageUtilities.getContentType(part);

    ContentDisposition xcdisposition = MessageUtilities.getContentDisposition(part);

    if (xctype.match("multipart/*")) {
        attachIt = false;

        Multipart xmulti = (Multipart) MessageUtilities.getPartContent(part);

        int xparts = 0;

        try {
            xparts = xmulti.getCount();
        } catch (MessagingException e) {
            attachIt = true;
            xparts = 0;
        }

        for (int xindex = 0; xindex < xparts; xindex++) {
            MessageUtilities.subDecodeContent(xmulti.getBodyPart(xindex), buffer, dmailParts, chooseHtml,
                    breakLine);
        }
    } else if (xctype.match("message/rfc822")) {
        MimeMessage newMessage = new MimeMessage((Session) null, part.getInputStream());
        decodeContent(newMessage, buffer, dmailParts, chooseHtml, breakLine);
    } else if (xctype.match("text/plain") && !chooseHtml) {
        if (xcdisposition.match("inline")) {
            attachIt = false;

            String xjcharset = xctype.getParameter("charset");

            if (xjcharset == null) {
                // not present, assume ASCII character encoding                       
                try {
                    Header xheader;
                    Enumeration xe = part.getAllHeaders();

                    for (; xe.hasMoreElements();) {
                        xheader = (Header) xe.nextElement();

                        String aux = xheader.getName().toLowerCase().trim();

                        if (aux.indexOf("subject") > -1) {
                            int pos1 = aux.indexOf("=?");
                            int pos2 = aux.indexOf("?q?");

                            if ((pos1 > -1) && (pos2 > -1)) {
                                xjcharset = aux.substring(pos1, pos2);
                            }

                            break;
                        }
                    }
                } catch (Exception ex) {
                    System.out.print(ex.getMessage());
                }

                if (xjcharset == null) {
                    xjcharset = Charset.defaultCharset().displayName(); // US-ASCII in JAVA terms
                }
            }

            MessageUtilities.decodeTextPlain(buffer, part, breakLine, xjcharset);
        }
    } else if (xctype.match("text/html") && chooseHtml) {
        if (xcdisposition.match("inline")) {
            attachIt = false;

            String xjcharset = xctype.getParameter("charset");

            if (xjcharset == null) {
                // not present, assume ASCII character encoding                       
                try {
                    Header xheader;
                    Enumeration xe = part.getAllHeaders();

                    for (; xe.hasMoreElements();) {
                        xheader = (Header) xe.nextElement();

                        String aux = xheader.getName().toLowerCase().trim();

                        if (aux.indexOf("subject") > -1) {
                            int pos1 = aux.indexOf("=?");
                            int pos2 = aux.indexOf("?q?");

                            if ((pos1 > -1) && (pos2 > -1)) {
                                xjcharset = aux.substring(pos1, pos2);
                            }

                            break;
                        }
                    }
                } catch (Exception ex) {
                }

                if (xjcharset == null) {
                    xjcharset = Charset.defaultCharset().displayName(); // US-ASCII in JAVA terms
                }
            }

            MessageUtilities.decodeTextHtml(buffer, part, xjcharset);
        }
    }

    if (attachIt) {
        // UNDONE should simple single line entries be
        //        created for other types and attachments?
        //
        // UNDONE should attachements only be created for "attachments" or all
        // unknown content types?
        if (dmailParts != null) {
            MailPart aux = new MailPart();
            aux.setPart(part);
            aux.setId(dmailParts.size());
            aux.setName(MessageUtilities.encodeStringToXml(MessageUtilities.getPartName(part)));
            aux.setContentType(xctype.getBaseType());
            aux.setSize(part.getSize());

            dmailParts.addElement(aux);
        }
    }

    return buffer;
}

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!/*from w  w  w  .j  av a  2s . com*/
 *
 * @param dmailParts DOCUMENT ME!
 * @param contentType DOCUMENT ME!
 * @param buffer DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MessagingException DOCUMENT ME!
 */
protected static StringBuffer scanDmailParts(Vector dmailParts, StringBuffer buffer, boolean chooseHtml,
        String breakLine) throws MessagingException {
    if ((buffer.length() == 0) && (dmailParts != null)) {
        int size = dmailParts.size();
        int j = 0;

        for (int i = 0; i < size; i++) {
            //message/rfc822
            MailPart dmailPart = (MailPart) dmailParts.get(j);

            //ContentType xctype = MessageUtilities.getContentType(dmailPart.getContentType());
            ContentType xctype = MessageUtilities.getContentType(dmailPart.getPart());

            if (xctype.match("text/plain") && !chooseHtml) {
                String xjcharset = xctype.getParameter("charset");

                if (xjcharset == null) {
                    // not present, assume ASCII character encoding                       
                    try {
                        Header xheader;
                        Enumeration xe = dmailPart.getPart().getAllHeaders();

                        for (; xe.hasMoreElements();) {
                            xheader = (Header) xe.nextElement();

                            String aux = xheader.getName().toLowerCase().trim();

                            if (aux.indexOf("subject") > -1) {
                                int pos1 = aux.indexOf("=?");
                                int pos2 = aux.indexOf("?q?");

                                if ((pos1 > -1) && (pos2 > -1)) {
                                    xjcharset = aux.substring(pos1, pos2);
                                }

                                break;
                            }
                        }
                    } catch (Exception ex) {
                    }

                    if (xjcharset == null) {
                        xjcharset = Charset.defaultCharset().displayName(); // US-ASCII in JAVA terms
                    }
                }

                //String str = JavaScriptFilter.apply(buff.toString());
                xjcharset = MimeUtility.javaCharset(xjcharset);

                MessageUtilities.decodeTextPlain(buffer, dmailPart.getPart(), breakLine, xjcharset);

                dmailParts.removeElementAt(j);

                break;
            } else if (xctype.match("text/html") && chooseHtml) {
                String xjcharset = xctype.getParameter("charset");

                if (xjcharset == null) {
                    // not present, assume ASCII character encoding                       
                    try {
                        Header xheader;
                        Enumeration xe = dmailPart.getPart().getAllHeaders();

                        for (; xe.hasMoreElements();) {
                            xheader = (Header) xe.nextElement();

                            String aux = xheader.getName().toLowerCase().trim();

                            if (aux.indexOf("subject") > -1) {
                                int pos1 = aux.indexOf("=?");
                                int pos2 = aux.indexOf("?q?");

                                if ((pos1 > -1) && (pos2 > -1)) {
                                    xjcharset = aux.substring(pos1, pos2);
                                }

                                break;
                            }
                        }
                    } catch (Exception ex) {
                    }

                    if (xjcharset == null) {
                        xjcharset = Charset.defaultCharset().displayName(); // US-ASCII in JAVA terms
                    }
                }

                xjcharset = MimeUtility.javaCharset(xjcharset);

                MessageUtilities.decodeTextHtml(buffer, dmailPart.getPart(), xjcharset);

                dmailParts.removeElementAt(j);

                break;
            } else {
                j++;
            }
        }
    }

    return buffer;
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * this method returns the text content of the message as a list of strings
 * // each element of the list could be the content of a multipart message
 * // m is the top level subject//from  w  ww  .j  a v a  2  s. com
 * // p is the specific part that we are processing (p could be == m)
 * also sets up names of attachments (though it will not download the
 * attachment unless downloadAttachments is true)
 */
private List<String> processMessagePart(int messageNum, Message m, Part p, List<Blob> attachmentsList)
        throws MessagingException, IOException {
    List<String> list = new ArrayList<String>(); // return list
    if (p == null) {
        dataErrors.add("part is null: " + folder_name() + " idx " + messageNum);
        return list;
    }

    if (p == m && p.isMimeType("text/html")) {
        /*
        String s = "top level part is html! message:" + m.getSubject() + " " + m.getDescription();
        dataErrors.add(s);
        */
        // we don't normally expect the top-level part to have content-type text/html
        // but we saw this happen on some sample archives pst -> emailchemy. so allow it and handle it by parsing the html
        String html = (String) p.getContent();
        String text = Util.unescapeHTML(html);
        org.jsoup.nodes.Document doc = Jsoup.parse(text);

        StringBuilder sb = new StringBuilder();
        HTMLUtils.extractTextFromHTML(doc.body(), sb);
        list.add(sb.toString());
        return list;
    }

    if (p.isMimeType("text/plain")) {
        //make sure, p is not wrongly labelled as plain text.
        Enumeration headers = p.getAllHeaders();
        boolean dirty = false;
        if (headers != null)
            while (headers.hasMoreElements()) {
                Header h = (Header) headers.nextElement();
                String name = h.getName();
                String value = h.getValue();
                if (name != null && value != null) {
                    if (name.equals("Content-transfer-encoding") && value.equals("base64")) {
                        dirty = true;
                        break;
                    }
                }
            }
        String fname = p.getFileName();
        if (fname != null) {
            int idx = fname.lastIndexOf('.');
            if ((idx < fname.length()) && (idx >= 0)) {
                String extension = fname.substring(idx);
                //anything extension other than .txt is suspicious.
                if (!extension.equals(".txt"))
                    dirty = true;
            }
        }
        if (dirty) {
            dataErrors.add("Dirty message part, has conflicting message part headers." + folder_name()
                    + " Message# " + messageNum);
            return list;
        }

        log.debug("Message part with content type text/plain");
        String content;
        String type = p.getContentType(); // new InputStreamReader(p.getInputStream(), "UTF-8");
        try {
            // if forced encoding is set, we read the string with that encoding, otherwise we just use whatever p.getContent gives us
            if (FORCED_ENCODING != null) {
                byte b[] = Util.getBytesFromStream(p.getInputStream());
                content = new String(b, FORCED_ENCODING);
            } else
                content = (String) p.getContent();
        } catch (UnsupportedEncodingException uee) {
            dataErrors.add("Unsupported encoding: " + folder_name() + " Message #" + messageNum + " type "
                    + type + ", using brute force conversion");
            // a particularly nasty issue:javamail can't handle utf-7 encoding which is common with hotmail and exchange servers.
            // we're using the workaround suggested on this page: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304013
            // though it may be better to consider official support for utf-7 or other encodings.

            // TOFIX: I get an exception for utfutf8-encoding which has a base64 encoding embedded on it.
            // Unsupported encoding: gmail-sent Message #10477 type text/plain; charset=x-utf8utf8; name="newyorker.txt",
            // the hack below doesn't work for it.
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            p.writeTo(bao);
            content = bao.toString();
        }
        list.add(content);
    } else if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) {
        // rfc822 mime type is for embedded mbox format or some such (appears for things like
        // forwarded messages). the content appears to be just a multipart.
        Object o = p.getContent();
        if (o instanceof Multipart) {
            Multipart allParts = (Multipart) o;
            if (p.isMimeType("multipart/alternative")) {
                // this is an alternative mime type. v common case to have text and html alternatives
                // so just process the text part if there is one, and avoid fetching the alternatives.
                // useful esp. because many ordinary messages are alternative: text and html and we don't want to fetch the html.
                // revisit in future we want to retain the html alternative for display purposes
                Part[] parts = new Part[allParts.getCount()];
                for (int i = 0; i < parts.length; i++)
                    parts[i] = allParts.getBodyPart(i);

                for (int i = 0; i < parts.length; i++) {
                    Part thisPart = parts[i];
                    if (thisPart.isMimeType("text/plain")) {
                        // common case, return quickly
                        list.add((String) thisPart.getContent());
                        log.debug("Multipart/alternative with content type text/plain");
                        return list;
                    }
                }

                // no text part, let's look for an html part. this happens for html parts.
                for (int i = 0; i < allParts.getCount(); i++) {
                    Part thisPart = parts[i];
                    if (thisPart.isMimeType("text/html")) {
                        // common case, return quickly
                        String html = (String) thisPart.getContent();
                        String text = Util.unescapeHTML(html);
                        org.jsoup.nodes.Document doc = Jsoup.parse(text);

                        StringBuilder sb = new StringBuilder();
                        HTMLUtils.extractTextFromHTML(doc.body(), sb);
                        list.add(sb.toString());

                        log.debug("Multipart/alternative with content type text/html");
                        return list;
                    }
                }

                // no text or html part. hmmm... blindly process the first part only
                if (allParts.getCount() >= 1)
                    list.addAll(processMessagePart(messageNum, m, allParts.getBodyPart(0), attachmentsList));
            } else {
                // process it like a regular multipart
                for (int i = 0; i < allParts.getCount(); i++) {
                    BodyPart bp = allParts.getBodyPart(i);
                    list.addAll(processMessagePart(messageNum, m, bp, attachmentsList));
                }
            }
        } else if (o instanceof Part)
            list.addAll(processMessagePart(messageNum, m, (Part) o, attachmentsList));
        else
            dataErrors.add("Unhandled part content, " + folder_name() + " Message #" + messageNum
                    + "Java type: " + o.getClass() + " Content-Type: " + p.getContentType());
    } else {
        try {
            // do attachments only if downloadAttachments is set.
            // some apps do not need attachments, so this saves some time.
            // however, it seems like a lot of time is taken in imap prefetch, which gets attachments too?
            if (fetchConfig.downloadAttachments)
                handleAttachments(messageNum, m, p, list, attachmentsList);
        } catch (Exception e) {
            dataErrors.add("Ignoring attachment for " + folder_name() + " Message #" + messageNum + ": "
                    + Util.stackTrace(e));
        }
    }

    return list;
}

From source file:com.sonicle.webtop.mail.Service.java

public void processGetSource(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
    MailAccount account = getAccount(request);
    String foldername = request.getParameter("folder");
    String uid = request.getParameter("id");
    String sheaders = request.getParameter("headers");
    boolean headers = sheaders.equals("true");
    String sout = null;//  ww w  .  j a  va  2 s.  c om
    try {
        account.checkStoreConnected();
        //StringBuffer sb = new StringBuffer("<pre>");
        StringBuffer sb = new StringBuffer();
        FolderCache mcache = account.getFolderCache(foldername);
        Message msg = mcache.getMessage(Long.parseLong(uid));
        //Folder folder=msg.getFolder();
        for (Enumeration e = msg.getAllHeaders(); e.hasMoreElements();) {
            Header header = (Header) e.nextElement();
            //sb.append(MailUtils.htmlescape(header.getName()) + ": " + MailUtils.htmlescape(header.getValue()) + "\n");
            sb.append(header.getName() + ": " + header.getValue() + "\n");
        }
        if (!headers) {
            BufferedReader br = new BufferedReader(new InputStreamReader(msg.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                //sb.append(MailUtils.htmlescape(line) + "\n");
                sb.append(line + "\n");
            }
        }
        //sb.append("</pre>");
        sout = "{\nresult: true, source: '" + StringEscapeUtils.escapeEcmaScript(sb.toString()) + "'\n}";
    } catch (Exception exc) {
        Service.logger.error("Exception", exc);
        sout = "{\nresult: false, text:'" + StringEscapeUtils.escapeEcmaScript(exc.getMessage()) + "'\n}";
    }
    out.println(sout);
}

From source file:org.alfresco.repo.imap.ImapServiceImpl.java

@SuppressWarnings("unchecked")
public void persistMessageHeaders(NodeRef messageRef, MimeMessage message) {
    try {//  ww  w.ja v  a  2 s  .  co  m
        Enumeration<Header> headers = message.getAllHeaders();
        List<String> messaheHeadersProperties = new ArrayList<String>();
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            if (isPersistableHeader(header)) {
                messaheHeadersProperties.add(
                        header.getName() + ImapModel.MESSAGE_HEADER_TO_PERSIST_SPLITTER + header.getValue());

                if (logger.isDebugEnabled()) {
                    logger.debug("[persistHeaders] Persisting Header " + header.getName() + " : "
                            + header.getValue());
                }
            }
        }

        Map<QName, Serializable> props = new HashMap<QName, Serializable>();
        props.put(ImapModel.PROP_MESSAGE_HEADERS, (Serializable) messaheHeadersProperties);

        serviceRegistry.getNodeService().addAspect(messageRef, ImapModel.ASPECT_IMAP_MESSAGE_HEADERS, props);
    } catch (MessagingException me) {

    }
}

From source file:org.alfresco.repo.imap.ImapServiceImpl.java

private boolean isPersistableHeader(Header header) {
    for (String headerToPersist : messageHeadersToPersist) {
        if (headerToPersist.equalsIgnoreCase(header.getName())) {
            return true;
        }//from  w w  w . j a v  a  2s  .c  om
    }
    return false;
}

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

/**
 * Gets the header length for any part./*from w w w . j  a v  a 2  s .  co  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.axis.attachments.MultiPartRelatedInputStream.java

/**
 * Create a new Multipart stream.//from ww  w. jav  a  2 s  .  c o m
 * @param contentType  the string that holds the contentType
 * @param stream       the true input stream from where the source
 *
 * @throws org.apache.axis.AxisFault if the stream could not be created
 */
public MultiPartRelatedInputStream(String contentType, java.io.InputStream stream)
        throws org.apache.axis.AxisFault {

    super(null); // don't cache this stream.

    if (!(stream instanceof BufferedInputStream)) {
        stream = new BufferedInputStream(stream);
    }

    try {

        // First find the start and boundary parameters. There are real weird rules regard what
        // can be in real headers what needs to be escaped etc  let mail parse it.
        javax.mail.internet.ContentType ct = new javax.mail.internet.ContentType(contentType);
        String rootPartContentId = ct.getParameter("start"); // Get the root part content.

        if (rootPartContentId != null) {
            rootPartContentId = rootPartContentId.trim();

            if (rootPartContentId.startsWith("<")) {
                rootPartContentId = rootPartContentId.substring(1);
            }

            if (rootPartContentId.endsWith(">")) {
                rootPartContentId = rootPartContentId.substring(0, rootPartContentId.length() - 1);
            }

        }

        if (ct.getParameter("boundary") != null) {
            String boundaryStr = "--" + ct.getParameter("boundary"); // The boundary with -- add as always the case.

            // if start is null then the first attachment is the rootpart
            // First read the start boundary -- this is done with brute force since the servlet may swallow the crlf between headers.
            // after this we use the more efficient boundarydelimeted stream.  There should never be any data here anyway.
            byte[][] boundaryMarker = new byte[2][boundaryStr.length() + 2];

            IOUtils.readFully(stream, boundaryMarker[0]);

            boundary = (boundaryStr + "\r\n").getBytes("US-ASCII");

            int current = 0;

            // This just goes brute force one byte at a time to find the first boundary.
            // in most cases this just a crlf.
            for (boolean found = false; !found; ++current) {
                if (!(found = java.util.Arrays.equals(boundaryMarker[current & 0x1], boundary))) {
                    System.arraycopy(boundaryMarker[current & 0x1], 1, boundaryMarker[(current + 1) & 0x1], 0,
                            boundaryMarker[0].length - 1);

                    if (stream.read(boundaryMarker[(current + 1) & 0x1], boundaryMarker[0].length - 1, 1) < 1) {
                        throw new org.apache.axis.AxisFault(
                                Messages.getMessage("mimeErrorNoBoundary", new String(boundary)));
                    }
                }
            }

            // after the first boundary each boundary will have a cr lf at the beginning since after the data in any part there
            // is a cr lf added to put the boundary at the begining of a line.
            boundaryStr = "\r\n" + boundaryStr;
            boundary = boundaryStr.getBytes("US-ASCII");
        } else {
            // Since boundary is not specified, we try to find one.
            for (boolean found = false; !found;) {
                boundary = readLine(stream);
                if (boundary == null)
                    throw new org.apache.axis.AxisFault(Messages.getMessage("mimeErrorNoBoundary", "--"));
                found = boundary.length > 4 && boundary[2] == '-' && boundary[3] == '-';
            }
        }

        // create the boundary delmited stream.
        boundaryDelimitedStream = new org.apache.axis.attachments.BoundaryDelimitedStream(stream, boundary,
                1024);

        // Now read through all potential streams until we have found the root part.
        String contentTransferEncoding = null;

        do {
            contentId = null;
            contentLocation = null;
            contentTransferEncoding = null;

            // Read this attachments headers from the stream.
            javax.mail.internet.InternetHeaders headers = new javax.mail.internet.InternetHeaders(
                    boundaryDelimitedStream);

            // Use java mail utility to read through the headers.
            contentId = headers.getHeader(HTTPConstants.HEADER_CONTENT_ID, null);

            // Clean up the headers and remove any < >
            if (contentId != null) {
                contentId = contentId.trim();

                if (contentId.startsWith("<")) {
                    contentId = contentId.substring(1);
                }

                if (contentId.endsWith(">")) {
                    contentId = contentId.substring(0, contentId.length() - 1);
                }

                contentId = contentId.trim();

                //  if (!contentId.startsWith("cid:")) {
                //      contentId =
                //              "cid:"
                //              + contentId;        // make sure its identified as cid
                //  }
            }

            contentLocation = headers.getHeader(HTTPConstants.HEADER_CONTENT_LOCATION, null);

            if (contentLocation != null) {
                contentLocation = contentLocation.trim();

                if (contentLocation.startsWith("<")) {
                    contentLocation = contentLocation.substring(1);
                }

                if (contentLocation.endsWith(">")) {
                    contentLocation = contentLocation.substring(0, contentLocation.length() - 1);
                }

                contentLocation = contentLocation.trim();
            }

            contentType = headers.getHeader(HTTPConstants.HEADER_CONTENT_TYPE, null);

            if (contentType != null) {
                contentType = contentType.trim();
            }

            contentTransferEncoding = headers.getHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, null);

            if (contentTransferEncoding != null) {
                contentTransferEncoding = contentTransferEncoding.trim();
            }

            java.io.InputStream decodedStream = boundaryDelimitedStream;

            if ((contentTransferEncoding != null) && (0 != contentTransferEncoding.length())) {
                decodedStream = MimeUtility.decode(decodedStream, contentTransferEncoding);
            }

            if ((rootPartContentId != null) && !rootPartContentId.equals(contentId)) { // This is a part that has come in prior to the root part. Need to buffer it up.
                javax.activation.DataHandler dh = new javax.activation.DataHandler(
                        new org.apache.axis.attachments.ManagedMemoryDataSource(decodedStream, MAX_CACHED,
                                contentType, true));
                AttachmentPart ap = new AttachmentPart(dh);

                if (contentId != null) {
                    ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, contentId);
                }

                if (contentLocation != null) {
                    ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, contentLocation);
                }

                for (java.util.Enumeration en = headers.getNonMatchingHeaders(
                        new String[] { HTTPConstants.HEADER_CONTENT_ID, HTTPConstants.HEADER_CONTENT_LOCATION,
                                HTTPConstants.HEADER_CONTENT_TYPE }); en.hasMoreElements();) {
                    javax.mail.Header header = (javax.mail.Header) en.nextElement();
                    String name = header.getName();
                    String value = header.getValue();

                    if ((name != null) && (value != null)) {
                        name = name.trim();

                        if (name.length() != 0) {
                            ap.addMimeHeader(name, value);
                        }
                    }
                }

                addPart(contentId, contentLocation, ap);

                boundaryDelimitedStream = boundaryDelimitedStream.getNextStream(); // Gets the next stream.
            }
        } while ((null != boundaryDelimitedStream) && (rootPartContentId != null)
                && !rootPartContentId.equals(contentId));

        if (boundaryDelimitedStream == null) {
            throw new org.apache.axis.AxisFault(Messages.getMessage("noRoot", rootPartContentId));
        }

        soapStreamBDS = boundaryDelimitedStream;

        if ((contentTransferEncoding != null) && (0 != contentTransferEncoding.length())) {
            soapStream = MimeUtility.decode(boundaryDelimitedStream, contentTransferEncoding);
        } else {
            soapStream = boundaryDelimitedStream; // This should be the SOAP part
        }

        // Read from the input stream all attachments prior to the root part.
    } catch (javax.mail.internet.ParseException e) {
        throw new org.apache.axis.AxisFault(Messages.getMessage("mimeErrorParsing", e.getMessage()));
    } catch (java.io.IOException e) {
        throw new org.apache.axis.AxisFault(Messages.getMessage("readError", e.getMessage()));
    } catch (javax.mail.MessagingException e) {
        throw new org.apache.axis.AxisFault(Messages.getMessage("readError", e.getMessage()));
    }
}