Search in sources :

Example 1 with ResponseElement

use of org.bedework.calsvc.scheduling.hosts.Response.ResponseElement in project bw-calendar-engine by Bedework.

the class IScheduleHandler method sendExternalRequest.

/* External iSchedule requests */
protected void sendExternalRequest(final ScheduleResult sr, final EventInfo ei, final Collection<UserInbox> inboxes) throws CalFacadeException {
    /* Each entry in inboxes should have the same hostinfo */
    HostInfo hi = null;
    final BwEvent ev = ei.getEvent();
    final boolean freeBusyRequest = ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy;
    Set<String> recipients = null;
    /*
    class Request {
      HostInfo hi;

      String url;

      String content;
    }
    */
    final HashMap<String, UserInbox> uimap = new HashMap<>();
    /* For realtime or caldav we make up a single meeting request or freebusy request.
     * For freebusy url we just execute one url at a time
     */
    for (final UserInbox ui : inboxes) {
        if (hi == null) {
            // First time
            hi = ui.host;
            if (hi.getSupportsBedework() || hi.getSupportsCaldav() || hi.getSupportsISchedule()) {
                recipients = new TreeSet<>();
            }
        }
        if (recipients == null) {
            // request per recipient - only freebusy
            if (debug) {
                trace("freebusy request to " + hi.getFbUrl() + " for " + ui.recipient);
            }
        } else {
            recipients.add(ui.recipient);
            uimap.put(ui.recipient, ui);
        }
    }
    if (recipients == null) {
        // No ischedule requests
        return;
    }
    if (debug) {
        final String meth;
        if (freeBusyRequest) {
            meth = "freebusy";
        } else {
            meth = "meeting";
        }
        trace(meth + " request to " + hi.getFbUrl() + " for " + recipients);
    }
    final EventInfo cei = copyEventInfo(ei, getPrincipal());
    cei.getEvent().setRecipients(recipients);
    final Response r;
    if (freeBusyRequest) {
        try {
            r = getCalDavClient().getFreeBusy(hi, cei);
        } catch (final CalFacadeException cfe) {
            error(cfe);
            return;
        }
        for (final ResponseElement re : r.getResponses()) {
            final UserInbox ui = uimap.get(re.getRecipient());
            if (ui == null) {
                continue;
            }
            if (re.getCalData() == null) {
                ui.setStatus(ScheduleStates.scheduleUnprocessed);
                continue;
            }
            ui.freeBusy = re.getCalData().getEvent();
            ui.setStatus(ScheduleStates.scheduleOk);
            sr.externalRcs.remove(ui.recipient);
        }
        return;
    }
    try {
        r = getCalDavClient().scheduleMeeting(hi, cei);
    } catch (final CalFacadeException cfe) {
        error(cfe);
        return;
    }
    for (final ResponseElement re : r.getResponses()) {
        final UserInbox ui = uimap.get(re.getRecipient());
        if (ui == null) {
            continue;
        }
        ui.setStatus(ScheduleStates.scheduleOk);
        sr.externalRcs.remove(ui.recipient);
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) HashMap(java.util.HashMap) BwEvent(org.bedework.calfacade.BwEvent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) Response(org.bedework.calsvc.scheduling.hosts.Response) HostInfo(org.bedework.calsvc.scheduling.hosts.HostInfo) ResponseElement(org.bedework.calsvc.scheduling.hosts.Response.ResponseElement)

Example 2 with ResponseElement

use of org.bedework.calsvc.scheduling.hosts.Response.ResponseElement 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)

Aggregations

CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 ResponseElement (org.bedework.calsvc.scheduling.hosts.Response.ResponseElement)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 BwEvent (org.bedework.calfacade.BwEvent)1 EventInfo (org.bedework.calfacade.svc.EventInfo)1 HostInfo (org.bedework.calsvc.scheduling.hosts.HostInfo)1 Response (org.bedework.calsvc.scheduling.hosts.Response)1 Icalendar (org.bedework.icalendar.Icalendar)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1