Search in sources :

Example 1 with IcsImportParseHandler

use of com.zimbra.cs.mailbox.calendar.IcsImportParseHandler in project zm-mailbox by Zimbra.

the class ArchiveFormatter method addData.

private void addData(UserServletContext context, Folder fldr, Map<Object, Folder> fmap, Set<MailItem.Type> types, Resolve r, boolean timestamp, ArchiveInputStream ais, ArchiveInputEntry aie, List<ServiceException> errs) throws ServiceException {
    try {
        int defaultFldr;
        Mailbox mbox = fldr.getMailbox();
        String dir, file;
        String name = aie.getName();
        int idx = name.lastIndexOf('/');
        MailItem newItem = null, oldItem;
        OperationContext oc = context.opContext;
        BufferedReader reader;
        MailItem.Type type, view;
        if (idx == -1) {
            file = name;
            dir = "";
        } else {
            file = name.substring(idx + 1);
            dir = name.substring(0, idx + 1);
            if (!dir.startsWith("/"))
                dir = '/' + dir;
        }
        if (file.length() == 0) {
            return;
        } else if (file.endsWith(".csv") || file.endsWith(".vcf")) {
            defaultFldr = Mailbox.ID_FOLDER_CONTACTS;
            type = MailItem.Type.CONTACT;
            view = MailItem.Type.CONTACT;
        } else if (file.endsWith(".eml")) {
            defaultFldr = Mailbox.ID_FOLDER_INBOX;
            type = MailItem.Type.MESSAGE;
            view = MailItem.Type.MESSAGE;
        } else if (file.endsWith(".ics")) {
            if (dir.startsWith("Tasks/")) {
                defaultFldr = Mailbox.ID_FOLDER_TASKS;
                type = MailItem.Type.TASK;
                view = MailItem.Type.TASK;
            } else {
                defaultFldr = Mailbox.ID_FOLDER_CALENDAR;
                type = MailItem.Type.APPOINTMENT;
                view = MailItem.Type.APPOINTMENT;
            }
        } else if (file.endsWith(".wiki")) {
            defaultFldr = Mailbox.ID_FOLDER_NOTEBOOK;
            type = MailItem.Type.WIKI;
            view = MailItem.Type.WIKI;
        } else {
            defaultFldr = Mailbox.ID_FOLDER_BRIEFCASE;
            type = MailItem.Type.DOCUMENT;
            view = MailItem.Type.DOCUMENT;
        }
        if (types != null && !types.contains(type)) {
            return;
        }
        if (dir.equals("")) {
            if (fldr.getPath().equals("/")) {
                fldr = mbox.getFolderById(oc, defaultFldr);
            }
            if (fldr.getDefaultView() != MailItem.Type.UNKNOWN && fldr.getDefaultView() != view && !((view == MailItem.Type.DOCUMENT || view == MailItem.Type.WIKI) && (fldr.getDefaultView() == MailItem.Type.DOCUMENT || fldr.getDefaultView() == MailItem.Type.WIKI))) {
                throw FormatterServiceException.INVALID_TYPE(view.toString(), fldr.getPath());
            }
        } else {
            String s = fldr.getPath();
            if (!s.endsWith("/"))
                s += '/';
            if (dir.startsWith(s))
                dir = dir.substring(s.length());
            fldr = createPath(context, fmap, fldr.getPath() + dir, view);
        }
        switch(type) {
            case APPOINTMENT:
            case TASK:
                boolean continueOnError = context.ignoreAndContinueOnError();
                boolean preserveExistingAlarms = context.preserveAlarms();
                InputStream is = ais.getInputStream();
                try {
                    if (aie.getSize() <= LC.calendar_ics_import_full_parse_max_size.intValue()) {
                        List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, UTF8);
                        ImportInviteVisitor visitor = new ImportInviteVisitor(oc, fldr, preserveExistingAlarms);
                        Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
                    } else {
                        ZICalendarParseHandler handler = new IcsImportParseHandler(oc, context.targetAccount, fldr, continueOnError, preserveExistingAlarms);
                        ZCalendarBuilder.parse(is, UTF8, handler);
                    }
                } finally {
                    is.close();
                }
                break;
            case CONTACT:
                if (file.endsWith(".csv")) {
                    reader = new BufferedReader(new InputStreamReader(ais.getInputStream(), UTF8));
                    ImportContacts.ImportCsvContacts(oc, context.targetMailbox, new ItemId(fldr), ContactCSV.getContacts(reader, null));
                } else {
                    List<VCard> cards = VCard.parseVCard(new String(readArchiveEntry(ais, aie), UTF8));
                    if (cards == null || cards.size() == 0 || (cards.size() == 1 && cards.get(0).fields.isEmpty())) {
                        addError(errs, FormatterServiceException.MISSING_VCARD_FIELDS(name));
                        return;
                    }
                    for (VCard vcf : cards) {
                        if (vcf.fields.isEmpty())
                            continue;
                        mbox.createContact(oc, vcf.asParsedContact(), fldr.getId(), null);
                    }
                }
                break;
            case DOCUMENT:
            case WIKI:
                String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
                try {
                    oldItem = mbox.getItemByPath(oc, file, fldr.getId());
                    if (oldItem.getType() != type) {
                        addError(errs, FormatterServiceException.MISMATCHED_TYPE(name));
                    } else if (r == Resolve.Replace) {
                        mbox.delete(oc, oldItem.getId(), type);
                        throw MailServiceException.NO_SUCH_ITEM(oldItem.getId());
                    } else if (r != Resolve.Skip) {
                        newItem = mbox.addDocumentRevision(oc, oldItem.getId(), creator, oldItem.getName(), null, ais.getInputStream());
                    }
                } catch (NoSuchItemException e) {
                    if (type == MailItem.Type.WIKI) {
                        newItem = mbox.createWiki(oc, fldr.getId(), file, creator, null, ais.getInputStream());
                    } else {
                        newItem = mbox.createDocument(oc, fldr.getId(), file, null, creator, null, ais.getInputStream());
                    }
                }
                if (newItem != null) {
                    if (timestamp)
                        mbox.setDate(oc, newItem.getId(), type, aie.getModTime());
                }
                break;
            case MESSAGE:
                int flags = aie.isUnread() ? Flag.BITMASK_UNREAD : 0;
                DeliveryOptions opt = new DeliveryOptions().setFolderId(fldr.getId()).setNoICal(true).setFlags(flags);
                mbox.addMessage(oc, ais.getInputStream(), (int) aie.getSize(), timestamp ? aie.getModTime() : ParsedMessage.DATE_HEADER, opt, null);
                break;
        }
    } catch (Exception e) {
        if (e instanceof MailServiceException && ((MailServiceException) e).getCode() == MailServiceException.QUOTA_EXCEEDED)
            throw (MailServiceException) e;
        else
            addError(errs, FormatterServiceException.UNKNOWN_ERROR(aie.getName(), e));
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IcsImportParseHandler(com.zimbra.cs.mailbox.calendar.IcsImportParseHandler) ItemId(com.zimbra.cs.service.util.ItemId) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ExportPeriodNotSpecifiedException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodNotSpecifiedException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ExportPeriodTooLongException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodTooLongException) UserServletException(com.zimbra.cs.service.UserServletException) MailItem(com.zimbra.cs.mailbox.MailItem) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ImportInviteVisitor(com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor) BufferedReader(java.io.BufferedReader) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZICalendarParseHandler(com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions)

Example 2 with IcsImportParseHandler

use of com.zimbra.cs.mailbox.calendar.IcsImportParseHandler in project zm-mailbox by Zimbra.

the class IcsFormatter method saveCallback.

@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws UserServletException, ServiceException, IOException, ServletException {
    boolean continueOnError = context.ignoreAndContinueOnError();
    boolean preserveExistingAlarms = context.preserveAlarms();
    InputStream is = context.getRequestInputStream(-1);
    String charset = MimeConstants.P_CHARSET_UTF8;
    String ctStr = context.req.getContentType();
    if (ctStr != null) {
        String cs = Mime.getCharset(ctStr);
        if (cs != null)
            charset = cs;
    }
    try {
        if (context.req.getContentLength() <= LC.calendar_ics_import_full_parse_max_size.intValue()) {
            // Build a list of ZVCalendar objects by fully parsing the ics file, then iterate them
            // and add them one by one.  Memory hungry if there are very many events/tasks, but it allows
            // TZID reference before VTIMEZONE of that timezone appears in the ics file.
            List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, charset);
            ImportInviteVisitor visitor = new ImportInviteVisitor(context.opContext, folder, preserveExistingAlarms);
            Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
        } else {
            // Events/tasks are added in callbacks during parse.  This is more memory efficient than the
            // other method, but it doesn't allow forward referencing TZIDs.  ics files generated by
            // clients that put VTIMEZONEs at the end will not parse.  Evolution client does this.
            ZICalendarParseHandler handler = new IcsImportParseHandler(context.opContext, context.targetAccount, folder, continueOnError, preserveExistingAlarms);
            ZCalendarBuilder.parse(is, charset, handler);
        }
    } finally {
        is.close();
    }
}
Also used : ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) InputStream(java.io.InputStream) ImportInviteVisitor(com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor) IcsImportParseHandler(com.zimbra.cs.mailbox.calendar.IcsImportParseHandler) ZICalendarParseHandler(com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler)

Aggregations

ZICalendarParseHandler (com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler)2 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)2 IcsImportParseHandler (com.zimbra.cs.mailbox.calendar.IcsImportParseHandler)2 ImportInviteVisitor (com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor)2 InputStream (java.io.InputStream)2 ServiceException (com.zimbra.common.service.ServiceException)1 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 ExportPeriodNotSpecifiedException (com.zimbra.cs.mailbox.MailServiceException.ExportPeriodNotSpecifiedException)1 ExportPeriodTooLongException (com.zimbra.cs.mailbox.MailServiceException.ExportPeriodTooLongException)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 UserServletException (com.zimbra.cs.service.UserServletException)1 ItemId (com.zimbra.cs.service.util.ItemId)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1