Example usage for javax.mail Part setDisposition

List of usage examples for javax.mail Part setDisposition

Introduction

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

Prototype

public void setDisposition(String disposition) throws MessagingException;

Source Link

Document

Set the disposition of this part.

Usage

From source file:org.orbeon.oxf.processor.EmailProcessor.java

private void handlePart(PipelineContext pipelineContext, String dataInputSystemId, Part parentPart,
        Element partOrBodyElement) throws Exception {
    final String name = partOrBodyElement.attributeValue("name");
    String contentTypeAttribute = partOrBodyElement.attributeValue("content-type");
    final String contentType = NetUtils.getContentTypeMediaType(contentTypeAttribute);
    final String charset;
    {//from   ww  w.  j a v a2s. c  om
        final String c = NetUtils.getContentTypeCharset(contentTypeAttribute);
        charset = (c != null) ? c : DEFAULT_CHARACTER_ENCODING;
    }
    final String contentTypeWithCharset = contentType + "; charset=" + charset;
    final String src = partOrBodyElement.attributeValue("src");

    // Either a String or a FileItem
    final Object content;
    if (src != null) {
        // Content of the part is not inline

        // Generate a FileItem from the source
        final SAXSource source = getSAXSource(EmailProcessor.this, pipelineContext, src, dataInputSystemId,
                contentType);
        content = handleStreamedPartContent(pipelineContext, source);
    } else {
        // Content of the part is inline

        // In the cases of text/html and XML, there must be exactly one root element
        final boolean needsRootElement = "text/html".equals(contentType);// || ProcessorUtils.isXMLContentType(contentType);
        if (needsRootElement && partOrBodyElement.elements().size() != 1)
            throw new ValidationException(
                    "The <body> or <part> element must contain exactly one element for text/html",
                    (LocationData) partOrBodyElement.getData());

        // Create Document and convert it into a String
        final Element rootElement = (Element) (needsRootElement ? partOrBodyElement.elements().get(0)
                : partOrBodyElement);
        final Document partDocument = new NonLazyUserDataDocument();
        partDocument.setRootElement((Element) rootElement.clone());
        content = handleInlinePartContent(partDocument, contentType);
    }

    if (!XMLUtils.isTextOrJSONContentType(contentType)) {
        // This is binary content (including application/xml)
        if (content instanceof FileItem) {
            final FileItem fileItem = (FileItem) content;
            parentPart.setDataHandler(new DataHandler(new DataSource() {
                public String getContentType() {
                    return contentType;
                }

                public InputStream getInputStream() throws IOException {
                    return fileItem.getInputStream();
                }

                public String getName() {
                    return name;
                }

                public OutputStream getOutputStream() throws IOException {
                    throw new IOException("Write operation not supported");
                }
            }));
        } else {
            byte[] data = NetUtils.base64StringToByteArray((String) content);
            parentPart.setDataHandler(new DataHandler(new SimpleBinaryDataSource(name, contentType, data)));
        }
    } else {
        // This is text content (including text/xml)
        if (content instanceof FileItem) {
            // The text content was encoded when written to the FileItem
            final FileItem fileItem = (FileItem) content;
            parentPart.setDataHandler(new DataHandler(new DataSource() {
                public String getContentType() {
                    // This always contains a charset
                    return contentTypeWithCharset;
                }

                public InputStream getInputStream() throws IOException {
                    // This is encoded with the appropriate charset (user-defined, or the default)
                    return fileItem.getInputStream();
                }

                public String getName() {
                    return name;
                }

                public OutputStream getOutputStream() throws IOException {
                    throw new IOException("Write operation not supported");
                }
            }));
        } else {
            parentPart.setDataHandler(
                    new DataHandler(new SimpleTextDataSource(name, contentTypeWithCharset, (String) content)));
        }
    }

    // Set content-disposition header
    String contentDisposition = partOrBodyElement.attributeValue("content-disposition");
    if (contentDisposition != null)
        parentPart.setDisposition(contentDisposition);

    // Set content-id header
    String contentId = partOrBodyElement.attributeValue("content-id");
    if (contentId != null)
        parentPart.setHeader("content-id", "<" + contentId + ">");
    //part.setContentID(contentId);
}