use of com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor 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.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor in project zm-mailbox by Zimbra.
the class ArchiveFormatter method addData.
private void addData(UserServletContext context, Folder fldr, Map<Object, Folder> fmap, Set<MailItem.Type> types, Resolve r, boolean timestamp, ArchiveInputStream ais, ArchiveInputEntry aie, List<ServiceException> errs) throws ServiceException {
try {
int defaultFldr;
Mailbox mbox = fldr.getMailbox();
String dir, file;
String name = aie.getName();
int idx = name.lastIndexOf('/');
MailItem newItem = null, oldItem;
OperationContext oc = context.opContext;
BufferedReader reader;
MailItem.Type type, view;
if (idx == -1) {
file = name;
dir = "";
} else {
file = name.substring(idx + 1);
dir = name.substring(0, idx + 1);
if (!dir.startsWith("/"))
dir = '/' + dir;
}
if (file.length() == 0) {
return;
} else if (file.endsWith(".csv") || file.endsWith(".vcf")) {
defaultFldr = Mailbox.ID_FOLDER_CONTACTS;
type = MailItem.Type.CONTACT;
view = MailItem.Type.CONTACT;
} else if (file.endsWith(".eml")) {
defaultFldr = Mailbox.ID_FOLDER_INBOX;
type = MailItem.Type.MESSAGE;
view = MailItem.Type.MESSAGE;
} else if (file.endsWith(".ics")) {
if (dir.startsWith("Tasks/")) {
defaultFldr = Mailbox.ID_FOLDER_TASKS;
type = MailItem.Type.TASK;
view = MailItem.Type.TASK;
} else {
defaultFldr = Mailbox.ID_FOLDER_CALENDAR;
type = MailItem.Type.APPOINTMENT;
view = MailItem.Type.APPOINTMENT;
}
} else if (file.endsWith(".wiki")) {
defaultFldr = Mailbox.ID_FOLDER_NOTEBOOK;
type = MailItem.Type.WIKI;
view = MailItem.Type.WIKI;
} else {
defaultFldr = Mailbox.ID_FOLDER_BRIEFCASE;
type = MailItem.Type.DOCUMENT;
view = MailItem.Type.DOCUMENT;
}
if (types != null && !types.contains(type)) {
return;
}
if (dir.equals("")) {
if (fldr.getPath().equals("/")) {
fldr = mbox.getFolderById(oc, defaultFldr);
}
if (fldr.getDefaultView() != MailItem.Type.UNKNOWN && fldr.getDefaultView() != view && !((view == MailItem.Type.DOCUMENT || view == MailItem.Type.WIKI) && (fldr.getDefaultView() == MailItem.Type.DOCUMENT || fldr.getDefaultView() == MailItem.Type.WIKI))) {
throw FormatterServiceException.INVALID_TYPE(view.toString(), fldr.getPath());
}
} else {
String s = fldr.getPath();
if (!s.endsWith("/"))
s += '/';
if (dir.startsWith(s))
dir = dir.substring(s.length());
fldr = createPath(context, fmap, fldr.getPath() + dir, view);
}
switch(type) {
case APPOINTMENT:
case TASK:
boolean continueOnError = context.ignoreAndContinueOnError();
boolean preserveExistingAlarms = context.preserveAlarms();
InputStream is = ais.getInputStream();
try {
if (aie.getSize() <= LC.calendar_ics_import_full_parse_max_size.intValue()) {
List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, UTF8);
ImportInviteVisitor visitor = new ImportInviteVisitor(oc, fldr, preserveExistingAlarms);
Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
} else {
ZICalendarParseHandler handler = new IcsImportParseHandler(oc, context.targetAccount, fldr, continueOnError, preserveExistingAlarms);
ZCalendarBuilder.parse(is, UTF8, handler);
}
} finally {
is.close();
}
break;
case CONTACT:
if (file.endsWith(".csv")) {
reader = new BufferedReader(new InputStreamReader(ais.getInputStream(), UTF8));
ImportContacts.ImportCsvContacts(oc, context.targetMailbox, new ItemId(fldr), ContactCSV.getContacts(reader, null));
} else {
List<VCard> cards = VCard.parseVCard(new String(readArchiveEntry(ais, aie), UTF8));
if (cards == null || cards.size() == 0 || (cards.size() == 1 && cards.get(0).fields.isEmpty())) {
addError(errs, FormatterServiceException.MISSING_VCARD_FIELDS(name));
return;
}
for (VCard vcf : cards) {
if (vcf.fields.isEmpty())
continue;
mbox.createContact(oc, vcf.asParsedContact(), fldr.getId(), null);
}
}
break;
case DOCUMENT:
case WIKI:
String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
try {
oldItem = mbox.getItemByPath(oc, file, fldr.getId());
if (oldItem.getType() != type) {
addError(errs, FormatterServiceException.MISMATCHED_TYPE(name));
} else if (r == Resolve.Replace) {
mbox.delete(oc, oldItem.getId(), type);
throw MailServiceException.NO_SUCH_ITEM(oldItem.getId());
} else if (r != Resolve.Skip) {
newItem = mbox.addDocumentRevision(oc, oldItem.getId(), creator, oldItem.getName(), null, ais.getInputStream());
}
} catch (NoSuchItemException e) {
if (type == MailItem.Type.WIKI) {
newItem = mbox.createWiki(oc, fldr.getId(), file, creator, null, ais.getInputStream());
} else {
newItem = mbox.createDocument(oc, fldr.getId(), file, null, creator, null, ais.getInputStream());
}
}
if (newItem != null) {
if (timestamp)
mbox.setDate(oc, newItem.getId(), type, aie.getModTime());
}
break;
case MESSAGE:
int flags = aie.isUnread() ? Flag.BITMASK_UNREAD : 0;
DeliveryOptions opt = new DeliveryOptions().setFolderId(fldr.getId()).setNoICal(true).setFlags(flags);
mbox.addMessage(oc, ais.getInputStream(), (int) aie.getSize(), timestamp ? aie.getModTime() : ParsedMessage.DATE_HEADER, opt, null);
break;
}
} catch (Exception e) {
if (e instanceof MailServiceException && ((MailServiceException) e).getCode() == MailServiceException.QUOTA_EXCEEDED)
throw (MailServiceException) e;
else
addError(errs, FormatterServiceException.UNKNOWN_ERROR(aie.getName(), e));
}
}
use of com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor in project zm-mailbox by Zimbra.
the class IcsFormatter method saveCallback.
@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws UserServletException, ServiceException, IOException, ServletException {
boolean continueOnError = context.ignoreAndContinueOnError();
boolean preserveExistingAlarms = context.preserveAlarms();
InputStream is = context.getRequestInputStream(-1);
String charset = MimeConstants.P_CHARSET_UTF8;
String ctStr = context.req.getContentType();
if (ctStr != null) {
String cs = Mime.getCharset(ctStr);
if (cs != null)
charset = cs;
}
try {
if (context.req.getContentLength() <= LC.calendar_ics_import_full_parse_max_size.intValue()) {
// Build a list of ZVCalendar objects by fully parsing the ics file, then iterate them
// and add them one by one. Memory hungry if there are very many events/tasks, but it allows
// TZID reference before VTIMEZONE of that timezone appears in the ics file.
List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, charset);
ImportInviteVisitor visitor = new ImportInviteVisitor(context.opContext, folder, preserveExistingAlarms);
Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
} else {
// Events/tasks are added in callbacks during parse. This is more memory efficient than the
// other method, but it doesn't allow forward referencing TZIDs. ics files generated by
// clients that put VTIMEZONEs at the end will not parse. Evolution client does this.
ZICalendarParseHandler handler = new IcsImportParseHandler(context.opContext, context.targetAccount, folder, continueOnError, preserveExistingAlarms);
ZCalendarBuilder.parse(is, charset, handler);
}
} finally {
is.close();
}
}
Aggregations