Search in sources :

Example 1 with ImportContactsRequest

use of com.zimbra.soap.mail.message.ImportContactsRequest in project zm-mailbox by Zimbra.

the class ZMailbox method importContacts.

public ZImportContactsResult importContacts(String folderId, String type, String attachmentId) throws ServiceException {
    ImportContactsRequest request = new ImportContactsRequest();
    request.setContentType(type);
    request.setFolderId(folderId);
    Content importContent = new Content();
    importContent.setAttachUploadId(attachmentId);
    request.setContent(importContent);
    ImportContactsResponse res = this.invokeJaxb(request);
    return new ZImportContactsResult(res);
}
Also used : ImportContactsRequest(com.zimbra.soap.mail.message.ImportContactsRequest) Content(com.zimbra.soap.mail.type.Content) ImportContactsResponse(com.zimbra.soap.mail.message.ImportContactsResponse)

Example 2 with ImportContactsRequest

use of com.zimbra.soap.mail.message.ImportContactsRequest in project zm-mailbox by Zimbra.

the class JaxbToElementTest method importContactsWithContentTypeAsElementTest.

/**
     * Check that @{link JaxbUtil.elementToJaxb} will accept XML where
     * JAXB expects content type as an attribute but it is specified as
     * an element.
     * @throws Exception
     */
@Test
public void importContactsWithContentTypeAsElementTest() throws Exception {
    Element icrElem = Element.XMLElement.mFactory.createElement(MailConstants.IMPORT_CONTACTS_REQUEST);
    icrElem.addAttribute(MailConstants.A_CSVLOCALE, "fr");
    icrElem.addNonUniqueElement(MailConstants.A_CONTENT_TYPE).setText("csv");
    icrElem.addNonUniqueElement(MailConstants.E_CONTENT).setText("CONTENT");
    ImportContactsRequest icr = JaxbUtil.elementToJaxb(icrElem);
    Assert.assertEquals("ImportContactsRequest content type:", "csv", icr.getContentType());
    Assert.assertEquals("ImportContactsRequest csvlocale:", "fr", icr.getCsvLocale());
    Assert.assertEquals("ImportContactsRequest contents:", "CONTENT", icr.getContent().getValue());
}
Also used : ImportContactsRequest(com.zimbra.soap.mail.message.ImportContactsRequest) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 3 with ImportContactsRequest

use of com.zimbra.soap.mail.message.ImportContactsRequest in project zm-mailbox by Zimbra.

the class ImportContacts method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    ImportContactsRequest req = JaxbUtil.elementToJaxb(request);
    String ct = req.getContentType();
    if (!ZMailbox.CONTACT_IMPORT_TYPE_CSV.equals(ct))
        throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
    String folder = req.getFolderId();
    if (folder == null)
        folder = this.DEFAULT_FOLDER_ID;
    ItemId iidFolder = new ItemId(folder, zsc);
    String format = req.getCsvFormat();
    String locale = req.getCsvLocale();
    Content reqContent = req.getContent();
    List<Map<String, String>> contacts = null;
    List<Upload> uploads = null;
    BufferedReader reader = null;
    String attachment = reqContent.getAttachUploadId();
    try {
        if (attachment == null) {
            // Convert LF to CRLF because the XML parser normalizes element text to LF.
            String text = StringUtil.lfToCrlf(reqContent.getValue());
            reader = new BufferedReader(new StringReader(text));
        } else {
            reader = parseUploadedContent(zsc, attachment, uploads = new ArrayList<Upload>());
        }
        contacts = ContactCSV.getContacts(reader, format, locale);
        reader.close();
    } catch (IOException e) {
        throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS(e.getMessage(), e);
    } catch (ParseException e) {
        throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS(e.getMessage(), e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        if (attachment != null) {
            FileUploadServlet.deleteUploads(uploads);
        }
    }
    List<ItemId> idsList = ImportCsvContacts(octxt, mbox, iidFolder, contacts);
    ImportContactsResponse resp = new ImportContactsResponse();
    ImportContact impCntct = new ImportContact();
    for (ItemId iid : idsList) {
        impCntct.addCreatedId(iid.toString(ifmt));
    }
    impCntct.setNumImported(contacts.size());
    resp.setContact(impCntct);
    return zsc.jaxbToElement(resp);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ImportContact(com.zimbra.soap.mail.type.ImportContact) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) ImportContactsRequest(com.zimbra.soap.mail.message.ImportContactsRequest) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) ImportContactsResponse(com.zimbra.soap.mail.message.ImportContactsResponse) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Content(com.zimbra.soap.mail.type.Content) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ParseException(com.zimbra.cs.service.formatter.ContactCSV.ParseException) Map(java.util.Map)

Aggregations

ImportContactsRequest (com.zimbra.soap.mail.message.ImportContactsRequest)3 ImportContactsResponse (com.zimbra.soap.mail.message.ImportContactsResponse)2 Content (com.zimbra.soap.mail.type.Content)2 ZMailbox (com.zimbra.client.ZMailbox)1 Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 Upload (com.zimbra.cs.service.FileUploadServlet.Upload)1 ParseException (com.zimbra.cs.service.formatter.ContactCSV.ParseException)1 ItemId (com.zimbra.cs.service.util.ItemId)1 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 ImportContact (com.zimbra.soap.mail.type.ImportContact)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Map (java.util.Map)1 JAXBElement (javax.xml.bind.JAXBElement)1