Search in sources :

Example 1 with Content

use of com.zimbra.soap.mail.type.Content 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 Content

use of com.zimbra.soap.mail.type.Content 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)2 ImportContactsResponse (com.zimbra.soap.mail.message.ImportContactsResponse)2 Content (com.zimbra.soap.mail.type.Content)2 ZMailbox (com.zimbra.client.ZMailbox)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