Search in sources :

Example 21 with Document

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

the class PatchInputStreamTest method createNewDocManyDataNoRef.

@Test
public void createNewDocManyDataNoRef() throws Exception {
    PatchBuilder patchBuilder = new PatchBuilder();
    patchBuilder.addData("hello world ");
    patchBuilder.addData("vmware rocks");
    patchBuilder.addData(" foo bar");
    InputStream is = new ByteArrayInputStream(patchBuilder.toByteArray());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    PatchInputStream pis = PatchInputStream.create(is, mbox, null, 0, 0, null, null);
    Document doc = createDocument(mbox, "filename", pis);
    Assert.assertEquals("filename", doc.getName());
    Assert.assertEquals("hello world vmware rocks foo bar", doc.getFragment());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Test(org.junit.Test)

Example 22 with Document

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

the class PatchInputStreamTest method createNewDocRefNoData.

@Test
public void createNewDocRefNoData() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Document doc = createDocument(mbox, "filename", "hello world");
    Assert.assertEquals("filename", doc.getName());
    Assert.assertEquals("hello world", doc.getFragment());
    PatchBuilder patchBuilder = new PatchBuilder();
    PatchRef pref = new PatchRef();
    pref.fileId = doc.getId();
    pref.fileVersion = doc.getVersion();
    pref.offset = 6;
    pref.length = 5;
    pref.hashKey = patchBuilder.calcSHA256("world");
    patchBuilder.addRef(pref);
    InputStream is = new ByteArrayInputStream(patchBuilder.toByteArray());
    PatchInputStream pis = PatchInputStream.create(is, mbox, null, 0, 0, null, null);
    Document doc2 = createDocument(mbox, "filename2", pis);
    Assert.assertEquals("filename2", doc2.getName());
    Assert.assertEquals("world", doc2.getFragment());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Test(org.junit.Test)

Example 23 with Document

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

the class PatchInputStreamTest method createNewRevisionRefAndData.

@Test
public void createNewRevisionRefAndData() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Document doc = createDocument(mbox, "filename", "hello world");
    Assert.assertEquals("filename", doc.getName());
    Assert.assertEquals("hello world", doc.getFragment());
    PatchBuilder patchBuilder = new PatchBuilder();
    PatchRef pref = new PatchRef();
    pref.fileId = doc.getId();
    pref.fileVersion = doc.getVersion();
    pref.offset = 0;
    pref.length = 5;
    pref.hashKey = patchBuilder.calcSHA256("hello");
    patchBuilder.addRef(pref);
    patchBuilder.addData(" foobar");
    InputStream is = new ByteArrayInputStream(patchBuilder.toByteArray());
    PatchInputStream pis = PatchInputStream.create(is, mbox, null, 0, 0, null, null);
    Document doc2 = addDocumentVersion(mbox, "filename", doc.getId(), pis);
    Assert.assertEquals("filename", doc2.getName());
    Assert.assertEquals("hello foobar", doc2.getFragment());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Test(org.junit.Test)

Example 24 with Document

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

the class OctopusPatchFormatter method formatCallback.

// Formatter API
@Override
public void formatCallback(UserServletContext context) throws UserServletException, ServiceException, IOException, ServletException {
    if (!(context.target instanceof Document)) {
        throw UserServletException.notImplemented("can only handle documents");
    }
    Document doc = (Document) context.target;
    String v = context.params.get(UserServlet.QP_VERSION);
    int version = v != null ? Integer.parseInt(v) : -1;
    if (log.isDebugEnabled()) {
        log.debug("Request received for patch for " + doc.getName() + ", id: " + doc.getId() + (version == -1 ? ", latest version" : (", version: " + version)));
    }
    NativeFormatter.sendZimbraHeaders(context, context.resp, context.target);
    HttpUtil.Browser browser = HttpUtil.guessBrowser(context.req);
    if (browser == HttpUtil.Browser.IE) {
        // turn off content detection..
        context.resp.addHeader("X-Content-Type-Options", "nosniff");
    }
    if (version > 0) {
        doc = (Document) doc.getMailbox().getItemRevision(context.opContext, doc.getId(), doc.getType(), version);
    } else {
        version = doc.getVersion();
        if (log.isDebugEnabled()) {
            log.debug("Latest version of " + doc.getName() + " is " + version);
        }
    }
    StoredPatch sp = patchStore.lookupPatch(context.targetAccount.getId(), doc.getId(), version);
    if (sp != null) {
        sendPatch(context, doc, version, sp);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Patch not available for " + doc.getName() + ", id: " + doc.getId() + ", version: " + version + "; will return the entire file");
        }
        sendFullFile(context, doc, version);
    }
}
Also used : HttpUtil(com.zimbra.common.util.HttpUtil) StoredPatch(com.zimbra.cs.octosync.store.PatchStore.StoredPatch) Document(com.zimbra.cs.mailbox.Document) ParsedDocument(com.zimbra.cs.mime.ParsedDocument)

Example 25 with Document

use of com.zimbra.cs.mailbox.Document 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

Document (com.zimbra.cs.mailbox.Document)34 Mailbox (com.zimbra.cs.mailbox.Mailbox)23 MailItem (com.zimbra.cs.mailbox.MailItem)14 Test (org.junit.Test)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)12 ServiceException (com.zimbra.common.service.ServiceException)9 InputStream (java.io.InputStream)9 Element (com.zimbra.common.soap.Element)8 Account (com.zimbra.cs.account.Account)8 Folder (com.zimbra.cs.mailbox.Folder)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)8 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)8 OperationContext (com.zimbra.cs.mailbox.OperationContext)8 IOException (java.io.IOException)8 Message (com.zimbra.cs.mailbox.Message)7 MimeMessage (javax.mail.internet.MimeMessage)6 ZMailbox (com.zimbra.client.ZMailbox)5 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)5 Contact (com.zimbra.cs.mailbox.Contact)5