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());
}
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());
}
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());
}
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);
}
}
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);
}
}
}
Aggregations