use of com.zimbra.cs.service.formatter.ContactCSV.ParseException 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);
}
Aggregations