Search in sources :

Example 16 with ContentDisposition

use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.

the class ParsedContact method parseBlob.

private static List<Attachment> parseBlob(InputStream in) throws ServiceException, MessagingException, IOException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), in);
    MimeMultipart multi = null;
    try {
        multi = (MimeMultipart) mm.getContent();
    } catch (ClassCastException x) {
        throw ServiceException.FAILURE("MimeMultipart content expected but got " + mm.getContent().toString(), x);
    }
    List<Attachment> attachments = new ArrayList<Attachment>(multi.getCount());
    for (int i = 1; i <= multi.getCount(); i++) {
        MimeBodyPart bp = (MimeBodyPart) multi.getBodyPart(i - 1);
        ContentDisposition cdisp = new ContentDisposition(bp.getHeader("Content-Disposition", null));
        Attachment attachment = new Attachment(bp.getDataHandler(), cdisp.getParameter("field"));
        attachment.setPartName(Integer.toString(i));
        attachments.add(attachment);
    }
    return attachments;
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ArrayList(java.util.ArrayList) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 17 with ContentDisposition

use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.

the class ParseMimeMessage method attachUpload.

private static void attachUpload(MimeMultipart mmp, Upload up, String contentID, ParseMessageContext ctxt, ContentType ctypeOverride, String contentDescription, String contentDisposition) throws ServiceException, MessagingException {
    // make sure we haven't exceeded the max size
    ctxt.incrementSize("upload " + up.getName(), (long) (up.getSize() * 1.33));
    // scan upload for viruses
    StringBuffer info = new StringBuffer();
    UploadScanner.Result result = UploadScanner.accept(up, info);
    if (result == UploadScanner.REJECT) {
        throw MailServiceException.UPLOAD_REJECTED(up.getName(), info.toString());
    } else if (result == UploadScanner.ERROR) {
        throw MailServiceException.SCAN_ERROR(up.getName());
    }
    String filename = up.getName();
    // create the part and override the DataSource's default ctype, if required
    MimeBodyPart mbp = new ForceBase64MimeBodyPart();
    UploadDataSource uds = new UploadDataSource(up);
    if (ctypeOverride != null && !ctypeOverride.equals("")) {
        uds.setContentType(ctypeOverride);
    }
    mbp.setDataHandler(new DataHandler(uds));
    // set headers -- ctypeOverride non-null has magical properties that I'm going to regret tomorrow
    ContentType ctype = ctypeOverride;
    ContentDisposition cdisp;
    if (Part.INLINE.equalsIgnoreCase(contentDisposition)) {
        cdisp = new ContentDisposition(Part.INLINE, ctxt.use2231);
    } else {
        cdisp = new ContentDisposition(Part.ATTACHMENT, ctxt.use2231);
    }
    if (ctype == null) {
        ctype = new ContentType(up.getContentType() == null ? MimeConstants.CT_APPLICATION_OCTET_STREAM : up.getContentType());
        ctype.cleanup().setParameter("name", filename);
        cdisp.setParameter("filename", filename);
    }
    mbp.setHeader("Content-Type", ctype.setCharset(ctxt.defaultCharset).toString());
    mbp.setHeader("Content-Disposition", cdisp.setCharset(ctxt.defaultCharset).toString());
    if (contentDescription != null) {
        mbp.setHeader("Content-Description", contentDescription);
    }
    if (ctype.getContentType().equals(MimeConstants.CT_APPLICATION_PDF)) {
        mbp.setHeader("Content-Transfer-Encoding", "base64");
    }
    mbp.setContentID(contentID);
    // add to the parent part
    mmp.addBodyPart(mbp);
}
Also used : ContentType(com.zimbra.common.mime.ContentType) ContentDisposition(com.zimbra.common.mime.ContentDisposition) UploadDataSource(com.zimbra.cs.service.UploadDataSource) DataHandler(javax.activation.DataHandler) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 18 with ContentDisposition

use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.

the class CollectLDAPConfigZimbra method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        //check the auth token
        AuthToken authToken = getAdminAuthTokenFromCookie(req, resp);
        if (authToken == null) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
        if (!authToken.isAdmin()) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
        //find the LDAP master
        Provisioning prov = Provisioning.getInstance();
        String ldapHost = LC.ldap_host.value();
        if (ldapHost == null) {
            throw ServiceException.INVALID_REQUEST("Cannot find value for ldap_host in local config", null);
        }
        Server server = prov.get(Key.ServerBy.name, ldapHost);
        if (server == null) {
            throw ServiceException.INVALID_REQUEST("Cannot find server record for LDAP master host: " + ldapHost, null);
        }
        //call RemoteManager
        RemoteManager rmgr = RemoteManager.getRemoteManager(server);
        RemoteResult rr = rmgr.execute(RemoteCommands.COLLECT_LDAP_ZIMBRA);
        //stream the data
        resp.setContentType(DOWNLOAD_CONTENT_TYPE);
        ContentDisposition cd = new ContentDisposition(Part.INLINE).setParameter("filename", ldapHost + ".ldif.gz");
        resp.addHeader("Content-Disposition", cd.toString());
        ByteUtil.copy(new ByteArrayInputStream(rr.getMStdout()), true, resp.getOutputStream(), false);
    } catch (ServiceException e) {
        returnError(resp, e);
        return;
    }
}
Also used : RemoteResult(com.zimbra.cs.rmgmt.RemoteResult) Server(com.zimbra.cs.account.Server) ContentDisposition(com.zimbra.common.mime.ContentDisposition) ServiceException(com.zimbra.common.service.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteManager(com.zimbra.cs.rmgmt.RemoteManager) AuthToken(com.zimbra.cs.account.AuthToken) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

ContentDisposition (com.zimbra.common.mime.ContentDisposition)18 ContentType (com.zimbra.common.mime.ContentType)8 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)8 IOException (java.io.IOException)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 ServiceException (com.zimbra.common.service.ServiceException)6 DataHandler (javax.activation.DataHandler)6 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)5 MessagingException (javax.mail.MessagingException)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Server (com.zimbra.cs.account.Server)4 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)4 AuthToken (com.zimbra.cs.account.AuthToken)3 Provisioning (com.zimbra.cs.account.Provisioning)3 InputStream (java.io.InputStream)3 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)3 Header (org.apache.commons.httpclient.Header)3 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2