use of org.apache.james.mime4j.dom.BinaryBody in project sling by apache.
the class AttachmentFilterImpl method isEligible.
@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;
}
use of org.apache.james.mime4j.dom.BinaryBody 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.BinaryBody in project Gargoyle by callakrsos.
the class MimeViewerExam method extracted.
private void extracted(StringBuilder sb, Body body) {
if (body instanceof Multipart) {
Multipart mbody = (Multipart) body;
for (Entity part : mbody.getBodyParts()) {
extracted(sb, part);
}
} else if (body instanceof MessageImpl) {
extracted(sb, body);
} else if (body instanceof TextBody) {
/*
* A text body. Display its contents.
*/
TextBody textBody = (TextBody) body;
try {
Reader r = textBody.getReader();
StringBuilder _sb = new StringBuilder();
int c;
while ((c = r.read()) != -1) {
_sb.append((char) c);
}
System.out.println(_sb.toString());
sb.append(_sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (body instanceof BinaryBody) {
BinaryBody bBody = (BinaryBody) body;
Entity parent = bBody.getParent();
// String dispositionType = parent.getDispositionType();
// String filename = parent.getFilename();
// String contentTransferEncoding = parent.getContentTransferEncoding();
// String mimeType = parent.getMimeType();
Field field = parent.getHeader().getField("Content-ID");
String body2 = field.getBody();
String contentId = body2.replace("<", "").replace(">", "");
StringBuffer buf = new StringBuffer();
try (InputStream is = bBody.getInputStream()) {
int read = -1;
while ((read = is.read()) != -1) {
buf.append((char) read);
}
} catch (IOException e) {
e.printStackTrace();
}
meta.put(contentId, buf.toString());
} else /*
* Ignore Fields </br>
*
* ContentTypeField,AddressListField,DateTimeField UnstructuredField,
* Field
*
*/
{
sb.append(body.toString());
}
}
use of org.apache.james.mime4j.dom.BinaryBody 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.BinaryBody in project sling by apache.
the class MessageStoreImplAttachmentsTest method assertSaveMessageWithAttachments.
private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
store.save(msg);
List<BodyPart> attList = new LinkedList<BodyPart>();
MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList);
@SuppressWarnings("unchecked") Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
assertTrue("No attachments found", attachmentsMsg.size() > 0);
assertEquals("", num, attachmentsMsg.size());
final Resource r = resolver.getResource(getResourcePath(msg, store));
assertNotNull("Expecting non-null Resource", r);
for (Resource aRes : r.getChildren()) {
final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
BodyPart aMsg = attachmentsMsg.poll();
assertNotNull("JCR contains more attachments", aMsg);
for (Field f : aMsg.getHeader().getFields()) {
String name = f.getName();
assertEquals("Field " + name + " is different", (aMap.get(name, String.class)), f.getBody());
}
if (aMsg.getBody() instanceof TextBody) {
assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
} else if (aMsg.getBody() instanceof BinaryBody) {
assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
} else {
fail("Unknown type of attachment body");
}
}
assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
Aggregations