Search in sources :

Example 1 with PatchInputStream

use of com.zimbra.cs.octosync.PatchInputStream in project zm-mailbox by Zimbra.

the class OctopusPatchFormatter method saveCallback.

// Formatter API
@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws IOException, ServiceException, UserServletException {
    log.info("Uploading patch for " + filename);
    if (filename == null) {
        throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "Missing filename");
    }
    MailItem item = null;
    Mailbox mbox = folder.getMailbox();
    try {
        item = mbox.getItemByPath(context.opContext, filename, folder.getId());
        if (!(item instanceof Document))
            throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "cannot overwrite existing object at that path");
    } catch (NoSuchItemException e) {
        log.debug("No document found at " + filename + "(folder id=" + folder.getId() + "; will create new one");
    }
    PatchInputStream pis = null;
    PatchStore.IncomingPatch ip = null;
    try {
        ip = patchStore.createIncomingPatch(context.targetAccount.getId());
        int defaultFileId = 0;
        int defaultVersion = 0;
        if (item != null) {
            defaultFileId = item.getId();
            defaultVersion = item.getVersion();
        }
        pis = PatchInputStream.create(context.getRequestInputStream(), // the current (target) user's view
        context.targetMailbox, context.opContext, defaultFileId, defaultVersion, ip.getOutputStream(), ip.getManifest());
        String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
        if (contentType == null) {
            contentType = MimeDetect.getMimeDetect().detect(filename);
            if (contentType == null)
                contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
        }
        log.debug("Storing blob");
        Blob blob = StoreManager.getInstance().storeIncoming(pis);
        log.debug("Creating parsed document; filename=" + filename + ", contentType=" + contentType + ", creator=" + creator);
        ParsedDocument pd = new ParsedDocument(blob, filename, contentType, System.currentTimeMillis(), creator, context.req.getHeader("X-Zimbra-Description"), true);
        log.debug("Parsed document created " + filename);
        // scan upload for viruses
        StringBuffer info = new StringBuffer();
        UploadScanner.Result result = UploadScanner.acceptStream(pis, info);
        if (result == UploadScanner.REJECT)
            throw MailServiceException.UPLOAD_REJECTED(filename, info.toString());
        if (result == UploadScanner.ERROR)
            throw MailServiceException.SCAN_ERROR(filename);
        if (item == null) {
            log.debug("Creating new document " + filename);
            item = mbox.createDocument(context.opContext, folder.getId(), pd, MailItem.Type.DOCUMENT, 0);
        } else {
            log.debug("Creating new version of the document " + filename + ", current version: " + item.getVersion());
            item = mbox.addDocumentRevision(context.opContext, item.getId(), pd);
        }
        patchStore.acceptPatch(ip, item.getId(), item.getVersion());
        NativeFormatter.sendZimbraHeaders(context, context.resp, item);
    } catch (PatchException e) {
        log.error("Patch upload failed: " + e);
        patchStore.rejectPatch(ip);
        throw new UserServletException(HttpServletResponse.SC_CONFLICT, "patch cannot be applied, try uploading whole file", e);
    } finally {
        try {
            pis.close();
        } catch (Exception e) {
            log.error("Exception during PatchInputStream close, ignored: " + e);
        }
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) UserServletException(com.zimbra.cs.service.UserServletException) PatchStore(com.zimbra.cs.octosync.store.PatchStore) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) UploadScanner(com.zimbra.cs.service.mail.UploadScanner) PatchException(com.zimbra.cs.octosync.PatchException) ServletException(javax.servlet.ServletException) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UserServletException(com.zimbra.cs.service.UserServletException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) PatchInputStream(com.zimbra.cs.octosync.PatchInputStream) PatchException(com.zimbra.cs.octosync.PatchException)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)1 Document (com.zimbra.cs.mailbox.Document)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)1 PatchException (com.zimbra.cs.octosync.PatchException)1 PatchInputStream (com.zimbra.cs.octosync.PatchInputStream)1 PatchStore (com.zimbra.cs.octosync.store.PatchStore)1 UserServletException (com.zimbra.cs.service.UserServletException)1 UploadScanner (com.zimbra.cs.service.mail.UploadScanner)1 Blob (com.zimbra.cs.store.Blob)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1