use of org.bedework.util.dav.DavUtil.PropstatElement 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) {
}
}
}
use of org.bedework.util.dav.DavUtil.PropstatElement in project bw-calendar-engine by Bedework.
the class AbstractDirImpl method getCard.
/**
* Get the vcard for the given principal
*
* @param p - who we want card for
* @return card or null
* @throws CalFacadeException
*/
private String getCard(final BasicHttpClient cl, final String context, final AccessPrincipal p) throws CalFacadeException {
try {
StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + "<D:propfind xmlns:D=\"DAV:\"\n" + " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n" + " <D:prop>\n" + " <C:principal-address/>\n" + " </D:prop>\n" + "</D:propfind>\n");
byte[] content = sb.toString().getBytes();
int res = cl.sendRequest("PROPFIND", context + p.getPrincipalRef(), // hdrs
Collections.singletonList(DavUtil.depth0), "text/xml", content.length, content);
// not defined for some reason
int SC_MULTI_STATUS = 207;
if (res != SC_MULTI_STATUS) {
return null;
}
/* Extract the principal address from something like
* <?xml version="1.0" encoding="UTF-8" ?>
* <multistatus xmlns="DAV:" xmlns:ns1="urn:ietf:params:xml:ns:carddav">
* <response>
* <href>/ucarddav/principals/users/douglm/</href>
* <propstat>
* <prop>
* <ns1:principal-address>
* <href>/ucarddav/public/people/douglm.vcf/</href>
* </ns1:principal-address>
* </prop>
* <status>HTTP/1.1 200 ok</status>
* </propstat>
* </response>
* </multistatus>
*/
DavUtil du = new DavUtil();
MultiStatusResponse msr = du.getMultiStatusResponse(cl.getResponseBodyAsStream());
if (msr.responses.size() != 1) {
throw new CalFacadeException("Bad response. Expected exactly 1 response element");
}
MultiStatusResponseElement msre = msr.responses.get(0);
if (msre.propstats.size() != 1) {
if (debug) {
debug("Found " + msre.propstats.size() + " propstat elements");
}
return null;
}
PropstatElement pse = msre.propstats.get(0);
if (pse.status != HttpServletResponse.SC_OK) {
if (debug) {
debug("propstat status was " + pse.status);
}
return null;
}
// We expect one principal-address property
if (pse.props.size() != 1) {
if (debug) {
debug("Found " + pse.props.size() + " prop elements");
}
return null;
}
final Element pr = pse.props.iterator().next();
if (!XmlUtil.nodeMatches(pr, CarddavTags.principalAddress)) {
if (debug) {
debug("Expected principal-address - found " + pr);
}
return null;
}
/* Expect a single href element */
final Element hrefEl = DavUtil.getOnlyChild(pr);
if (!XmlUtil.nodeMatches(hrefEl, WebdavTags.href)) {
if (debug) {
debug("Expected href element for principal-address - found " + hrefEl);
}
return null;
}
final String href = URLDecoder.decode(XmlUtil.getElementContent((Element) hrefEl), // href should be escaped
HTTP.UTF_8);
/*
final DavUtil du = new DavUtil();
Collection<QName> props = new ArrayList<>();
props.add(CarddavTags.principalAddress);
final DavChild dc = du.getProps(cl, context + p.getPrincipalRef(), props);
if ((dc == null) ||
(dc.status != HttpServletResponse.SC_OK)){
return null;
}
/* New request for card * /
final InputStream is = cl.get(dc.uri);
*/
/* New request for card */
final InputStream is = cl.get(href);
if (is == null) {
return null;
}
final LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
final StringBuilder card = new StringBuilder();
for (; ; ) {
final String ln = lnr.readLine();
if (ln == null) {
break;
}
card.append(ln);
card.append("\n");
}
if (card.length() == 0) {
return null;
}
return card.toString();
} catch (final Throwable t) {
throw new CalFacadeException(t);
} finally {
try {
cl.release();
} catch (final Throwable ignored) {
}
}
}
Aggregations