use of javax.mail.internet.ContentType in project zm-mailbox by Zimbra.
the class ForwardAppointmentInvite method getMessagePair.
public static Pair<MimeMessage, MimeMessage> getMessagePair(Mailbox mbox, Account senderAcct, Message msg, MimeMessage mmFwdWrapper) throws ServiceException {
Pair<MimeMessage, MimeMessage> msgPair;
mbox.lock.lock();
try {
MimeMessage mmInv = msg.getMimeMessage();
List<Invite> invs = new ArrayList<Invite>();
for (Iterator<CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext(); ) {
CalendarItemInfo cii = iter.next();
Invite inv = cii.getInvite();
if (inv != null) {
invs.add(inv);
}
}
ZVCalendar cal = null;
Invite firstInv = null;
if (!invs.isEmpty()) {
// Recreate the VCALENDAR from Invites.
boolean first = true;
for (Invite inv : invs) {
if (first) {
first = false;
firstInv = inv;
cal = inv.newToICalendar(true);
} else {
ZComponent comp = inv.newToVComponent(true, true);
cal.addComponent(comp);
}
}
} else {
// If no invites found in metadata, parse from text/calendar MIME part.
try {
CalPartDetectVisitor visitor = new CalPartDetectVisitor();
visitor.accept(mmInv);
MimeBodyPart calPart = visitor.getCalendarPart();
if (calPart != null) {
String ctHdr = calPart.getContentType();
ContentType ct = new ContentType(ctHdr);
String charset = ct.getParameter(MimeConstants.P_CHARSET);
if (charset == null || charset.length() == 0)
charset = MimeConstants.P_CHARSET_UTF8;
InputStream is = calPart.getInputStream();
try {
cal = ZCalendarBuilder.build(is, charset);
} finally {
ByteUtil.closeStream(is);
}
List<Invite> invList = Invite.createFromCalendar(senderAcct, msg.getFragment(), cal, false);
if (invList != null && !invList.isEmpty())
firstInv = invList.get(0);
if (firstInv == null)
throw ServiceException.FAILURE("Error building Invite for calendar part in message " + msg.getId(), null);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
} catch (IOException e) {
throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
}
}
msgPair = getInstanceFwdMsg(senderAcct, firstInv, cal, mmInv, mmFwdWrapper);
} finally {
mbox.lock.release();
}
return msgPair;
}
use of javax.mail.internet.ContentType in project stanbol by apache.
the class SimpleMailExtractor method processContent.
// the recursive part
protected void processContent(Object content, StringBuilder buffer, RDFContainer rdf) throws MessagingException, IOException, ExtractorException {
if (content instanceof String) {
buffer.append(content);
buffer.append(' ');
} else if (content instanceof BodyPart) {
BodyPart bodyPart = (BodyPart) content;
DataHandler handler = bodyPart.getDataHandler();
String encoding = null;
if (handler != null) {
encoding = MimeUtility.getEncoding(handler);
}
String fileName = bodyPart.getFileName();
String contentType = bodyPart.getContentType();
if (fileName != null) {
try {
fileName = MimeUtility.decodeWord(fileName);
} catch (MessagingException e) {
// happens on unencoded file names! so just ignore it and leave the file name as it is
}
URI attachURI = URIGenerator.createNewRandomUniqueURI();
rdf.add(NMO.hasAttachment, attachURI);
Model m = rdf.getModel();
m.addStatement(attachURI, RDF.type, NFO.Attachment);
m.addStatement(attachURI, NFO.fileName, fileName);
if (handler != null) {
if (encoding != null) {
m.addStatement(attachURI, NFO.encoding, encoding);
}
}
if (contentType != null) {
contentType = (new ContentType(contentType)).getBaseType();
m.addStatement(attachURI, NIE.mimeType, contentType.trim());
}
// TODO: encoding?
}
// append the content, if any
content = bodyPart.getContent();
// remove any html markup if necessary
if (contentType != null && content instanceof String) {
contentType = contentType.toLowerCase();
if (contentType.indexOf("text/html") >= 0) {
if (encoding != null) {
encoding = MimeUtility.javaCharset(encoding);
}
content = extractTextFromHtml((String) content, encoding, rdf);
}
}
processContent(content, buffer, rdf);
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
String subType = null;
String contentType = multipart.getContentType();
if (contentType != null) {
ContentType ct = new ContentType(contentType);
subType = ct.getSubType();
if (subType != null) {
subType = subType.trim().toLowerCase();
}
}
if ("alternative".equals(subType)) {
handleAlternativePart(multipart, buffer, rdf);
} else if ("signed".equals(subType)) {
handleProtectedPart(multipart, 0, buffer, rdf);
} else if ("encrypted".equals(subType)) {
handleProtectedPart(multipart, 1, buffer, rdf);
} else {
// handles multipart/mixed, /digest, /related, /parallel, /report and unknown subtypes
handleMixedPart(multipart, buffer, rdf);
}
}
}
use of javax.mail.internet.ContentType in project rest.li by linkedin.
the class RestResponseDecoder method decodeResponse.
public void decodeResponse(final StreamResponse streamResponse, final Callback<Response<T>> responseCallback) throws RestLiDecodingException {
//Determine content type and take appropriate action.
//If 'multipart/related', then use MultiPartMIMEReader to read first part (which can be json or pson).
final String contentTypeString = streamResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE);
if (contentTypeString != null) {
ContentType contentType = null;
try {
contentType = new ContentType(contentTypeString);
} catch (ParseException parseException) {
responseCallback.onError(new RestLiDecodingException("Could not decode Content-Type header in response", parseException));
return;
}
if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
final MultiPartMIMEReader multiPartMIMEReader = MultiPartMIMEReader.createAndAcquireStream(streamResponse);
final TopLevelReaderCallback topLevelReaderCallback = new TopLevelReaderCallback(responseCallback, streamResponse, multiPartMIMEReader);
multiPartMIMEReader.registerReaderCallback(topLevelReaderCallback);
return;
}
}
//Otherwise if the whole body is json/pson then read everything in.
//This will not have an extra copy due to assembly since FullEntityReader uses a compound ByteString.
final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
responseCallback.onError(e);
}
@Override
public void onSuccess(ByteString result) {
try {
responseCallback.onSuccess(createResponse(streamResponse.getHeaders(), streamResponse.getStatus(), result, streamResponse.getCookies()));
} catch (Exception exception) {
onError(exception);
}
}
});
streamResponse.getEntityStream().setReader(fullEntityReader);
}
use of javax.mail.internet.ContentType in project rest.li by linkedin.
the class MIMETestUtils method createSmallDataSource.
private static final MimeBodyPart createSmallDataSource() {
try {
//Small body.
final String body = "A small body";
final MimeBodyPart dataPart = new MimeBodyPart();
final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
dataPart.setContent(body, contentType.getBaseType());
dataPart.setHeader(HEADER_CONTENT_TYPE, contentType.toString());
dataPart.setHeader("SomeCustomHeader", "SomeCustomValue");
return dataPart;
} catch (Exception exception) {
Assert.fail();
}
return null;
}
use of javax.mail.internet.ContentType in project rest.li by linkedin.
the class MIMETestUtils method createBodyLessBody.
private static final MimeBodyPart createBodyLessBody() {
try {
//Body-less body. This has no body but does have headers, some of which are folded.
final MimeBodyPart dataPart = new MimeBodyPart();
final ParameterList parameterList = new ParameterList();
parameterList.set("AVeryVeryVeryVeryLongHeader", "AVeryVeryVeryVeryLongValue");
parameterList.set("AVeryVeryVeryVeryLongHeader2", "AVeryVeryVeryVeryLongValue2");
parameterList.set("AVeryVeryVeryVeryLongHeader3", "AVeryVeryVeryVeryLongValue3");
parameterList.set("AVeryVeryVeryVeryLongHeader4", "AVeryVeryVeryVeryLongValue4");
final ContentType contentType = new ContentType("text", "plain", parameterList);
dataPart.setContent("", contentType.getBaseType());
dataPart.setHeader(HEADER_CONTENT_TYPE, contentType.toString());
dataPart.setHeader("YetAnotherCustomHeader", "YetAnotherCustomValue");
return dataPart;
} catch (Exception exception) {
Assert.fail();
}
return null;
}
Aggregations