Loading src/main/java/at/bitfire/dav4android/DavCollection.kt +21 −20 Original line number Diff line number Diff line Loading @@ -22,7 +22,8 @@ open class DavCollection @JvmOverloads constructor( httpClient: OkHttpClient, location: HttpUrl, log: Logger = Constants.log ): DavResource(httpClient, location, log) { // TODO Is it okay if accessToken is null below? ) : DavResource(httpClient, location, null, log) { /** * Sends a REPORT sync-collection request. If a sync-token is returned, it will be made Loading src/main/java/at/bitfire/dav4android/DavResource.kt +73 −39 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import java.util.logging.Logger open class DavResource @JvmOverloads constructor( val httpClient: OkHttpClient, var location: HttpUrl, var accessToken: String? = null, val log: Logger = Constants.log ) { Loading Loading @@ -86,11 +87,24 @@ open class DavResource @JvmOverloads constructor( resetResponse() capabilities.clear() val response = httpClient.newCall(Request.Builder() val response: Response if (accessToken.isNullOrEmpty()) { response = httpClient.newCall(Request.Builder() .method("OPTIONS", null) .header("Content-Length", "0") .url(location) .build()).execute() } else { response = httpClient.newCall(Request.Builder() .method("OPTIONS", null) .header("Content-Length", "0") .header("Authorization", "Bearer $accessToken") .url(location) .build()).execute() } checkStatus(response, true) HttpUtils.listHeader(response, "DAV").mapTo(capabilities) { it.trim() } Loading Loading @@ -196,7 +210,8 @@ open class DavResource @JvmOverloads constructor( if (response.isRedirect) { processRedirect(response) redirected = true } else } else break } checkStatus(response!!, true) Loading Loading @@ -280,11 +295,22 @@ open class DavResource @JvmOverloads constructor( var response: Response? = null for (attempt in 1..MAX_REDIRECTS) { if (accessToken.isNullOrEmpty()) { response = httpClient.newCall(Request.Builder() .url(location) .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) .header("Depth", depth.toString()) .build()).execute() } else { response = httpClient.newCall(Request.Builder() .url(location) .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) .header("Depth", depth.toString()) .header("Authorization", "Bearer $accessToken") .build()).execute() } if (response.isRedirect) processRedirect(response) else Loading Loading @@ -368,7 +394,8 @@ open class DavResource @JvmOverloads constructor( if (mediaType != null) { if (((mediaType.type() != "application" && mediaType.type() != "text")) || mediaType.subtype() != "xml") throw InvalidDavResponseException("Received non-XML 207 Multi-Status") } else } else log.warning("Received 207 Multi-Status without Content-Type, assuming XML") } Loading @@ -383,10 +410,12 @@ open class DavResource @JvmOverloads constructor( if (target != null) { log.fine("Redirected, new location = $target") location = target } else } else throw HttpException("Redirected without new Location") } } finally { } finally { response.body()?.close() } } Loading Loading @@ -442,7 +471,8 @@ open class DavResource @JvmOverloads constructor( "status" -> status = try { StatusLine.parse(parser.nextText()) } catch(e: ProtocolException) { } catch (e: ProtocolException) { log.warning("Invalid status line, treating as 500 Server Error") StatusLine(Protocol.HTTP_1_1, 500, "Invalid status line") } Loading Loading @@ -486,7 +516,8 @@ open class DavResource @JvmOverloads constructor( try { if (sHref.substring(firstColon, firstColon + 3) == "://") hierarchical = true } catch (e: IndexOutOfBoundsException) { } catch (e: IndexOutOfBoundsException) { // no "://" } if (!hierarchical) Loading @@ -498,7 +529,8 @@ open class DavResource @JvmOverloads constructor( "status" -> status = try { StatusLine.parse(parser.nextText()) } catch(e: ProtocolException) { } catch (e: ProtocolException) { log.warning("Invalid status line, treating as 500 Server Error") StatusLine(Protocol.HTTP_1_1, 500, "Invalid status line") } Loading Loading @@ -545,7 +577,8 @@ open class DavResource @JvmOverloads constructor( // it's about ourselves (and not 404) target = this } else if (location.scheme() == href.scheme() && location.host() == href.host() && location.port() == href.port()) { } else if (location.scheme() == href.scheme() && location.host() == href.host() && location.port() == href.port()) { val locationSegments = location.pathSegments() val hrefSegments = href.pathSegments() Loading @@ -562,7 +595,7 @@ open class DavResource @JvmOverloads constructor( val sameBasePath = (0 until nBasePathSegments).none { locationSegments[it] != hrefSegments[it] } if (sameBasePath) { // it's about a member target = DavResource(httpClient, href, log) target = DavResource(httpClient, href, accessToken, log) if (!removed) members += target else Loading @@ -573,7 +606,7 @@ open class DavResource @JvmOverloads constructor( if (target == null) { log.warning("Received <response> not for self and not for member resource: $href") target = DavResource(httpClient, href, log) target = DavResource(httpClient, href, accessToken, log) related.add(target) } Loading Loading @@ -621,7 +654,8 @@ open class DavResource @JvmOverloads constructor( if (!multiStatus) throw InvalidDavResponseException("Multi-Status response didn't contain <DAV:multistatus> root element") } catch (e: XmlPullParserException) { } catch (e: XmlPullParserException) { throw InvalidDavResponseException("Couldn't parse Multi-Status XML", e) } } Loading Loading
src/main/java/at/bitfire/dav4android/DavCollection.kt +21 −20 Original line number Diff line number Diff line Loading @@ -22,7 +22,8 @@ open class DavCollection @JvmOverloads constructor( httpClient: OkHttpClient, location: HttpUrl, log: Logger = Constants.log ): DavResource(httpClient, location, log) { // TODO Is it okay if accessToken is null below? ) : DavResource(httpClient, location, null, log) { /** * Sends a REPORT sync-collection request. If a sync-token is returned, it will be made Loading
src/main/java/at/bitfire/dav4android/DavResource.kt +73 −39 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import java.util.logging.Logger open class DavResource @JvmOverloads constructor( val httpClient: OkHttpClient, var location: HttpUrl, var accessToken: String? = null, val log: Logger = Constants.log ) { Loading Loading @@ -86,11 +87,24 @@ open class DavResource @JvmOverloads constructor( resetResponse() capabilities.clear() val response = httpClient.newCall(Request.Builder() val response: Response if (accessToken.isNullOrEmpty()) { response = httpClient.newCall(Request.Builder() .method("OPTIONS", null) .header("Content-Length", "0") .url(location) .build()).execute() } else { response = httpClient.newCall(Request.Builder() .method("OPTIONS", null) .header("Content-Length", "0") .header("Authorization", "Bearer $accessToken") .url(location) .build()).execute() } checkStatus(response, true) HttpUtils.listHeader(response, "DAV").mapTo(capabilities) { it.trim() } Loading Loading @@ -196,7 +210,8 @@ open class DavResource @JvmOverloads constructor( if (response.isRedirect) { processRedirect(response) redirected = true } else } else break } checkStatus(response!!, true) Loading Loading @@ -280,11 +295,22 @@ open class DavResource @JvmOverloads constructor( var response: Response? = null for (attempt in 1..MAX_REDIRECTS) { if (accessToken.isNullOrEmpty()) { response = httpClient.newCall(Request.Builder() .url(location) .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) .header("Depth", depth.toString()) .build()).execute() } else { response = httpClient.newCall(Request.Builder() .url(location) .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) .header("Depth", depth.toString()) .header("Authorization", "Bearer $accessToken") .build()).execute() } if (response.isRedirect) processRedirect(response) else Loading Loading @@ -368,7 +394,8 @@ open class DavResource @JvmOverloads constructor( if (mediaType != null) { if (((mediaType.type() != "application" && mediaType.type() != "text")) || mediaType.subtype() != "xml") throw InvalidDavResponseException("Received non-XML 207 Multi-Status") } else } else log.warning("Received 207 Multi-Status without Content-Type, assuming XML") } Loading @@ -383,10 +410,12 @@ open class DavResource @JvmOverloads constructor( if (target != null) { log.fine("Redirected, new location = $target") location = target } else } else throw HttpException("Redirected without new Location") } } finally { } finally { response.body()?.close() } } Loading Loading @@ -442,7 +471,8 @@ open class DavResource @JvmOverloads constructor( "status" -> status = try { StatusLine.parse(parser.nextText()) } catch(e: ProtocolException) { } catch (e: ProtocolException) { log.warning("Invalid status line, treating as 500 Server Error") StatusLine(Protocol.HTTP_1_1, 500, "Invalid status line") } Loading Loading @@ -486,7 +516,8 @@ open class DavResource @JvmOverloads constructor( try { if (sHref.substring(firstColon, firstColon + 3) == "://") hierarchical = true } catch (e: IndexOutOfBoundsException) { } catch (e: IndexOutOfBoundsException) { // no "://" } if (!hierarchical) Loading @@ -498,7 +529,8 @@ open class DavResource @JvmOverloads constructor( "status" -> status = try { StatusLine.parse(parser.nextText()) } catch(e: ProtocolException) { } catch (e: ProtocolException) { log.warning("Invalid status line, treating as 500 Server Error") StatusLine(Protocol.HTTP_1_1, 500, "Invalid status line") } Loading Loading @@ -545,7 +577,8 @@ open class DavResource @JvmOverloads constructor( // it's about ourselves (and not 404) target = this } else if (location.scheme() == href.scheme() && location.host() == href.host() && location.port() == href.port()) { } else if (location.scheme() == href.scheme() && location.host() == href.host() && location.port() == href.port()) { val locationSegments = location.pathSegments() val hrefSegments = href.pathSegments() Loading @@ -562,7 +595,7 @@ open class DavResource @JvmOverloads constructor( val sameBasePath = (0 until nBasePathSegments).none { locationSegments[it] != hrefSegments[it] } if (sameBasePath) { // it's about a member target = DavResource(httpClient, href, log) target = DavResource(httpClient, href, accessToken, log) if (!removed) members += target else Loading @@ -573,7 +606,7 @@ open class DavResource @JvmOverloads constructor( if (target == null) { log.warning("Received <response> not for self and not for member resource: $href") target = DavResource(httpClient, href, log) target = DavResource(httpClient, href, accessToken, log) related.add(target) } Loading Loading @@ -621,7 +654,8 @@ open class DavResource @JvmOverloads constructor( if (!multiStatus) throw InvalidDavResponseException("Multi-Status response didn't contain <DAV:multistatus> root element") } catch (e: XmlPullParserException) { } catch (e: XmlPullParserException) { throw InvalidDavResponseException("Couldn't parse Multi-Status XML", e) } } Loading