use of org.apache.james.mime4j.dom.TextBody 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.TextBody 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.TextBody in project sling by apache.
the class MessageStoreImpl method getTextPart.
/**
* code taken from http://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library/
*/
static String getTextPart(Entity part) throws IOException {
TextBody tb = (TextBody) part.getBody();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
tb.writeTo(baos);
return baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
}
use of org.apache.james.mime4j.dom.TextBody 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.TextBody 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 {@code MimePart} to parse
* @param textBuilder {@link StringBuilder} to append all plaintext parts
* @param htmlBuilder {@link StringBuilder} to append all html parts
* @throws IOException in case of a failure while transforming the input to a {@link String}
*/
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 (isMultipart(part.getMimeType())) {
Multipart multipart = (Multipart) part.getBody();
for (Entity e : multipart.getBodyParts()) {
handleMimePart(e, textBuilder, htmlBuilder);
}
}
}
Aggregations