Search in sources :

Example 11 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class TestCalDav method testFuzzyTimeZoneMatchGMT_06.

@Test
public void testFuzzyTimeZoneMatchGMT_06() throws Exception {
    try (ByteArrayInputStream bais = new ByteArrayInputStream(VtimeZoneGMT_0600_0500.getBytes())) {
        ZVCalendar tzcal = ZCalendar.ZCalendarBuilder.build(bais, MimeConstants.P_CHARSET_UTF8);
        assertNotNull("tzcal", tzcal);
        ZComponent tzcomp = tzcal.getComponent(ICalTok.VTIMEZONE);
        assertNotNull("tzcomp", tzcomp);
        ICalTimeZone tz = ICalTimeZone.fromVTimeZone(tzcomp, false, /* skipLookup */
        ICalTimeZone.TZID_NAME_ASSIGNMENT_BEHAVIOR.ALWAYS_KEEP);
        ICalTimeZone matchtz = ICalTimeZone.lookupMatchingWellKnownTZ(tz);
        assertEquals("ID of Timezone which fuzzy matches GMT=06.00/-05.00", "America/Chicago", matchtz.getID());
    }
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ByteArrayInputStream(java.io.ByteArrayInputStream) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) Test(org.junit.Test)

Example 12 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class ParsedMessage method analyzePart.

/**
     * @return Extracted toplevel text (any text that should go into the toplevel indexed document)
     */
private String analyzePart(boolean isMainBody, MPartInfo mpi) throws MessagingException, ServiceException {
    boolean ignoreCalendar;
    if (calendarPartInfo == null) {
        ignoreCalendar = isBouncedCalendar(mpi);
    } else {
        ignoreCalendar = true;
    }
    String methodParam = (new ContentType(mpi.getMimePart().getContentType())).getParameter("method");
    if (methodParam == null && !LC.calendar_allow_invite_without_method.booleanValue()) {
        ignoreCalendar = true;
    }
    String toRet = "";
    try {
        // ignore multipart "container" parts
        if (mpi.isMultipart()) {
            return toRet;
        }
        String ctype = mpi.getContentType();
        MimeHandler handler = MimeHandlerManager.getMimeHandler(ctype, mpi.getFilename());
        assert (handler != null);
        handler.setDefaultCharset(defaultCharset);
        Mime.repairTransferEncoding(mpi.getMimePart());
        if (handler.isIndexingEnabled()) {
            handler.init(mpi.getMimePart().getDataHandler().getDataSource());
            handler.setPartName(mpi.getPartName());
            handler.setFilename(mpi.getFilename());
            handler.setSize(mpi.getSize());
            // remember the first iCalendar attachment
            if (!ignoreCalendar && calendarPartInfo == null) {
                ZVCalendar cal = handler.getICalendar();
                if (cal != null) {
                    setCalendarPartInfo(mpi, cal);
                }
            }
            //     - IndexAttachments was set and !disableIndexingAttachmentsTogether
            if ((isMainBody && (!handler.runsExternally() || indexAttachments)) || (indexAttachments && !DebugConfig.disableIndexingAttachmentsTogether)) {
                toRet = handler.getContent();
            }
            if (indexAttachments && !DebugConfig.disableIndexingAttachmentsSeparately) {
                // Each non-text MIME part is also indexed as a separate
                // Lucene document.  This is necessary so that we can tell the
                // client what parts match if a search matched a particular
                // part.
                IndexDocument doc = new IndexDocument(handler.getDocument());
                String filename = handler.getFilename();
                if (!Strings.isNullOrEmpty(filename)) {
                    filenames.add(filename);
                }
                doc.addSortSize(mpi.getMimePart().getSize());
                luceneDocuments.add(setLuceneHeadersFromContainer(doc));
            }
        }
        // make sure we've got the text/calendar handler installed
        if (!ignoreCalendar && calendarPartInfo == null && ctype.equals(MimeConstants.CT_TEXT_CALENDAR)) {
            if (handler.isIndexingEnabled()) {
                ZimbraLog.index.warn("TextCalendarHandler not correctly installed");
            }
            InputStream is = null;
            try {
                String charset = mpi.getContentTypeParameter(MimeConstants.P_CHARSET);
                if (charset == null || charset.trim().isEmpty()) {
                    charset = MimeConstants.P_CHARSET_DEFAULT;
                }
                is = mpi.getMimePart().getInputStream();
                ZVCalendar cal = ZCalendarBuilder.build(is, charset);
                if (cal != null) {
                    setCalendarPartInfo(mpi, cal);
                }
            } catch (IOException ioe) {
                ZimbraLog.index.warn("error reading text/calendar mime part", ioe);
            } finally {
                ByteUtil.closeStream(is);
            }
        }
    } catch (MimeHandlerException e) {
        handleParseError(mpi, e);
    } catch (ObjectHandlerException e) {
        handleParseError(mpi, e);
    }
    return toRet;
}
Also used : IndexDocument(com.zimbra.cs.index.IndexDocument) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ContentType(com.zimbra.common.mime.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) SharedInputStream(javax.mail.internet.SharedInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) BlobInputStream(com.zimbra.cs.store.BlobInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectHandlerException(com.zimbra.cs.object.ObjectHandlerException)

Example 13 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class ICalReply method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element icalElem = request.getElement(MailConstants.E_CAL_ICAL);
    String icalStr = icalElem.getText();
    String sender = icalElem.getAttribute(MailConstants.E_CAL_ATTENDEE, null);
    ZVCalendar cal = ZCalendarBuilder.build(icalStr);
    List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, cal, false);
    for (Invite inv : invites) {
        String method = inv.getMethod();
        if (!ICalTok.REPLY.toString().equals(method)) {
            throw ServiceException.INVALID_REQUEST("iCalendar method must be REPLY (was " + method + ")", null);
        }
    }
    for (Invite inv : invites) {
        mbox.processICalReply(octxt, inv, sender);
    }
    Element response = getResponseElement(zsc);
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 14 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class ImportAppointments 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);
    String folder = request.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER_ID);
    ItemId iidFolder = new ItemId(folder, zsc);
    String ct = request.getAttribute(MailConstants.A_CONTENT_TYPE);
    if (!ct.equalsIgnoreCase("ics") && !ct.equalsIgnoreCase(MimeConstants.CT_TEXT_CALENDAR))
        throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
    Element content = request.getElement(MailConstants.E_CONTENT);
    List<Upload> uploads = null;
    InputStream is = null;
    String partStr = null;
    String attachment = content.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
    String messageId = content.getAttribute(MailConstants.A_MESSAGE_ID, null);
    if (attachment != null && messageId != null) {
        throw ServiceException.INVALID_REQUEST("use either aid or mid but not both", null);
    }
    SourceSpecMethod sourceSpecMethod;
    if (messageId != null) {
        sourceSpecMethod = SourceSpecMethod.MSG_PART;
    } else if (attachment != null) {
        sourceSpecMethod = SourceSpecMethod.ATTACH_ID;
    } else {
        sourceSpecMethod = SourceSpecMethod.INLINE_TEXT;
    }
    try {
        if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
            // Get content from part of existing message.
            ItemId iid = new ItemId(messageId, zsc);
            String part = content.getAttribute(MailConstants.A_PART);
            partStr = CreateContact.fetchItemPart(zsc, octxt, mbox, iid, part, null, MimeConstants.P_CHARSET_UTF8);
            is = new ByteArrayInputStream(partStr.getBytes(MimeConstants.P_CHARSET_UTF8));
        }
        // if the "target item" is remote, we will need to proxy the request
        ItemId iidTarget = getProxyTarget(zsc, octxt, iidFolder, true);
        if (iidTarget != null) {
            ZimbraLog.misc.info("Proxying ImportAppointments - folder=" + folder + ", acct=" + iidTarget.getAccountId());
            if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
                /* Bug 77131 change specification method to something that will work in the proxied context */
                content.addAttribute(MailConstants.A_MESSAGE_ID, (String) null);
                content.addAttribute(MailConstants.A_PART, (String) null);
                Upload up = FileUploadServlet.saveUpload(is, "upload.ics", MimeConstants.CT_TEXT_CALENDAR, zsc.getAuthtokenAccountId());
                content.addAttribute(MailConstants.A_ATTACHMENT_ID, up.getId());
            }
            return proxyRequest(request, context, iidFolder, iidTarget);
        }
        if (SourceSpecMethod.ATTACH_ID.equals(sourceSpecMethod)) {
            is = parseUploadedContent(zsc, attachment, uploads = new ArrayList<Upload>());
        } else if (SourceSpecMethod.INLINE_TEXT.equals(sourceSpecMethod)) {
            // Convert LF to CRLF because the XML parser normalizes element text to LF.
            String text = StringUtil.lfToCrlf(content.getText());
            is = new ByteArrayInputStream(text.getBytes(MimeConstants.P_CHARSET_UTF8));
        }
        List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, MimeConstants.P_CHARSET_UTF8);
        is.close();
        is = null;
        List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, icals, true, true, null);
        Set<String> uidsSeen = new HashSet<String>();
        StringBuilder ids = new StringBuilder();
        for (Invite inv : invites) {
            // handle missing UIDs on remote calendars by generating them as needed
            String uid = inv.getUid();
            if (uid == null) {
                uid = LdapUtil.generateUUID();
                inv.setUid(uid);
            }
            boolean addRevision;
            if (!uidsSeen.contains(uid)) {
                addRevision = true;
                uidsSeen.add(uid);
            } else {
                addRevision = false;
            }
            // and add the invite to the calendar!
            try {
                AddInviteData aid = mbox.addInvite(octxt, inv, iidFolder.getId(), false, addRevision);
                if (aid != null) {
                    if (ids.length() > 0)
                        ids.append(",");
                    ids.append(aid.calItemId).append("-").append(aid.invId);
                }
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Skipping bad iCalendar object during import: uid=" + inv.getUid(), e);
            }
        }
        Element response = zsc.createElement(MailConstants.IMPORT_APPOINTMENTS_RESPONSE);
        Element cn = response.addElement(MailConstants.E_APPOINTMENT);
        cn.addAttribute(MailConstants.A_IDS, ids.toString());
        cn.addAttribute(MailConstants.A_NUM, invites.size());
        return response;
    } catch (IOException e) {
        throw MailServiceException.UNABLE_TO_IMPORT_APPOINTMENTS(e.getMessage(), e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
            }
        if (attachment != null)
            FileUploadServlet.deleteUploads(uploads);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AddInviteData(com.zimbra.cs.mailbox.Mailbox.AddInviteData) Element(com.zimbra.common.soap.Element) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Invite(com.zimbra.cs.mailbox.calendar.Invite) HashSet(java.util.HashSet)

Example 15 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class GetICal method handle.

/* (non-Javadoc)
     * @see com.zimbra.soap.DocumentHandler#handle(org.dom4j.Element, java.util.Map)
     */
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account authAccount = getAuthenticatedAccount(zsc);
    Mailbox mbx = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    String iidStr = request.getAttribute(MailConstants.A_ID, null);
    long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME, -1);
    long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME, -1);
    //        int compNum = (int)request.getAttributeLong(MailService.A_CAL_COMPONENT_NUM);
    int compNum = 0;
    Browser browser = HttpUtil.guessBrowser(zsc.getUserAgent());
    boolean useOutlookCompatMode = Browser.IE.equals(browser);
    try {
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            OutputStreamWriter wout = new OutputStreamWriter(buf);
            ZVCalendar cal = null;
            ItemId iid = iidStr != null ? new ItemId(iidStr, zsc) : null;
            if (iid != null) {
                CalendarItem calItem = mbx.getCalendarItemById(octxt, iid.getId());
                if (calItem == null) {
                    throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Could not find calendar item");
                }
                Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
                if (inv == null) {
                    throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
                }
                boolean allowPrivateAccess = calItem.allowPrivateAccess(authAccount, zsc.isUsingAdminPrivileges());
                cal = inv.newToICalendar(useOutlookCompatMode, allowPrivateAccess);
                cal.toICalendar(wout);
            } else {
                mbx.writeICalendarForRange(wout, octxt, rangeStart, rangeEnd, Mailbox.ID_FOLDER_CALENDAR, useOutlookCompatMode, false, false);
            }
            wout.flush();
            Element response = getResponseElement(zsc);
            Element icalElt = response.addElement(MailConstants.E_CAL_ICAL);
            if (iid != null)
                icalElt.addAttribute(MailConstants.A_ID, new ItemIdFormatter(zsc).formatItemId(iid));
            icalElt.addText(buf.toString());
            return response;
        } catch (MailServiceException.NoSuchItemException e) {
            throw ServiceException.FAILURE("Error could get default invite for Invite: " + iidStr + "-" + compNum, e);
        } catch (IOException e) {
            throw ServiceException.FAILURE("IO Exception while outputing Calendar for Invite: " + iidStr + "-" + compNum, e);
        }
    } catch (MailServiceException.NoSuchItemException e) {
        throw ServiceException.FAILURE("No Such Invite Message: " + iidStr, e);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) OutputStreamWriter(java.io.OutputStreamWriter) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite) Browser(com.zimbra.common.util.HttpUtil.Browser)

Aggregations

ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)47 Invite (com.zimbra.cs.mailbox.calendar.Invite)21 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)16 Account (com.zimbra.cs.account.Account)12 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)10 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)10 IOException (java.io.IOException)10 MimeMessage (javax.mail.internet.MimeMessage)10 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)9 ItemId (com.zimbra.cs.service.util.ItemId)9 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)8 ServiceException (com.zimbra.common.service.ServiceException)7 Mailbox (com.zimbra.cs.mailbox.Mailbox)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Locale (java.util.Locale)7 Test (org.junit.Test)7 Element (com.zimbra.common.soap.Element)6 Date (java.util.Date)6