Search in sources :

Example 6 with ZComponent

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

the class CalendarRequest method notifyCalendarItem.

// Notify attendees following an update to the series of a recurring appointment.  Only the
// added attendees are notified if notifyAllAttendees is false.  If it is true all attendees
// for each invite are notified.  (Some invites may have more attendees than others.)
protected static void notifyCalendarItem(ZimbraSoapContext zsc, OperationContext octxt, Account acct, Mailbox mbox, CalendarItem calItem, boolean notifyAllAttendees, List<ZAttendee> addedAttendees, boolean ignorePastExceptions, MailSendQueue sendQueue) throws ServiceException {
    boolean onBehalfOf = isOnBehalfOfRequest(zsc);
    Account authAcct = getAuthenticatedAccount(zsc);
    boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAcct, zsc.isUsingAdminPrivileges());
    Address from = AccountUtil.getFriendlyEmailAddress(acct);
    Address sender = null;
    if (onBehalfOf)
        sender = AccountUtil.getFriendlyEmailAddress(authAcct);
    List<Address> addedRcpts = CalendarMailSender.toListFromAttendees(addedAttendees);
    long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
    mbox.lock.lock();
    try {
        // Refresh the cal item so we see the latest blob, whose path may have been changed
        // earlier in the current request.
        calItem = mbox.getCalendarItemById(octxt, calItem.getId());
        Invite[] invites = calItem.getInvites();
        // Get exception instances.  These will be included in the series update email.
        List<Invite> exceptions = new ArrayList<Invite>();
        for (Invite inv : invites) {
            if (inv.hasRecurId()) {
                exceptions.add(inv);
            }
        }
        // Send the update invites.
        boolean didExceptions = false;
        for (Invite inv : invites) {
            if (ignorePastExceptions && inv.hasRecurId() && !inviteIsAfterTime(inv, now)) {
                continue;
            }
            // Make the new iCalendar part to send.
            ZVCalendar cal = inv.newToICalendar(!hidePrivate);
            // For series invite, append the exception instances.
            if (inv.isRecurrence() && !didExceptions) {
                // Find the VEVENT/VTODO for the series.
                ZComponent seriesComp = null;
                for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
                    ZComponent comp = compIter.next();
                    ICalTok compName = comp.getTok();
                    if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
                        if (comp.getProperty(ICalTok.RRULE) != null) {
                            seriesComp = comp;
                            break;
                        }
                    }
                }
                for (Invite except : exceptions) {
                    if (except.isCancel() && seriesComp != null) {
                        // Cancels are added as EXDATEs in the series VEVENT/VTODO.
                        RecurId rid = except.getRecurId();
                        if (rid != null && rid.getDt() != null) {
                            ZProperty exdate = rid.getDt().toProperty(ICalTok.EXDATE, false);
                            seriesComp.addProperty(exdate);
                        }
                    } else {
                        // Exception instances are added as additional VEVENTs/VTODOs.
                        ZComponent exceptComp = except.newToVComponent(false, !hidePrivate);
                        cal.addComponent(exceptComp);
                    }
                }
                didExceptions = true;
            }
            // Compose email using the existing MimeMessage as template and send it.
            MimeMessage mmInv = calItem.getSubpartMessage(inv.getMailItemId());
            List<Address> rcpts;
            if (notifyAllAttendees) {
                rcpts = CalendarMailSender.toListFromAttendees(inv.getAttendees());
            } else {
                rcpts = addedRcpts;
            }
            if (rcpts != null && !rcpts.isEmpty()) {
                MimeMessage mmModify = CalendarMailSender.createCalendarMessage(authAcct, from, sender, rcpts, mmInv, inv, cal, true);
                CalSendData csd = new CalSendData();
                csd.mMm = mmModify;
                csd.mOrigId = new ItemId(mbox, inv.getMailItemId());
                MailSendQueueEntry entry = new MailSendQueueEntry(octxt, mbox, csd, null);
                sendQueue.add(entry);
            }
        }
    } finally {
        mbox.lock.release();
    }
}
Also used : Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) RecurId(com.zimbra.cs.mailbox.calendar.RecurId) ItemId(com.zimbra.cs.service.util.ItemId) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 7 with ZComponent

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

the class Mailbox method writeICalendarForCalendarItems.

public void writeICalendarForCalendarItems(Writer writer, OperationContext octxt, Collection<CalendarItem> calItems, Folder f, boolean useOutlookCompatMode, boolean ignoreErrors, boolean needAppleICalHacks, boolean trimCalItemsList, boolean escapeHtmlTags, boolean includeAttaches) throws ServiceException {
    lock.lock();
    try {
        writer.write("BEGIN:VCALENDAR\r\n");
        if (f != null) {
            writer.write("X-WR-CALNAME:");
            writer.write(f.getName());
            writer.write("\r\n");
            writer.write("X-WR-CALID:");
            writer.write(new ItemId(f).toString());
            writer.write("\r\n");
        }
        ZProperty prop;
        prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
        prop.toICalendar(writer, needAppleICalHacks);
        prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
        prop.toICalendar(writer, needAppleICalHacks);
        prop = new ZProperty(ICalTok.METHOD, ICalTok.PUBLISH.toString());
        prop.toICalendar(writer, needAppleICalHacks);
        // timezones
        ICalTimeZone localTz = Util.getAccountTimeZone(getAccount());
        TimeZoneMap tzmap = new TimeZoneMap(localTz);
        for (CalendarItem calItem : calItems) {
            tzmap.add(calItem.getTimeZoneMap());
        }
        // iterate the tzmap and add all the VTimeZone's
        for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
            ICalTimeZone tz = iter.next();
            tz.newToVTimeZone().toICalendar(writer, needAppleICalHacks);
        }
        // help keep memory consumption low
        tzmap = null;
        // build all the event components and add them to the Calendar
        for (Iterator<CalendarItem> iter = calItems.iterator(); iter.hasNext(); ) {
            CalendarItem calItem = iter.next();
            boolean allowPrivateAccess = calItem.isPublic() || calItem.allowPrivateAccess(octxt.getAuthenticatedUser(), octxt.isUsingAdminPrivileges());
            if (trimCalItemsList) {
                // help keep memory consumption low
                iter.remove();
            }
            Invite[] invites = calItem.getInvites();
            if (invites != null && invites.length > 0) {
                boolean appleICalExdateHack = LC.calendar_apple_ical_compatible_canceled_instances.booleanValue();
                ZComponent[] comps = null;
                try {
                    comps = Invite.toVComponents(invites, allowPrivateAccess, useOutlookCompatMode, appleICalExdateHack, includeAttaches);
                } catch (ServiceException e) {
                    if (ignoreErrors) {
                        ZimbraLog.calendar.warn("Error retrieving iCalendar data for item %s: %s", calItem.getId(), e.getMessage(), e);
                    } else {
                        throw e;
                    }
                }
                if (comps != null) {
                    for (ZComponent comp : comps) {
                        comp.toICalendar(writer, needAppleICalHacks, escapeHtmlTags);
                    }
                }
            }
        }
        writer.write("END:VCALENDAR\r\n");
    } catch (IOException e) {
        throw ServiceException.FAILURE("Error writing iCalendar", e);
    } finally {
        lock.release();
    }
}
Also used : IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) SetCalendarItem(com.zimbra.cs.redolog.op.SetCalendarItem) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) CreateInvite(com.zimbra.cs.redolog.op.CreateInvite) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 8 with ZComponent

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

the class TestCalDav method testLondonTimeZoneCalledGMT.

@Test
public void testLondonTimeZoneCalledGMT() throws Exception {
    try (ByteArrayInputStream bais = new ByteArrayInputStream(LOTUS_NOTES_WITH_BAD_GMT_TZID.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.KEEP_IF_DOESNT_CLASH);
        assertEquals("ID that London Timezone originally with TZID=GMT maps to", "Europe/London", tz.getID());
        assertEquals("that London Timezone originally with TZID=GMT maps to this daylightTzname", "GMT/BST", tz.getDaylightTzname());
    }
}
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 9 with ZComponent

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

the class TestCalDav method simpleEvent.

public byte[] simpleEvent(Account organizer) throws IOException {
    ZVCalendar vcal = new ZVCalendar();
    vcal.addVersionAndProdId();
    vcal.addProperty(new ZProperty(ICalTok.METHOD, ICalTok.PUBLISH.toString()));
    ICalTimeZone tz = ICalTimeZone.lookupByTZID("Africa/Harare");
    vcal.addComponent(tz.newToVTimeZone());
    ZComponent vevent = new ZComponent(ICalTok.VEVENT);
    ParsedDateTime dtstart = parsedDateTime(2020, java.util.Calendar.APRIL, 1, 9, 0, tz);
    vevent.addProperty(dtstart.toProperty(ICalTok.DTSTART, false));
    ParsedDateTime dtend = parsedDateTime(2020, java.util.Calendar.APRIL, 1, 13, 0, tz);
    vevent.addProperty(dtend.toProperty(ICalTok.DTEND, false));
    vevent.addProperty(new ZProperty(ICalTok.DTSTAMP, "20140108T224700Z"));
    vevent.addProperty(new ZProperty(ICalTok.SUMMARY, "Simple Event"));
    vevent.addProperty(new ZProperty(ICalTok.UID, "d1102-42a7-4283-b025-3376dabe53b3"));
    vevent.addProperty(new ZProperty(ICalTok.STATUS, ICalTok.CONFIRMED.toString()));
    vevent.addProperty(new ZProperty(ICalTok.SEQUENCE, "1"));
    // vevent.addProperty(organizer(organizer));
    vcal.addComponent(vevent);
    return zvcalendarToBytes(vcal);
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 10 with ZComponent

use of com.zimbra.common.calendar.ZCalendar.ZComponent 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)

Aggregations

ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)35 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)18 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)16 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)14 ArrayList (java.util.ArrayList)10 ICalTok (com.zimbra.common.calendar.ZCalendar.ICalTok)9 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)7 ZParameter (com.zimbra.common.calendar.ZCalendar.ZParameter)7 Account (com.zimbra.cs.account.Account)7 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 IOException (java.io.IOException)7 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.Test)5 ServiceException (com.zimbra.common.service.ServiceException)4 InputStream (java.io.InputStream)4 MessagingException (javax.mail.MessagingException)4 MimeMessage (javax.mail.internet.MimeMessage)4 StringWriter (java.io.StringWriter)3 Address (javax.mail.Address)3