use of com.zimbra.cs.fb.FreeBusyQuery in project zm-mailbox by Zimbra.
the class ScheduleOutbox method handleFreebusyRequest.
private void handleFreebusyRequest(DavContext ctxt, ZComponent vfreebusy, String originator, String rcpt, Element resp) throws DavException, ServiceException {
ZProperty dtstartProp = vfreebusy.getProperty(ICalTok.DTSTART);
ZProperty dtendProp = vfreebusy.getProperty(ICalTok.DTEND);
ZProperty durationProp = vfreebusy.getProperty(ICalTok.DURATION);
if (dtstartProp == null || dtendProp == null && durationProp == null)
throw new DavException("missing dtstart or dtend/duration in the schedule request", HttpServletResponse.SC_BAD_REQUEST, null);
long start, end;
try {
ParsedDateTime startTime = ParsedDateTime.parseUtcOnly(dtstartProp.getValue());
start = startTime.getUtcTime();
if (dtendProp != null) {
end = ParsedDateTime.parseUtcOnly(dtendProp.getValue()).getUtcTime();
} else {
ParsedDuration dur = ParsedDuration.parse(durationProp.getValue());
ParsedDateTime endTime = startTime.add(dur);
end = endTime.getUtcTime();
}
} catch (ParseException pe) {
throw new DavException("can't parse date", HttpServletResponse.SC_BAD_REQUEST, pe);
}
ZimbraLog.dav.debug("rcpt: " + rcpt + ", start: " + new Date(start) + ", end: " + new Date(end));
FreeBusy fb = null;
if (ctxt.isFreebusyEnabled()) {
FreeBusyQuery fbQuery = new FreeBusyQuery(ctxt.getRequest(), ctxt.getAuthAccount(), start, end, null);
fbQuery.addEmailAddress(getAddressFromPrincipalURL(rcpt), FreeBusyQuery.CALENDAR_FOLDER_ALL);
java.util.Collection<FreeBusy> fbResult = fbQuery.getResults();
if (fbResult.size() > 0)
fb = fbResult.iterator().next();
}
if (fb != null) {
String fbMsg = fb.toVCalendar(FreeBusy.Method.REPLY, originator, rcpt, null);
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
resp.addElement(DavElements.E_CALENDAR_DATA).setText(fbMsg);
} else {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No f/b for the user");
}
}
use of com.zimbra.cs.fb.FreeBusyQuery in project zm-mailbox by Zimbra.
the class GetFreeBusy method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zc = getZimbraSoapContext(context);
long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME);
long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME);
validateRange(rangeStart, rangeEnd);
Element response = getResponseElement(zc);
// MailConstants.A_UID should be deprecated at some point, bug 21776, comment #14
// comma-separated list of account emails or zimbraId GUIDs that *must* match UUID format
String uidParam = request.getAttribute(MailConstants.A_UID, null);
// comma-separated list of account zimbraId GUIDs
String idParam = request.getAttribute(MailConstants.A_ID, null);
// comma-separated list of account emails
String nameParam = request.getAttribute(MailConstants.A_NAME, null);
String exApptUid = request.getAttribute(MailConstants.A_APPT_FREEBUSY_EXCLUDE_UID, null);
Account requestor = Provisioning.getInstance().get(Key.AccountBy.id, zc.getAuthtokenAccountId());
FreeBusyQuery fbQuery = new FreeBusyQuery((HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST), zc, requestor, rangeStart, rangeEnd, exApptUid);
String[] idStrs = null;
// uidParam should be deprecated at some point, bug 21776 comment #14
if (uidParam != null) {
idStrs = uidParam.split(",");
for (String idStr : idStrs) fbQuery.addId(idStr, FreeBusyQuery.CALENDAR_FOLDER_ALL);
}
if (idParam != null) {
idStrs = idParam.split(",");
for (String idStr : idStrs) fbQuery.addAccountId(idStr, FreeBusyQuery.CALENDAR_FOLDER_ALL);
}
if (nameParam != null) {
idStrs = nameParam.split(",");
for (String idStr : idStrs) fbQuery.addEmailAddress(idStr, FreeBusyQuery.CALENDAR_FOLDER_ALL);
}
for (Iterator<Element> usrIter = request.elementIterator(MailConstants.E_FREEBUSY_USER); usrIter.hasNext(); ) {
Element usrElem = usrIter.next();
int folderId = (int) usrElem.getAttributeLong(MailConstants.A_FOLDER, FreeBusyQuery.CALENDAR_FOLDER_ALL);
if (folderId == Mailbox.ID_FOLDER_USER_ROOT || folderId == 0)
folderId = FreeBusyQuery.CALENDAR_FOLDER_ALL;
String id = usrElem.getAttribute(MailConstants.A_ID, null);
if (id != null)
fbQuery.addAccountId(id, folderId);
String name = usrElem.getAttribute(MailConstants.A_NAME, null);
if (name != null)
fbQuery.addEmailAddress(name, folderId);
}
fbQuery.getResults(response);
return response;
}
use of com.zimbra.cs.fb.FreeBusyQuery in project zm-mailbox by Zimbra.
the class PublicICalServlet method doGetFreeBusy.
/**
*
* http://localhost:7070/service/pubcal/freebusy.ifb?acct=user@host.com
*
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
public final void doGetFreeBusy(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String acctName = req.getParameter(QP_ACCOUNT);
String startStr = req.getParameter(QP_START_TIME);
String endStr = req.getParameter(QP_END_TIME);
resp.setContentType(MimeConstants.CT_TEXT_CALENDAR);
if (checkBlankOrNull(resp, QP_ACCOUNT, acctName))
return;
long now = new Date().getTime();
long rangeStart = now - Constants.MILLIS_PER_WEEK;
long rangeEnd = now + (2 * Constants.MILLIS_PER_MONTH);
if (startStr != null)
rangeStart = Long.parseLong(startStr);
if (endStr != null)
rangeEnd = Long.parseLong(endStr);
if (rangeEnd < rangeStart) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "End time must be after Start time");
return;
}
long days = (rangeEnd - rangeStart) / Constants.MILLIS_PER_DAY;
long maxDays = LC.calendar_freebusy_max_days.longValueWithinRange(0, 36600);
if (days > maxDays) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Requested range is too large (max " + maxDays + " days)");
return;
}
String targetAccountId = null;
try {
Account targetAccount = Provisioning.getInstance().get(AccountBy.name, acctName);
if (targetAccount != null)
targetAccountId = targetAccount.getId();
} catch (ServiceException e) {
}
AuthToken at = getAuthTokenFromCookie(req, resp, true);
if (at == null) {
String authTokenParam = req.getParameter(QP_AUTH_TOKEN);
if (authTokenParam != null) {
try {
at = AuthProvider.getAuthToken(authTokenParam);
} catch (AuthTokenException e) {
sLog.warn("Auth error: " + e.getMessage(), e);
}
}
}
Account authAccount = null;
ZimbraSoapContext zsc = null;
if (at != null) {
try {
authAccount = Provisioning.getInstance().get(AccountBy.id, at.getAccountId(), at);
} catch (ServiceException e) {
sLog.warn("Auth error: " + e.getMessage(), e);
}
try {
zsc = new ZimbraSoapContext(at, targetAccountId, SoapProtocol.SoapJS, SoapProtocol.SoapJS);
} catch (ServiceException e) {
sLog.error("Error initializing request context", e);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error initializing request context");
return;
}
} else {
authAccount = GuestAccount.ANONYMOUS_ACCT;
}
FreeBusyQuery fbQuery = new FreeBusyQuery(req, zsc, authAccount, rangeStart, rangeEnd, null);
fbQuery.addEmailAddress(acctName, FreeBusyQuery.CALENDAR_FOLDER_ALL);
Collection<FreeBusy> result = fbQuery.getResults();
FreeBusy fb = null;
if (result.size() > 0)
fb = result.iterator().next();
else
fb = FreeBusy.emptyFreeBusy(acctName, rangeStart, rangeEnd);
String url = req.getRequestURL() + "?" + req.getQueryString();
String fbMsg = fb.toVCalendar(FreeBusy.Method.PUBLISH, acctName, null, url);
resp.getOutputStream().write(fbMsg.getBytes());
}
use of com.zimbra.cs.fb.FreeBusyQuery in project zm-mailbox by Zimbra.
the class CheckRecurConflicts method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account authAcct = getAuthenticatedAccount(zsc);
Element response = getResponseElement(zsc);
long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME, 0);
if (rangeStart == 0)
rangeStart = System.currentTimeMillis();
long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME, 0);
if (rangeEnd == 0)
rangeEnd = Long.MAX_VALUE;
boolean allInstances = request.getAttributeBool(MailConstants.A_CAL_ALL, false);
String exApptUid = request.getAttribute(MailConstants.A_APPT_FREEBUSY_EXCLUDE_UID, null);
// Parse and expand the recurrence.
TimeZoneMap tzmap = new TimeZoneMap(Util.getAccountTimeZone(authAcct));
ParsedRecurrence parsed = parseRecur(request, tzmap);
List<Instance> instances = getInstances(parsed, rangeStart, rangeEnd);
if (instances == null || instances.isEmpty())
return response;
// Find the range covered by the instances.
long rangeStartActual = rangeEnd, rangeEndActual = rangeStart;
for (Instance inst : instances) {
if (inst.hasStart() && inst.hasEnd()) {
rangeStartActual = Math.min(rangeStartActual, inst.getStart());
rangeEndActual = Math.max(rangeEndActual, inst.getEnd());
}
}
if (rangeStartActual >= rangeEndActual)
return response;
// Run free/busy search on the users.
FreeBusyQuery fbQuery = new FreeBusyQuery((HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST), zsc, authAcct, rangeStartActual, rangeEndActual, exApptUid);
for (Iterator<Element> usrIter = request.elementIterator(MailConstants.E_FREEBUSY_USER); usrIter.hasNext(); ) {
Element usrElem = usrIter.next();
int folderId = (int) usrElem.getAttributeLong(MailConstants.A_FOLDER, FreeBusyQuery.CALENDAR_FOLDER_ALL);
if (folderId == Mailbox.ID_FOLDER_USER_ROOT || folderId == 0)
folderId = FreeBusyQuery.CALENDAR_FOLDER_ALL;
String id = usrElem.getAttribute(MailConstants.A_ID, null);
if (id != null)
fbQuery.addAccountId(id, folderId);
String name = usrElem.getAttribute(MailConstants.A_NAME, null);
if (name != null)
fbQuery.addEmailAddress(name, folderId);
}
Collection<FreeBusy> fbResults = fbQuery.getResults();
List<UserConflicts> conflicts = new ArrayList<UserConflicts>();
for (FreeBusy fb : fbResults) {
UserConflicts ucon = getConflicts(fb, instances);
conflicts.add(ucon);
}
// Find conflicts for each instance.
for (Instance inst : instances) {
Element instElem = addInstance(response, inst);
int numConflicts = 0;
for (UserConflicts ucon : conflicts) {
String fbStatus = ucon.get(inst);
if (fbStatus != null) {
++numConflicts;
String username = ucon.getUsername();
Element usrElem = instElem.addElement(MailConstants.E_FREEBUSY_USER);
usrElem.addAttribute(MailConstants.A_NAME, username);
usrElem.addAttribute(MailConstants.A_APPT_FREEBUSY, fbStatus);
}
}
if (numConflicts == 0 && !allInstances)
instElem.detach();
}
return response;
}
Aggregations