use of com.zimbra.cs.service.formatter.VCard in project zm-mailbox by Zimbra.
the class AddressObject method uploadToVCard.
public static VCard uploadToVCard(DavContext ctxt) throws DavException, IOException {
FileUploadServlet.Upload upload = ctxt.getUpload();
String contentType = upload.getContentType();
if (!acceptableVCardContentType(contentType, true)) {
throw new DavException.InvalidData(DavElements.CardDav.E_SUPPORTED_ADDRESS_DATA, String.format("Incorrect Content-Type '%s', expected '%s'", contentType, DavProtocol.VCARD_CONTENT_TYPE));
}
long uploadSize = upload.getSize();
if (uploadSize <= 0) {
throw new DavException.InvalidData(DavElements.CardDav.E_VALID_ADDRESS_DATA, "empty request");
}
List<VCard> vcards = null;
try (InputStream is = ctxt.getUpload().getInputStream()) {
String buf = new String(ByteUtil.getContent(is, (int) uploadSize), MimeConstants.P_CHARSET_UTF8);
vcards = VCard.parseVCard(buf);
} catch (ServiceException se) {
throw new DavException.InvalidData(DavElements.CardDav.E_VALID_ADDRESS_DATA, String.format("Problem parsing %s data - %s", DavProtocol.VCARD_CONTENT_TYPE, se.getMessage()));
}
if (vcards == null) {
throw new DavException.InvalidData(DavElements.CardDav.E_VALID_ADDRESS_DATA, String.format("Problem parsing %s data - no cards produced.", DavProtocol.VCARD_CONTENT_TYPE));
}
if (vcards.size() != 1) {
throw new DavException.InvalidData(DavElements.CardDav.E_VALID_ADDRESS_DATA, String.format("Problem parsing %s data - %d cards produced, only 1 allowed.", DavProtocol.VCARD_CONTENT_TYPE, vcards.size()));
}
VCard vcard = vcards.get(0);
if (vcard.uid == null) {
vcard.uid = UUIDUtil.generateUUID();
}
return vcard;
}
use of com.zimbra.cs.service.formatter.VCard in project zm-mailbox by Zimbra.
the class CreateContact method parseAttachedVCard.
private static List<ParsedContact> parseAttachedVCard(ZimbraSoapContext zsc, OperationContext octxt, Mailbox mbox, Element vcard) throws ServiceException {
String text = null;
String messageId = vcard.getAttribute(MailConstants.A_MESSAGE_ID, null);
String attachId = vcard.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
if (attachId != null) {
// separately-uploaded vcard attachment
Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
try {
text = new String(ByteUtil.getContent(up.getInputStream(), 0));
} catch (IOException e) {
throw ServiceException.FAILURE("error reading vCard", e);
}
} else if (messageId == null) {
// inlined content in the <vcard> element
text = vcard.getText();
} else {
// part of existing message
ItemId iid = new ItemId(messageId, zsc);
String part = vcard.getAttribute(MailConstants.A_PART);
String[] acceptableTypes = new String[] { MimeConstants.CT_TEXT_PLAIN, MimeConstants.CT_TEXT_VCARD, MimeConstants.CT_TEXT_VCARD_LEGACY, MimeConstants.CT_TEXT_VCARD_LEGACY2 };
String charsetWanted = mbox.getAccount().getAttr(Provisioning.A_zimbraPrefMailDefaultCharset, null);
text = fetchItemPart(zsc, octxt, mbox, iid, part, acceptableTypes, charsetWanted);
}
List<VCard> cards = VCard.parseVCard(text);
if (cards == null || cards.size() == 0)
throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS("no vCards present in attachment", null);
List<ParsedContact> pclist = new ArrayList<ParsedContact>(cards.size());
for (VCard vcf : cards) pclist.add(vcf.asParsedContact());
return pclist;
}
use of com.zimbra.cs.service.formatter.VCard 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.service.formatter.VCard in project zm-mailbox by Zimbra.
the class ParseMimeMessage method attachContact.
private static void attachContact(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
if (!iid.isLocal()) {
attachRemoteItem(mmp, iid, contentID, ctxt, FETCH_CONTACT_PARAMS, null);
return;
}
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
VCard vcf = VCard.formatContact(mbox.getContactById(ctxt.octxt, iid.getId()));
ctxt.incrementSize("contact", vcf.getFormatted().length());
String filename = vcf.fn + ".vcf";
String charset = CharsetUtil.checkCharset(vcf.getFormatted(), ctxt.defaultCharset);
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setText(vcf.getFormatted(), charset);
mbp.setHeader("Content-Type", new ContentType("text/x-vcard", ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("name", filename).setParameter("charset", charset).toString());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT, ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("filename", filename).toString());
mbp.setContentID(contentID);
mmp.addBodyPart(mbp);
}
use of com.zimbra.cs.service.formatter.VCard in project zm-mailbox by Zimbra.
the class SharedImapTests method testListContactsContents.
@Test(timeout = 100000)
public void testListContactsContents() throws IOException, ServiceException, MessagingException {
// create a contact
ZMailbox zmbox = TestUtil.getZMailbox(USER);
Map<String, String> attrs = new HashMap<String, String>();
String contactName = "testListContactsContents";
attrs.put("fullName", contactName);
zmbox.createContact(Integer.toString(Mailbox.ID_FOLDER_CONTACTS), null, attrs);
// connect to IMAP
String folderName = "Contacts";
connection = connect();
connection.login(PASS);
connection.select(folderName);
// fetch
Map<Long, MessageData> mdMap = connection.fetch("1:*", "(ENVELOPE BODY BODY.PEEK[])");
// verify
assertEquals("Size of map returned by fetch", 1, mdMap.size());
MessageData md = mdMap.values().iterator().next();
assertNotNull("MessageData should not be null", md);
Envelope env = md.getEnvelope();
assertNotNull("Envelope should not be null", env);
BodyStructure bs = md.getBodyStructure();
assertNotNull("Body Structure should not be null", bs);
Body[] body = md.getBodySections();
assertNotNull("body sections should not be null", body);
assertEquals("Expecting 1 body section. Found " + body.length, 1, body.length);
assertEquals("Envelope subject is wrong", contactName, env.getSubject());
assertEquals("Body type should be TEXT", "TEXT", bs.getType());
assertEquals("Body subtype should be X-VCARD", "X-VCARD", bs.getSubtype());
// fetch one contact
List<Long> uids = connection.getUids("1:*");
assertNotNull("uids should not be null", uids);
assertEquals("expecting to find 1 UID", 1, uids.size());
byte[] b = getBody(fetchMessage(connection, uids.get(0)));
assertNotNull("fetched body should not be null", b);
List<VCard> cards = VCard.parseVCard(new String(b, MimeConstants.P_CHARSET_UTF8));
assertNotNull("parsed vcards list should not be null", cards);
assertEquals("expecting to find 1 Vcard", 1, cards.size());
assertNotNull("parsed vcard should not be null", cards.get(0));
assertEquals("VCArd's full name is wrong", contactName, cards.get(0).fn);
}
Aggregations