org.apache.james.mime4j.dom.Body Java Examples

The following examples show how to use org.apache.james.mime4j.dom.Body. 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: 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 #2
Source File: ResponseDecoder.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
private void setEnvelopeIfAbsent() {
  try {
    currentMessage.getEnvelope();
  } catch (UnfetchedFieldException e) {
    try {
      Body body = currentMessage.getBody();
      if (body instanceof AbstractEntity) {
        AbstractEntity message = (AbstractEntity) body;
        currentMessage.setEnvelope(EnvelopeParser.parseHeader(message.getHeader()));
      }
    } catch (UnfetchedFieldException e1) {
      // ignored - no body, nothing to build envelope from
    }
  }
}
 
Example #3
Source File: ImapClientTest.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppend() throws Exception {
  Header header = new HeaderImpl();
  header.addField(DefaultFieldParser.parse("Subject: This is the subject"));
  header.addField(DefaultFieldParser.parse("To: [email protected]"));
  header.addField(DefaultFieldParser.parse("From: [email protected]"));
  header.addField(DefaultFieldParser.parse("Date: 10-MAY-1994 00:00:00 -0000 (UTC)"));
  header.addField(DefaultFieldParser.parse("Message-ID: 12345"));

  Envelope envelope = new Envelope.Builder().setDate(ZonedDateTime.of(1994, 5, 10, 0, 0, 0, 0, ZoneId.of("UTC")));

  Body body = BasicBodyFactory.INSTANCE.textBody("This is a test");

  Message message = new MessageImpl();
  message.setBody(body);
  message.setHeader(header);

  ImapMessage imapMessage = new ImapMessage.Builder()
      .setFlags(ImmutableSet.of(StandardMessageFlag.SEEN, StandardMessageFlag.RECENT))
      .setEnvelope(envelope)
      .setBody(message);

  FetchResponse preAppendFetchAll = client.fetch(1, Optional.empty(), FetchDataItemType.UID, FetchDataItemType.FLAGS, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(preAppendFetchAll.getMessages().size()).isEqualTo(1);

  TaggedResponse appendResponse = client.append(DEFAULT_FOLDER, imapMessage.getFlags(), imapMessage.getEnvelope().getDate(), imapMessage).get();
  assertThat(appendResponse.getCode()).isEqualTo(ResponseCode.OK);
  long uid = Long.parseLong(appendResponse.getMessage().substring(25, 26));

  FetchResponse postAppendFetchAll = client.fetch(1, Optional.empty(), FetchDataItemType.UID, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(postAppendFetchAll.getMessages().size()).isEqualTo(2);

  FetchResponse postAppendFetchUid = client.uidfetch(uid, Optional.of(uid), FetchDataItemType.UID, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(postAppendFetchUid.getMessages().size()).isEqualTo(1);
  assertThat(postAppendFetchUid.getMessages().iterator().next().getBody().getSubject()).isEqualToIgnoringCase("This is the subject");
  assertThat(postAppendFetchUid.getMessages().iterator().next().getEnvelope().getMessageId()).isEqualToIgnoringCase("12345");
}
 
Example #4
Source File: AttachmentFilterImpl.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEligible(BodyPart attachment) {
    // extension check
    final String filename = attachment.getFilename();
    String ext = "";
    int idx = filename.lastIndexOf('.');
    if (idx > -1) {
        ext = filename.substring(idx + 1);
    }
    if (eligibleExtensions != null && !eligibleExtensions.contains(ext)) {
        return false;
    }
    
    // size check
    final Body body = attachment.getBody();
    try {
        if (
                body instanceof BinaryBody 
                && IOUtils.toByteArray(((BinaryBody) body).getInputStream()).length > maxSize
                || 
                body instanceof TextBody
                && IOUtils.toByteArray(((TextBody) body).getInputStream()).length > maxSize ) {
            return false;
        }
    } catch (IOException e) {
        return false;
    }

    // true, if nothing wrong
    return true;
}
 
Example #5
Source File: MessageStoreImplAttachmentsTest.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private static BodyPart createRandomBinaryAttachment(int numberOfBytes) throws IOException {
    byte[] data = new byte[numberOfBytes];
    new Random().nextBytes(data);

    Body body = new StorageBodyFactory().binaryBody(new ByteArrayInputStream(data));

    BodyPart bodyPart = new BodyPart();
    bodyPart.setContentDisposition("attachment", "file"+Math.random());
    bodyPart.setBody(body, "application/octet-stream");

    return bodyPart;
}
 
Example #6
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGetNext(JCas jCas) throws IOException, CollectionException {
  if (!attachments.isEmpty()) {
    // If we have attachments, first process those
    Map.Entry<String, Body> entry = attachments.firstEntry();
    getMonitor().info("Processing attachment {}", entry.getKey());

    processBody(jCas, entry.getValue(), entry.getKey());

    attachments.remove(entry.getKey());
  } else {
    // No attachments so process the next message
    String raw = mboxIterator.next().toString();
    count++;

    String uri = "mbox://" + mbox + "#" + count;
    getMonitor().info("Processing message {}", uri);

    // Parse message and get body
    Message msg = messageBuilder.parseMessage(new ByteArrayInputStream(raw.getBytes(charset)));
    Body body = msg.getBody();

    boolean doneBody = false;

    // Decide how to process body of message
    if (body instanceof SingleBody) {
      doneBody = processBody(jCas, body, uri);
    } else if (body instanceof Multipart) {
      Multipart mp = (Multipart) body;
      doneBody = processMultipart(jCas, mp, uri);
    }

    // No body found (just attachments? Or invalid message?)
    if (!doneBody) {
      throw new IOException("No processable body found");
    }
  }
}
 
Example #7
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
/** Process a single body part */
private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException {
  if (body instanceof TextBody) {
    // Process plain text body
    processTextBody(jCas, (TextBody) body);

    // Add fields from parent
    for (Field f : body.getParent().getHeader().getFields()) {
      addMetadata(jCas, f.getName(), f.getBody());
    }

    // Set up document annotation - this is done by the content extractor in other cases
    DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas);
    doc.setSourceUri(sourceUri);
    doc.setTimestamp(System.currentTimeMillis());
  } else if (body instanceof BinaryBody) {
    processBinaryBody(jCas, (BinaryBody) body, sourceUri);
  } else if (body instanceof Multipart) {
    // Multipart message, so recurse
    Multipart mp = (Multipart) body;
    return processMultipart(jCas, mp, sourceUri);
  } else {
    // No body processed
    return false;
  }

  return true;
}
 
Example #8
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
public MessageContent extract(org.apache.james.mime4j.dom.Message message) throws IOException {
    Body body = message.getBody();
    if (body instanceof TextBody) {
        return parseTextBody(message, (TextBody)body);
    }
    if (body instanceof Multipart) {
        return parseMultipart(message, (Multipart)body);
    }
    return MessageContent.empty();
}
 
Example #9
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 4 votes vote down vote up
private void save(ResourceResolver resolver, Message msg) throws IOException, LoginException {
    // apply message processors
    for(MessageProcessor processor : getSortedMessageProcessors()) {
        logger.debug("Calling {}", processor);
        processor.processMessage(msg);
    }

    // into path: archive/domain/list/thread/message
    final Map<String, Object> msgProps = new HashMap<String, Object>();
    final List<BodyPart> attachments = new ArrayList<BodyPart>(); 

    msgProps.put(resourceTypeKey, MailArchiveServerConstants.MESSAGE_RT);

    StringBuilder plainBody = new StringBuilder();
    StringBuilder htmlBody = new StringBuilder();
    Boolean hasBody = false;

    if (!msg.isMultipart()) {
        plainBody = new StringBuilder(getTextPart(msg)); 
    } else {
        Multipart multipart = (Multipart) msg.getBody();
        recursiveMultipartProcessing(multipart, plainBody, htmlBody, hasBody, attachments);
    }

    msgProps.put(PLAIN_BODY, plainBody.toString().replaceAll("\r\n", "\n"));
    if (htmlBody.length() > 0) {
        msgProps.put(HTML_BODY, htmlBody.toString());
    }

    msgProps.putAll(getMessagePropertiesFromHeader(msg.getHeader()));
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new DefaultMessageWriter().writeHeader(msg.getHeader(), baos);
    String origHdr = baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
    msgProps.put(X_ORIGINAL_HEADER, origHdr);
    
    final Header hdr = msg.getHeader();
    final String listIdRaw = hdr.getField(LIST_ID).getBody();
    final String listId = listIdRaw.substring(1, listIdRaw.length()-1); // remove < and >

    final String list = getListNodeName(listId);
    final String domain = getDomainNodeName(listId);
    final String subject = (String) msgProps.get(SUBJECT);
    final String threadPath = threadKeyGen.getThreadKey(subject);
    final String threadName = removeRe(subject);

    Resource parentResource = assertEachNode(resolver, archivePath, domain, list, threadPath, threadName);

    String msgNodeName = makeJcrFriendly((String) msgProps.get(NAME));
    boolean isMsgNodeCreated = assertResource(resolver, parentResource, msgNodeName, msgProps);
    if (isMsgNodeCreated) {
        Resource msgResource = resolver.getResource(parentResource, msgNodeName);
        for (BodyPart att : attachments) {
            if (!attachmentFilter.isEligible(att)) {
                continue;
            }
            final Map<String, Object> attProps = new HashMap<String, Object>();
            parseHeaderToProps(att.getHeader(), attProps);
            Body body = att.getBody();
            if (body instanceof BinaryBody) {
                attProps.put(CONTENT, ((BinaryBody) body).getInputStream());
            } else if (body instanceof TextBody) {
                attProps.put(CONTENT, ((TextBody) body).getInputStream());
            }

            String attNodeName = Text.escapeIllegalJcrChars(att.getFilename());
            assertResource(resolver, msgResource, attNodeName, attProps);
        }

        updateThread(resolver, parentResource, msgProps);
    }
}
 
Example #10
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private byte[] getContent(Body body) throws IOException {
    DefaultMessageWriter messageWriter = new DefaultMessageWriter();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    messageWriter.writeBody(body, out);
    return out.toByteArray();
}