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:org.exist.xquery.modules.mail.MessageListFunctions.java

private Sequence getMessageListAsXML(Sequence[] args, Sequence contextSequence) throws XPathException {
    Message[] msgList;/*from  w  w  w.ja  v a2s. c o m*/
    Sequence ret = Sequence.EMPTY_SEQUENCE;

    // was a msgList handle specified?
    if (args[0].isEmpty()) {
        throw (new XPathException(this, "Message List handle not specified"));
    }

    // get the MessageList
    long msgListHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
    msgList = MailModule.retrieveMessageList(context, msgListHandle);
    if (msgList == null) {
        throw (new XPathException(this, "Invalid Message List handle specified"));
    }

    if (msgList.length > 0) {

        boolean includeHeaders = args[1].effectiveBooleanValue();

        MemTreeBuilder builder = context.getDocumentBuilder();

        builder.startDocument();
        builder.startElement(new QName("messages", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        builder.addAttribute(new QName("count", null, null), String.valueOf(msgList.length));

        try {
            for (int i = 0; i < msgList.length; i++) {
                Message message = msgList[i];

                builder.startElement(new QName("message", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);

                builder.addAttribute(new QName("number", null, null),
                        String.valueOf(message.getMessageNumber()));

                // Sent Date
                if (message.getSentDate() != null) {
                    builder.startElement(new QName("sent", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                    builder.characters(formatDate(message.getSentDate()));
                    builder.endElement();
                }

                // Received Date
                if (message.getReceivedDate() != null) {
                    builder.startElement(new QName("received", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                            null);
                    builder.characters(formatDate(message.getReceivedDate()));
                    builder.endElement();
                }

                // From
                if (message.getFrom() != null) {
                    builder.startElement(new QName("from", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                    builder.characters(message.getFrom()[0].toString());
                    builder.endElement();
                }

                // Recipients
                builder.startElement(new QName("recipients", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                        null);
                // To Recipients
                Address[] toAddresses = message.getRecipients(Message.RecipientType.TO);
                if (toAddresses != null) {
                    for (Address to : toAddresses) {
                        builder.startElement(
                                new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                        builder.addAttribute(new QName("type", null, null), "to");
                        builder.characters(to.toString());
                        builder.endElement();
                    }
                }

                // cc Recipients
                Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC);
                if (ccAddresses != null) {
                    for (Address ccAddress : ccAddresses) {
                        builder.startElement(
                                new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                        builder.addAttribute(new QName("type", null, null), "cc");
                        builder.characters(ccAddress.toString());
                        builder.endElement();
                    }
                }

                // bcc Recipients
                Address[] bccAddresses = message.getRecipients(Message.RecipientType.BCC);
                if (bccAddresses != null) {
                    for (Address bccAddress : bccAddresses) {
                        builder.startElement(
                                new QName("recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                        builder.addAttribute(new QName("type", null, null), "bcc");
                        builder.characters(bccAddress.toString());
                        builder.endElement();
                    }
                }
                builder.endElement();

                // Flags

                Flags flags = message.getFlags();
                Flags.Flag[] systemFlags = flags.getSystemFlags();
                String[] userFlags = flags.getUserFlags();

                if (systemFlags.length > 0 || userFlags.length > 0) {
                    builder.startElement(new QName("flags", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);

                    for (Flags.Flag systemFlag : systemFlags) {
                        if (systemFlag == Flags.Flag.ANSWERED) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "answered");
                            builder.endElement();
                        } else if (systemFlag == Flags.Flag.DELETED) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "deleted");
                            builder.endElement();
                        } else if (systemFlag == Flags.Flag.DRAFT) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "draft");
                            builder.endElement();
                        } else if (systemFlag == Flags.Flag.FLAGGED) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "flagged");
                            builder.endElement();
                        } else if (systemFlag == Flags.Flag.RECENT) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "recent");
                            builder.endElement();
                        } else if (systemFlag == Flags.Flag.SEEN) {
                            builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                    null);
                            builder.addAttribute(new QName("type", null, null), "seen");
                            builder.endElement();
                        }
                    }

                    for (String userFlag : userFlags) {
                        builder.startElement(new QName("flag", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                null);
                        builder.addAttribute(new QName("type", null, null), "user");
                        builder.addAttribute(new QName("value", null, null), userFlag);
                        builder.endElement();
                    }

                    builder.endElement();
                }

                // Headers

                if (includeHeaders) {
                    Enumeration headers = message.getAllHeaders();

                    if (headers.hasMoreElements()) {
                        builder.startElement(new QName("headers", MailModule.NAMESPACE_URI, MailModule.PREFIX),
                                null);

                        while (headers.hasMoreElements()) {
                            Header header = (Header) headers.nextElement();

                            builder.startElement(
                                    new QName("header", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                            builder.addAttribute(new QName("name", null, null), header.getName());
                            builder.addAttribute(new QName("value", null, null), header.getValue());
                            builder.endElement();
                        }

                        builder.endElement();
                    }
                }

                // Subject
                builder.startElement(new QName("subject", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
                builder.characters(message.getSubject());
                builder.endElement();

                builder.endElement();
            }
        } catch (MessagingException me) {
            throw (new XPathException(this, "Failed to retrieve messages from list", me));
        }

        builder.endElement();

        ret = (NodeValue) builder.getDocument().getDocumentElement();
    }

    return (ret);
}

From source file:org.mule.module.http.internal.HttpParser.java

public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType)
        throws IOException {
    MimeMultipart mimeMultipart = null;// w  w w.  j  av  a2 s .  c o  m
    List<HttpPart> parts = Lists.newArrayList();

    try {
        mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType));
    } catch (MessagingException e) {
        throw new IOException(e);
    }

    try {
        int partCount = mimeMultipart.getCount();

        for (int i = 0; i < partCount; i++) {
            BodyPart part = mimeMultipart.getBodyPart(i);

            String filename = part.getFileName();
            String partName = filename;
            String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION_PART_HEADER);
            if (contentDispositions != null) {
                String contentDisposition = contentDispositions[0];
                if (contentDisposition.contains(NAME_ATTRIBUTE)) {
                    partName = contentDisposition.substring(
                            contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2);
                    partName = partName.substring(0, partName.indexOf("\""));
                }
            }
            HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()),
                    part.getContentType(), part.getSize());

            Enumeration<Header> headers = part.getAllHeaders();

            while (headers.hasMoreElements()) {
                Header header = headers.nextElement();
                httpPart.addHeader(header.getName(), header.getValue());
            }
            parts.add(httpPart);
        }
    } catch (MessagingException e) {
        throw new IOException(e);
    }

    return parts;
}

From source file:org.mule.service.http.impl.service.server.grizzly.HttpParser.java

public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType)
        throws IOException {
    MimeMultipart mimeMultipart = null;// ww  w  . j  av  a2 s  .  c om
    List<HttpPart> parts = Lists.newArrayList();

    try {
        mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType));
    } catch (MessagingException e) {
        throw new IOException(e);
    }

    try {
        int partCount = mimeMultipart.getCount();

        for (int i = 0; i < partCount; i++) {
            BodyPart part = mimeMultipart.getBodyPart(i);

            String filename = part.getFileName();
            String partName = filename;
            String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION);
            if (contentDispositions != null) {
                String contentDisposition = contentDispositions[0];
                if (contentDisposition.contains(NAME_ATTRIBUTE)) {
                    partName = contentDisposition.substring(
                            contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2);
                    partName = partName.substring(0, partName.indexOf("\""));
                }
            }

            if (partName == null && mimeMultipart.getContentType().contains(MULTIPART_RELATED.toString())) {
                String[] contentIdHeader = part.getHeader(CONTENT_ID);
                if (contentIdHeader != null && contentIdHeader.length > 0) {
                    partName = contentIdHeader[0];
                }
            }

            HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()),
                    part.getContentType(), part.getSize());

            Enumeration<Header> headers = part.getAllHeaders();

            while (headers.hasMoreElements()) {
                Header header = headers.nextElement();
                httpPart.addHeader(header.getName(), header.getValue());
            }
            parts.add(httpPart);
        }
    } catch (MessagingException e) {
        throw new IOException(e);
    }

    return parts;
}

From source file:org.mule.transport.email.MailMuleMessageFactory.java

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void addMailHeadersToMessageProperties(Message mailMessage, DefaultMuleMessage muleMessage)
        throws MessagingException {
    for (Enumeration<?> e = mailMessage.getAllHeaders(); e.hasMoreElements();) {
        Header header = (Header) e.nextElement();

        String name = header.getName();
        String listName = MailUtils.toListHeader(name);
        String value = header.getValue();

        if (null == muleMessage.getOutboundProperty(name)) {
            muleMessage.setInboundProperty(name, value);
        }/*from  w  w w .j a  v a  2 s . co  m*/

        Object listPropertyValue = muleMessage.getOutboundProperty(listName);
        if (null == listPropertyValue) {
            listPropertyValue = new LinkedList<Object>();
            muleMessage.setInboundProperty(listName, listPropertyValue);
        }
        if (listPropertyValue instanceof List<?>) {
            ((List) listPropertyValue).add(header.getValue());
        }
    }
}

From source file:org.mule.transport.email.MailMuleMessageFactory.java

protected void addAttachmentHeaders(String name, Part part, MuleMessage muleMessage)
        throws javax.mail.MessagingException {
    Map<String, String> headers = new HashMap<String, String>();
    for (Enumeration<?> e = part.getAllHeaders(); e.hasMoreElements();) {
        Header h = (Header) e.nextElement();
        headers.put(h.getName(), h.getValue());
    }//from  w w  w . j  a  va 2s . c  o m

    if (headers.size() > 0) {
        muleMessage.setProperty(name + AbstractMailConnector.ATTACHMENT_HEADERS_PROPERTY_POSTFIX, headers,
                PropertyScope.INBOUND);
    }
}

From source file:org.openhim.mediator.normalization.XDSbMimeProcessorActor.java

private Enumeration createCopyOfHeaders(BodyPart part) throws MessagingException {
    Enumeration headers = part.getAllHeaders();
    InternetHeaders internetHeaders = new InternetHeaders();
    while (headers.hasMoreElements()) {
        Header header = (Header) headers.nextElement();
        internetHeaders.addHeader(header.getName(), header.getValue());
    }// ww w . j a  v a2  s .c  o  m
    return internetHeaders.getAllHeaders();
}

From source file:org.openhim.mediator.normalization.XDSbMimeProcessorActor.java

private void copyHeadersToPart(Enumeration headers, BodyPart part) throws MessagingException {
    while (headers.hasMoreElements()) {
        Header header = (Header) headers.nextElement();
        part.setHeader(header.getName(), header.getValue());
    }/*  w ww  . java2  s  .  c o m*/
}

From source file:org.sakaiproject.nakamura.smtp.SakaiSmtpServer.java

@SuppressWarnings("unchecked")
private Content writeMessage(Session session, Map<String, Object> mapProperties, InputStream data,
        String storePath)/*from w  ww  .  j  a  v  a 2 s .  c o m*/
        throws MessagingException, AccessDeniedException, StorageClientException, IOException {
    InternetHeaders internetHeaders = new InternetHeaders(data);
    // process the headers into a map.
    for (Enumeration<Header> e = internetHeaders.getAllHeaders(); e.hasMoreElements();) {
        Header h = e.nextElement();
        String name = h.getName();
        String[] values = internetHeaders.getHeader(name);
        if (values != null) {
            if (values.length == 1) {
                mapProperties.put("sakai:" + name.toLowerCase(), values[0]);
            } else {
                mapProperties.put("sakai:" + name.toLowerCase(), values);
            }
        }
    }
    String[] contentType = internetHeaders.getHeader("content-type");
    if (contentType != null && contentType.length > 0 && contentType[0].contains("boundary")
            && contentType[0].contains("multipart/")) {
        MimeMultipart multipart = new MimeMultipart(new SMTPDataSource(contentType[0], data));
        Content message = messagingService.create(session, mapProperties,
                (String) mapProperties.get("sakai:message-id"), storePath);
        writeMultipartToNode(session, message, multipart);
        return message;
    } else {
        Content node = messagingService.create(session, mapProperties);
        // set up to stream the body.
        session.getContentManager().writeBody(node.getPath(), data);
        return node;
    }
}

From source file:org.sakaiproject.nakamura.smtp.SakaiSmtpServer.java

@SuppressWarnings("unchecked")
private void writePartPropertiesToNode(BodyPart part, Content childNode) throws MessagingException {
    Enumeration<Header> headers = part.getAllHeaders();
    while (headers.hasMoreElements()) {
        Header header = headers.nextElement();
        childNode.setProperty(header.getName(), header.getValue());
    }/*w w w.j av  a  2s . co  m*/
}

From source file:org.simplejavamail.internal.util.MimeMessageParser.java

private void extractCustomUserHeaders(final MimePart part) throws MessagingException {
    final Enumeration e = part.getAllHeaders();
    while (e.hasMoreElements()) {
        final Object headerObj = e.nextElement();
        if (headerObj instanceof Header) {
            final Header header = (Header) headerObj;
            if (isCustomUserHeader(header)) {
                headers.put(header.getName(), header.getValue());
            }//from  ww  w . j av a 2 s . com
        }
    }
}