Try URL and .well-known/caldav before failing

pull/898/head
Alex Baker 6 years ago
parent 041e68c0f3
commit 53c1e81249

@ -4,7 +4,6 @@ import static android.text.TextUtils.isEmpty;
import static at.bitfire.dav4jvm.XmlUtils.NS_CALDAV; import static at.bitfire.dav4jvm.XmlUtils.NS_CALDAV;
import static at.bitfire.dav4jvm.XmlUtils.NS_CARDDAV; import static at.bitfire.dav4jvm.XmlUtils.NS_CARDDAV;
import static at.bitfire.dav4jvm.XmlUtils.NS_WEBDAV; import static at.bitfire.dav4jvm.XmlUtils.NS_WEBDAV;
import static java.util.Arrays.asList;
import android.content.Context; import android.content.Context;
import at.bitfire.cert4android.CustomCertManager; import at.bitfire.cert4android.CustomCertManager;
@ -62,7 +61,7 @@ public class CaldavClient {
private boolean foreground; private boolean foreground;
@Inject @Inject
public CaldavClient( CaldavClient(
@ForApplication Context context, @ForApplication Context context,
Encryption encryption, Encryption encryption,
Preferences preferences, Preferences preferences,
@ -121,32 +120,23 @@ public class CaldavClient {
return forUrl(account.getUrl(), account.getUsername(), account.getPassword(encryption)); return forUrl(account.getUrl(), account.getUsername(), account.getPassword(encryption));
} }
public CaldavClient forCalendar(CaldavAccount account, CaldavCalendar calendar) CaldavClient forCalendar(CaldavAccount account, CaldavCalendar calendar)
throws NoSuchAlgorithmException, KeyManagementException { throws NoSuchAlgorithmException, KeyManagementException {
return forUrl(calendar.getUrl(), account.getUsername(), account.getPassword(encryption)); return forUrl(calendar.getUrl(), account.getUsername(), account.getPassword(encryption));
} }
public CaldavClient forUrl(String url, String username, String password) CaldavClient forUrl(String url, String username, String password)
throws KeyManagementException, NoSuchAlgorithmException { throws KeyManagementException, NoSuchAlgorithmException {
return new CaldavClient( return new CaldavClient(
context, encryption, preferences, interceptor, url, username, password, foreground); context, encryption, preferences, interceptor, url, username, password, foreground);
} }
private String tryFindPrincipal() throws DavException, IOException { private String tryFindPrincipal(String link) throws DavException, IOException {
for (String link : asList("", "/.well-known/caldav")) {
HttpUrl url = httpUrl.resolve(link); HttpUrl url = httpUrl.resolve(link);
Timber.d("Checking for principal: %s", url); Timber.d("Checking for principal: %s", url);
DavResource davResource = new DavResource(httpClient, url); DavResource davResource = new DavResource(httpClient, url);
ResponseList responses = new ResponseList(); ResponseList responses = new ResponseList();
try {
davResource.propfind(0, new Name[] {CurrentUserPrincipal.NAME}, responses); davResource.propfind(0, new Name[] {CurrentUserPrincipal.NAME}, responses);
} catch (HttpException e) {
if (e.getCode() == 405) {
Timber.w(e);
} else {
throw e;
}
}
if (!responses.isEmpty()) { if (!responses.isEmpty()) {
Response response = responses.get(0); Response response = responses.get(0);
CurrentUserPrincipal currentUserPrincipal = response.get(CurrentUserPrincipal.class); CurrentUserPrincipal currentUserPrincipal = response.get(CurrentUserPrincipal.class);
@ -157,7 +147,6 @@ public class CaldavClient {
} }
} }
} }
}
return null; return null;
} }
@ -181,8 +170,16 @@ public class CaldavClient {
return davResource.getLocation().resolve(homeSet).toString(); return davResource.getLocation().resolve(homeSet).toString();
} }
public String getHomeSet() throws IOException, DavException { String getHomeSet() throws IOException, DavException {
String principal = tryFindPrincipal(); String principal = null;
try {
principal = tryFindPrincipal("/.well-known/caldav");
} catch (Exception e) {
Timber.w(e);
}
if (principal == null) {
principal = tryFindPrincipal("");
}
return findHomeset(isEmpty(principal) ? httpUrl : httpUrl.resolve(principal)); return findHomeset(isEmpty(principal) ? httpUrl : httpUrl.resolve(principal));
} }
@ -215,11 +212,11 @@ public class CaldavClient {
return urls; return urls;
} }
public void deleteCollection() throws IOException, HttpException { void deleteCollection() throws IOException, HttpException {
new DavResource(httpClient, httpUrl).delete(null, response -> null); new DavResource(httpClient, httpUrl).delete(null, response -> null);
} }
public String makeCollection(String displayName) String makeCollection(String displayName)
throws IOException, XmlPullParserException, HttpException { throws IOException, XmlPullParserException, HttpException {
DavResource davResource = DavResource davResource =
new DavResource(httpClient, httpUrl.resolve(UUIDHelper.newUUID() + "/")); new DavResource(httpClient, httpUrl.resolve(UUIDHelper.newUUID() + "/"));
@ -266,7 +263,7 @@ public class CaldavClient {
return httpClient; return httpClient;
} }
public CaldavClient setForeground() { CaldavClient setForeground() {
foreground = true; foreground = true;
return this; return this;
} }

Loading…
Cancel
Save