use of com.zimbra.common.calendar.ZCalendar.ZComponent 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.ZComponent 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.ZComponent in project zm-mailbox by Zimbra.
the class ICalTimeZone method newToVTimeZone.
public ZComponent newToVTimeZone() {
ZComponent vtz = new ZComponent(ICalTok.VTIMEZONE);
vtz.addProperty(new ZProperty(ICalTok.TZID, getID()));
if (mDayToStdDtStart != null) {
ZComponent standard = new ZComponent(ICalTok.STANDARD);
vtz.addComponent(standard);
standard.addProperty(new ZProperty(ICalTok.DTSTART, mDayToStdDtStart));
standard.addProperty(new ZProperty(ICalTok.TZOFFSETTO, timeToTzOffsetString(mStandardOffset)));
standard.addProperty(new ZProperty(ICalTok.TZOFFSETFROM, timeToTzOffsetString(mDaylightOffset)));
if (mDayToStdRule != null)
standard.addProperty(new ZProperty(ICalTok.RRULE, mDayToStdRule));
if (mStandardTzname != null)
standard.addProperty(new ZProperty(ICalTok.TZNAME, mStandardTzname));
}
if (mStdToDayDtStart != null && (mStandardOffset != mDaylightOffset || mDayToStdDtStart == null)) {
ZComponent daylight = new ZComponent(ICalTok.DAYLIGHT);
vtz.addComponent(daylight);
daylight.addProperty(new ZProperty(ICalTok.DTSTART, mStdToDayDtStart));
daylight.addProperty(new ZProperty(ICalTok.TZOFFSETTO, timeToTzOffsetString(mDaylightOffset)));
daylight.addProperty(new ZProperty(ICalTok.TZOFFSETFROM, timeToTzOffsetString(mStandardOffset)));
if (mStdToDayRule != null)
daylight.addProperty(new ZProperty(ICalTok.RRULE, mStdToDayRule));
if (mDaylightTzname != null)
daylight.addProperty(new ZProperty(ICalTok.TZNAME, mDaylightTzname));
}
return vtz;
}
use of com.zimbra.common.calendar.ZCalendar.ZComponent 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.ZComponent in project zm-mailbox by Zimbra.
the class FreeBusy method toVCalendarAsVEvents.
public String toVCalendarAsVEvents() throws ServiceException {
StringWriter writer = new StringWriter();
String publish = ICalTok.PUBLISH.toString();
long now = System.currentTimeMillis();
writer.append("BEGIN:VCALENDAR").append(NL);
writer.append("PRODID:").append(ZCalendar.sZimbraProdID).append(NL);
writer.append("VERSION:").append(ZCalendar.sIcalVersion).append(NL);
writer.append("METHOD:").append(publish).append(NL);
String uidBase = "tmp_" + LdapUtil.generateUUID() + "_";
int uidCount = 0;
TimeZoneMap tzMap = new TimeZoneMap(ICalTimeZone.getUTC());
for (Iterator<Interval> iter = this.iterator(); iter.hasNext(); ) {
FreeBusy.Interval cur = iter.next();
String status = cur.getStatus();
if (status.equals(IcalXmlStrMap.FBTYPE_FREE)) {
continue;
} else if (status.equals(IcalXmlStrMap.FBTYPE_NODATA)) {
// Treat no-data case same as free, because other apps probably won't understand a non-standard fbtype value.
continue;
}
Invite inv = new Invite(publish, tzMap, true);
inv.setUid(uidBase + (++uidCount));
inv.setSeqNo(0);
inv.setDtStamp(now);
inv.setDtStart(ParsedDateTime.fromUTCTime(cur.getStart()));
inv.setDtEnd(ParsedDateTime.fromUTCTime(cur.getEnd()));
inv.setFreeBusy(status);
if (status.equals(IcalXmlStrMap.FBTYPE_BUSY_TENTATIVE)) {
inv.setStatus(IcalXmlStrMap.STATUS_TENTATIVE);
} else if (status.equals(IcalXmlStrMap.FBTYPE_BUSY_UNAVAILABLE)) {
inv.setStatus(IcalXmlStrMap.STATUS_CONFIRMED);
} else {
// busy
inv.setStatus(IcalXmlStrMap.STATUS_CONFIRMED);
}
ZComponent comp = inv.newToVComponent(false, true);
try {
comp.toICalendar(writer);
} catch (IOException e) {
throw ServiceException.FAILURE("can't write iCalendar object", e);
}
}
writer.append("END:VCALENDAR").append(NL);
return writer.toString();
}
Aggregations