Search in sources :

Example 21 with ZVCalendar

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

the class ForwardAppointmentInvite method getMessagePair.

public static Pair<MimeMessage, MimeMessage> getMessagePair(Mailbox mbox, Account senderAcct, Message msg, MimeMessage mmFwdWrapper) throws ServiceException {
    Pair<MimeMessage, MimeMessage> msgPair;
    mbox.lock.lock();
    try {
        MimeMessage mmInv = msg.getMimeMessage();
        List<Invite> invs = new ArrayList<Invite>();
        for (Iterator<CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext(); ) {
            CalendarItemInfo cii = iter.next();
            Invite inv = cii.getInvite();
            if (inv != null) {
                invs.add(inv);
            }
        }
        ZVCalendar cal = null;
        Invite firstInv = null;
        if (!invs.isEmpty()) {
            // Recreate the VCALENDAR from Invites.
            boolean first = true;
            for (Invite inv : invs) {
                if (first) {
                    first = false;
                    firstInv = inv;
                    cal = inv.newToICalendar(true);
                } else {
                    ZComponent comp = inv.newToVComponent(true, true);
                    cal.addComponent(comp);
                }
            }
        } else {
            // If no invites found in metadata, parse from text/calendar MIME part.
            try {
                CalPartDetectVisitor visitor = new CalPartDetectVisitor();
                visitor.accept(mmInv);
                MimeBodyPart calPart = visitor.getCalendarPart();
                if (calPart != null) {
                    String ctHdr = calPart.getContentType();
                    ContentType ct = new ContentType(ctHdr);
                    String charset = ct.getParameter(MimeConstants.P_CHARSET);
                    if (charset == null || charset.length() == 0)
                        charset = MimeConstants.P_CHARSET_UTF8;
                    InputStream is = calPart.getInputStream();
                    try {
                        cal = ZCalendarBuilder.build(is, charset);
                    } finally {
                        ByteUtil.closeStream(is);
                    }
                    List<Invite> invList = Invite.createFromCalendar(senderAcct, msg.getFragment(), cal, false);
                    if (invList != null && !invList.isEmpty())
                        firstInv = invList.get(0);
                    if (firstInv == null)
                        throw ServiceException.FAILURE("Error building Invite for calendar part in message " + msg.getId(), null);
                }
            } catch (MessagingException e) {
                throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
            } catch (IOException e) {
                throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
            }
        }
        msgPair = getInstanceFwdMsg(senderAcct, firstInv, cal, mmInv, mmFwdWrapper);
    } finally {
        mbox.lock.release();
    }
    return msgPair;
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CalendarItemInfo(com.zimbra.cs.mailbox.Message.CalendarItemInfo) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) MimeMessage(javax.mail.internet.MimeMessage) MimeBodyPart(javax.mail.internet.MimeBodyPart) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 22 with ZVCalendar

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

the class WellKnownTimeZones method loadFromFile.

/**
     * Should be called once at server start
     * @param tzFile
     * @throws IOException
     * @throws ServiceException
     */
public static void loadFromFile(File tzFile) throws IOException, ServiceException {
    FileInputStream fis = null;
    ZVCalendar tzs = null;
    try {
        fis = new FileInputStream(tzFile);
        tzs = ZCalendar.ZCalendarBuilder.build(new FileInputStream(tzFile), MimeConstants.P_CHARSET_UTF8);
    } finally {
        if (fis != null)
            fis.close();
    }
    for (Iterator<ZComponent> compIter = tzs.getComponentIterator(); compIter.hasNext(); ) {
        ZComponent tzComp = compIter.next();
        if (!ICalTok.VTIMEZONE.equals(tzComp.getTok()))
            continue;
        ICalTimeZone tz = ICalTimeZone.fromVTimeZone(tzComp, true, TZID_NAME_ASSIGNMENT_BEHAVIOR.ALWAYS_KEEP);
        sTZIDMap.put(tz.getID(), tz);
    }
    // Add aliases from TZIDMapper.  Build map of TZID to match score.
    Map<String, Integer> matchScoreMap = new HashMap<String, Integer>();
    for (Iterator<TZIDMapper.TZ> tzIter = TZIDMapper.iterator(false); tzIter.hasNext(); ) {
        TZIDMapper.TZ tz = tzIter.next();
        String id = tz.getID();
        matchScoreMap.put(id, tz.getMatchScore());
        ICalTimeZone itz = getTimeZoneById(id);
        if (itz != null) {
            String[] aliases = tz.getAliases();
            if (aliases != null) {
                for (String alias : aliases) {
                    addAlias(itz, alias);
                }
            }
        }
    }
    // the highest match score is added to the map.
    for (Iterator<TZIDMapper.TZ> tzIter = TZIDMapper.iterator(false); tzIter.hasNext(); ) {
        TZIDMapper.TZ tz = tzIter.next();
        String id = tz.getID();
        ICalTimeZone itz = getTimeZoneById(id);
        if (itz != null) {
            ICalTimeZone current = sOffsetRuleMatches.get(itz);
            if (current == null) {
                sOffsetRuleMatches.put(itz, itz);
            } else {
                String currentId = current.getID();
                int currentMatchScore = matchScoreMap.containsKey(currentId) ? matchScoreMap.get(currentId) : 0;
                // Higher score wins.  In a tie, the TZID that comes earlier lexicographically wins.
                if (currentMatchScore < tz.getMatchScore() || (currentMatchScore == tz.getMatchScore() && currentId.compareTo(id) > 0)) {
                    sOffsetRuleMatches.remove(itz);
                    sOffsetRuleMatches.put(itz, itz);
                }
            }
            current = fuzzyOffsetRuleMatches.get(itz);
            if (current == null) {
                fuzzyOffsetRuleMatches.put(itz, itz);
            } else {
                String currentId = current.getID();
                int currentMatchScore = matchScoreMap.containsKey(currentId) ? matchScoreMap.get(currentId) : 0;
                // Higher score wins.  In a tie, the TZID that comes earlier lexicographically wins.
                if (currentMatchScore < tz.getMatchScore() || (currentMatchScore == tz.getMatchScore() && currentId.compareTo(id) > 0)) {
                    fuzzyOffsetRuleMatches.remove(itz);
                    fuzzyOffsetRuleMatches.put(itz, itz);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) TZ(com.zimbra.common.calendar.TZIDMapper.TZ) FileInputStream(java.io.FileInputStream) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) TZ(com.zimbra.common.calendar.TZIDMapper.TZ)

Example 23 with ZVCalendar

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

the class RecurringTaskTest method testTaskRecurrenceDuration.

@Test
public void testTaskRecurrenceDuration() throws ServiceException, UnsupportedEncodingException {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    String task = "BEGIN:VCALENDAR\n" + "PRODID:Zimbra-Calendar-Provider\n" + "VERSION:2.0\n" + "METHOD:PUBLISH\n" + "BEGIN:VTIMEZONE\n" + "TZID:Africa/Harare\n" + "BEGIN:STANDARD\n" + "DTSTART:16010101T000000\n" + "TZOFFSETTO:+0200\n" + "TZOFFSETFROM:+0200\n" + "TZNAME:CAT\n" + "END:STANDARD\n" + "END:VTIMEZONE\n" + "BEGIN:VTODO\n" + "UID:93077c29_ab51_41ad_aaa8_f63a68f963a6_migwiz\n" + "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=5\n" + "SUMMARY:Test Recurring Task\n" + "ATTENDEE;CN=User Test;CUTYPE=RESOURCE;ROLE=NON-PARTICIPANT;PARTSTAT=NEEDS-ACTION:mailto:testuser@zimbra.com\n" + "ATTENDEE;CN=User Test;ROLE=OPT-PARTICIPANT;PARTSTAT=NEEDS-ACTION:mailto:testuser@zimbra.com\n" + "PRIORITY:5\n" + "PERCENT-COMPLETE:0\n" + "ORGANIZER:mailto:admin@zimbra.com\n" + "DTSTART;VALUE=DATE:20120831\n" + "DUE;VALUE=DATE:20440709\n" + "STATUS:NEEDS-ACTION\n" + "CLASS:PUBLIC\n" + "LAST-MODIFIED:20131125T135454Z\n" + "DTSTAMP:20131125T135454Z\n" + "SEQUENCE:0\n" + "END:VTODO\n" + "END:VCALENDAR";
    OperationContext octxt = new OperationContext(acct);
    Folder taskFolder = mbox.getFolderById(octxt, 15);
    String charset = MimeConstants.P_CHARSET_UTF8;
    InputStream is = new ByteArrayInputStream(task.getBytes(charset));
    List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, charset);
    ImportInviteVisitor visitor = new ImportInviteVisitor(octxt, taskFolder, false);
    List<Invite> invites = Invite.createFromCalendar(acct, null, icals, true, false, visitor);
    Recurrence.IRecurrence recur = invites.get(0).getRecurrence();
    Metadata meta = recur.encodeMetadata();
    assertEquals("P1D", meta.get("duration"));
}
Also used : Account(com.zimbra.cs.account.Account) Recurrence(com.zimbra.cs.mailbox.calendar.Recurrence) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ByteArrayInputStream(java.io.ByteArrayInputStream) ImportInviteVisitor(com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor) Invite(com.zimbra.cs.mailbox.calendar.Invite) Test(org.junit.Test)

Example 24 with ZVCalendar

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

the class GetMsgTest method testHandle.

@Test
public void testHandle() throws Exception {
    Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
    Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
    Folder calendarFolder = mbox1.getCalendarFolders(null, SortBy.NONE).get(0);
    String fragment = "Some message";
    ZVCalendar calendar = new ZVCalendar();
    calendar.addDescription(desc, null);
    ZComponent comp = new ZComponent("VEVENT");
    calendar.addComponent(comp);
    Invite invite = MailboxTestUtil.generateInvite(acct1, fragment, calendar);
    ICalTimeZone ical = invite.getTimeZoneMap().getLocalTimeZone();
    long utc = 5 * 60 * 60 * 1000;
    ParsedDateTime s = ParsedDateTime.fromUTCTime(System.currentTimeMillis() + utc, ical);
    ParsedDateTime e = ParsedDateTime.fromUTCTime(System.currentTimeMillis() + (30 * 60 * 1000) + utc, ical);
    invite.setDtStart(s);
    invite.setDtEnd(e);
    invite.setPriority("5");
    invite.setClassProp("PRI");
    invite.setOrganizer(new ZOrganizer("test@zimbra.com", null));
    invite.setUid(UUID.randomUUID().toString());
    invite.setMethod("REQUEST");
    invite.setName("Testing");
    invite.setFreeBusy("B");
    invite.setIsOrganizer(true);
    invite.setItemType(MailItem.Type.APPOINTMENT);
    invite.setUid(UUID.randomUUID().toString());
    AddInviteData inviteData = mbox1.addInvite(null, invite, calendarFolder.getId());
    calendarFolder = mbox1.getCalendarFolders(null, SortBy.NONE).get(0);
    Element request = new Element.XMLElement("GetCalendarItem");
    Element action = request.addElement(MailConstants.E_MSG);
    action.addAttribute(MailConstants.A_ID, acct1.getId() + ":" + inviteData.calItemId + "-" + inviteData.invId);
    action.addAttribute(MailConstants.A_WANT_HTML, "1");
    action.addAttribute(MailConstants.A_NEED_EXP, "1");
    Element response = new GetMsg().handle(request, ServiceTestUtil.getRequestContext(acct1));
    Element organizer = response.getElement("m").getElement("inv").getElement("comp").getElement("or");
    String organizerString = organizer.prettyPrint();
    assertTrue(organizerString.contains("a=\"test@zimbra.com\" url=\"test@zimbra.com\""));
    mbox1.grantAccess(null, 10, acct2.getId(), ACL.GRANTEE_USER, ACL.RIGHT_READ, null);
    request = new Element.XMLElement("CreateMountPoint");
    Element link = request.addElement("link");
    link.addAttribute("f", "#");
    link.addAttribute("reminder", 0);
    link.addAttribute("name", "sharedcal");
    link.addAttribute("path", "/Calendar");
    link.addAttribute("owner", "test@zimbra.com");
    link.addAttribute("l", 10);
    link.addAttribute("view", "appoinment");
    response = new CreateMountpoint().handle(request, ServiceTestUtil.getRequestContext(acct2));
    String mptId = response.getElement("link").getAttribute("id");
    request = new Element.XMLElement("GetMsgRequest");
    action = request.addElement(MailConstants.E_MSG);
    action.addAttribute(MailConstants.A_ID, acct1.getId() + ":" + inviteData.calItemId + "-" + mptId);
    action.addAttribute(MailConstants.A_WANT_HTML, "1");
    action.addAttribute(MailConstants.A_NEED_EXP, "1");
    response = new GetMsg().handle(request, ServiceTestUtil.getRequestContext(acct2, acct1));
    organizerString = response.getElement("m").prettyPrint();
    assertTrue(!organizerString.contains("a=\"test@zimbra.com\" url=\"test@zimbra.com\""));
    request = new Element.XMLElement("FolderAction");
    action = request.addElement("action");
    action.addAttribute("id", mptId);
    action.addAttribute("op", "delete");
    response = new FolderAction().handle(request, ServiceTestUtil.getRequestContext(acct2));
    mbox1.revokeAccess(null, 10, acct2.getId());
}
Also used : Account(com.zimbra.cs.account.Account) AddInviteData(com.zimbra.cs.mailbox.Mailbox.AddInviteData) Element(com.zimbra.common.soap.Element) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) Folder(com.zimbra.cs.mailbox.Folder) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) Invite(com.zimbra.cs.mailbox.calendar.Invite) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) Test(org.junit.Test)

Example 25 with ZVCalendar

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

the class FeedManager method retrieveRemoteDatasource.

@VisibleForTesting
protected static SubscriptionData<?> retrieveRemoteDatasource(Account acct, RemoteDataInfo rdi, Folder.SyncData fsd) throws ServiceException, IOException {
    StringBuilder charset = new StringBuilder(rdi.expectedCharset);
    switch(getLeadingChar(rdi.content, charset)) {
        case -1:
            throw ServiceException.PARSE_ERROR("empty body in response when fetching remote subscription", null);
        case '<':
            return parseRssFeed(Element.parseXML(rdi.content), fsd, rdi.lastModified);
        case 'B':
        case 'b':
            List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(rdi.content, charset.toString());
            List<Invite> invites = Invite.createFromCalendar(acct, null, icals, true, true, null);
            // handle missing UIDs on remote calendars by generating them as needed
            for (Invite inv : invites) {
                if (inv.getUid() == null) {
                    inv.setUid(LdapUtil.generateUUID());
                }
            }
            return new SubscriptionData<Invite>(invites, rdi.lastModified);
        default:
            throw ServiceException.PARSE_ERROR("unrecognized remote content", null);
    }
}
Also used : ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Invite(com.zimbra.cs.mailbox.calendar.Invite) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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