1
0
mirror of https://github.com/etesync/android synced 2024-11-26 01:48:34 +00:00

Handle absolute URIs in resource detection (+ tests)

This commit is contained in:
R Hirner 2015-01-01 18:53:03 +01:00
parent ca0ad612a7
commit 3cba163c3b
6 changed files with 85 additions and 40 deletions

View File

@ -30,15 +30,23 @@ public class DavResourceFinderTest extends InstrumentationTestCase {
ServerInfo info = new ServerInfo(new URI(TestConstants.ROBOHYDRA_BASE), "test", "test", true); ServerInfo info = new ServerInfo(new URI(TestConstants.ROBOHYDRA_BASE), "test", "test", true);
finder.findResources(info); finder.findResources(info);
// CardDAV /*** CardDAV ***/
assertTrue(info.isCardDAV()); assertTrue(info.isCardDAV());
List<ResourceInfo> collections = info.getAddressBooks(); List<ResourceInfo> collections = info.getAddressBooks();
assertEquals(1, collections.size()); // two address books
assertEquals(2, collections.size());
// first one
ResourceInfo collection = collections.get(0);
assertEquals(TestConstants.roboHydra.resolve("/dav/addressbooks/test/default-v4.vcf/").toString(), collection.getURL());
assertEquals("Default Address Book", collection.getDescription());
assertEquals(VCardVersion.V4_0, collection.getVCardVersion());
// second one
collection = collections.get(1);
assertEquals("https://my.server/absolute:uri/my-address-book/", collection.getURL());
assertEquals("Absolute URI VCard3 Book", collection.getDescription());
assertEquals(VCardVersion.V3_0, collection.getVCardVersion());
assertEquals("Default Address Book", collections.get(0).getDescription()); /*** CalDAV ***/
assertEquals(VCardVersion.V4_0, collections.get(0).getVCardVersion());
// CalDAV
assertTrue(info.isCalDAV()); assertTrue(info.isCalDAV());
collections = info.getCalendars(); collections = info.getCalendars();
assertEquals(2, collections.size()); assertEquals(2, collections.size());

View File

@ -14,6 +14,7 @@ import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLPeerUnverifiedException;
import ezvcard.VCardVersion;
import lombok.Cleanup; import lombok.Cleanup;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -75,7 +76,7 @@ public class WebDavResourceTest extends InstrumentationTestCase {
public void testPropfindCurrentUserPrincipal() throws Exception { public void testPropfindCurrentUserPrincipal() throws Exception {
davCollection.propfind(HttpPropfind.Mode.CURRENT_USER_PRINCIPAL); davCollection.propfind(HttpPropfind.Mode.CURRENT_USER_PRINCIPAL);
assertEquals("/dav/principals/users/test", davCollection.getCurrentUserPrincipal()); assertEquals(new URI("/dav/principals/users/test"), davCollection.getCurrentUserPrincipal());
WebDavResource simpleFile = new WebDavResource(davAssets, "test.random"); WebDavResource simpleFile = new WebDavResource(davAssets, "test.random");
try { try {
@ -90,21 +91,36 @@ public class WebDavResourceTest extends InstrumentationTestCase {
public void testPropfindHomeSets() throws Exception { public void testPropfindHomeSets() throws Exception {
WebDavResource dav = new WebDavResource(davCollection, "principals/users/test"); WebDavResource dav = new WebDavResource(davCollection, "principals/users/test");
dav.propfind(HttpPropfind.Mode.HOME_SETS); dav.propfind(HttpPropfind.Mode.HOME_SETS);
assertEquals("/dav/addressbooks/test/", dav.getAddressbookHomeSet()); assertEquals(new URI("/dav/addressbooks/test/"), dav.getAddressbookHomeSet());
assertEquals("/dav/calendars/test/", dav.getCalendarHomeSet()); assertEquals(new URI("/dav/calendars/test/"), dav.getCalendarHomeSet());
} }
public void testPropfindAddressBooks() throws Exception { public void testPropfindAddressBooks() throws Exception {
WebDavResource dav = new WebDavResource(davCollection, "addressbooks/test"); WebDavResource dav = new WebDavResource(davCollection, "addressbooks/test");
dav.propfind(HttpPropfind.Mode.CARDDAV_COLLECTIONS); dav.propfind(HttpPropfind.Mode.CARDDAV_COLLECTIONS);
assertEquals(2, dav.getMembers().size());
for (WebDavResource member : dav.getMembers()) { // there should be two address books
if (member.getName().equals("default-v4.vcf")) assertEquals(3, dav.getMembers().size());
assertTrue(member.isAddressBook());
else // the first one is not an address book and not even a collection (referenced by relative URI)
assertFalse(member.isAddressBook()); WebDavResource ab = dav.getMembers().get(0);
assertFalse(member.isCalendar()); assertEquals(TestConstants.roboHydra.resolve("/dav/addressbooks/test/useless-member"), ab.getLocation());
} assertEquals("useless-member", ab.getName());
assertFalse(ab.isAddressBook());
// the second one is a VCard4-capable address book (referenced by relative URI)
ab = dav.getMembers().get(1);
assertEquals(TestConstants.roboHydra.resolve("/dav/addressbooks/test/default-v4.vcf/"), ab.getLocation());
assertEquals("default-v4.vcf", ab.getName());
assertTrue(ab.isAddressBook());
assertEquals(VCardVersion.V4_0, ab.getVCardVersion());
// the third one is a (non-VCard4-capable) address book (referenced by an absolute URI)
ab = dav.getMembers().get(2);
assertEquals(new URI("https://my.server/absolute:uri/my-address-book/"), ab.getLocation());
assertEquals("my-address-book", ab.getName());
assertTrue(ab.isAddressBook());
assertNull(ab.getVCardVersion());
} }
public void testPropfindCalendars() throws Exception { public void testPropfindCalendars() throws Exception {
@ -134,7 +150,7 @@ public class WebDavResourceTest extends InstrumentationTestCase {
for (String path : requestPaths) { for (String path : requestPaths) {
WebDavResource davSlash = new WebDavResource(davCollection, path); WebDavResource davSlash = new WebDavResource(davCollection, path);
davSlash.propfind(Mode.CARDDAV_COLLECTIONS); davSlash.propfind(Mode.CARDDAV_COLLECTIONS);
assertEquals(principalOK, davSlash.getCurrentUserPrincipal()); assertEquals(new URI(principalOK), davSlash.getCurrentUserPrincipal());
} }
} }

View File

@ -110,7 +110,7 @@ exports.getBodyParts = function(conf) {
</propstat>\ </propstat>\
</response>\ </response>\
<response>\ <response>\
<href>/dav/addressbooks/test/default-v4.vcf/</href>\ <href>/dav/addressbooks/test/default-v4.vcf</href>\
<propstat>\ <propstat>\
<prop xmlns:CARD="urn:ietf:params:xml:ns:carddav">\ <prop xmlns:CARD="urn:ietf:params:xml:ns:carddav">\
<resourcetype>\ <resourcetype>\
@ -126,6 +126,19 @@ exports.getBodyParts = function(conf) {
<status>HTTP/1.1 200 OK</status>\ <status>HTTP/1.1 200 OK</status>\
</propstat>\ </propstat>\
</response>\ </response>\
<response>\
<href>https://my.server/absolute:uri/my-address-book</href>\
<propstat>\
<prop xmlns:CARD="urn:ietf:params:xml:ns:carddav">\
<resourcetype>\
<collection/>\
<CARD:addressbook/>\
</resourcetype>\
<CARD:addressbook-description>Absolute URI VCard3 Book</CARD:addressbook-description>\
</prop>\
<status>HTTP/1.1 200 OK</status>\
</propstat>\
</response>\
</multistatus>\ </multistatus>\
'); ');
} }

View File

@ -63,15 +63,15 @@ public class DavResourceFinder implements Closeable {
serverInfo.setCardDAV(true); serverInfo.setCardDAV(true);
principal.propfind(Mode.HOME_SETS); principal.propfind(Mode.HOME_SETS);
String pathAddressBooks = principal.getAddressbookHomeSet(); URI uriAddressBookHomeSet = principal.getAddressbookHomeSet();
if (pathAddressBooks != null) { if (uriAddressBookHomeSet != null) {
Log.i(TAG, "Found address book home set: " + pathAddressBooks); Log.i(TAG, "Found address book home set: " + uriAddressBookHomeSet);
WebDavResource homeSetAddressBooks = new WebDavResource(principal, pathAddressBooks); WebDavResource homeSetAddressBooks = new WebDavResource(principal, uriAddressBookHomeSet);
if (checkHomesetCapabilities(homeSetAddressBooks, "addressbook")) { if (checkHomesetCapabilities(homeSetAddressBooks, "addressbook")) {
homeSetAddressBooks.propfind(Mode.CARDDAV_COLLECTIONS); homeSetAddressBooks.propfind(Mode.CARDDAV_COLLECTIONS);
List<ServerInfo.ResourceInfo> addressBooks = new LinkedList<ServerInfo.ResourceInfo>(); List<ServerInfo.ResourceInfo> addressBooks = new LinkedList<>();
if (homeSetAddressBooks.getMembers() != null) if (homeSetAddressBooks.getMembers() != null)
for (WebDavResource resource : homeSetAddressBooks.getMembers()) for (WebDavResource resource : homeSetAddressBooks.getMembers())
if (resource.isAddressBook()) { if (resource.isAddressBook()) {
@ -103,15 +103,15 @@ public class DavResourceFinder implements Closeable {
serverInfo.setCalDAV(true); serverInfo.setCalDAV(true);
principal.propfind(Mode.HOME_SETS); principal.propfind(Mode.HOME_SETS);
String pathCalendars = principal.getCalendarHomeSet(); URI uriCalendarHomeSet = principal.getCalendarHomeSet();
if (pathCalendars != null) { if (uriCalendarHomeSet != null) {
Log.i(TAG, "Found calendar home set: " + pathCalendars); Log.i(TAG, "Found calendar home set: " + uriCalendarHomeSet);
WebDavResource homeSetCalendars = new WebDavResource(principal, pathCalendars); WebDavResource homeSetCalendars = new WebDavResource(principal, uriCalendarHomeSet);
if (checkHomesetCapabilities(homeSetCalendars, "calendar-access")) { if (checkHomesetCapabilities(homeSetCalendars, "calendar-access")) {
homeSetCalendars.propfind(Mode.CALDAV_COLLECTIONS); homeSetCalendars.propfind(Mode.CALDAV_COLLECTIONS);
List<ServerInfo.ResourceInfo> calendars = new LinkedList<ServerInfo.ResourceInfo>(); List<ServerInfo.ResourceInfo> calendars = new LinkedList<>();
if (homeSetCalendars.getMembers() != null) if (homeSetCalendars.getMembers() != null)
for (WebDavResource resource : homeSetCalendars.getMembers()) for (WebDavResource resource : homeSetCalendars.getMembers())
if (resource.isCalendar()) { if (resource.isCalendar()) {
@ -159,7 +159,7 @@ public class DavResourceFinder implements Closeable {
*/ */
public URI getInitialContextURL(ServerInfo serverInfo, String serviceName) throws URISyntaxException, MalformedURLException { public URI getInitialContextURL(ServerInfo serverInfo, String serviceName) throws URISyntaxException, MalformedURLException {
String scheme = null, String scheme = null,
domain = null; domain;
int port = -1; int port = -1;
String path = "/"; String path = "/";
@ -227,9 +227,9 @@ public class DavResourceFinder implements Closeable {
* Detects the current-user-principal for a given WebDavResource. At first, /.well-known/ is tried. Only * Detects the current-user-principal for a given WebDavResource. At first, /.well-known/ is tried. Only
* if no current-user-principal can be detected for the .well-known location, the given location of the resource * if no current-user-principal can be detected for the .well-known location, the given location of the resource
* is tried. * is tried.
* @param resource Location that will be queried * @param serverInfo Location that will be queried
* @param serviceName Well-known service name ("carddav", "caldav") * @param serviceName Well-known service name ("carddav", "caldav")
* @return WebDavResource of current-user-principal for the given service, or null if it can't be found * @return WebDavResource of current-user-principal for the given service, or null if it can't be found
* *
* TODO: If a TXT record is given, always use it instead of trying .well-known first * TODO: If a TXT record is given, always use it instead of trying .well-known first
*/ */

View File

@ -59,7 +59,11 @@ public class ServerInfo implements Serializable {
final Type type; final Type type;
final boolean readOnly; final boolean readOnly;
final String URL, title, description, color;
final String URL, // absolute URL of resource
title,
description,
color;
VCardVersion vCardVersion; VCardVersion vCardVersion;

View File

@ -25,6 +25,7 @@ import org.apache.http.client.methods.HttpGetHC4;
import org.apache.http.client.methods.HttpOptionsHC4; import org.apache.http.client.methods.HttpOptionsHC4;
import org.apache.http.client.methods.HttpPutHC4; import org.apache.http.client.methods.HttpPutHC4;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtilsHC4;
import org.apache.http.entity.ByteArrayEntityHC4; import org.apache.http.entity.ByteArrayEntityHC4;
import org.apache.http.impl.auth.BasicSchemeHC4; import org.apache.http.impl.auth.BasicSchemeHC4;
import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicAuthCache;
@ -129,7 +130,7 @@ public class WebDavResource {
location = parent.location; location = parent.location;
} }
protected WebDavResource(WebDavResource parent, URI url) { public WebDavResource(WebDavResource parent, URI url) {
this(parent); this(parent);
location = parent.location.resolve(url); location = parent.location.resolve(url);
} }
@ -191,16 +192,19 @@ public class WebDavResource {
/* property methods */ /* property methods */
public String getCurrentUserPrincipal() { public URI getCurrentUserPrincipal() throws URISyntaxException {
return properties.get(Property.CURRENT_USER_PRINCIPAL); String principal = properties.get(Property.CURRENT_USER_PRINCIPAL);
return principal != null ? URIUtils.parseURI(principal, false) : null;
} }
public String getAddressbookHomeSet() { public URI getAddressbookHomeSet() throws URISyntaxException {
return properties.get(Property.ADDRESSBOOK_HOMESET); String homeset = properties.get(Property.ADDRESSBOOK_HOMESET);
return homeset != null ? URIUtils.parseURI(homeset, false) : null;
} }
public String getCalendarHomeSet() { public URI getCalendarHomeSet() throws URISyntaxException {
return properties.get(Property.CALENDAR_HOMESET); String homeset = properties.get(Property.CALENDAR_HOMESET);
return homeset != null ? URIUtils.parseURI(homeset, false) : null;
} }
public String getContentType() { public String getContentType() {