Search in sources :

Example 1 with XmlEmit

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

the class DumpEntity method dump.

/**
 * Dump the entire entity into the given file.
 *
 * @param f - the file
 * @throws CalFacadeException
 */
@NoWrap
public void dump(final File f) throws CalFacadeException {
    Dump dCl = getClass().getAnnotation(Dump.class);
    if (dCl.format() == DumpFormat.xml) {
        Writer wtr = null;
        try {
            XmlEmit xml = new XmlEmit();
            wtr = new FileWriter(f);
            xml.startEmit(wtr);
            dump(xml, DumpType.def, false);
            xml.flush();
            return;
        } catch (CalFacadeException cfe) {
            throw cfe;
        } catch (Throwable t) {
            throw new CalFacadeException(t);
        } finally {
            if (wtr != null) {
                try {
                    wtr.close();
                } catch (Throwable t) {
                    throw new CalFacadeException(t);
                }
            }
        }
    }
    if (dCl.format() == DumpFormat.vCard) {
        Writer wtr = null;
        try {
            VCard vc = new VCard();
            dump(vc, DumpType.def);
            String vcStr = vc.toString();
            wtr = new FileWriter(f);
            wtr.append(vcStr);
            return;
        } catch (CalFacadeException cfe) {
            throw cfe;
        } catch (Throwable t) {
            throw new CalFacadeException(t);
        } finally {
            if (wtr != null) {
                try {
                    wtr.close();
                } catch (Throwable t) {
                    throw new CalFacadeException(t);
                }
            }
        }
    }
    throw new CalFacadeException("Unsupported dump format " + dCl.format());
}
Also used : NoDump(org.bedework.calfacade.annotations.NoDump) Dump(org.bedework.calfacade.annotations.Dump) FileWriter(java.io.FileWriter) XmlEmit(org.bedework.util.xml.XmlEmit) VCard(net.fortuna.ical4j.vcard.VCard) FileWriter(java.io.FileWriter) Writer(java.io.Writer) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) NoWrap(org.bedework.calfacade.annotations.NoWrap)

Example 2 with XmlEmit

use of org.bedework.util.xml.XmlEmit 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 XmlEmit

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

the class BwSysIntfImpl method writeCalendar.

@Override
public String writeCalendar(final Collection<CalDAVEvent> evs, final MethodEmitted method, final XmlEmit xml, final Writer wtr, final String contentType) throws WebdavException {
    try {
        Collection<EventInfo> bwevs = new ArrayList<EventInfo>();
        int meth = ScheduleMethods.methodTypeNone;
        if (method == MethodEmitted.publish) {
            meth = ScheduleMethods.methodTypePublish;
        }
        for (CalDAVEvent cde : evs) {
            BwCalDAVEvent bcde = (BwCalDAVEvent) cde;
            if (method == MethodEmitted.eventMethod) {
                meth = getEvent(bcde).getScheduleMethod();
            }
            bwevs.add(bcde.getEvinfo());
        }
        String ctype = contentType;
        if ((ctype == null) || (!ctype.equals("text/calendar") && !ctype.equals("application/calendar+json") && !ctype.equals(XcalTags.mimetype))) {
            ctype = getDefaultContentType();
        }
        if (ctype.equals("text/calendar")) {
            Calendar ical = trans.toIcal(bwevs, meth);
            if (xml == null) {
                IcalTranslator.writeCalendar(ical, wtr);
            } else {
                xml.cdataValue(toIcalString(ical, "text/calendar"));
            }
        } else if (ctype.equals("application/calendar+json")) {
            if (xml == null) {
                trans.writeJcal(bwevs, meth, wtr);
            } else {
                final StringWriter sw = new StringWriter();
                trans.writeJcal(bwevs, meth, sw);
                xml.cdataValue(sw.toString());
            }
        } else if (ctype.equals(XcalTags.mimetype)) {
            XmlEmit x;
            if (xml == null) {
                x = new XmlEmit();
                x.startEmit(wtr);
            } else {
                x = xml;
            }
            trans.writeXmlCalendar(bwevs, meth, x);
        }
        return ctype;
    } catch (WebdavException we) {
        throw we;
    } catch (Throwable t) {
        throw new WebdavException(t);
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) StringWriter(java.io.StringWriter) BwCalendar(org.bedework.calfacade.BwCalendar) Calendar(net.fortuna.ical4j.model.Calendar) ArrayList(java.util.ArrayList) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) CalDAVEvent(org.bedework.caldav.server.CalDAVEvent) XmlEmit(org.bedework.util.xml.XmlEmit)

Example 4 with XmlEmit

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

the class DumpGlobals method setOut.

/**
 * @param val output writer for old style dump
 * @param aliases output writer for aliases
 * @throws Throwable on error
 */
public void setOut(final Writer val, final Writer aliases) throws Throwable {
    out = val;
    xml = new XmlEmit();
    xml.startEmit(out);
    aliasesOut = aliases;
    aliasesXml = new XmlEmit();
    aliasesXml.startEmit(aliases);
}
Also used : XmlEmit(org.bedework.util.xml.XmlEmit)

Aggregations

XmlEmit (org.bedework.util.xml.XmlEmit)4 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 FileWriter (java.io.FileWriter)1 Writer (java.io.Writer)1 Calendar (net.fortuna.ical4j.model.Calendar)1 VCard (net.fortuna.ical4j.vcard.VCard)1 BasicHeader (org.apache.http.message.BasicHeader)1 CalDAVEvent (org.bedework.caldav.server.CalDAVEvent)1 BwCalendar (org.bedework.calfacade.BwCalendar)1 Dump (org.bedework.calfacade.annotations.Dump)1 NoDump (org.bedework.calfacade.annotations.NoDump)1 NoWrap (org.bedework.calfacade.annotations.NoWrap)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 NameSpace (org.bedework.util.xml.XmlEmit.NameSpace)1