Example usage for javax.mail.internet InternetHeaders InternetHeaders

List of usage examples for javax.mail.internet InternetHeaders InternetHeaders

Introduction

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

Prototype

public InternetHeaders(InputStream is) throws MessagingException 

Source Link

Document

Read and parse the given RFC822 message stream till the blank line separating the header from the body.

Usage

From source file:com.adaptris.util.text.mime.ReadonlyFileDataSource.java

private static InternetHeaders readHeaders(File f) throws IOException, MessagingException {
    InternetHeaders hdrs = null;/*w  ww  .  j  av  a2 s . c  o  m*/
    try (InputStream in = new FileInputStream(f)) {
        hdrs = new InternetHeaders(in);
    }
    return hdrs;
}

From source file:com.adaptris.util.text.mime.InputStreamDataSource.java

private void initialise(InputStream input) throws IOException, MessagingException {
    headers = new InternetHeaders(input);
    initContent(input);
}

From source file:foo.domaintest.email.EmailApiModule.java

/**
 * Provides parsed email headers from the "headers" param in a multipart/form-data request.
 * <p>/*from ww w . j  av  a 2  s  .com*/
 * Although SendGrid parses some headers for us, it doesn't parse "reply-to", so we need to do
 * this. Once we are doing it, it's easier to be consistent and use this as the sole source of
 * truth for information that originates in the headers.
 */
@Provides
@Singleton
InternetHeaders provideHeaders(FileItemIterator iterator) {
    try {
        while (iterator != null && iterator.hasNext()) {
            FileItemStream item = iterator.next();
            // SendGrid sends us the headers in the "headers" param.
            if (item.getFieldName().equals("headers")) {
                try (InputStream stream = item.openStream()) {
                    // SendGrid always sends headers in UTF-8 encoding.
                    return new InternetHeaders(new ByteArrayInputStream(
                            CharStreams.toString(new InputStreamReader(stream, UTF_8.name())).getBytes(UTF_8)));
                }
            }
        }
    } catch (MessagingException | FileUploadException | IOException e) {
        // If we fail parsing the headers fall through returning the empty header object below.
    }
    return new InternetHeaders(); // Parsing failed or there was no "headers" param.
}

From source file:org.sonews.daemon.command.PostCommand.java

/**
 * Process the given line String. line.trim() was called by NNTPConnection.
 *
 * @param conn//from w ww .  j  av a 2  s . c  o  m
 * @param line
 * @throws java.io.IOException
 * @throws org.sonews.storage.StorageBackendException
 */
@Override
// TODO: Refactor this method to reduce complexity!
public void processLine(NNTPConnection conn, String line, byte[] raw)
        throws IOException, StorageBackendException {
    switch (state) {
    case WaitForLineOne: {
        if (line.equalsIgnoreCase("POST")) {
            conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
            state = PostState.ReadingHeaders;
        } else {
            conn.println("500 invalid command usage");
        }
        break;
    }
    case ReadingHeaders: {
        strHead.append(line);
        strHead.append(SynchronousNNTPConnection.NEWLINE);

        if ("".equals(line) || ".".equals(line)) {
            // we finally met the blank line
            // separating headers from body

            try {
                // Parse the header using the InternetHeader class from
                // JavaMail API
                headers = new InternetHeaders(
                        new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset())));

                // add the header entries for the article
                article.setHeaders(headers);
            } catch (MessagingException ex) {
                Log.get().log(Level.INFO, ex.getLocalizedMessage(), ex);
                conn.println("500 posting failed - invalid header");
                state = PostState.Finished;
                break;
            }

            // Change charset for reading body;
            // for multipart messages UTF-8 is returned
            // conn.setCurrentCharset(article.getBodyCharset());

            state = PostState.ReadingBody;

            // WTF: do we need articles without bodies?
            if (".".equals(line)) {
                // Post an article without body
                postArticle(conn, article);
                state = PostState.Finished;
            }
        }
        break;
    }
    case ReadingBody: {
        if (".".equals(line)) {
            // Set some headers needed for Over command
            headers.setHeader(Headers.LINES, Integer.toString(lineCount));
            headers.setHeader(Headers.BYTES, Long.toString(bodySize));

            byte[] body = bufBody.toByteArray();
            if (body.length >= 2) {
                // Remove trailing CRLF
                body = Arrays.copyOf(body, body.length - 2);
            }
            article.setBody(body); // set the article body

            postArticle(conn, article);
            state = PostState.Finished;
        } else {
            bodySize += line.length() + 1;
            lineCount++;

            // Add line to body buffer
            bufBody.write(raw, 0, raw.length);
            bufBody.write(SynchronousNNTPConnection.NEWLINE.getBytes("UTF-8"));

            if (bodySize > maxBodySize) {
                conn.println("500 article is too long");
                state = PostState.Finished;
                break;
            }
        }
        break;
    }
    default: {
        // Should never happen
        Log.get().severe("PostCommand::processLine(): already finished...");
    }
    }
}

From source file:com.trsst.server.AbstractMultipartAdapter.java

private Map<String, String> getHeaders(MultipartInputStream multipart) throws IOException, MessagingException {
    Map<String, String> mapHeaders = new HashMap<String, String>();
    moveToHeaders(multipart);/* w  w w .  jav a  2  s  .c om*/
    InternetHeaders headers = new InternetHeaders(multipart);

    Enumeration<Header> allHeaders = headers.getAllHeaders();
    if (allHeaders != null) {
        while (allHeaders.hasMoreElements()) {
            Header header = allHeaders.nextElement();
            mapHeaders.put(header.getName().toLowerCase(), header.getValue());
        }
    }

    return mapHeaders;
}

From source file:com.duroty.application.mail.manager.SendManager.java

/**
 * DOCUMENT ME!//from  ww w .  j  a va  2  s. c  o  m
 *
 * @param mid DOCUMENT ME!
 * @param user DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws OutOfMemoryError DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 * @throws Throwable DOCUMENT ME!
 */
public InternetHeaders getHeaders(org.hibernate.Session hsession, String repositoryName, String mid) {
    InternetHeaders xheaders = null;

    try {
        Criteria crit = hsession.createCriteria(Message.class);
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));
        crit.add(Restrictions.eq("mesName", mid));

        Message message = (Message) crit.uniqueResult();

        if (message != null) {
            String headers = message.getMesHeaders();
            xheaders = new InternetHeaders(IOUtils.toInputStream(headers));
        }

        /*MimeMessage mime = messageable.getMimeMessage(mid,
            getUser(hsession, repositoryName));
                
        InternetHeaders xheaders = MessageUtilities.getHeadersWithFrom(mime);*/
        return xheaders;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    } catch (Throwable e) {
        return null;
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:org.apache.james.core.builder.MimeMessageBuilder.java

public MimeMessageBuilder setMultipartWithSubMessage(MimeMessage mimeMessage)
        throws MessagingException, IOException {
    return setMultipartWithBodyParts(new MimeBodyPart(
            new InternetHeaders(new ByteArrayInputStream(
                    "Content-Type: multipart/mixed".getBytes(StandardCharsets.US_ASCII))),
            IOUtils.toByteArray(mimeMessage.getInputStream())));
}

From source file:org.apache.mailet.base.test.MimeMessageBuilder.java

public MimeMessageBuilder setMultipartWithSubMessage(MimeMessage mimeMessage)
        throws MessagingException, IOException {
    return setMultipartWithBodyParts(new MimeBodyPart(
            new InternetHeaders(
                    new ByteArrayInputStream("Content-Type: multipart/mixed".getBytes(Charsets.US_ASCII))),
            IOUtils.toByteArray(mimeMessage.getInputStream())));
}

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

@SuppressWarnings("unchecked")
private Content writeMessage(Session session, Map<String, Object> mapProperties, InputStream data,
        String storePath)/*ww  w  .j a va 2  s.  co  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;
    }
}