org.apache.james.mime4j.stream.MimeConfig Java Examples

The following examples show how to use org.apache.james.mime4j.stream.MimeConfig. 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: MailListExtractor.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
public void run(GraphDatabaseService db) {
    this.db = db;
    MboxHandler myHandler = new MboxHandler();
    myHandler.setDb(db);
    MimeConfig config=new MimeConfig();
    config.setMaxLineLen(-1);
    parser = new MimeStreamParser(config);
    parser.setContentHandler(myHandler);
    parse(new File(mboxPath));
    try (Transaction tx = db.beginTx()) {
        for (String address : myHandler.getMailUserNameMap().keySet()) {
            Node node = myHandler.getMailUserMap().get(address);
            node.setProperty(MAILUSER_NAMES, String.join(", ", myHandler.getMailUserNameMap().get(address)));
        }
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        for (String mailId : myHandler.getMailReplyMap().keySet()) {
            Node mailNode = myHandler.getMailMap().get(mailId);
            Node replyNode = myHandler.getMailMap().get(myHandler.getMailReplyMap().get(mailId));
            if (mailNode != null & replyNode != null)
                mailNode.createRelationshipTo(replyNode, RelationshipType.withName(MailListExtractor.MAIL_IN_REPLY_TO));
        }
        tx.success();
    }
}
 
Example #2
Source File: MessageParser.java    From james-project with Apache License 2.0 6 votes vote down vote up
public List<ParsedAttachment> retrieveAttachments(InputStream fullContent) throws IOException {
    DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder();
    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    defaultMessageBuilder.setDecodeMonitor(DecodeMonitor.SILENT);
    Message message = defaultMessageBuilder.parseMessage(fullContent);
    Body body = message.getBody();
    try {
        if (isAttachment(message, Context.BODY)) {
            return ImmutableList.of(retrieveAttachment(message));
        }

        if (body instanceof Multipart) {
            Multipart multipartBody = (Multipart) body;
            return listAttachments(multipartBody, Context.fromSubType(multipartBody.getSubType()))
                .collect(Guavate.toImmutableList());
        } else {
            return ImmutableList.of();
        }
    } finally {
        body.dispose();
    }
}
 
Example #3
Source File: EnvelopeTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Message toMime4JMessage(String messageAsString) throws IOException {
    DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder();
    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    defaultMessageBuilder.setDecodeMonitor(DecodeMonitor.SILENT);
    return defaultMessageBuilder.parseMessage(
        new ByteArrayInputStream(messageAsString.getBytes(StandardCharsets.UTF_8)));
}
 
Example #4
Source File: ResponseDecoder.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
public ResponseDecoder(ImapClientConfiguration configuration,
                       ImapClientState clientState,
                       EventExecutorGroup executorGroup) {
  super(State.SKIP_CONTROL_CHARS);
  this.logger = LogUtils.loggerWithName(ResponseDecoder.class, clientState.getClientName());
  this.clientState = clientState;
  this.executorGroup = executorGroup;

  this.charSeq = new SoftReferencedAppendableCharSequence(configuration.defaultResponseBufferSize());
  this.lineParser = new LineParser(charSeq, configuration.maxLineLength());
  this.wordParser = new WordParser(charSeq, configuration.maxLineLength());
  this.fetchResponseTypeParser = new FetchResponseTypeParser(charSeq, configuration.maxLineLength());
  this.atomOrStringParser = new AtomOrStringParser(charSeq, configuration.maxLineLength());
  this.literalStringParser = new LiteralStringParser(charSeq, configuration.maxLineLength());
  this.bufferedBodyParser = new BufferedBodyParser(charSeq);
  this.numberParser = new NumberParser(charSeq, 19);
  this.envelopeParser = new EnvelopeParser();
  this.nestedArrayParserRecycler = new NestedArrayParser.Recycler<>(literalStringParser);
  this.messageBuilder = ((DefaultMessageBuilder) MESSAGE_SERVICE_FACTORY.newMessageBuilder());

  MimeConfig mimeConfig = MimeConfig.custom()
      .setMaxLineLen(configuration.maxLineLength())
      .setMaxHeaderLen(configuration.maxLineLength())
      .setMaxHeaderCount(configuration.maxHeaderCount())
      .build();

  messageBuilder.setMimeEntityConfig(mimeConfig);

  this.untaggedResponses = new ArrayList<>();
  this.responseBuilder = new TaggedResponse.Builder();

  this.allBytesParser = configuration.tracingEnabled() ? new AllBytesParser(charSeq) : null;
}
 
Example #5
Source File: MailDto.java    From james-project with Apache License 2.0 5 votes vote down vote up
private static Message convertMessage(MimeMessage message) throws IOException, MessagingException {
    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    message.writeTo(rawMessage);
    return Message.Builder
            .of()
            .use(MimeConfig.PERMISSIVE)
            .parse(new ByteArrayInputStream(rawMessage.toByteArray()))
            .build();
}
 
Example #6
Source File: MessageViewFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
static Message parse(InputStream messageContent) throws IOException {
    return Message.Builder
        .of()
        .use(MimeConfig.PERMISSIVE)
        .parse(messageContent)
        .build();
}
 
Example #7
Source File: SendMDNProcessor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Message retrieveOriginalMessage(JmapMDN mdn, MailboxSession mailboxSession) throws MailboxException, IOException, MessageNotFoundException {
    List<MessageResult> messages = messageIdManager.getMessage(mdn.getMessageId(), FetchGroup.HEADERS, mailboxSession);

    if (messages.size() == 0) {
        throw new MessageNotFoundException();
    }

    DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder();
    messageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    messageBuilder.setDecodeMonitor(DecodeMonitor.SILENT);
    return messageBuilder.parseMessage(messages.get(0).getHeaders().getInputStream());
}
 
Example #8
Source File: StoreMessageManager.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MimeTokenStream getParser(BodyOffsetInputStream bIn) {
    final MimeTokenStream parser = new MimeTokenStream(MimeConfig.PERMISSIVE, new DefaultBodyDescriptorBuilder());

    parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
    parser.parse(bIn);
    return parser;
}
 
Example #9
Source File: MessageSearches.java    From james-project with Apache License 2.0 5 votes vote down vote up
private HeaderImpl buildTextHeaders(MailboxMessage message) throws IOException, MimeIOException {
    DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder();
    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    Message headersMessage = defaultMessageBuilder
        .parseMessage(message.getHeaderContent());
    HeaderImpl headerImpl = new HeaderImpl();
    addFrom(headerImpl, headersMessage.getFrom());
    addAddressList(headerImpl, headersMessage.getTo());
    addAddressList(headerImpl, headersMessage.getCc());
    addAddressList(headerImpl, headersMessage.getBcc());
    headerImpl.addField(Fields.subject(headersMessage.getSubject()));
    return headerImpl;
}
 
Example #10
Source File: MimeDescriptorImpl.java    From james-project with Apache License 2.0 5 votes vote down vote up
public static MimeDescriptorImpl build(InputStream stream) throws IOException, MimeException {
    // Disable line length limit
    // See https://issues.apache.org/jira/browse/IMAP-132
    //
    final MimeTokenStream parser = new MimeTokenStream(MimeConfig.PERMISSIVE, new DefaultBodyDescriptorBuilder());
    
    parser.parse(stream);
    
    // TODO: Shouldn't this get set before we call the parse ?
    parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
    return createDescriptor(parser);
}
 
Example #11
Source File: MimePartParser.java    From james-project with Apache License 2.0 5 votes vote down vote up
public MimePartParser(Message message, TextExtractor textExtractor) {
    this.message = message;
    this.textExtractor = textExtractor;
    this.builderStack = new LinkedList<>();
    this.currentlyBuildMimePart = new RootMimePartContainerBuilder();
    this.stream = new MimeTokenStream(
        MimeConfig.PERMISSIVE,
        new DefaultBodyDescriptorBuilder());
}
 
Example #12
Source File: DeletedMessageConverter.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<Message> parseMessage(org.apache.james.mailbox.store.mail.model.Message message) throws IOException {
    DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder();
    messageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    messageBuilder.setDecodeMonitor(DecodeMonitor.SILENT);
    try {
        return Optional.ofNullable(messageBuilder.parseMessage(message.getFullContent()));
    } catch (MimeIOException e) {
        LOGGER.warn("Can not parse the message {}", message.getMessageId(), e);
        return Optional.empty();
    }
}
 
Example #13
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doInitialize(UimaContext context) throws ResourceInitializationException {

  // Initialise charset for MBOX processing
  try {
    charset = Charset.forName(charsetName);
  } catch (UnsupportedCharsetException | IllegalCharsetNameException ce) {
    getMonitor().warn("Unsupported charset, {}. UTF-8 will be used.", charsetName, ce);
    charset = StandardCharsets.UTF_8;
  }

  // Initialise MBOX iterator
  try {
    mboxIterator =
        MboxIterator.fromFile(mbox)
            .charset(charset)
            .maxMessageSize(messageSize)
            .build()
            .iterator();
  } catch (IOException ioe) {
    throw new ResourceInitializationException(ioe);
  }

  // Initialise message parser
  messageBuilder = new DefaultMessageBuilder();
  messageBuilder.setContentDecoding(true);

  MimeConfig config = new MimeConfig.Builder().setMaxLineLen(10000).build();
  messageBuilder.setMimeEntityConfig(config);

  // Build list of extensions to ignore
  for (String s : ignoreExtensions) {
    ignoreExtensionsList.add(s.trim().toLowerCase());
  }
}
 
Example #14
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private void parseEmail(String messageFileName, ContentHandler contentHandler) throws FileNotFoundException,
		MimeException, IOException {

	MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;
	BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();		
	MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
	mime4jParser.setContentDecoding(true);
	mime4jParser.setContentHandler(contentHandler);
	
	URL url = this.getClass().getClassLoader().getResource(messageFileName);
	
	InputStream mailIn = new FileInputStream(new File(url.getFile()));
	mime4jParser.parse(mailIn);
	
}
 
Example #15
Source File: EmailParseManager.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public Email getParsedEmail() throws MimeException, IOException {
	
	MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;
	BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
	MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
	mime4jParser.setContentDecoding(true);
	mime4jParser.setContentHandler(contentHandler);		
	
	mime4jParser.parse(rawEmailFile);
	
	return ((CustomContentHandler)contentHandler).getEmail();
}
 
Example #16
Source File: MaildirMessage.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Parse message if needed
 */
private synchronized void parseMessage() {
    if (parsed) {
        return;
    }
    try (SharedFileInputStream tmpMsgIn = new SharedFileInputStream(messageName.getFile())) {

        bodyStartOctet = bodyStartOctet(tmpMsgIn);

        // Disable line length... This should be handled by the smtp server
        // component and not the parser itself
        // https://issues.apache.org/jira/browse/IMAP-122
        final MimeTokenStream parser = new MimeTokenStream(MimeConfig.PERMISSIVE, new DefaultBodyDescriptorBuilder());
        parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
        parser.parse(tmpMsgIn.newStream(0, -1));

        EntityState next = parser.next();
        while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM && next != EntityState.T_START_MULTIPART) {
            next = parser.next();
        }
        final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) parser.getBodyDescriptor();
        final String mediaType;
        final String mediaTypeFromHeader = descriptor.getMediaType();
        final String subType;
        if (mediaTypeFromHeader == null) {
            mediaType = "text";
            subType = "plain";
        } else {
            mediaType = mediaTypeFromHeader;
            subType = descriptor.getSubType();
        }
        propertyBuilder.setMediaType(mediaType);
        propertyBuilder.setSubType(subType);
        propertyBuilder.setContentID(descriptor.getContentId());
        propertyBuilder.setContentDescription(descriptor.getContentDescription());
        propertyBuilder.setContentLocation(descriptor.getContentLocation());
        propertyBuilder.setContentMD5(descriptor.getContentMD5Raw());
        propertyBuilder.setContentTransferEncoding(descriptor.getTransferEncoding());
        propertyBuilder.setContentLanguage(descriptor.getContentLanguage());
        propertyBuilder.setContentDispositionType(descriptor.getContentDispositionType());
        propertyBuilder.setContentDispositionParameters(descriptor.getContentDispositionParameters());
        propertyBuilder.setContentTypeParameters(descriptor.getContentTypeParameters());
        // Add missing types
        final String codeset = descriptor.getCharset();
        if (codeset == null) {
            if ("TEXT".equalsIgnoreCase(mediaType)) {
                propertyBuilder.setCharset("us-ascii");
            }
        } else {
            propertyBuilder.setCharset(codeset);
        }

        final String boundary = descriptor.getBoundary();
        if (boundary != null) {
            propertyBuilder.setBoundary(boundary);
        }
        if ("text".equalsIgnoreCase(mediaType)) {
            long lines = -1;
            try (CountingInputStream bodyStream = new CountingInputStream(parser.getInputStream())) {
                bodyStream.readAll();
                lines = bodyStream.getLineCount();
            }

            next = parser.next();
            if (next == EntityState.T_EPILOGUE) {
                try (CountingInputStream epilogueStream = new CountingInputStream(parser.getInputStream())) {
                    epilogueStream.readAll();
                    lines += epilogueStream.getLineCount();
                }
            }
            propertyBuilder.setTextualLineCount(lines);
        }
    } catch (IOException | MimeException e) {
        // has successfully been parsen when appending, shouldn't give any
        // problems
    } finally {
        // ignore on close
        parsed = true;
    }
}
 
Example #17
Source File: PartContentBuilder.java    From james-project with Apache License 2.0 4 votes vote down vote up
public PartContentBuilder() {
    parser = new MimeTokenStream(MimeConfig.PERMISSIVE);
}
 
Example #18
Source File: Preview.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Message parse(InputStream inputStream) throws IOException {
    return Message.Builder.of()
        .use(MimeConfig.PERMISSIVE)
        .parse(inputStream)
        .build();
}
 
Example #19
Source File: EmailVerificationTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private void parseEmail(String messageFileName,
                        ContentHandler contentHandler) throws
        MimeException, IOException {

    MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;

    BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
    MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig, DecodeMonitor.SILENT, bodyDescriptorBuilder);

    mime4jParser.setContentDecoding(true);
    mime4jParser.setContentHandler(contentHandler);

    URL url = this.getClass().getClassLoader().getResource(messageFileName);

    InputStream mailIn = new FileInputStream(new File(url.getFile()));
    mime4jParser.parse(mailIn);

}