use of org.apache.james.mime4j.dom.TextBody 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);
}
use of org.apache.james.mime4j.dom.TextBody in project sling by apache.
the class MessageStoreImplAttachmentsTest method createTextBody.
/*
* taken from http://svn.apache.org/repos/asf/james/mime4j/trunk/examples/src/main/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
*/
private static BodyPart createTextBody(String text, String subtype, boolean isAttachment) {
TextBody body = new StorageBodyFactory().textBody(text, MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
BodyPart bodyPart = new BodyPart();
if (isAttachment) {
bodyPart.setContentDisposition("attachment", "file" + Math.random());
}
bodyPart.setText(body, subtype);
return bodyPart;
}
use of org.apache.james.mime4j.dom.TextBody in project Gargoyle by callakrsos.
the class MimeToHtmlWordAdapter 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
*
*/
{
sb.append(body.toString());
}
}
use of org.apache.james.mime4j.dom.TextBody 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());
}
}
Aggregations