Search in sources :

Example 1 with Icalendar

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;
    }
}
Also used : CoreEventInfo(org.bedework.calcorei.CoreEventInfo) EventInfo(org.bedework.calfacade.svc.EventInfo) Icalendar(org.bedework.icalendar.Icalendar) StringReader(java.io.StringReader) BwEvent(org.bedework.calfacade.BwEvent) IcalTranslator(org.bedework.icalendar.IcalTranslator) BwAlarm(org.bedework.calfacade.BwAlarm) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 2 with Icalendar

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;
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) Icalendar(org.bedework.icalendar.Icalendar) SysiIcalendar(org.bedework.caldav.server.SysiIcalendar) StringReader(java.io.StringReader) BwEvent(org.bedework.calfacade.BwEvent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with Icalendar

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);
    }
}
Also used : Icalendar(org.bedework.icalendar.Icalendar) QName(javax.xml.namespace.QName) ResponseElement(org.bedework.calsvc.scheduling.hosts.Response.ResponseElement) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) ResponseElement(org.bedework.calsvc.scheduling.hosts.Response.ResponseElement)

Example 4 with Icalendar

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);
    }
}
Also used : TimeZone(net.fortuna.ical4j.model.TimeZone) Icalendar(org.bedework.icalendar.Icalendar) SysiIcalendar(org.bedework.caldav.server.SysiIcalendar) WebdavForbidden(org.bedework.webdav.servlet.shared.WebdavForbidden) StringReader(java.io.StringReader) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 5 with Icalendar

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) {
            }
        }
    }
}
Also used : SysIntfReader(org.bedework.caldav.server.SysIntfReader) EventInfo(org.bedework.calfacade.svc.EventInfo) WebdavForbidden(org.bedework.webdav.servlet.shared.WebdavForbidden) Icalendar(org.bedework.icalendar.Icalendar) SysiIcalendar(org.bedework.caldav.server.SysiIcalendar) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) BwCalendar(org.bedework.calfacade.BwCalendar) SysiIcalendar(org.bedework.caldav.server.SysiIcalendar) IcalMalformedException(org.bedework.icalendar.IcalMalformedException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

Icalendar (org.bedework.icalendar.Icalendar)6 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)5 StringReader (java.io.StringReader)4 SysiIcalendar (org.bedework.caldav.server.SysiIcalendar)4 EventInfo (org.bedework.calfacade.svc.EventInfo)4 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)3 WebdavForbidden (org.bedework.webdav.servlet.shared.WebdavForbidden)3 BwCalendar (org.bedework.calfacade.BwCalendar)2 BwEvent (org.bedework.calfacade.BwEvent)2 IcalMalformedException (org.bedework.icalendar.IcalMalformedException)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 QName (javax.xml.namespace.QName)1 TimeZone (net.fortuna.ical4j.model.TimeZone)1 CoreEventInfo (org.bedework.calcorei.CoreEventInfo)1 SysIntfReader (org.bedework.caldav.server.SysIntfReader)1 BwAlarm (org.bedework.calfacade.BwAlarm)1 ResponseElement (org.bedework.calsvc.scheduling.hosts.Response.ResponseElement)1 IcalTranslator (org.bedework.icalendar.IcalTranslator)1 WebdavBadRequest (org.bedework.webdav.servlet.shared.WebdavBadRequest)1