use of org.apache.james.mime4j.dom.Multipart in project gerrit by GerritCodeReview.
the class RawMailParser method handleMimePart.
/**
* Traverses a mime tree and parses out text and html parts. All other parts will be dropped.
*
* @param part MimePart to parse
* @param textBuilder StringBuilder to append all plaintext parts
* @param htmlBuilder StringBuilder to append all html parts
* @throws IOException
*/
private static void handleMimePart(Entity part, StringBuilder textBuilder, StringBuilder htmlBuilder) throws IOException {
if (isPlainOrHtml(part.getMimeType()) && !isAttachment(part.getDispositionType())) {
TextBody tb = (TextBody) part.getBody();
String result = CharStreams.toString(new InputStreamReader(tb.getInputStream(), tb.getMimeCharset()));
if (part.getMimeType().equals("text/plain")) {
textBuilder.append(result);
} else if (part.getMimeType().equals("text/html")) {
htmlBuilder.append(result);
}
} else if (isMixedOrAlternative(part.getMimeType())) {
Multipart multipart = (Multipart) part.getBody();
for (Entity e : multipart.getBodyParts()) {
handleMimePart(e, textBuilder, htmlBuilder);
}
}
}
use of org.apache.james.mime4j.dom.Multipart in project Gargoyle by callakrsos.
the class MimeToHtmlAdapter method append.
/***************************************************************************/
/* sb에 번역된 텍스트데이터를 덧붙임 */
/***************************************************************************/
private void append(StringBuilder sb, Body body) {
if (body instanceof TextBody) {
/*
* A text body. Display its contents.
*/
TextBody textBody = (TextBody) body;
try {
Reader r = textBody.getReader();
int c;
while ((c = r.read()) != -1) {
sb.append((char) c);
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (body instanceof BinaryBody) {
BinaryBody bBody = (BinaryBody) body;
append(sb, bBody);
} else if (body instanceof Multipart) {
Multipart mbody = (Multipart) body;
for (Entity part : mbody.getBodyParts()) {
append(sb, part);
}
} else /*
* Ignore Fields </br>
*
* ContentTypeField,AddressListField,DateTimeField UnstructuredField,
* Field
*
*/
{
LOGGER.debug("{}", body);
sb.append(body.toString());
}
}
use of org.apache.james.mime4j.dom.Multipart in project sling by apache.
the class MessageStoreImpl method save.
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();
// remove < and >
final String listId = listIdRaw.substring(1, listIdRaw.length() - 1);
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);
}
}
use of org.apache.james.mime4j.dom.Multipart in project sling by apache.
the class MessageStoreImplAttachmentsTest method recursiveMultipartMessageTest.
@Test
public void recursiveMultipartMessageTest() throws IOException {
Multipart multipart = new MultipartImpl("mixed");
BodyPart att1 = createRandomBinaryAttachment(100);
multipart.addBodyPart(att1);
BodyPart att2 = createRandomBinaryAttachment(133);
multipart.addBodyPart(att2);
Multipart nestedMultipart = new MultipartImpl("mixed");
BodyPart nBody = createTextBody("Some sample text here...?!", "plain", false);
nestedMultipart.addBodyPart(nBody);
BodyPart nAtt1 = createRandomBinaryAttachment(300);
nestedMultipart.addBodyPart(nAtt1);
BodyPart NAtt2 = createRandomBinaryAttachment(100);
nestedMultipart.addBodyPart(NAtt2);
BodyPart nAtt3 = createTextBody("Some other text here...<br>?!", "html", true);
nestedMultipart.addBodyPart(nAtt3);
BodyPart nestedMessage = new BodyPart();
nestedMessage.setMultipart(nestedMultipart);
multipart.addBodyPart(nestedMessage);
MessageImpl message = new MessageImpl();
message.setMultipart(multipart);
message.setSubject("Template message");
message.setDate(new Date());
message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
assertSaveMessageWithAttachments(message, 5);
}
use of org.apache.james.mime4j.dom.Multipart in project sling by apache.
the class MessageStoreImplAttachmentsTest method simpleMultipartMessageTest.
@Test
public void simpleMultipartMessageTest() throws IOException {
Multipart multipart = new MultipartImpl("mixed");
BodyPart att0 = createTextBody("This is the first part of the template..", "plain", true);
multipart.addBodyPart(att0);
BodyPart att1 = createRandomBinaryAttachment(200);
multipart.addBodyPart(att1);
BodyPart att2 = createRandomBinaryAttachment(300);
multipart.addBodyPart(att2);
BodyPart att3 = createTextBody("Some sample text here...?!", "html", true);
multipart.addBodyPart(att3);
BodyPart att4 = createRandomBinaryAttachment(100);
multipart.addBodyPart(att4);
BodyPart att5 = createTextBody("Some other text here...?!", "plain", true);
multipart.addBodyPart(att5);
MessageImpl message = new MessageImpl();
message.setMultipart(multipart);
message.setSubject("Template message");
message.setDate(new Date());
message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
assertSaveMessageWithAttachments(message, 6);
}
Aggregations