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;
}
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);
}
}
}
}
}
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"));
}
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());
}
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);
}
}
Aggregations