use of org.bedework.icalendar.Icalendar in project bw-calendar-engine by Bedework.
the class Events method compileAlarms.
/**
* Compile an alarm component
*
* @param val
* @return alarms or null
* @throws CalFacadeException
*/
public Set<BwAlarm> compileAlarms(final String val) throws CalFacadeException {
try {
StringReader sr = new StringReader(ValidateAlarmPrefix + val + ValidateAlarmSuffix);
IcalTranslator trans = new IcalTranslator(getSvc().getIcalCallback());
Icalendar ic = trans.fromIcal(null, sr);
if ((ic == null) || (ic.getEventInfo() == null)) {
if (debug) {
trace("Not single event");
}
return null;
}
/* There should be alarms in the Calendar object
*/
EventInfo ei = ic.getEventInfo();
BwEvent ev = ei.getEvent();
Set<BwAlarm> alarms = ev.getAlarms();
if (Util.isEmpty(alarms)) {
return null;
}
return alarms;
} catch (CalFacadeException cfe) {
if (debug) {
error(cfe);
}
return null;
}
}
use of org.bedework.icalendar.Icalendar in project bw-calendar-engine by Bedework.
the class BwSysIntfImpl method validateAlarm.
/**
* Validate an alarm component
*
* @param val
* @return boolean false for failure
* @throws WebdavException
*/
@Override
public boolean validateAlarm(final String val) throws WebdavException {
try {
// Ensure open
getSvci();
StringReader sr = new StringReader(ValidateAlarmPrefix + val + ValidateAlarmSuffix);
Icalendar ic = trans.fromIcal(null, sr);
if ((ic == null) || (ic.getEventInfo() == null)) {
if (debug) {
debug("Not single event");
}
return false;
}
/* There should be alarms in the Calendar object
*/
EventInfo ei = ic.getEventInfo();
BwEvent ev = ei.getEvent();
if ((ev.getAlarms() == null) || ev.getAlarms().isEmpty()) {
return false;
}
return true;
} catch (CalFacadeException cfe) {
if (debug) {
error(cfe);
}
return false;
}
}
use of org.bedework.icalendar.Icalendar in project bw-calendar-engine by Bedework.
the class IscheduleClient method parseResponse.
private void parseResponse(final HostInfo hi, final Response resp) throws CalFacadeException {
try {
final Document doc = parseContent(resp);
if (doc == null) {
throw new CalFacadeException(CalFacadeException.badResponse);
}
final QName sresponseTag;
final QName responseTag;
final QName recipientTag;
final QName requestStatusTag;
final QName calendarDataTag;
final QName errorTag;
final QName descriptionTag;
if (hi.getSupportsISchedule()) {
sresponseTag = IscheduleTags.scheduleResponse;
responseTag = IscheduleTags.response;
recipientTag = IscheduleTags.recipient;
requestStatusTag = IscheduleTags.requestStatus;
calendarDataTag = IscheduleTags.calendarData;
errorTag = IscheduleTags.error;
descriptionTag = IscheduleTags.responseDescription;
} else {
sresponseTag = CaldavTags.scheduleResponse;
responseTag = CaldavTags.response;
recipientTag = CaldavTags.recipient;
requestStatusTag = CaldavTags.requestStatus;
calendarDataTag = CaldavTags.calendarData;
errorTag = WebdavTags.error;
descriptionTag = WebdavTags.responseDescription;
}
final Element root = doc.getDocumentElement();
if (!XmlUtil.nodeMatches(root, sresponseTag)) {
throw new CalFacadeException(CalFacadeException.badResponse);
}
for (final Element el : getChildren(root)) {
final ResponseElement fbel = new ResponseElement();
if (!XmlUtil.nodeMatches(el, responseTag)) {
throw new CalFacadeException(CalFacadeException.badResponse);
}
/* ================================================================
11.2. CALDAV/ISCHEDULE:response XML Element
Name: response
Namespace: urn:ietf:params:xml:ns:caldav or
urn:ietf:params:xml:ns:ischedule or
Purpose: Contains a single response for a POST method request.
Description: See Section 6.1.4.
Definition:
<!ELEMENT response (recipient,
request-status,
calendar-data?,
error?,
response-description?)>
================================================================ */
final Iterator<Element> respels = getChildren(el).iterator();
Element respel = respels.next();
if (!XmlUtil.nodeMatches(respel, recipientTag)) {
throw new CalFacadeException(CalFacadeException.badResponse);
}
fbel.setRecipient(getElementContent(respel));
respel = respels.next();
if (!XmlUtil.nodeMatches(respel, requestStatusTag)) {
throw new CalFacadeException(CalFacadeException.badResponse);
}
fbel.setReqStatus(getElementContent(respel));
if (respels.hasNext()) {
respel = respels.next();
if (XmlUtil.nodeMatches(respel, calendarDataTag)) {
final String calData = getElementContent(respel);
final Reader rdr = new StringReader(calData);
final Icalendar ical = trans.fromIcal(null, rdr);
fbel.setCalData(ical.getEventInfo());
} else if (XmlUtil.nodeMatches(respel, errorTag)) {
fbel.setDavError(respel.getFirstChild().getLocalName());
} else if (XmlUtil.nodeMatches(respel, descriptionTag)) {
// XXX Not processed yet
} else {
throw new CalFacadeException(CalFacadeException.badResponse);
}
}
resp.addResponse(fbel);
}
} catch (final Throwable t) {
if (debug) {
error(t);
}
resp.setException(t);
}
}
use of org.bedework.icalendar.Icalendar in project bw-calendar-engine by Bedework.
the class BwSysIntfImpl method tzidFromTzdef.
@Override
public String tzidFromTzdef(final String val) throws WebdavException {
try {
// Ensure open
getSvci();
StringReader sr = new StringReader(val);
Icalendar ic = trans.fromIcal(null, sr);
if ((ic == null) || // No components other than timezones
(ic.size() != 0) || (ic.getTimeZones().size() != 1)) {
if (debug) {
debug("Not single timezone");
}
throw new WebdavForbidden(CaldavTags.calendarTimezone, "Not single timezone");
}
/* This should be the only timezone ion the Calendar object
*/
TimeZone tz = ic.getTimeZones().iterator().next().tz;
return tz.getID();
} catch (CalFacadeException cfe) {
throw new WebdavException(cfe);
}
}
use of org.bedework.icalendar.Icalendar in project bw-calendar-engine by Bedework.
the class BwSysIntfImpl method fromIcal.
/* (non-Javadoc)
* @see org.bedework.caldav.server.sysinterface.SysIntf#fromIcal(org.bedework.caldav.server.CalDAVCollection, java.io.Reader, java.lang.String, org.bedework.caldav.server.sysinterface.SysIntf.IcalResultType, boolean)
*/
@Override
public SysiIcalendar fromIcal(final CalDAVCollection col, final Reader rdr, final String contentType, final IcalResultType rtype, final boolean mergeAttendees) throws WebdavException {
// Ensure open
getSvci();
boolean rollback = true;
/* (CALDAV:supported-calendar-data) */
if ((contentType == null) || (!contentType.equals("text/calendar") && !contentType.equals("application/calendar+json"))) {
if (debug) {
debug("Bad content type: " + contentType);
}
throw new WebdavForbidden(CaldavTags.supportedCalendarData, "Bad content type: " + contentType);
}
try {
BwCalendar bwcol = null;
if (col != null) {
bwcol = unwrap(col);
}
final Icalendar ic = trans.fromIcal(bwcol, new SysIntfReader(rdr), contentType, // diff the contents
true, mergeAttendees);
if (rtype == IcalResultType.OneComponent) {
if (ic.getComponents().size() != 1) {
throw new WebdavForbidden(CaldavTags.validCalendarObjectResource);
}
if (!(ic.getComponents().iterator().next() instanceof EventInfo)) {
throw new WebdavForbidden(CaldavTags.validCalendarObjectResource);
}
} else if (rtype == IcalResultType.TimeZone) {
if (ic.getTimeZones().size() != 1) {
throw new WebdavForbidden("Expected one timezone");
}
}
final SysiIcalendar sic = new MySysiIcalendar(this, ic);
rollback = false;
return sic;
} catch (final IcalMalformedException ime) {
throw new WebdavForbidden(CaldavTags.validCalendarData, ime.getMessage());
} catch (final CalFacadeException cfe) {
if (CalFacadeException.unknownTimezone.equals(cfe.getDetailMessage())) {
throw new WebdavForbidden(CaldavTags.validTimezone, cfe.getMessage());
}
throw new WebdavForbidden(CaldavTags.validCalendarObjectResource, cfe.getMessage());
} catch (final WebdavException wde) {
throw wde;
} catch (final Throwable t) {
if (debug) {
error(t);
}
// Assume bad data in some way
throw new WebdavForbidden(CaldavTags.validCalendarObjectResource, t.getMessage());
} finally {
if (rollback) {
try {
getSvci().rollbackTransaction();
} catch (final Throwable ignored) {
}
}
}
}
Aggregations