Search code examples
caldavsardinebiweekly

Using Sardines report method to query events from CalDAV server


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?


Solution

  • 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).