use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method staleReference.
@Test
public void staleReference() throws Exception {
Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// first, create the original draft
OperationContext octxt = new OperationContext(acct);
Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
attach = el.addElement(MailConstants.E_ATTACH);
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
ZimbraSoapContext zsc = getMockSoapContext();
MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// then, create a new draft that references one of the original draft's attachments
attach.detach();
(attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
// delete the draft itself and then try to save the new draft
mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// check that the attachment's content is present and correct
MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
Assert.assertEquals("2 parts in draft", 2, multi.getCount());
Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method attachZimbraDocument.
@Test
public void attachZimbraDocument() throws Exception {
Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
OperationContext octxt = new OperationContext(acct);
Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
Element el = new Element.JSONElement(MailConstants.E_MSG);
el.addAttribute(MailConstants.E_SUBJECT, "attach message");
el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
ZimbraSoapContext zsc = getMockSoapContext();
MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
MimeMultipart mmp = (MimeMultipart) mm.getContent();
MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class PatchInputStreamTest method createNewDocOneDataNoRef.
@Test
public void createNewDocOneDataNoRef() throws Exception {
PatchBuilder patchBuilder = new PatchBuilder();
patchBuilder.addData("hello world");
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", doc.getFragment());
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class Collection method createItem.
// create Document at the URI
public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException {
Mailbox mbox = null;
try {
mbox = getMailbox(ctxt);
} catch (ServiceException e) {
throw new DavException("cannot get mailbox", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
FileUploadServlet.Upload upload = ctxt.getUpload();
String ctype = upload.getContentType();
if (ctype != null && (ctype.startsWith(DavProtocol.VCARD_CONTENT_TYPE) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY2))) {
// vCard MIME types such as text/x-vcard or text/directory.
return createVCard(ctxt, name);
}
String author = ctxt.getAuthAccount().getName();
try {
// add a revision if the resource already exists
MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), ctxt.getPath());
if (item.getType() != MailItem.Type.DOCUMENT && item.getType() != MailItem.Type.WIKI) {
throw new DavException("no DAV resource for " + item.getType(), HttpServletResponse.SC_NOT_ACCEPTABLE, null);
}
Document doc = mbox.addDocumentRevision(ctxt.getOperationContext(), item.getId(), author, name, null, upload.getInputStream());
return new Notebook(ctxt, doc);
} catch (ServiceException e) {
if (!(e instanceof NoSuchItemException))
throw new DavException("cannot get item ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
// create
try {
Document doc = mbox.createDocument(ctxt.getOperationContext(), mId, name, ctype, author, null, upload.getInputStream());
Notebook notebook = new Notebook(ctxt, doc);
notebook.mNewlyCreated = true;
return notebook;
} catch (ServiceException se) {
throw new DavException("cannot create ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, se);
}
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class UrlNamespace method getResourceFromMailItem.
/* Returns DavResource for the MailItem. */
public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException {
DavResource resource = null;
if (item == null) {
return resource;
}
MailItem.Type itemType = item.getType();
try {
MailItem.Type viewType;
switch(itemType) {
case MOUNTPOINT:
Mountpoint mp = (Mountpoint) item;
viewType = mp.getDefaultView();
// don't expose mounted calendars when using iCal style delegation model.
if (!ctxt.useIcalDelegation() && (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK)) {
resource = new RemoteCalendarCollection(ctxt, mp);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new RemoteAddressbookCollection(ctxt, mp);
} else {
resource = new RemoteCollection(ctxt, mp);
}
break;
case FOLDER:
Folder f = (Folder) item;
viewType = f.getDefaultView();
if (f.getId() == Mailbox.ID_FOLDER_INBOX && DavResource.isSchedulingEnabled()) {
resource = new ScheduleInbox(ctxt, f);
} else if (f.getId() == Mailbox.ID_FOLDER_SENT && DavResource.isSchedulingEnabled()) {
resource = new ScheduleOutbox(ctxt, f);
} else if (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK) {
resource = getCalendarCollection(ctxt, f);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new AddressbookCollection(ctxt, f);
} else {
resource = new Collection(ctxt, f);
}
break;
case DOCUMENT:
resource = new Notebook(ctxt, (Document) item);
break;
case APPOINTMENT:
case TASK:
resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem) item);
break;
case MESSAGE:
resource = getCalendarItemForMessage(ctxt, (Message) item);
break;
case CONTACT:
resource = new AddressObject(ctxt, (Contact) item);
break;
default:
break;
}
} catch (ServiceException e) {
ZimbraLog.dav.info("cannot create DavResource", e);
}
return resource;
}
Aggregations