Search in sources :

Example 21 with ICalTimeZone

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

the class RdateExdate method parse.

public static RdateExdate parse(ZProperty prop, TimeZoneMap tzmap) throws ServiceException {
    ICalTok propName = prop.getToken();
    ZParameter valueParam = prop.getParameter(ICalTok.VALUE);
    ICalTok valueType = ICalTok.DATE_TIME;
    if (valueParam != null) {
        String typeStr = valueParam.getValue();
        if (typeStr != null) {
            valueType = ICalTok.lookup(typeStr);
            if (valueType == null)
                throw ServiceException.INVALID_REQUEST("Invalid " + propName.toString() + " value type " + typeStr, null);
        }
    }
    String tzid = prop.getParameterVal(ICalTok.TZID, null);
    ICalTimeZone tz = null;
    if (tzid != null)
        tz = tzmap.lookupAndAdd(tzid);
    RdateExdate rexdate = new RdateExdate(propName, valueType, tz);
    String csv = prop.getValue();
    if (csv == null || csv.length() == 0)
        throw ServiceException.INVALID_REQUEST("Empty value not allowed for " + propName.toString() + " property", null);
    for (String value : csv.split(",")) {
        try {
            switch(valueType) {
                case DATE_TIME:
                case DATE:
                    ParsedDateTime dt = ParsedDateTime.parse(value, tzmap, tz, tzmap.getLocalTimeZone());
                    rexdate.addValue(dt);
                    break;
                case PERIOD:
                    Period p = Period.parse(value, tz, tzmap);
                    rexdate.addValue(p);
                    break;
            }
        } catch (ParseException e) {
            throw ServiceException.INVALID_REQUEST("Unable to parse " + propName.toString() + " value \"" + value + "\"", e);
        }
    }
    return rexdate;
}
Also used : ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ParseException(java.text.ParseException) ZParameter(com.zimbra.common.calendar.ZCalendar.ZParameter) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok)

Example 22 with ICalTimeZone

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

the class RdateExdate method decodeMetadata.

public static RdateExdate decodeMetadata(Metadata meta, TimeZoneMap tzmap) throws ServiceException {
    boolean isRdate = meta.getBool(FN_IS_RDATE, true);
    ICalTok propName = isRdate ? ICalTok.RDATE : ICalTok.EXDATE;
    ICalTimeZone tz = null;
    String tzid = meta.get(FN_TZID, null);
    if (tzid != null)
        tz = tzmap.lookupAndAdd(tzid);
    String vt = meta.get(FN_VALUE_TYPE, VT_DATE_TIME);
    ICalTok valueType;
    if (vt.equals(VT_DATE_TIME))
        valueType = ICalTok.DATE_TIME;
    else if (vt.equals(VT_DATE))
        valueType = ICalTok.DATE;
    else
        valueType = ICalTok.PERIOD;
    RdateExdate rexdate = new RdateExdate(propName, valueType, tz);
    int numValues = (int) meta.getLong(FN_NUM_VALUES, 0);
    for (int i = 0; i < numValues; i++) {
        String key = FN_VALUE + i;
        if (valueType.equals(ICalTok.DATE_TIME) || valueType.equals(ICalTok.DATE)) {
            String dtStr = meta.get(key);
            ParsedDateTime dt;
            try {
                dt = ParsedDateTime.parse(dtStr, tzmap, tz, tzmap.getLocalTimeZone());
            } catch (ParseException e) {
                throw ServiceException.INVALID_REQUEST("Invalid " + propName.toString() + " date/time in metadata: " + meta.toString(), e);
            }
            rexdate.addValue(dt);
        } else if (valueType.equals(ICalTok.PERIOD)) {
            Period p = Period.decodeMetadata(meta.getMap(key), tz, tzmap);
            rexdate.addValue(p);
        }
    }
    return rexdate;
}
Also used : ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ParseException(java.text.ParseException) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok)

Example 23 with ICalTimeZone

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

the class RecurId method fromXml.

public static RecurId fromXml(Element e, TimeZoneMap tzMap) throws ServiceException {
    assert (tzMap != null);
    String recurrenceId = e.getAttribute(MailConstants.A_CAL_RECURRENCE_ID, null);
    if (recurrenceId == null)
        return null;
    String tzId = e.getAttribute(MailConstants.A_CAL_TIMEZONE, null);
    ICalTimeZone tz = tzId != null ? tzMap.lookupAndAdd(tzId) : null;
    String rangeType = e.getAttribute(MailConstants.A_CAL_RECURRENCE_RANGE_TYPE, null);
    try {
        ParsedDateTime dt = ParsedDateTime.parse(recurrenceId, tzMap, tz, tzMap.getLocalTimeZone());
        if (rangeType != null)
            return new RecurId(dt, rangeType);
        else
            return new RecurId(dt, RANGE_NONE);
    } catch (ParseException x) {
        throw ServiceException.FAILURE("recurId=" + recurrenceId + ", tz=" + tzId, x);
    }
}
Also used : ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ParseException(java.text.ParseException) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 24 with ICalTimeZone

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

the class ContactCSV method populateDateFieldsIfUnambiguous.

/**
     *
     * @param testY   candidate year
     * @param testM   candidate month in range 1 to 12
     * @param testD   candidate day in range 1 to 31
     * @param leniant succeed even if the day and month might be ambiguous
     * @return Formatted date if certain date fields are OK and wouldn't be OK in a different order, else null
     */
private String populateDateFieldsIfUnambiguous(int testY, int testM, int testD, boolean leniant) {
    if ((testY < 1600) || (testY > 4500)) {
        // Year does not fit within what Outlook allows, probably not what is intended
        return null;
    }
    int firstAllowableDay = leniant ? 1 : 13;
    if ((testD < firstAllowableDay) || (testD > 31)) {
        // Either an invalid date or could be a month number if guessed date string order wrong
        return null;
    }
    if ((testM < 1) || (testM > 12)) {
        // Not a valid month
        return null;
    }
    ICalTimeZone tzUTC = ICalTimeZone.getUTC();
    GregorianCalendar cal = new GregorianCalendar(tzUTC);
    cal.set(testY, testM - 1, 1, 0, 0);
    if (testD > cal.getActualMaximum(Calendar.DAY_OF_MONTH))
        return null;
    cal.set(Calendar.DAY_OF_MONTH, testD);
    SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    ymd.setCalendar(cal);
    return ymd.format(cal.getTime());
}
Also used : GregorianCalendar(java.util.GregorianCalendar) SimpleDateFormat(java.text.SimpleDateFormat) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 25 with ICalTimeZone

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

the class CalendarMailSender method createForwardedPrivateInviteMessage.

public static MimeMessage createForwardedPrivateInviteMessage(Account account, Locale lc, String method, List<Invite> invites, String origSenderEmail, String forwarderEmail, String[] forwardTo) throws ServiceException {
    if (invites == null || invites.isEmpty())
        return null;
    List<Address> rcpts = new ArrayList<Address>();
    for (String to : forwardTo) {
        try {
            rcpts.add(new JavaMailInternetAddress(to));
        } catch (AddressException e) {
            ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
        }
    }
    if (rcpts.isEmpty())
        return null;
    String subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, lc);
    // Create filtered version of invites.
    List<Invite> filteredInvs = new ArrayList<Invite>();
    for (Invite inv : invites) {
        Invite filtered = inv.newCopy();
        filtered.clearAlarms();
        filtered.clearPrivateInfo();
        filtered.setName(subject);
        // Add ATTENDEE for forwarder.
        List<ZAttendee> atts = inv.getAttendees();
        if (atts != null && forwarderEmail != null) {
            for (ZAttendee att : atts) {
                if (forwarderEmail.equalsIgnoreCase(att.getAddress())) {
                    filtered.addAttendee(att);
                }
            }
        }
        filteredInvs.add(filtered);
    }
    MimeMessage mm = null;
    try {
        mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
        mm.setFrom(new JavaMailInternetAddress(origSenderEmail));
        mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
        // Set special header to indicate the forwarding attendee.
        mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
        mm.setSubject(subject);
        StringWriter writer = new StringWriter();
        try {
            writer.write("BEGIN:VCALENDAR\r\n");
            ZProperty prop;
            prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.METHOD, method);
            prop.toICalendar(writer);
            // timezones
            Invite firstInv = filteredInvs.get(0);
            TimeZoneMap tzmap = new TimeZoneMap(firstInv.getTimeZoneMap().getLocalTimeZone());
            for (Invite inv : filteredInvs) {
                tzmap.add(inv.getTimeZoneMap());
            }
            for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
                ICalTimeZone tz = iter.next();
                tz.newToVTimeZone().toICalendar(writer);
            }
            // VEVENTs/VTODOs
            for (Invite inv : filteredInvs) {
                ZComponent comp = inv.newToVComponent(false, true);
                comp.toICalendar(writer);
            }
            writer.write("END:VCALENDAR\r\n");
        } catch (IOException e) {
            throw ServiceException.FAILURE("Error writing iCalendar", e);
        } finally {
            Closeables.closeQuietly(writer);
        }
        mm.setText(writer.toString());
        ContentType ct = new ContentType(MimeConstants.CT_TEXT_CALENDAR);
        ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
        ct.setParameter("method", method);
        mm.setHeader("Content-Type", ct.toString());
    } catch (MessagingException e) {
        ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
    }
    return mm;
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mime(com.zimbra.cs.mime.Mime) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) StringWriter(java.io.StringWriter) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Aggregations

ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)62 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)17 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)14 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)12 Element (com.zimbra.common.soap.Element)11 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)10 ServiceException (com.zimbra.common.service.ServiceException)10 ArrayList (java.util.ArrayList)10 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)8 Account (com.zimbra.cs.account.Account)8 Invite (com.zimbra.cs.mailbox.calendar.Invite)8 Mailbox (com.zimbra.cs.mailbox.Mailbox)7 IOException (java.io.IOException)7 Metadata (com.zimbra.cs.mailbox.Metadata)6 ItemId (com.zimbra.cs.service.util.ItemId)6 ParseException (java.text.ParseException)6 OperationContext (com.zimbra.cs.mailbox.OperationContext)5 RecurId (com.zimbra.cs.mailbox.calendar.RecurId)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)5 GregorianCalendar (java.util.GregorianCalendar)5