use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class CreateContact method parseAttachment.
private static Attachment parseAttachment(Element elt, String name, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
// check for uploaded attachment
String attachId = elt.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
if (attachId != null) {
if (Contact.isSMIMECertField(name)) {
elt.setText(parseCertificate(elt, name, zsc, octxt, existing));
return null;
} else {
Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
UploadDataSource uds = new UploadDataSource(up);
return new Attachment(new DataHandler(uds), name, (int) up.getSize());
}
}
int itemId = (int) elt.getAttributeLong(MailConstants.A_ID, -1);
String part = elt.getAttribute(MailConstants.A_PART, null);
if (itemId != -1 || (part != null && existing != null)) {
MailItem item = itemId == -1 ? existing : getRequestedMailbox(zsc).getItemById(octxt, itemId, MailItem.Type.UNKNOWN);
try {
if (item instanceof Contact) {
Contact contact = (Contact) item;
if (part != null && !part.equals("")) {
try {
int partNum = Integer.parseInt(part) - 1;
if (partNum >= 0 && partNum < contact.getAttachments().size()) {
Attachment att = contact.getAttachments().get(partNum);
return new Attachment(att.getDataHandler(), name, att.getSize());
}
} catch (NumberFormatException nfe) {
}
throw ServiceException.INVALID_REQUEST("invalid contact part number: " + part, null);
} else {
VCard vcf = VCard.formatContact(contact);
return new Attachment(vcf.getFormatted().getBytes("utf-8"), "text/x-vcard; charset=utf-8", name, vcf.fn + ".vcf");
}
} else if (item instanceof Message) {
Message msg = (Message) item;
if (part != null && !part.equals("")) {
try {
MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
if (mp == null) {
throw MailServiceException.NO_SUCH_PART(part);
}
DataSource ds = new MimePartDataSource(mp);
return new Attachment(new DataHandler(ds), name);
} catch (MessagingException me) {
throw ServiceException.FAILURE("error parsing blob", me);
}
} else {
DataSource ds = new MessageDataSource(msg);
return new Attachment(new DataHandler(ds), name, (int) msg.getSize());
}
} else if (item instanceof Document) {
Document doc = (Document) item;
if (part != null && !part.equals("")) {
throw MailServiceException.NO_SUCH_PART(part);
}
DataSource ds = new DocumentDataSource(doc);
return new Attachment(new DataHandler(ds), name, (int) doc.getSize());
}
} catch (IOException ioe) {
throw ServiceException.FAILURE("error attaching existing item data", ioe);
} catch (MessagingException e) {
throw ServiceException.FAILURE("error attaching existing item data", e);
}
}
return null;
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class WikiPage method findTemplatePage.
private static WikiPage findTemplatePage(WikiPage.WikiContext ctxt, String wikiAccountName, String template) throws ServiceException {
Provisioning prov = Provisioning.getInstance();
Account acct = prov.get(Key.AccountBy.name, wikiAccountName);
if (acct == null)
throw ServiceException.FAILURE("wiki account " + wikiAccountName + " does not exist, please check " + Provisioning.A_zimbraNotebookAccount + " on the domain or global config", null);
WikiPage page = null;
if (Provisioning.onLocalServer(acct)) {
try {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
MailItem item = mbox.getItemByPath(ctxt.octxt, template);
if (item instanceof Document)
page = WikiPage.create((Document) item);
} catch (ServiceException se) {
}
} else {
page = WikiPage.create(acct.getId(), template);
try {
page.getContents(ctxt);
} catch (ServiceException se) {
page = null;
}
}
return page;
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method attachPdfDocument.
@Test
public void attachPdfDocument() 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_PDF, "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_APPLICATION_PDF, new ContentType(part.getContentType()).getContentType());
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class DbVolumeBlobsTest method revisionBlobs.
@Test
public void revisionBlobs() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Map<String, String> digestToPath = new HashMap<String, String>();
Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
InputStream in = new ByteArrayInputStream("testcontent".getBytes());
ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
int baseId = doc.getId();
for (int i = 0; i < 10; i++) {
in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
doc = mbox.addDocumentRevision(null, baseId, pd);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
}
Iterable<MailboxBlobInfo> allBlobs = null;
allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
for (MailboxBlobInfo info : allBlobs) {
DbVolumeBlobs.addBlobReference(conn, info);
}
List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
Assert.assertEquals(digestToPath.size(), blobs.size());
for (BlobReference blob : blobs) {
String path = digestToPath.remove(blob.getDigest());
Assert.assertNotNull(path);
Assert.assertEquals(path, getPath(blob));
}
Assert.assertTrue(digestToPath.isEmpty());
}
use of com.zimbra.cs.mailbox.Document in project zm-mailbox by Zimbra.
the class DbVolumeBlobsTest method revisionDumpsterBlobs.
@Test
public void revisionDumpsterBlobs() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Map<String, String> digestToPath = new HashMap<String, String>();
Volume vol = VolumeManager.getInstance().getCurrentMessageVolume();
InputStream in = new ByteArrayInputStream("testcontent".getBytes());
ParsedDocument pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
Document doc = mbox.createDocument(null, Mailbox.ID_FOLDER_BRIEFCASE, pd, MailItem.Type.DOCUMENT, 0);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
int baseId = doc.getId();
for (int i = 0; i < 10; i++) {
in = new ByteArrayInputStream(("testcontent-new-" + i).getBytes());
pd = new ParsedDocument(in, "docname", "text/plain", System.currentTimeMillis(), null, null);
doc = mbox.addDocumentRevision(null, baseId, pd);
digestToPath.put(doc.getDigest(), doc.getBlob().getLocalBlob().getFile().getPath());
}
mbox.delete(null, baseId, MailItem.Type.DOCUMENT);
mbox.emptyFolder(null, Mailbox.ID_FOLDER_TRASH, false);
Iterable<MailboxBlobInfo> allBlobs = null;
allBlobs = DbMailItem.getAllBlobs(conn, mbox.getSchemaGroupId(), vol.getId(), -1, -1);
for (MailboxBlobInfo info : allBlobs) {
DbVolumeBlobs.addBlobReference(conn, info);
}
List<BlobReference> blobs = DbVolumeBlobs.getBlobReferences(conn, vol);
Assert.assertEquals(digestToPath.size(), blobs.size());
for (BlobReference blob : blobs) {
String path = digestToPath.remove(blob.getDigest());
Assert.assertNotNull(path);
Assert.assertEquals(path, getPath(blob));
}
Assert.assertTrue(digestToPath.isEmpty());
}
Aggregations