Search in sources :

Example 1 with WebdavProperty

use of org.bedework.webdav.servlet.shared.WebdavProperty in project bw-calendar-engine by Bedework.

the class BwSysIntfImpl method getPrincipals.

@Override
public Collection<CalPrincipalInfo> getPrincipals(String resourceUri, final PrincipalPropertySearch pps) throws WebdavException {
    List<CalPrincipalInfo> principals = null;
    if (pps.applyToPrincipalCollectionSet) {
        /* I believe it's valid (if unhelpful) to return nothing
       */
        return new ArrayList<>();
    }
    if (!resourceUri.endsWith("/")) {
        resourceUri += "/";
    }
    try {
        String proot = BwPrincipal.principalRoot;
        if (!proot.endsWith("/")) {
            proot += "/";
        }
        if (!resourceUri.equals(proot)) {
            return new ArrayList<>();
        }
    } catch (final Throwable t) {
        throw new WebdavException(t);
    }
    /* If we don't support any of the properties in the searches we don't match.
     *
     * Currently we only support calendarUserAddressSet or calendarHomeSet.
     *
     * For calendarUserAddressSet the value to match must be a valid CUA
     *
     * For calendarHomeSet it must be a valid home uri
     */
    final List<WebdavProperty> props = new ArrayList<>();
    String cutype = null;
    for (final WebdavProperty prop : pps.props) {
        if (debug) {
            debug("Try to match " + prop);
        }
        final String pval = prop.getPval();
        if (CaldavTags.calendarUserAddressSet.equals(prop.getTag())) {
            principals = and(principals, getCalPrincipalInfo(caladdrToPrincipal(pval)));
        } else if (CaldavTags.calendarHomeSet.equals(prop.getTag())) {
            final String path = getUrlHandler().unprefix(pval);
            final CalDAVCollection col = getCollection(path);
            if (col != null) {
                principals = and(principals, getCalPrincipalInfo(col.getOwner()));
            }
        } else if (CaldavTags.calendarUserType.equals(prop.getTag())) {
            cutype = pval;
        } else if (WebdavTags.displayname.equals(prop.getTag())) {
            // Store for directory search
            props.add(prop);
        }
    }
    try {
        if (props.size() != 0) {
            // Directory search
            final Holder<Boolean> truncated = new Holder<>();
            if (principals == null) {
                principals = new ArrayList<>();
            }
            final List<BwPrincipalInfo> pis = getSvci().getDirectories().find(props, pps.pr.props, cutype, truncated);
            if (pis != null) {
                for (final BwPrincipalInfo pi : pis) {
                    principals.add(getCalPrincipalInfo(pi));
                }
            }
        }
    } catch (final Throwable t) {
        throw new WebdavException(t);
    }
    if (principals == null) {
        return new ArrayList<>();
    }
    return principals;
}
Also used : Holder(javax.xml.ws.Holder) ArrayList(java.util.ArrayList) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) CalDAVCollection(org.bedework.caldav.server.CalDAVCollection) WebdavProperty(org.bedework.webdav.servlet.shared.WebdavProperty) CalPrincipalInfo(org.bedework.caldav.server.sysinterface.CalPrincipalInfo) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo)

Example 2 with WebdavProperty

use of org.bedework.webdav.servlet.shared.WebdavProperty 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 3 with WebdavProperty

use of org.bedework.webdav.servlet.shared.WebdavProperty in project bw-calendar-engine by Bedework.

the class AbstractDirImpl method find.

private List<BwPrincipalInfo> find(final BasicHttpClient cdc, final CardDavInfo cdi, final String cua, final String cutype) throws CalFacadeException {
    /* Typically a group entry in a directory doesn't have a mail -
       The cua is a uri which often looks like a mailto.
     */
    final List<WebdavProperty> props = new ArrayList<>();
    final CalAddr ca = new CalAddr(cua);
    if ((cutype != null) && cutype.equalsIgnoreCase(CuType.GROUP.getValue())) {
        final WebdavProperty fnProp = new WebdavProperty(WebdavTags.displayname, ca.getId());
        props.add(fnProp);
    }
    final WebdavProperty emailProp = new WebdavProperty(BedeworkServerTags.emailProp, ca.getNoScheme());
    props.add(emailProp);
    final List<BwPrincipalInfo> pis = new ArrayList<>();
    final List<MatchResult> mrs = matching(cdc, cdi.getContextPath() + getCutypePath(cutype, cdi), null, props);
    if (mrs == null) {
        return pis;
    }
    for (final MatchResult mr : mrs) {
        final BwPrincipalInfo pi = new BwPrincipalInfo();
        pi.setPropertiesFromVCard(mr.card, null);
        pis.add(pi);
    }
    return pis;
}
Also used : WebdavProperty(org.bedework.webdav.servlet.shared.WebdavProperty) ArrayList(java.util.ArrayList) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo)

Example 4 with WebdavProperty

use of org.bedework.webdav.servlet.shared.WebdavProperty in project bw-calendar-engine by Bedework.

the class AbstractDirImpl method find.

@Override
public List<BwPrincipalInfo> find(final List<WebdavProperty> props, final List<WebdavProperty> returnProps, final String cutype, final Holder<Boolean> truncated) throws CalFacadeException {
    final CardDavInfo cdi = getCardDavInfo(false);
    if ((cdi == null) || (cdi.getHost() == null)) {
        return null;
    }
    BasicHttpClient cdc = null;
    final String path = getCutypePath(cutype, cdi);
    String addrCtype = null;
    /* See if we want address data in any particular form */
    for (final WebdavProperty wd : returnProps) {
        if (!wd.getTag().equals(CarddavTags.addressData)) {
            continue;
        }
        addrCtype = wd.getAttr("content-type");
        break;
    }
    try {
        cdc = new BasicHttpClient(cdi.getHost(), cdi.getPort(), null, 15 * 1000);
        final List<MatchResult> mrs = matching(cdc, cdi.getContextPath() + path, addrCtype, props);
        final List<BwPrincipalInfo> pis = new ArrayList<>();
        if (mrs == null) {
            return pis;
        }
        for (final MatchResult mr : mrs) {
            final BwPrincipalInfo pi = new BwPrincipalInfo();
            pi.setPropertiesFromVCard(mr.card, addrCtype);
            pis.add(pi);
        }
        return pis;
    } catch (final Throwable t) {
        if (getLogger().isDebugEnabled()) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        if (cdc != null) {
            cdc.close();
        }
    }
}
Also used : WebdavProperty(org.bedework.webdav.servlet.shared.WebdavProperty) ArrayList(java.util.ArrayList) BasicHttpClient(org.bedework.util.http.BasicHttpClient) CardDavInfo(org.bedework.calfacade.configs.CardDavInfo) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

ArrayList (java.util.ArrayList)4 WebdavProperty (org.bedework.webdav.servlet.shared.WebdavProperty)4 BwPrincipalInfo (org.bedework.calfacade.BwPrincipalInfo)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 StringWriter (java.io.StringWriter)1 Holder (javax.xml.ws.Holder)1 BasicHeader (org.apache.http.message.BasicHeader)1 CalDAVCollection (org.bedework.caldav.server.CalDAVCollection)1 CalPrincipalInfo (org.bedework.caldav.server.sysinterface.CalPrincipalInfo)1 CardDavInfo (org.bedework.calfacade.configs.CardDavInfo)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 BasicHttpClient (org.bedework.util.http.BasicHttpClient)1 XmlEmit (org.bedework.util.xml.XmlEmit)1 NameSpace (org.bedework.util.xml.XmlEmit.NameSpace)1 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)1 Element (org.w3c.dom.Element)1