Using Sardines report method to query events from CalDAV server

房东的猫 提交于 2019-12-24 06:30:01

问题


I'm trying to fetch events from a CalDAV server using Sardine (and biweekly). Fetching an entire calendar is working for me:

Sardine sardine = SardineFactory.begin();
InputStream is = sardine.get(CALDAV_URL_STRING);
ICalendar iCalendar = Biweekly.parse(is).first();

Now I would like to fetch events for specific time range. Based on this "Building a CalDAV client" article, I assume you should use Sardines report method to do so, right?

If so, how should you use that method? It's not documented in the wiki, and the Javadoc is also not very clear.

Should I write my own SardineReport? I looks like I should end up with something like:

Sardine sardine = SardineFactory.begin();
SardineReport<List<VEvent>> report = new MyRangeReport(FROM_DATE, END_DATE);
List<VEvent> result = sardine.report(CALDAV_URL_STRING, 1, report);

Am I on the right track? Does anyone have any pointers to how to write your own Sardine report?


回答1:


You are correct, to run a time range query you need to issue an HTTP REPORT request containing a calendar-query. You can find a sample in RFC 4719 Section 7.8.1. I don't know Sardine, but yes, report sounds right ;-)

Simplified even further:

 REPORT /bernard/work/ HTTP/1.1
 Host: cal.example.com
 Depth: 1
 Content-Type: application/xml; charset="utf-8"
 Content-Length: xxxx

 <?xml version="1.0" encoding="utf-8" ?>
 <calendar-query xmlns:D="DAV:" xmlns="urn:ietf:params:xml:ns:caldav">
   <D:prop>
     <D:getetag/>
     <calendar-data />
   </D:prop>
   <filter>
     <comp-filter name="VCALENDAR">
       <comp-filter name="VEVENT">
         <time-range start="20060104T000000Z" end="20060105T000000Z"/>
       </comp-filter>
     </comp-filter>
  </filter>
</calendar-query>

Should I write my own SardineReport?

Looks like, scanning over the GitHub of Sardine it doesn't seem to include CalDAV queries. You can probably base yours on their SyncCollectionReport.java.

Note that the response to the calendar-query REPORT is just a regular WebDAV 207 multi-status response. Nothing extra required. (the Result object would be the same like in their SyncCollectionReport but w/o the extra syncToken).




回答2:


I've implemented a report class which works, however, the result returned by Radicale seem to be incorrect. It looks like Radicale does not support the time-range filter.

For what it's worth, here are the relevant bits of the report:

public class VEventsTimeRangeSardineReport extends SardineReport<List<VEvent>>
{

    ...

    @Override
    public String toXml() throws IOException
    {
        // Hardcode for now
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                     "<c:calendar-query xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n" +
                     "  <d:prop>\n" +
                     "    <c:calendar-data/>\n" +
                     "    <d:getetag/>\n" +
                     "  </d:prop>\n" +
                     "  <c:filter>\n" +
                     "    <c:comp-filter name=\"VCALENDAR\">\n" +
                     "      <c:comp-filter name=\"VEVENT\">\n" +
                     "        <c:time-range start=\"%s\" end=\"%s\"/>\n" +
                     "      </c:comp-filter>\n" +
                     "    </c:comp-filter>\n" +
                     "  </c:filter>\n" +
                     "</c:calendar-query>";
        return String.format(xml, SDF.format(fromDate), SDF.format(toDate));
    }


    @Override
    public List<VEvent> fromMultistatus(Multistatus multistatus)
    {
        List<VEvent> events = new ArrayList<>(multistatus.getResponse().size());
        for (Response response : multistatus.getResponse()) {
            // Here response.getPropstat().get(0).getProp().getAny().get(0).getFirstChild().getTextContent() is parsed
            events.addAll(ICalendarSardineReport.getICalendar(response).getEvents());
        }
        return events;
    }


    @Override
    public Object toJaxb()
    {
        return null;
    }

}


来源:https://stackoverflow.com/questions/38057038/using-sardines-report-method-to-query-events-from-caldav-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!