Search in sources :

Example 1 with NameSpace

use of org.bedework.util.xml.XmlEmit.NameSpace in project bw-calendar-engine by Bedework.

the class AbstractDirImpl method matching.

private List<MatchResult> matching(final BasicHttpClient cl, final String url, final String addrDataCtype, final List<WebdavProperty> props) throws CalFacadeException {
    try {
        final XmlEmit xml = new XmlEmit();
        xml.addNs(new NameSpace(WebdavTags.namespace, "DAV"), true);
        xml.addNs(new NameSpace(CarddavTags.namespace, "C"), false);
        final StringWriter sw = new StringWriter();
        xml.startEmit(sw);
        xml.openTag(CarddavTags.addressbookQuery);
        xml.openTag(WebdavTags.prop);
        xml.emptyTag(WebdavTags.getetag);
        if (addrDataCtype == null) {
            xml.emptyTag(CarddavTags.addressData);
        } else {
            xml.emptyTag(CarddavTags.addressData, "content-type", addrDataCtype);
        }
        xml.closeTag(WebdavTags.prop);
        xml.openTag(CarddavTags.filter, "test", "anyof");
        for (final WebdavProperty wd : props) {
            if (wd.getTag().equals(CaldavTags.calendarUserType)) {
                // Should match onto KIND
                continue;
            }
            if (wd.getTag().equals(BedeworkServerTags.emailProp)) {
                // Match Email
                xml.openTag(CarddavTags.propFilter, "name", "EMAIL");
                xml.startTagSameLine(CarddavTags.textMatch);
                xml.attribute("collation", "i;unicode-casemap");
                xml.attribute("match-type", "contains");
                xml.endOpeningTag();
                xml.value(wd.getPval());
                xml.closeTagSameLine(CarddavTags.textMatch);
                xml.closeTag(CarddavTags.propFilter);
                continue;
            }
            if (wd.getTag().equals(WebdavTags.displayname)) {
                // Match FN
                xml.openTag(CarddavTags.propFilter, "name", "FN");
                xml.startTagSameLine(CarddavTags.textMatch);
                xml.attribute("collation", "i;unicode-casemap");
                xml.attribute("match-type", "contains");
                xml.endOpeningTag();
                xml.value(wd.getPval());
                xml.closeTagSameLine(CarddavTags.textMatch);
                xml.closeTag(CarddavTags.propFilter);
            // continue;
            }
        }
        xml.closeTag(CarddavTags.filter);
        xml.closeTag(CarddavTags.addressbookQuery);
        final DavUtil du = new DavUtil();
        final byte[] content = sw.toString().getBytes();
        final int res = du.sendRequest(cl, "REPORT", url, new BasicHeader("depth", "infinity"), // contentType,
        "text/xml", // contentLen,
        content.length, content);
        // not defined for some reason
        final int SC_MULTI_STATUS = 207;
        if (res != SC_MULTI_STATUS) {
            if (debug) {
                debug("Got response " + res + " for path " + url);
            }
            return null;
        }
        final List<MatchResult> mrs = new ArrayList<>();
        final MultiStatusResponse msr = du.getMultiStatusResponse(cl.getResponseBodyAsStream());
        for (final MultiStatusResponseElement msre : msr.responses) {
            MatchResult mr = new MatchResult();
            mrs.add(mr);
            mr.href = msre.href;
            for (final PropstatElement pe : msre.propstats) {
                if (pe.status != HttpServletResponse.SC_OK) {
                    continue;
                }
                for (final Element e : pe.props) {
                    if (XmlUtil.nodeMatches(e, WebdavTags.getetag)) {
                        mr.etag = XmlUtil.getElementContent(e);
                        continue;
                    }
                    if (XmlUtil.nodeMatches(e, CarddavTags.addressData)) {
                        mr.card = XmlUtil.getElementContent(e);
                    }
                }
            }
        }
        return mrs;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    } finally {
        try {
            cl.release();
        } catch (final Throwable ignored) {
        }
    }
}
Also used : MultiStatusResponseElement(org.bedework.util.dav.DavUtil.MultiStatusResponseElement) MultiStatusResponseElement(org.bedework.util.dav.DavUtil.MultiStatusResponseElement) PropstatElement(org.bedework.util.dav.DavUtil.PropstatElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MultiStatusResponse(org.bedework.util.dav.DavUtil.MultiStatusResponse) NameSpace(org.bedework.util.xml.XmlEmit.NameSpace) DavUtil(org.bedework.util.dav.DavUtil) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) StringWriter(java.io.StringWriter) WebdavProperty(org.bedework.webdav.servlet.shared.WebdavProperty) PropstatElement(org.bedework.util.dav.DavUtil.PropstatElement) XmlEmit(org.bedework.util.xml.XmlEmit) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with NameSpace

use of org.bedework.util.xml.XmlEmit.NameSpace in project bw-calendar-engine by Bedework.

the class IcalTranslator method writeXmlCalendar.

/**
 * Write a collection of calendar data as xml
 *
 * @param vals
 * @param methodType    int value fromIcalendar
 * @param xml for output
 * @throws CalFacadeException
 */
public void writeXmlCalendar(final Collection vals, final int methodType, final XmlEmit xml) throws CalFacadeException {
    try {
        xml.addNs(new NameSpace(XcalTags.namespace, "X"), false);
        xml.openTag(XcalTags.icalendar);
        xml.openTag(XcalTags.vcalendar);
        xml.openTag(XcalTags.properties);
        xmlProp(xml, Property.PRODID, XcalTags.textVal, prodId);
        xmlProp(xml, Property.VERSION, XcalTags.textVal, Version.VERSION_2_0.getValue());
        xml.closeTag(XcalTags.properties);
        boolean componentsOpen = false;
        if (!cb.getTimezonesByReference()) {
            // To collect timezones
            Calendar cal = newIcal(methodType);
            addIcalTimezones(cal, vals);
            // Emit timezones
            for (Object o : cal.getComponents()) {
                if (!(o instanceof VTimeZone)) {
                    continue;
                }
                if (!componentsOpen) {
                    xml.openTag(XcalTags.components);
                    componentsOpen = true;
                }
                xmlComponent(xml, (Component) o);
            }
        }
        String currentPrincipal = null;
        final BwPrincipal principal = cb.getPrincipal();
        if (principal != null) {
            currentPrincipal = principal.getPrincipalRef();
        }
        for (final Object o : vals) {
            if (o instanceof EventInfo) {
                final EventInfo ei = (EventInfo) o;
                BwEvent ev = ei.getEvent();
                final EventTimeZonesRegistry tzreg = new EventTimeZonesRegistry(this, ev);
                final Component comp;
                if (ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy) {
                    comp = VFreeUtil.toVFreeBusy(ev);
                } else {
                    comp = VEventUtil.toIcalComponent(ei, false, tzreg, currentPrincipal);
                }
                if (!componentsOpen) {
                    xml.openTag(XcalTags.components);
                    componentsOpen = true;
                }
                xmlComponent(xml, comp);
                if (ei.getNumOverrides() > 0) {
                    for (final EventInfo oei : ei.getOverrides()) {
                        ev = oei.getEvent();
                        xmlComponent(xml, VEventUtil.toIcalComponent(oei, true, tzreg, currentPrincipal));
                    }
                }
            }
        }
        if (componentsOpen) {
            xml.closeTag(XcalTags.components);
        }
        xml.closeTag(XcalTags.vcalendar);
        xml.closeTag(XcalTags.icalendar);
    } catch (CalFacadeException cfe) {
        throw cfe;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) BwCalendar(org.bedework.calfacade.BwCalendar) Calendar(net.fortuna.ical4j.model.Calendar) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) NameSpace(org.bedework.util.xml.XmlEmit.NameSpace) BwEvent(org.bedework.calfacade.BwEvent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwPrincipal(org.bedework.calfacade.BwPrincipal) CalendarComponent(net.fortuna.ical4j.model.component.CalendarComponent) StartEndComponent(org.bedework.calfacade.base.StartEndComponent) Component(net.fortuna.ical4j.model.Component)

Aggregations

CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 NameSpace (org.bedework.util.xml.XmlEmit.NameSpace)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Calendar (net.fortuna.ical4j.model.Calendar)1 Component (net.fortuna.ical4j.model.Component)1 CalendarComponent (net.fortuna.ical4j.model.component.CalendarComponent)1 VTimeZone (net.fortuna.ical4j.model.component.VTimeZone)1 BasicHeader (org.apache.http.message.BasicHeader)1 BwCalendar (org.bedework.calfacade.BwCalendar)1 BwEvent (org.bedework.calfacade.BwEvent)1 BwPrincipal (org.bedework.calfacade.BwPrincipal)1 StartEndComponent (org.bedework.calfacade.base.StartEndComponent)1 EventInfo (org.bedework.calfacade.svc.EventInfo)1 DavUtil (org.bedework.util.dav.DavUtil)1 MultiStatusResponse (org.bedework.util.dav.DavUtil.MultiStatusResponse)1 MultiStatusResponseElement (org.bedework.util.dav.DavUtil.MultiStatusResponseElement)1 PropstatElement (org.bedework.util.dav.DavUtil.PropstatElement)1 XmlEmit (org.bedework.util.xml.XmlEmit)1 WebdavProperty (org.bedework.webdav.servlet.shared.WebdavProperty)1