Java Code Examples for javax.mail.internet.MimePart#getHeader()

The following examples show how to use javax.mail.internet.MimePart#getHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MimePackage.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Get the specified header value from a specified MIME part
 *
 * @param headerName
 * @param partNum
 * @param headerIndex
 * @return
 * @throws PackageException
 */
@PublicAtsApi
public String getPartHeader(
                             String headerName,
                             int partNum,
                             int headerIndex ) throws PackageException {

    try {
        String[] headers;
        if (partNum >= parts.size()) {
            throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'");
        }

        MimePart part = parts.get(partNum);
        headers = part.getHeader(headerName);

        if ( (headers != null) && (headers.length > headerIndex)) {
            return headers[headerIndex];
        } else {
            throw new NoSuchHeaderException(headerName, partNum, headerIndex);
        }
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
 
Example 2
Source File: MimePackage.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Get all header values from a specified MIME part
 *
 * @param headerName
 * @param partNum
 * @return
 * @throws PackageException
 */
@PublicAtsApi
public String[] getPartHeaderValues(
                                     String headerName,
                                     int partNum ) throws PackageException {

    try {
        String[] headers;
        if (partNum >= parts.size()) {
            throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'");
        }

        MimePart part = parts.get(partNum);
        headers = part.getHeader(headerName);

        if ( (headers != null) && (headers.length > 0)) {
            return headers;
        } else {
            throw new NoSuchHeaderException(headerName, partNum);
        }
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
 
Example 3
Source File: HttpResponse.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Get the response body as a XML Document. Note that Content-Type must indicate
 * that the body is XML, e.g. "text/xml", "application/soap+xml".
 * For multipart messages, the first part that has a Content-Type that indicates XML, (if any),
 * will be converted to a XML Document and returned.
 *
 * @return The body as a XML Document
 * @throws HttpException
 */
@PublicAtsApi
public Document getBodyAsXML() throws HttpException {

    if (body == null) {
        return null;
    }

    String contentType = null;
    for (HttpHeader header : headers) {
        if ("Content-Type".equalsIgnoreCase(header.getKey())) {
            contentType = header.getValue();
            break;
        }
    }

    if (contentType != null) {
        if (contentType.contains("xml")) {
            return getDocument(body);
        } else if (contentType.contains("multipart")) {
            // Get the first part of the multipart that has "xml" in its
            // Content-Type header as a Document.
            try {
                InputStream is = new ByteArrayInputStream(body);
                MimePackage mime = new MimePackage(is);
                List<MimePart> parts = mime.getMimeParts();
                for (MimePart part : parts) {
                    String[] partContentTypes = part.getHeader("Content-Type");
                    for (String partContentType : partContentTypes) {
                        if (partContentType.contains("xml")) {
                            // We have an XML document
                            MimeBodyPart p = (MimeBodyPart) part;
                            return getDocument(IOUtils.toByteArray(p.getRawInputStream()));
                        }
                    }
                }
            } catch (Exception e) {
                throw new HttpException("Error trying to extract main XML document from multipart message.",
                                        e);
            }
        }
    }

    return null;
}
 
Example 4
Source File: HttpResponse.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Get the response body as a XMLText object. Note that Content-Type must indicate
 * that the body is XML, e.g. "text/xml", "application/soap+xml".
 * For multipart messages, the first part that has a Content-Type that indicates XML, (if any),
 * will be converted to a XMLText object and returned.
 *
 * @return The body as a XMLText object
 * @throws HTTPException
 */

@PublicAtsApi
public XmlText getBodyAsXmlText() {

    if (body == null) {
        return null;
    }

    String contentType = null;
    for (HttpHeader header : headers) {
        if (header.getKey().equalsIgnoreCase("Content-Type")) {
            contentType = header.getValue();
            break;
        }
    }

    if (contentType != null) {
        if (contentType.contains("xml")) {
            return new XmlText(new String(body));
        } else if (contentType.contains("multipart")) {
            // Get the first part of the multipart that has "xml" in its
            // Content-Type header as a XMLText.
            try {
                InputStream is = new ByteArrayInputStream(body);
                MimePackage mime = new MimePackage(is);
                List<MimePart> parts = mime.getMimeParts();
                for (MimePart part : parts) {
                    String[] partContentTypes = part.getHeader("Content-Type");
                    for (String partContentType : partContentTypes) {
                        if (partContentType.contains("xml")) {
                            // We have an XMLText object
                            MimeBodyPart p = (MimeBodyPart) part;
                            return new XmlText(new String(IOUtils.toByteArray(p.getRawInputStream())));
                        }
                    }
                }
            } catch (Exception e) {
                throw new HttpException("Error while trying to convert multipart message's content to a XMLText object",
                                        e);
            }
        }
    }

    return null;

}
 
Example 5
Source File: HttpResponse.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Get the response body as a JSONText object. Note that Content-Type must indicate
 * that the body is JSON, e.g. "application/json".
 * For multipart messages, the first part that has a Content-Type that indicates JSON, (if any),
 * will be converted to a JSONText object and returned.
 *
 * @return The body as a JSONText object
 * @throws HTTPException
 */

@PublicAtsApi
public JsonText getBodyAsJsonText() {

    if (body == null) {
        return null;
    }

    String contentType = null;
    for (HttpHeader header : headers) {
        if (header.getKey().equalsIgnoreCase("Content-Type")) {
            contentType = header.getValue();
            break;
        }
    }

    if (contentType != null) {
        if (contentType.contains("json")) {
            return new JsonText(new String(body));
        } else if (contentType.contains("multipart")) {
            // Get the first part of the multipart that has "json" in its
            // Content-Type header as a JSONText.
            try {
                InputStream is = new ByteArrayInputStream(body);
                MimePackage mime = new MimePackage(is);
                List<MimePart> parts = mime.getMimeParts();
                for (MimePart part : parts) {
                    String[] partContentTypes = part.getHeader("Content-Type");
                    for (String partContentType : partContentTypes) {
                        if (partContentType.contains("json")) {
                            // We have an JSONText object
                            MimeBodyPart p = (MimeBodyPart) part;
                            return new JsonText(new String(IOUtils.toByteArray(p.getRawInputStream())));
                        }
                    }
                }
            } catch (Exception e) {
                throw new HttpException("Error while trying to convert multipart message's content to a JSONText object",
                                        e);
            }
        }
    }

    return null;

}