use of com.zimbra.common.mime.ContentType in project zm-mailbox by Zimbra.
the class ParseMimeMessage method setContent.
/**
* The <mp>'s from the client and the MimeBodyParts in alternatives[] all want to be "content"
* of this MimeMessage. The alternatives[] all need to be "alternative" to whatever the client sends
* us....but we want to be careful so that we do NOT create a nested multipart/alternative structure
* within another one (that would be very tacky)....so this is a bit complicated.
*/
private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt) throws MessagingException, ServiceException, IOException {
String type = elem.getAttribute(MailConstants.A_CONTENT_TYPE, MimeConstants.CT_DEFAULT).trim();
ContentType ctype = new ContentType(type, ctxt.use2231).cleanup();
// is the client passing us a multipart?
if (ctype.getPrimaryType().equals("multipart")) {
// handle multipart content separately...
setMultipartContent(ctype, mm, mmp, elem, alternatives, ctxt);
return;
}
Element inline = elem.getOptionalElement(MailConstants.E_ATTACH);
if (inline != null) {
handleAttachments(inline, mmp, ctxt, elem.getAttribute(MailConstants.A_CONTENT_ID, null), Part.INLINE);
return;
}
if (alternatives != null) {
// create a multipart/alternative to hold all the alternatives
MimeMultipart mmpNew = new ZMimeMultipart("alternative");
if (mmp == null) {
mm.setContent(mmpNew);
} else {
MimeBodyPart mbpWrapper = new ZMimeBodyPart();
mbpWrapper.setContent(mmpNew);
mmp.addBodyPart(mbpWrapper);
}
mmp = mmpNew;
}
// once we get here, mmp is either NULL, a multipart/mixed from the toplevel,
// or a multipart/alternative created just above....either way we are safe to stick
// the client's nice and simple body right here
String text = elem.getAttribute(MailConstants.E_CONTENT, "");
byte[] raw = text.getBytes(Charsets.UTF_8);
if (raw.length > 0 || !LC.mime_exclude_empty_content.booleanValue() || ctype.getPrimaryType().equals("text")) {
ctxt.incrementSize("message body", raw.length);
// if the user has specified an alternative charset, make sure it exists and can encode the content
String charset = CharsetUtil.checkCharset(text, ctxt.defaultCharset);
ctype.setCharset(charset).setParameter(MimeConstants.P_CHARSET, charset);
Object content = ctype.getContentType().equals(ContentType.MESSAGE_RFC822) ? new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw)) : text;
if (mmp != null) {
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setContent(content, ctype.toString());
mmp.addBodyPart(mbp);
} else {
mm.setContent(content, ctype.toString());
}
}
if (alternatives != null) {
for (int i = 0; i < alternatives.length; i++) {
ctxt.incrementSize("alternative body", alternatives[i].getSize());
mmp.addBodyPart(alternatives[i]);
}
}
}
use of com.zimbra.common.mime.ContentType in project zm-mailbox by Zimbra.
the class ParseMimeMessage method attachContact.
private static void attachContact(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
if (!iid.isLocal()) {
attachRemoteItem(mmp, iid, contentID, ctxt, FETCH_CONTACT_PARAMS, null);
return;
}
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
VCard vcf = VCard.formatContact(mbox.getContactById(ctxt.octxt, iid.getId()));
ctxt.incrementSize("contact", vcf.getFormatted().length());
String filename = vcf.fn + ".vcf";
String charset = CharsetUtil.checkCharset(vcf.getFormatted(), ctxt.defaultCharset);
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setText(vcf.getFormatted(), charset);
mbp.setHeader("Content-Type", new ContentType("text/x-vcard", ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("name", filename).setParameter("charset", charset).toString());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT, ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("filename", filename).toString());
mbp.setContentID(contentID);
mmp.addBodyPart(mbp);
}
use of com.zimbra.common.mime.ContentType in project zm-mailbox by Zimbra.
the class ParseMimeMessage method attachDocument.
private static void attachDocument(MimeMultipart mmp, Document doc, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
ctxt.incrementSize("attached document", (long) (doc.getSize() * 1.33));
ContentType ct = new ContentType(doc.getContentType());
if (MimeConstants.isZimbraDocument(ct.getContentType())) {
ct = new ContentType(MimeConstants.CT_TEXT_HTML);
}
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(doc.getBlob(), ct.getContentType())));
mbp.setHeader("Content-Type", ct.cleanup().setParameter("name", doc.getName()).setCharset(ctxt.defaultCharset).toString());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT).setParameter("filename", doc.getName()).toString());
mbp.setContentID(contentID);
mmp.addBodyPart(mbp);
}
use of com.zimbra.common.mime.ContentType in project zm-mailbox by Zimbra.
the class ModifyProfileImage method updateDatabase.
private int updateDatabase(Mailbox mbox, OperationContext octxt, String imageFolderName, String contentType, InputStream in, String imageFileName, ZimbraSoapContext zsc) throws ServiceException {
int folderId;
try {
Folder imgFolder = mbox.getFolderByName(octxt, Mailbox.ID_FOLDER_ROOT, imageFolderName);
folderId = imgFolder.getId();
} catch (ServiceException exception) {
if (MailServiceException.NO_SUCH_FOLDER.equals(exception.getCode())) {
byte hidden = Folder.FOLDER_IS_IMMUTABLE | Folder.FOLDER_DONT_TRACK_COUNTS;
Folder.FolderOptions fopt = new Folder.FolderOptions();
fopt.setAttributes(hidden).setDefaultView(MailItem.Type.DOCUMENT).setFlags(0).setColor(MailItem.DEFAULT_COLOR_RGB);
Folder profileFolder = mbox.createFolder(octxt, imageFolderName, Mailbox.ID_FOLDER_ROOT, fopt);
folderId = profileFolder.getId();
} else {
throw ServiceException.FAILURE(FAILURE_MESSAGE, exception);
}
}
TypedIdList ids = mbox.getItemIds(octxt, folderId);
List<Integer> idList = ids.getAllIds();
MailItem[] itemList = mbox.getItemById(octxt, idList, MailItem.Type.DOCUMENT);
for (MailItem item : itemList) {
CustomMetadata customData = item.getCustomData(IMAGE_CUSTOM_DATA_SECTION);
if (customData != null && customData.containsKey("p") && customData.get("p").equals("1")) {
mbox.delete(octxt, item.getId(), MailItem.Type.DOCUMENT);
}
}
Doc imageDoc = new Doc(in, new ContentType(contentType, "image/jpeg"), imageFileName, null, "Account Profile Image");
CustomMetadata customMetaData = new CustomMetadata(IMAGE_CUSTOM_DATA_SECTION);
customMetaData.put("p", "1");
Document imgDocItem = createDocument(imageDoc, zsc, octxt, mbox, null, in, folderId, MailItem.Type.DOCUMENT, null, customMetaData, false);
return imgDocItem.getId();
}
use of com.zimbra.common.mime.ContentType in project zm-mailbox by Zimbra.
the class UserServletContext method getRequestInputStream.
public InputStream getRequestInputStream(long limit) throws IOException, ServiceException, UserServletException {
String contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
String filename = null;
InputStream is = null;
final long DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
if (limit == 0) {
if (req.getParameter("lbfums") != null) {
limit = Provisioning.getInstance().getLocalServer().getLongAttr(Provisioning.A_zimbraFileUploadMaxSize, DEFAULT_MAX_SIZE);
} else {
limit = Provisioning.getInstance().getConfig().getLongAttr(Provisioning.A_zimbraMtaMaxMessageSize, DEFAULT_MAX_SIZE);
}
}
boolean doCsrfCheck = false;
if (req.getAttribute(CsrfFilter.CSRF_TOKEN_CHECK) != null) {
doCsrfCheck = (Boolean) req.getAttribute(CsrfFilter.CSRF_TOKEN_CHECK);
}
if (ServletFileUpload.isMultipartContent(req)) {
ServletFileUpload sfu = new ServletFileUpload();
try {
FileItemIterator iter = sfu.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream fis = iter.next();
if (fis.isFormField()) {
is = fis.openStream();
params.put(fis.getFieldName(), new String(ByteUtil.getContent(is, -1), "UTF-8"));
if (doCsrfCheck && !this.csrfAuthSucceeded) {
String csrfToken = params.get(FileUploadServlet.PARAM_CSRF_TOKEN);
if (UserServlet.log.isDebugEnabled()) {
String paramValue = req.getParameter(UserServlet.QP_AUTH);
UserServlet.log.debug("CSRF check is: %s, CSRF token is: %s, Authentication recd with request is: %s", doCsrfCheck, csrfToken, paramValue);
}
if (!CsrfUtil.isValidCsrfToken(csrfToken, authToken)) {
setCsrfAuthSucceeded(Boolean.FALSE);
UserServlet.log.debug("CSRF token validation failed for account: %s" + ", Auth token is CSRF enabled: %s" + "CSRF token is: %s", authToken, authToken.isCsrfTokenEnabled(), csrfToken);
throw new UserServletException(HttpServletResponse.SC_UNAUTHORIZED, L10nUtil.getMessage(MsgKey.errMustAuthenticate));
} else {
setCsrfAuthSucceeded(Boolean.TRUE);
}
}
is.close();
is = null;
} else {
is = new UploadInputStream(fis.openStream(), limit);
break;
}
}
} catch (UserServletException e) {
throw new UserServletException(e.getHttpStatusCode(), e.getMessage(), e);
} catch (Exception e) {
throw new UserServletException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, e.toString());
}
if (is == null)
throw new UserServletException(HttpServletResponse.SC_NO_CONTENT, "No file content");
} else {
ContentType ctype = new ContentType(req.getContentType());
String contentEncoding = req.getHeader("Content-Encoding");
contentType = ctype.getContentType();
filename = ctype.getParameter("name");
if (filename == null || filename.trim().equals(""))
filename = new ContentDisposition(req.getHeader("Content-Disposition")).getParameter("filename");
is = new UploadInputStream(contentEncoding != null && contentEncoding.indexOf("gzip") != -1 ? new GZIPInputStream(req.getInputStream()) : req.getInputStream(), limit);
}
if (filename == null || filename.trim().equals(""))
filename = "unknown";
else
params.put(UserServlet.UPLOAD_NAME, filename);
params.put(UserServlet.UPLOAD_TYPE, contentType);
ZimbraLog.mailbox.info("UserServlet received file %s - %d request bytes", filename, req.getContentLength());
return is;
}
Aggregations