use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.
the class TestCalDav method testFuzzyTimeZoneMatchGMT_06.
@Test
public void testFuzzyTimeZoneMatchGMT_06() throws Exception {
try (ByteArrayInputStream bais = new ByteArrayInputStream(VtimeZoneGMT_0600_0500.getBytes())) {
ZVCalendar tzcal = ZCalendar.ZCalendarBuilder.build(bais, MimeConstants.P_CHARSET_UTF8);
assertNotNull("tzcal", tzcal);
ZComponent tzcomp = tzcal.getComponent(ICalTok.VTIMEZONE);
assertNotNull("tzcomp", tzcomp);
ICalTimeZone tz = ICalTimeZone.fromVTimeZone(tzcomp, false, /* skipLookup */
ICalTimeZone.TZID_NAME_ASSIGNMENT_BEHAVIOR.ALWAYS_KEEP);
ICalTimeZone matchtz = ICalTimeZone.lookupMatchingWellKnownTZ(tz);
assertEquals("ID of Timezone which fuzzy matches GMT=06.00/-05.00", "America/Chicago", matchtz.getID());
}
}
use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.
the class ParsedMessage method analyzePart.
/**
* @return Extracted toplevel text (any text that should go into the toplevel indexed document)
*/
private String analyzePart(boolean isMainBody, MPartInfo mpi) throws MessagingException, ServiceException {
boolean ignoreCalendar;
if (calendarPartInfo == null) {
ignoreCalendar = isBouncedCalendar(mpi);
} else {
ignoreCalendar = true;
}
String methodParam = (new ContentType(mpi.getMimePart().getContentType())).getParameter("method");
if (methodParam == null && !LC.calendar_allow_invite_without_method.booleanValue()) {
ignoreCalendar = true;
}
String toRet = "";
try {
// ignore multipart "container" parts
if (mpi.isMultipart()) {
return toRet;
}
String ctype = mpi.getContentType();
MimeHandler handler = MimeHandlerManager.getMimeHandler(ctype, mpi.getFilename());
assert (handler != null);
handler.setDefaultCharset(defaultCharset);
Mime.repairTransferEncoding(mpi.getMimePart());
if (handler.isIndexingEnabled()) {
handler.init(mpi.getMimePart().getDataHandler().getDataSource());
handler.setPartName(mpi.getPartName());
handler.setFilename(mpi.getFilename());
handler.setSize(mpi.getSize());
// remember the first iCalendar attachment
if (!ignoreCalendar && calendarPartInfo == null) {
ZVCalendar cal = handler.getICalendar();
if (cal != null) {
setCalendarPartInfo(mpi, cal);
}
}
// - IndexAttachments was set and !disableIndexingAttachmentsTogether
if ((isMainBody && (!handler.runsExternally() || indexAttachments)) || (indexAttachments && !DebugConfig.disableIndexingAttachmentsTogether)) {
toRet = handler.getContent();
}
if (indexAttachments && !DebugConfig.disableIndexingAttachmentsSeparately) {
// Each non-text MIME part is also indexed as a separate
// Lucene document. This is necessary so that we can tell the
// client what parts match if a search matched a particular
// part.
IndexDocument doc = new IndexDocument(handler.getDocument());
String filename = handler.getFilename();
if (!Strings.isNullOrEmpty(filename)) {
filenames.add(filename);
}
doc.addSortSize(mpi.getMimePart().getSize());
luceneDocuments.add(setLuceneHeadersFromContainer(doc));
}
}
// make sure we've got the text/calendar handler installed
if (!ignoreCalendar && calendarPartInfo == null && ctype.equals(MimeConstants.CT_TEXT_CALENDAR)) {
if (handler.isIndexingEnabled()) {
ZimbraLog.index.warn("TextCalendarHandler not correctly installed");
}
InputStream is = null;
try {
String charset = mpi.getContentTypeParameter(MimeConstants.P_CHARSET);
if (charset == null || charset.trim().isEmpty()) {
charset = MimeConstants.P_CHARSET_DEFAULT;
}
is = mpi.getMimePart().getInputStream();
ZVCalendar cal = ZCalendarBuilder.build(is, charset);
if (cal != null) {
setCalendarPartInfo(mpi, cal);
}
} catch (IOException ioe) {
ZimbraLog.index.warn("error reading text/calendar mime part", ioe);
} finally {
ByteUtil.closeStream(is);
}
}
} catch (MimeHandlerException e) {
handleParseError(mpi, e);
} catch (ObjectHandlerException e) {
handleParseError(mpi, e);
}
return toRet;
}
use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.
the class ICalReply method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
Element icalElem = request.getElement(MailConstants.E_CAL_ICAL);
String icalStr = icalElem.getText();
String sender = icalElem.getAttribute(MailConstants.E_CAL_ATTENDEE, null);
ZVCalendar cal = ZCalendarBuilder.build(icalStr);
List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, cal, false);
for (Invite inv : invites) {
String method = inv.getMethod();
if (!ICalTok.REPLY.toString().equals(method)) {
throw ServiceException.INVALID_REQUEST("iCalendar method must be REPLY (was " + method + ")", null);
}
}
for (Invite inv : invites) {
mbox.processICalReply(octxt, inv, sender);
}
Element response = getResponseElement(zsc);
return response;
}
use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.
the class ImportAppointments method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
String folder = request.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER_ID);
ItemId iidFolder = new ItemId(folder, zsc);
String ct = request.getAttribute(MailConstants.A_CONTENT_TYPE);
if (!ct.equalsIgnoreCase("ics") && !ct.equalsIgnoreCase(MimeConstants.CT_TEXT_CALENDAR))
throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
Element content = request.getElement(MailConstants.E_CONTENT);
List<Upload> uploads = null;
InputStream is = null;
String partStr = null;
String attachment = content.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
String messageId = content.getAttribute(MailConstants.A_MESSAGE_ID, null);
if (attachment != null && messageId != null) {
throw ServiceException.INVALID_REQUEST("use either aid or mid but not both", null);
}
SourceSpecMethod sourceSpecMethod;
if (messageId != null) {
sourceSpecMethod = SourceSpecMethod.MSG_PART;
} else if (attachment != null) {
sourceSpecMethod = SourceSpecMethod.ATTACH_ID;
} else {
sourceSpecMethod = SourceSpecMethod.INLINE_TEXT;
}
try {
if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
// Get content from part of existing message.
ItemId iid = new ItemId(messageId, zsc);
String part = content.getAttribute(MailConstants.A_PART);
partStr = CreateContact.fetchItemPart(zsc, octxt, mbox, iid, part, null, MimeConstants.P_CHARSET_UTF8);
is = new ByteArrayInputStream(partStr.getBytes(MimeConstants.P_CHARSET_UTF8));
}
// if the "target item" is remote, we will need to proxy the request
ItemId iidTarget = getProxyTarget(zsc, octxt, iidFolder, true);
if (iidTarget != null) {
ZimbraLog.misc.info("Proxying ImportAppointments - folder=" + folder + ", acct=" + iidTarget.getAccountId());
if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
/* Bug 77131 change specification method to something that will work in the proxied context */
content.addAttribute(MailConstants.A_MESSAGE_ID, (String) null);
content.addAttribute(MailConstants.A_PART, (String) null);
Upload up = FileUploadServlet.saveUpload(is, "upload.ics", MimeConstants.CT_TEXT_CALENDAR, zsc.getAuthtokenAccountId());
content.addAttribute(MailConstants.A_ATTACHMENT_ID, up.getId());
}
return proxyRequest(request, context, iidFolder, iidTarget);
}
if (SourceSpecMethod.ATTACH_ID.equals(sourceSpecMethod)) {
is = parseUploadedContent(zsc, attachment, uploads = new ArrayList<Upload>());
} else if (SourceSpecMethod.INLINE_TEXT.equals(sourceSpecMethod)) {
// Convert LF to CRLF because the XML parser normalizes element text to LF.
String text = StringUtil.lfToCrlf(content.getText());
is = new ByteArrayInputStream(text.getBytes(MimeConstants.P_CHARSET_UTF8));
}
List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, MimeConstants.P_CHARSET_UTF8);
is.close();
is = null;
List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, icals, true, true, null);
Set<String> uidsSeen = new HashSet<String>();
StringBuilder ids = new StringBuilder();
for (Invite inv : invites) {
// handle missing UIDs on remote calendars by generating them as needed
String uid = inv.getUid();
if (uid == null) {
uid = LdapUtil.generateUUID();
inv.setUid(uid);
}
boolean addRevision;
if (!uidsSeen.contains(uid)) {
addRevision = true;
uidsSeen.add(uid);
} else {
addRevision = false;
}
// and add the invite to the calendar!
try {
AddInviteData aid = mbox.addInvite(octxt, inv, iidFolder.getId(), false, addRevision);
if (aid != null) {
if (ids.length() > 0)
ids.append(",");
ids.append(aid.calItemId).append("-").append(aid.invId);
}
} catch (ServiceException e) {
ZimbraLog.calendar.warn("Skipping bad iCalendar object during import: uid=" + inv.getUid(), e);
}
}
Element response = zsc.createElement(MailConstants.IMPORT_APPOINTMENTS_RESPONSE);
Element cn = response.addElement(MailConstants.E_APPOINTMENT);
cn.addAttribute(MailConstants.A_IDS, ids.toString());
cn.addAttribute(MailConstants.A_NUM, invites.size());
return response;
} catch (IOException e) {
throw MailServiceException.UNABLE_TO_IMPORT_APPOINTMENTS(e.getMessage(), e);
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
}
if (attachment != null)
FileUploadServlet.deleteUploads(uploads);
}
}
use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.
the class GetICal method handle.
/* (non-Javadoc)
* @see com.zimbra.soap.DocumentHandler#handle(org.dom4j.Element, java.util.Map)
*/
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account authAccount = getAuthenticatedAccount(zsc);
Mailbox mbx = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
String iidStr = request.getAttribute(MailConstants.A_ID, null);
long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME, -1);
long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME, -1);
// int compNum = (int)request.getAttributeLong(MailService.A_CAL_COMPONENT_NUM);
int compNum = 0;
Browser browser = HttpUtil.guessBrowser(zsc.getUserAgent());
boolean useOutlookCompatMode = Browser.IE.equals(browser);
try {
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
OutputStreamWriter wout = new OutputStreamWriter(buf);
ZVCalendar cal = null;
ItemId iid = iidStr != null ? new ItemId(iidStr, zsc) : null;
if (iid != null) {
CalendarItem calItem = mbx.getCalendarItemById(octxt, iid.getId());
if (calItem == null) {
throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Could not find calendar item");
}
Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
if (inv == null) {
throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
}
boolean allowPrivateAccess = calItem.allowPrivateAccess(authAccount, zsc.isUsingAdminPrivileges());
cal = inv.newToICalendar(useOutlookCompatMode, allowPrivateAccess);
cal.toICalendar(wout);
} else {
mbx.writeICalendarForRange(wout, octxt, rangeStart, rangeEnd, Mailbox.ID_FOLDER_CALENDAR, useOutlookCompatMode, false, false);
}
wout.flush();
Element response = getResponseElement(zsc);
Element icalElt = response.addElement(MailConstants.E_CAL_ICAL);
if (iid != null)
icalElt.addAttribute(MailConstants.A_ID, new ItemIdFormatter(zsc).formatItemId(iid));
icalElt.addText(buf.toString());
return response;
} catch (MailServiceException.NoSuchItemException e) {
throw ServiceException.FAILURE("Error could get default invite for Invite: " + iidStr + "-" + compNum, e);
} catch (IOException e) {
throw ServiceException.FAILURE("IO Exception while outputing Calendar for Invite: " + iidStr + "-" + compNum, e);
}
} catch (MailServiceException.NoSuchItemException e) {
throw ServiceException.FAILURE("No Such Invite Message: " + iidStr, e);
}
}
Aggregations