Add DavCalendar and related properties

parent e6c3ee6d
......@@ -29,12 +29,11 @@ public class DavAddressBook extends DavResource {
MIME_VCARD3_UTF8 = MediaType.parse("text/vcard;charset=utf-8"),
MIME_VCARD4 = MediaType.parse("text/vcard;version=4.0");
public DavAddressBook(OkHttpClient httpClient, HttpUrl location) {
super(httpClient, location);
}
public void queryMemberETags() throws IOException, HttpException, DavException {
public void addressbookQuery() throws IOException, HttpException, DavException {
/* <!ELEMENT addressbook-query ((DAV:allprop |
DAV:propname |
DAV:prop)?, filter, limit?)>
......@@ -46,12 +45,12 @@ public class DavAddressBook extends DavResource {
serializer.startDocument("UTF-8", null);
serializer.setPrefix("", XmlUtils.NS_WEBDAV);
serializer.startTag(XmlUtils.NS_CARDDAV, "addressbook-query");
serializer.startTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_CARDDAV, "filter");
serializer.endTag(XmlUtils.NS_CARDDAV, "filter");
serializer.startTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_CARDDAV, "filter");
serializer.endTag(XmlUtils.NS_CARDDAV, "filter");
serializer.endTag(XmlUtils.NS_CARDDAV, "addressbook-query");
serializer.endDocument();
......@@ -84,7 +83,7 @@ public class DavAddressBook extends DavResource {
serializer.setPrefix("", XmlUtils.NS_WEBDAV);
serializer.startTag(XmlUtils.NS_CARDDAV, "addressbook-multiget");
serializer.startTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_WEBDAV, "getcontenttype");
serializer.startTag(XmlUtils.NS_WEBDAV, "getcontenttype"); // to determine the character set
serializer.endTag(XmlUtils.NS_WEBDAV, "getcontenttype");
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
......
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.StringWriter;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
public class DavCalendar extends DavResource {
public static final MediaType
MIME_ICALENDAR = MediaType.parse("text/calendar;charset=utf-8");
public DavCalendar(OkHttpClient httpClient, HttpUrl location) {
super(httpClient, location);
}
public void calendarQuery(String component) throws IOException, HttpException, DavException {
/* <!ELEMENT calendar-query ((DAV:allprop |
DAV:propname |
DAV:prop)?, filter, timezone?)>
<!ELEMENT filter (comp-filter)>
<!ELEMENT comp-filter (is-not-defined | (time-range?,
prop-filter*, comp-filter*))>
<!ATTLIST comp-filter name CDATA #REQUIRED>
name value: a calendar object or calendar component
type (e.g., VEVENT)
*/
XmlSerializer serializer = XmlUtils.newSerializer();
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", null);
serializer.setPrefix("", XmlUtils.NS_WEBDAV);
serializer.startTag(XmlUtils.NS_CALDAV, "calendar-query");
serializer.startTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_CALDAV, "filter");
serializer.startTag(XmlUtils.NS_CALDAV, "comp-filter");
serializer.attribute(null, "name", "VCALENDAR");
serializer.startTag(XmlUtils.NS_CALDAV, "comp-filter");
serializer.attribute(null, "name", component);
serializer.endTag(XmlUtils.NS_CALDAV, "comp-filter");
serializer.endTag(XmlUtils.NS_CALDAV, "comp-filter");
serializer.endTag(XmlUtils.NS_CALDAV, "filter");
serializer.endTag(XmlUtils.NS_CALDAV, "calendar-query");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
.header("Depth", "1")
.build()).execute();
checkStatus(response);
assertMultiStatus(response);
members.clear();
processMultiStatus(response.body().charStream());
}
public void multiget(HttpUrl[] urls) throws IOException, HttpException, DavException {
/* <!ELEMENT calendar-multiget ((DAV:allprop |
DAV:propname |
DAV:prop)?, DAV:href+)>
*/
XmlSerializer serializer = XmlUtils.newSerializer();
StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", null);
serializer.setPrefix("", XmlUtils.NS_WEBDAV);
serializer.startTag(XmlUtils.NS_CALDAV, "calendar-multiget");
serializer.startTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_WEBDAV, "getcontenttype"); // to determine the character set
serializer.endTag(XmlUtils.NS_WEBDAV, "getcontenttype");
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.startTag(XmlUtils.NS_CALDAV, "calendar-data");
serializer.endTag(XmlUtils.NS_CALDAV, "calendar-data");
serializer.endTag(XmlUtils.NS_WEBDAV, "prop");
for (HttpUrl url : urls) {
serializer.startTag(XmlUtils.NS_WEBDAV, "href");
serializer.text(url.encodedPath());
serializer.endTag(XmlUtils.NS_WEBDAV, "href");
}
serializer.startTag(XmlUtils.NS_CALDAV, "calendar-multiget");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
.build()).execute();
checkStatus(response);
assertMultiStatus(response);
members.clear();
processMultiStatus(response.body().charStream());
}
}
......@@ -9,6 +9,7 @@ import at.bitfire.dav4android.property.AddressData;
import at.bitfire.dav4android.property.AddressbookDescription;
import at.bitfire.dav4android.property.AddressbookHomeSet;
import at.bitfire.dav4android.property.CalendarColor;
import at.bitfire.dav4android.property.CalendarData;
import at.bitfire.dav4android.property.CalendarDescription;
import at.bitfire.dav4android.property.CalendarHomeSet;
import at.bitfire.dav4android.property.CalendarTimezone;
......@@ -48,6 +49,7 @@ public class PropertyRegistry {
DEFAULT.register(new CalendarDescription.Factory());
DEFAULT.register(new CalendarTimezone.Factory());
DEFAULT.register(new SupportedCalendarComponentSet.Factory());
DEFAULT.register(new CalendarData.Factory());
}
......
......@@ -79,9 +79,12 @@ public class HttpException extends Exception implements Serializable {
this.response = formatted.toString();
}
private static void appendByte(StringBuilder formatted, byte b) {
if (b >= 0x20 && b <= 0x7E) // printable ASCII
if (b == '\r')
formatted.append("[CR]");
else if (b == '\n')
formatted.append("[LF]\n");
else if (b >= 0x20 && b <= 0x7E) // printable ASCII
formatted.append((char)b);
else
formatted.append("[" + Integer.toHexString(b) + "]");
......
......@@ -6,6 +6,8 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import at.bitfire.dav4android.Constants;
import at.bitfire.dav4android.Property;
......@@ -17,7 +19,7 @@ import lombok.ToString;
public class CalendarColor implements Property {
public static final Name NAME = new Name(XmlUtils.NS_APPLE_ICAL, "calendar-color");
public String color;
public Integer color;
public static class Factory implements PropertyFactory {
......@@ -36,7 +38,7 @@ public class CalendarColor implements Property {
int eventType = parser.getEventType();
while (!(eventType == XmlPullParser.END_TAG && parser.getDepth() == depth)) {
if (eventType == XmlPullParser.TEXT && parser.getDepth() == depth)
calendarColor.color = parser.getText();
calendarColor.color = parseARGBColor(parser.getText());
eventType = parser.next();
}
} catch(XmlPullParserException|IOException e) {
......@@ -46,5 +48,21 @@ public class CalendarColor implements Property {
return calendarColor;
}
public static Integer parseARGBColor(String davColor) {
Integer color = null;
if (davColor != null) {
Pattern p = Pattern.compile("#?(\\p{XDigit}{6})(\\p{XDigit}{2})?");
Matcher m = p.matcher(davColor);
if (m.find()) {
int color_rgb = Integer.parseInt(m.group(1), 16);
int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
color = (color_alpha << 24) | color_rgb;
} else
Constants.log.warn("Couldn't parse color " + davColor + ", using DAVdroid green");
}
return color;
}
}
}
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android.property;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import at.bitfire.dav4android.Constants;
import at.bitfire.dav4android.Property;
import at.bitfire.dav4android.PropertyFactory;
import at.bitfire.dav4android.XmlUtils;
import lombok.ToString;
@ToString
public class CalendarData implements Property {
public static final Name NAME = new Name(XmlUtils.NS_CALDAV, "calendar-data");
public String iCalendar;
private CalendarData() {}
public static class Factory implements PropertyFactory {
@Override
public Name getName() {
return NAME;
}
@Override
public CalendarData create(XmlPullParser parser) {
CalendarData calendarData = new CalendarData();
// <!ELEMENT calendar-data (#PCDATA)>
try {
int eventType = parser.getEventType();
calendarData.iCalendar = parser.nextText();
} catch(XmlPullParserException|IOException e) {
Log.e(Constants.LOG_TAG, "Couldn't parse <calendar-data>", e);
return null;
}
return calendarData;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment