mirror of
https://github.com/etesync/android
synced 2025-02-23 21:02:26 +00:00
New collection/service discovery: CalDAV+CardDAV
This commit is contained in:
parent
18542adb2c
commit
7fc01503d5
@ -9,7 +9,6 @@ package at.bitfire.davdroid.resource;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.squareup.okhttp.HttpUrl;
|
import com.squareup.okhttp.HttpUrl;
|
||||||
|
|
||||||
@ -17,7 +16,6 @@ import org.xbill.DNS.Lookup;
|
|||||||
import org.xbill.DNS.Record;
|
import org.xbill.DNS.Record;
|
||||||
import org.xbill.DNS.SRVRecord;
|
import org.xbill.DNS.SRVRecord;
|
||||||
import org.xbill.DNS.TXTRecord;
|
import org.xbill.DNS.TXTRecord;
|
||||||
import org.xbill.DNS.TextParseException;
|
|
||||||
import org.xbill.DNS.Type;
|
import org.xbill.DNS.Type;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -33,6 +31,8 @@ import at.bitfire.dav4android.UrlUtils;
|
|||||||
import at.bitfire.dav4android.exception.DavException;
|
import at.bitfire.dav4android.exception.DavException;
|
||||||
import at.bitfire.dav4android.exception.HttpException;
|
import at.bitfire.dav4android.exception.HttpException;
|
||||||
import at.bitfire.dav4android.exception.NotFoundException;
|
import at.bitfire.dav4android.exception.NotFoundException;
|
||||||
|
import at.bitfire.dav4android.property.AddressbookDescription;
|
||||||
|
import at.bitfire.dav4android.property.AddressbookHomeSet;
|
||||||
import at.bitfire.dav4android.property.CalendarColor;
|
import at.bitfire.dav4android.property.CalendarColor;
|
||||||
import at.bitfire.dav4android.property.CalendarDescription;
|
import at.bitfire.dav4android.property.CalendarDescription;
|
||||||
import at.bitfire.dav4android.property.CalendarHomeSet;
|
import at.bitfire.dav4android.property.CalendarHomeSet;
|
||||||
@ -48,12 +48,22 @@ import lombok.NonNull;
|
|||||||
|
|
||||||
public class DavResourceFinder {
|
public class DavResourceFinder {
|
||||||
private final static String TAG = "davdroid.ResourceFinder";
|
private final static String TAG = "davdroid.ResourceFinder";
|
||||||
|
|
||||||
|
protected enum Service {
|
||||||
|
CALDAV("caldav"),
|
||||||
|
CARDDAV("carddav");
|
||||||
|
|
||||||
|
final String name;
|
||||||
|
Service(String name) { this.name = name;}
|
||||||
|
@Override public String toString() { return name; }
|
||||||
|
};
|
||||||
|
|
||||||
protected Context context;
|
protected Context context;
|
||||||
protected final HttpClient httpClient;
|
protected final HttpClient httpClient;
|
||||||
protected final ServerInfo serverInfo;
|
protected final ServerInfo serverInfo;
|
||||||
|
|
||||||
protected List<ServerInfo.ResourceInfo>
|
protected List<ServerInfo.ResourceInfo>
|
||||||
|
addressbooks = new LinkedList<>(),
|
||||||
calendars = new LinkedList<>(),
|
calendars = new LinkedList<>(),
|
||||||
taskLists = new LinkedList<>();
|
taskLists = new LinkedList<>();
|
||||||
|
|
||||||
@ -66,12 +76,25 @@ public class DavResourceFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void findResources() throws URISyntaxException, IOException, HttpException, DavException {
|
public void findResources() throws URISyntaxException, IOException, HttpException, DavException {
|
||||||
|
findResources(Service.CARDDAV);
|
||||||
|
findResources(Service.CALDAV);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findResources(Service service) throws URISyntaxException, IOException, HttpException, DavException { {
|
||||||
URI baseURI = serverInfo.getBaseURI();
|
URI baseURI = serverInfo.getBaseURI();
|
||||||
String domain = null;
|
String domain = null;
|
||||||
|
|
||||||
HttpUrl principalUrl = null;
|
HttpUrl principalUrl = null;
|
||||||
Set<HttpUrl> calendarHomeSets = new HashSet<>();
|
Set<HttpUrl> calendarHomeSets = new HashSet<>(),
|
||||||
|
addressbookHomeSets = new HashSet<>();
|
||||||
|
|
||||||
|
if (service == Service.CALDAV) {
|
||||||
|
calendars.clear();
|
||||||
|
taskLists.clear();
|
||||||
|
} else if (service == Service.CARDDAV)
|
||||||
|
addressbooks.clear();
|
||||||
|
|
||||||
|
Constants.log.info("STARTING COLLECTION DISCOVERY FOR SERVICE " + service);
|
||||||
if ("http".equals(baseURI.getScheme()) || "https".equals(baseURI.getScheme())) {
|
if ("http".equals(baseURI.getScheme()) || "https".equals(baseURI.getScheme())) {
|
||||||
HttpUrl userURL = HttpUrl.get(baseURI);
|
HttpUrl userURL = HttpUrl.get(baseURI);
|
||||||
|
|
||||||
@ -82,23 +105,44 @@ public class DavResourceFinder {
|
|||||||
Constants.log.info("Check whether user-given URL is a calendar collection and/or contains <calendar-home-set> and/or has <current-user-principal>");
|
Constants.log.info("Check whether user-given URL is a calendar collection and/or contains <calendar-home-set> and/or has <current-user-principal>");
|
||||||
DavResource davBase = new DavResource(httpClient, userURL);
|
DavResource davBase = new DavResource(httpClient, userURL);
|
||||||
try {
|
try {
|
||||||
davBase.propfind(0,
|
if (service == Service.CALDAV) {
|
||||||
CalendarHomeSet.NAME, SupportedCalendarComponentSet.NAME,
|
davBase.propfind(0,
|
||||||
ResourceType.NAME, DisplayName.NAME, CalendarColor.NAME, CalendarDescription.NAME, CalendarTimezone.NAME, CurrentUserPrivilegeSet.NAME,
|
CalendarHomeSet.NAME, SupportedCalendarComponentSet.NAME,
|
||||||
CurrentUserPrincipal.NAME
|
ResourceType.NAME, DisplayName.NAME, CalendarColor.NAME, CalendarDescription.NAME, CalendarTimezone.NAME, CurrentUserPrivilegeSet.NAME,
|
||||||
);
|
CurrentUserPrincipal.NAME
|
||||||
addIfCalendar(davBase);
|
);
|
||||||
} catch (IOException | HttpException | DavException e) {
|
addIfCalendar(davBase);
|
||||||
|
} else if (service == Service.CARDDAV) {
|
||||||
|
davBase.propfind(0,
|
||||||
|
AddressbookHomeSet.NAME,
|
||||||
|
ResourceType.NAME, DisplayName.NAME, AddressbookDescription.NAME, CurrentUserPrivilegeSet.NAME,
|
||||||
|
CurrentUserPrincipal.NAME
|
||||||
|
);
|
||||||
|
addIfAddressBook(davBase);
|
||||||
|
}
|
||||||
|
} catch (IOException|HttpException|DavException e) {
|
||||||
Constants.log.debug("PROPFIND on user-given URL failed", e);
|
Constants.log.debug("PROPFIND on user-given URL failed", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
CalendarHomeSet calendarHomeSet = (CalendarHomeSet) davBase.properties.get(CalendarHomeSet.NAME);
|
if (service == Service.CALDAV) {
|
||||||
if (calendarHomeSet != null) {
|
CalendarHomeSet calendarHomeSet = (CalendarHomeSet)davBase.properties.get(CalendarHomeSet.NAME);
|
||||||
Constants.log.info("Found <calendar-home-set> at user-given URL");
|
if (calendarHomeSet != null) {
|
||||||
for (String href : calendarHomeSet.hrefs) {
|
Constants.log.info("Found <calendar-home-set> at user-given URL");
|
||||||
HttpUrl url = userURL.resolve(href);
|
for (String href : calendarHomeSet.hrefs) {
|
||||||
if (url != null)
|
HttpUrl url = userURL.resolve(href);
|
||||||
calendarHomeSets.add(url);
|
if (url != null)
|
||||||
|
calendarHomeSets.add(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (service == Service.CARDDAV) {
|
||||||
|
AddressbookHomeSet addressbookHomeSet = (AddressbookHomeSet)davBase.properties.get(AddressbookHomeSet.NAME);
|
||||||
|
if (addressbookHomeSet != null) {
|
||||||
|
Constants.log.info("Found <addressbook-home-set> at user-given URL");
|
||||||
|
for (String href : addressbookHomeSet.hrefs) {
|
||||||
|
HttpUrl url = userURL.resolve(href);
|
||||||
|
if (url != null)
|
||||||
|
addressbookHomeSets.add(url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,12 +156,8 @@ public class DavResourceFinder {
|
|||||||
principalUrl = davBase.location.resolve(currentUserPrincipal.href);
|
principalUrl = davBase.location.resolve(currentUserPrincipal.href);
|
||||||
|
|
||||||
if (principalUrl == null) {
|
if (principalUrl == null) {
|
||||||
Constants.log.info("User-given URL doesn't contain <current-user-principal>, trying /.well-known/caldav");
|
Constants.log.info("User-given URL doesn't contain <current-user-principal>, trying /.well-known/" + service.name);
|
||||||
try {
|
principalUrl = getCurrentUserPrincipal(userURL.resolve("/.well-known/" + service.name));
|
||||||
principalUrl = getCurrentUserPrincipal(userURL.resolve("/.well-known/caldav"));
|
|
||||||
} catch (IOException|HttpException|DavException e) {
|
|
||||||
Constants.log.debug("PROPFIND on /.well-known/caldav failed", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,53 +176,99 @@ public class DavResourceFinder {
|
|||||||
|
|
||||||
if (principalUrl == null && domain != null) {
|
if (principalUrl == null && domain != null) {
|
||||||
Constants.log.info("No principal URL yet, trying SRV/TXT records with domain " + domain);
|
Constants.log.info("No principal URL yet, trying SRV/TXT records with domain " + domain);
|
||||||
principalUrl = discoverPrincipalUrl(domain, "caldavs");
|
principalUrl = discoverPrincipalUrl(domain, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
// principal URL has been found, get calendar-home-set
|
// principal URL has been found, get addressbook-home-set/calendar-home-set
|
||||||
if (principalUrl != null) {
|
if (principalUrl != null) {
|
||||||
Constants.log.info("Principal URL=" + principalUrl + ", getting <calendar-home-set>");
|
Constants.log.info("Principal URL=" + principalUrl + ", getting <calendar-home-set>");
|
||||||
try {
|
try {
|
||||||
DavResource principal = new DavResource(httpClient, principalUrl);
|
DavResource principal = new DavResource(httpClient, principalUrl);
|
||||||
principal.propfind(0, CalendarHomeSet.NAME);
|
|
||||||
CalendarHomeSet calendarHomeSet = (CalendarHomeSet)principal.properties.get(CalendarHomeSet.NAME);
|
if (service == Service.CALDAV) {
|
||||||
if (calendarHomeSet != null)
|
principal.propfind(0, CalendarHomeSet.NAME);
|
||||||
Constants.log.info("Found <calendar-home-set> at principal URL");
|
CalendarHomeSet calendarHomeSet = (CalendarHomeSet)principal.properties.get(CalendarHomeSet.NAME);
|
||||||
for (String href : calendarHomeSet.hrefs) {
|
if (calendarHomeSet != null) {
|
||||||
HttpUrl url = principal.location.resolve(href);
|
Constants.log.info("Found <calendar-home-set> at principal URL");
|
||||||
if (url != null)
|
for (String href : calendarHomeSet.hrefs) {
|
||||||
calendarHomeSets.add(url);
|
HttpUrl url = principal.location.resolve(href);
|
||||||
|
if (url != null)
|
||||||
|
calendarHomeSets.add(url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else if (service == Service.CARDDAV) {
|
||||||
|
principal.propfind(0, AddressbookHomeSet.NAME);
|
||||||
|
AddressbookHomeSet addressbookHomeSet = (AddressbookHomeSet)principal.properties.get(AddressbookHomeSet.NAME);
|
||||||
|
if (addressbookHomeSet != null) {
|
||||||
|
Constants.log.info("Found <addressbook-home-set> at principal URL");
|
||||||
|
for (String href : addressbookHomeSet.hrefs) {
|
||||||
|
HttpUrl url = principal.location.resolve(href);
|
||||||
|
if (url != null)
|
||||||
|
addressbookHomeSets.add(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException|HttpException|DavException e) {
|
} catch (IOException|HttpException|DavException e) {
|
||||||
Constants.log.debug("PROPFIND on " + principalUrl + " failed", e);
|
Constants.log.debug("PROPFIND on " + principalUrl + " failed", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now query all home sets
|
// now query all home sets
|
||||||
for (HttpUrl url : calendarHomeSets)
|
if (service == Service.CALDAV)
|
||||||
try {
|
for (HttpUrl url : calendarHomeSets)
|
||||||
Constants.log.info("Listing collections in home set " + url);
|
try {
|
||||||
DavResource homeSet = new DavResource(httpClient, url);
|
Constants.log.info("Listing calendar collections in home set " + url);
|
||||||
homeSet.propfind(1, SupportedCalendarComponentSet.NAME, ResourceType.NAME, DisplayName.NAME, CurrentUserPrivilegeSet.NAME,
|
DavResource homeSet = new DavResource(httpClient, url);
|
||||||
CalendarColor.NAME, CalendarDescription.NAME, CalendarTimezone.NAME);
|
homeSet.propfind(1, SupportedCalendarComponentSet.NAME, ResourceType.NAME, DisplayName.NAME, CurrentUserPrivilegeSet.NAME,
|
||||||
|
CalendarColor.NAME, CalendarDescription.NAME, CalendarTimezone.NAME);
|
||||||
|
|
||||||
// home set should not be a calendar, but some servers have only one calendar and it's the home set
|
// home set should not be a calendar, but some servers have only one calendar and it's the home set
|
||||||
addIfCalendar(homeSet);
|
addIfCalendar(homeSet);
|
||||||
|
|
||||||
// members of the home set can be calendars, too
|
// members of the home set can be calendars, too
|
||||||
for (DavResource member : homeSet.members)
|
for (DavResource member : homeSet.members)
|
||||||
addIfCalendar(member);
|
addIfCalendar(member);
|
||||||
} catch (IOException|HttpException|DavException e) {
|
} catch (IOException|HttpException|DavException e) {
|
||||||
Constants.log.debug("PROPFIND on " + url + " failed", e);
|
Constants.log.debug("PROPFIND on " + url + " failed", e);
|
||||||
}
|
}
|
||||||
|
else if (service == Service.CARDDAV)
|
||||||
|
for (HttpUrl url : addressbookHomeSets)
|
||||||
|
try {
|
||||||
|
Constants.log.info("Listing address books in home set " + url);
|
||||||
|
DavResource homeSet = new DavResource(httpClient, url);
|
||||||
|
homeSet.propfind(1, ResourceType.NAME, DisplayName.NAME, CurrentUserPrivilegeSet.NAME, AddressbookDescription.NAME);
|
||||||
|
|
||||||
// TODO CardDAV
|
// home set should not be an address book, but some servers have only one address book and it's the home set
|
||||||
|
addIfAddressBook(homeSet);
|
||||||
|
|
||||||
|
// members of the home set can be calendars, too
|
||||||
|
for (DavResource member : homeSet.members)
|
||||||
|
addIfAddressBook(member);
|
||||||
|
} catch (IOException|HttpException|DavException e) {
|
||||||
|
Constants.log.debug("PROPFIND on " + url + " failed", e);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO remove duplicates
|
// TODO remove duplicates
|
||||||
// TODO notify user on errors?
|
// TODO notify user on errors?
|
||||||
|
|
||||||
serverInfo.setCalendars(calendars);
|
if (service == Service.CALDAV) {
|
||||||
serverInfo.setTaskLists(taskLists);
|
serverInfo.setCalendars(calendars);
|
||||||
|
serverInfo.setTaskLists(taskLists);
|
||||||
|
} else if (service == Service.CARDDAV)
|
||||||
|
serverInfo.setAddressBooks(addressbooks);
|
||||||
|
}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the given DavResource is a #{@link ResourceType#ADDRESSBOOK}, add it to #{@link #addressbooks}.
|
||||||
|
* @param dav DavResource to check
|
||||||
|
*/
|
||||||
|
protected void addIfAddressBook(@NonNull DavResource dav) {
|
||||||
|
ResourceType resourceType = (ResourceType)dav.properties.get(ResourceType.NAME);
|
||||||
|
if (resourceType != null && resourceType.types.contains(ResourceType.ADDRESSBOOK)) {
|
||||||
|
Constants.log.info("Found address book at " + dav.location);
|
||||||
|
addressbooks.add(resourceInfo(dav, ServerInfo.ResourceInfo.Type.ADDRESS_BOOK));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -204,21 +290,24 @@ public class DavResourceFinder {
|
|||||||
supportsTasks = supportedCalendarComponentSet.supportsTasks;
|
supportsTasks = supportedCalendarComponentSet.supportsTasks;
|
||||||
}
|
}
|
||||||
if (supportsEvents)
|
if (supportsEvents)
|
||||||
calendars.add(resourceInfo(dav));
|
calendars.add(resourceInfo(dav, ServerInfo.ResourceInfo.Type.CALENDAR));
|
||||||
if (supportsTasks)
|
if (supportsTasks)
|
||||||
taskLists.add(resourceInfo(dav));
|
taskLists.add(resourceInfo(dav, ServerInfo.ResourceInfo.Type.CALENDAR));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a #{@link at.bitfire.davdroid.resource.ServerInfo.ResourceInfo} from a given
|
* Builds a #{@link at.bitfire.davdroid.resource.ServerInfo.ResourceInfo} from a given
|
||||||
* #{@link DavResource}. Uses the DAV properties current-user-properties, current-user-privilege-set,
|
* #{@link DavResource}. Uses these DAV properties:
|
||||||
* displayname, calendar-description and calendar-color. Make sure you have queried these
|
* <ul>
|
||||||
* properties from the DavResource.
|
* <li>calendars: current-user-properties, current-user-privilege-set, displayname, calendar-description, calendar-color</li>
|
||||||
|
* <li>address books: current-user-properties, current-user-privilege-set, displayname, addressbook-description</li>
|
||||||
|
* </ul>. Make sure you have queried these properties from the DavResource.
|
||||||
* @param dav DavResource to take the resource info from
|
* @param dav DavResource to take the resource info from
|
||||||
|
* @param type must be ADDRESS_BOOK or CALENDAR
|
||||||
* @return ResourceInfo which represents the DavResource
|
* @return ResourceInfo which represents the DavResource
|
||||||
*/
|
*/
|
||||||
protected ServerInfo.ResourceInfo resourceInfo(DavResource dav) {
|
protected ServerInfo.ResourceInfo resourceInfo(DavResource dav, ServerInfo.ResourceInfo.Type type) {
|
||||||
boolean readOnly = false;
|
boolean readOnly = false;
|
||||||
CurrentUserPrivilegeSet privilegeSet = (CurrentUserPrivilegeSet)dav.properties.get(CurrentUserPrivilegeSet.NAME);
|
CurrentUserPrivilegeSet privilegeSet = (CurrentUserPrivilegeSet)dav.properties.get(CurrentUserPrivilegeSet.NAME);
|
||||||
if (privilegeSet != null)
|
if (privilegeSet != null)
|
||||||
@ -232,17 +321,23 @@ public class DavResourceFinder {
|
|||||||
title = UrlUtils.lastSegment(dav.location);
|
title = UrlUtils.lastSegment(dav.location);
|
||||||
|
|
||||||
String description = null;
|
String description = null;
|
||||||
CalendarDescription calendarDescription = (CalendarDescription)dav.properties.get(CalendarDescription.NAME);
|
|
||||||
if (calendarDescription != null)
|
|
||||||
description = calendarDescription.description;
|
|
||||||
|
|
||||||
Integer color = null;
|
Integer color = null;
|
||||||
CalendarColor calendarColor = (CalendarColor)dav.properties.get(CalendarColor.NAME);
|
if (type == ServerInfo.ResourceInfo.Type.ADDRESS_BOOK) {
|
||||||
if (calendarColor != null)
|
AddressbookDescription addressbookDescription = (AddressbookDescription)dav.properties.get(AddressbookDescription.NAME);
|
||||||
|
if (addressbookDescription != null)
|
||||||
|
description = addressbookDescription.description;
|
||||||
|
} else if (type == ServerInfo.ResourceInfo.Type.CALENDAR) {
|
||||||
|
CalendarDescription calendarDescription = (CalendarDescription)dav.properties.get(CalendarDescription.NAME);
|
||||||
|
if (calendarDescription != null)
|
||||||
|
description = calendarDescription.description;
|
||||||
|
|
||||||
|
CalendarColor calendarColor = (CalendarColor)dav.properties.get(CalendarColor.NAME);
|
||||||
|
if (calendarColor != null)
|
||||||
color = calendarColor.color;
|
color = calendarColor.color;
|
||||||
|
}
|
||||||
|
|
||||||
return new ServerInfo.ResourceInfo(
|
return new ServerInfo.ResourceInfo(
|
||||||
ServerInfo.ResourceInfo.Type.CALENDAR,
|
type,
|
||||||
readOnly,
|
readOnly,
|
||||||
dav.location.toString(),
|
dav.location.toString(),
|
||||||
title,
|
title,
|
||||||
@ -254,17 +349,17 @@ public class DavResourceFinder {
|
|||||||
/**
|
/**
|
||||||
* Try to find the principal URL by performing service discovery on a given domain name.
|
* Try to find the principal URL by performing service discovery on a given domain name.
|
||||||
* @param domain domain name, e.g. "icloud.com"
|
* @param domain domain name, e.g. "icloud.com"
|
||||||
* @param serviceName service name: "caldavs" or "carddavs"
|
* @param service service to discover (CALDAV or CARDDAV)
|
||||||
* @return principal URL, or null if none found
|
* @return principal URL, or null if none found
|
||||||
*/
|
*/
|
||||||
protected HttpUrl discoverPrincipalUrl(String domain, String serviceName) {
|
protected HttpUrl discoverPrincipalUrl(String domain, Service service) {
|
||||||
String scheme = null;
|
String scheme = null;
|
||||||
String fqdn = null;
|
String fqdn = null;
|
||||||
Integer port = null;
|
Integer port = null;
|
||||||
String path = null;
|
List<String> paths = new LinkedList<>(); // there may be multiple paths to try
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final String query = "_" + serviceName + "._tcp." + domain;
|
final String query = "_" + service.name + "s._tcp." + domain;
|
||||||
Constants.log.debug("Looking up SRV records for " + query);
|
Constants.log.debug("Looking up SRV records for " + query);
|
||||||
Record[] records = new Lookup(query, Type.SRV).run();
|
Record[] records = new Lookup(query, Type.SRV).run();
|
||||||
if (records != null && records.length >= 1) {
|
if (records != null && records.length >= 1) {
|
||||||
@ -274,7 +369,7 @@ public class DavResourceFinder {
|
|||||||
scheme = "https";
|
scheme = "https";
|
||||||
fqdn = srv.getTarget().toString(true);
|
fqdn = srv.getTarget().toString(true);
|
||||||
port = srv.getPort();
|
port = srv.getPort();
|
||||||
Constants.log.info("Found " + serviceName + " service: fqdn=" + fqdn + ", port=" + port);
|
Constants.log.info("Found " + service + " service: fqdn=" + fqdn + ", port=" + port);
|
||||||
|
|
||||||
// look for TXT record too (for initial context path)
|
// look for TXT record too (for initial context path)
|
||||||
records = new Lookup(domain, Type.TXT).run();
|
records = new Lookup(domain, Type.TXT).run();
|
||||||
@ -282,33 +377,35 @@ public class DavResourceFinder {
|
|||||||
TXTRecord txt = (TXTRecord)records[0];
|
TXTRecord txt = (TXTRecord)records[0];
|
||||||
for (String segment : (String[])txt.getStrings().toArray(new String[0]))
|
for (String segment : (String[])txt.getStrings().toArray(new String[0]))
|
||||||
if (segment.startsWith("path=")) {
|
if (segment.startsWith("path=")) {
|
||||||
path = segment.substring(5);
|
paths.add(segment.substring(5));
|
||||||
Constants.log.info("Found TXT record; initial context path=" + path);
|
Constants.log.info("Found TXT record; initial context path=" + paths);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path == null) // no path from TXT records, use .well-known
|
// if there's TXT record if it it's wrong, try well-known
|
||||||
path = "/.well-known/caldav";
|
paths.add("/.well-known/" + service.name);
|
||||||
|
// if this fails, too, try "/"
|
||||||
|
paths.add("/");
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Constants.log.debug("SRV/TXT record discovery failed", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(fqdn) && port != null && path != null) {
|
for (String path : paths) {
|
||||||
|
if (!TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(fqdn) && port != null && paths != null) {
|
||||||
HttpUrl initialContextPath = new HttpUrl.Builder()
|
HttpUrl initialContextPath = new HttpUrl.Builder()
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.host(fqdn).port(port)
|
.host(fqdn).port(port)
|
||||||
.encodedPath(path)
|
.encodedPath(path)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpUrl principal = null;
|
Constants.log.info("Trying to determine principal from initial context path=" + initialContextPath);
|
||||||
try {
|
HttpUrl principal = getCurrentUserPrincipal(initialContextPath);
|
||||||
principal = getCurrentUserPrincipal(initialContextPath);
|
if (principal != null)
|
||||||
} catch(NotFoundException e) {
|
return principal;
|
||||||
principal = getCurrentUserPrincipal(initialContextPath.resolve("/"));
|
|
||||||
}
|
|
||||||
return principal;
|
|
||||||
}
|
}
|
||||||
} catch (IOException|HttpException|DavException e) {
|
|
||||||
Constants.log.debug("Service discovery failed", e);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -318,89 +415,24 @@ public class DavResourceFinder {
|
|||||||
* @param url URL to query with PROPFIND (Depth: 0)
|
* @param url URL to query with PROPFIND (Depth: 0)
|
||||||
* @return current-user-principal URL, or null if none
|
* @return current-user-principal URL, or null if none
|
||||||
*/
|
*/
|
||||||
protected HttpUrl getCurrentUserPrincipal(HttpUrl url) throws IOException, HttpException, DavException {
|
protected HttpUrl getCurrentUserPrincipal(HttpUrl url) {
|
||||||
DavResource dav = new DavResource(httpClient, url);
|
try {
|
||||||
dav.propfind(0, CurrentUserPrincipal.NAME);
|
DavResource dav = new DavResource(httpClient, url);
|
||||||
CurrentUserPrincipal currentUserPrincipal = (CurrentUserPrincipal)dav.properties.get(CurrentUserPrincipal.NAME);
|
dav.propfind(0, CurrentUserPrincipal.NAME);
|
||||||
if (currentUserPrincipal != null && currentUserPrincipal.href != null)
|
CurrentUserPrincipal currentUserPrincipal = (CurrentUserPrincipal) dav.properties.get(CurrentUserPrincipal.NAME);
|
||||||
return url.resolve(currentUserPrincipal.href);
|
if (currentUserPrincipal != null && currentUserPrincipal.href != null) {
|
||||||
|
HttpUrl principal = url.resolve(currentUserPrincipal.href);
|
||||||
|
if (principal != null) {
|
||||||
|
Constants.log.info("Found current-user-principal: " + principal);
|
||||||
|
return principal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(IOException|HttpException|DavException e) {
|
||||||
|
Constants.log.debug("PROPFIND for current-user-principal on " + url + " failed", e);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the initial service URL from a given base URI (HTTP[S] or mailto URI, user name, password)
|
|
||||||
* @param serverInfo User-given service information (including base URI, i.e. HTTP[S] URL+user name+password or mailto URI and password)
|
|
||||||
* @param serviceName Service name ("carddav" or "caldav")
|
|
||||||
* @return Initial service URL (HTTP/HTTPS), without user credentials
|
|
||||||
* @throws URISyntaxException when the user-given URI is invalid
|
|
||||||
*/
|
|
||||||
public HttpUrl getInitialContextURL(ServerInfo serverInfo, String serviceName) throws URISyntaxException {
|
|
||||||
String scheme,
|
|
||||||
domain;
|
|
||||||
int port = -1;
|
|
||||||
String path = "/";
|
|
||||||
|
|
||||||
URI baseURI = serverInfo.getBaseURI();
|
|
||||||
if ("mailto".equalsIgnoreCase(baseURI.getScheme())) {
|
|
||||||
// mailto URIs
|
|
||||||
String mailbox = serverInfo.getBaseURI().getSchemeSpecificPart();
|
|
||||||
|
|
||||||
// determine service FQDN
|
|
||||||
int pos = mailbox.lastIndexOf("@");
|
|
||||||
if (pos == -1)
|
|
||||||
throw new URISyntaxException(mailbox, "Missing @ sign");
|
|
||||||
|
|
||||||
scheme = "https";
|
|
||||||
domain = mailbox.substring(pos + 1);
|
|
||||||
if (domain.isEmpty())
|
|
||||||
throw new URISyntaxException(mailbox, "Missing domain name");
|
|
||||||
} else {
|
|
||||||
// HTTP(S) URLs
|
|
||||||
scheme = baseURI.getScheme();
|
|
||||||
domain = baseURI.getHost();
|
|
||||||
port = baseURI.getPort();
|
|
||||||
path = baseURI.getPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to determine FQDN and port number using SRV records
|
|
||||||
try {
|
|
||||||
String name = "_" + serviceName + "._tcp." + domain;
|
|
||||||
Constants.log.debug("Looking up SRV records for " + name);
|
|
||||||
Record[] records = new Lookup(name, Type.SRV).run();
|
|
||||||
if (records != null && records.length >= 1) {
|
|
||||||
SRVRecord srv = selectSRVRecord(records);
|
|
||||||
|
|
||||||
scheme = "https";
|
|
||||||
domain = srv.getTarget().toString(true);
|
|
||||||
port = srv.getPort();
|
|
||||||
Log.d(TAG, "Found " + serviceName + " service for " + domain + " -> " + domain + ":" + port);
|
|
||||||
|
|
||||||
// SRV record found, look for TXT record too (for initial context path)
|
|
||||||
records = new Lookup(name, Type.TXT).run();
|
|
||||||
if (records != null && records.length >= 1) {
|
|
||||||
TXTRecord txt = (TXTRecord)records[0];
|
|
||||||
for (Object o : txt.getStrings().toArray()) {
|
|
||||||
String segment = (String)o;
|
|
||||||
if (segment.startsWith("path=")) {
|
|
||||||
path = segment.substring(5);
|
|
||||||
Constants.log.debug("Found initial context path for " + serviceName + " at " + domain + " -> " + path);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (TextParseException e) {
|
|
||||||
throw new URISyntaxException(domain, "Invalid domain name");
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpUrl.Builder builder = new HttpUrl.Builder().scheme(scheme).host(domain);
|
|
||||||
if (port != -1)
|
|
||||||
builder.port(port);
|
|
||||||
if (TextUtils.isEmpty(path))
|
|
||||||
path = "/";
|
|
||||||
return builder.encodedPath(path).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user