Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit f5b89f4b authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Use proper interfaces for callbacks (closes #4)

parent ac68ad14
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package at.bitfire.dav4jvm

/**
 * Callback for the OPTIONS request.
 */
fun interface CapabilitiesCallback {
    fun onCapabilities(davCapabilities: Set<String>, response: okhttp3.Response)
}

/**
 * Callback for 207 Multi-Status responses.
 */
fun interface MultiResponseCallback {
    /**
     * Called for every `<response>` element in the `<multistatus>` body. For instance,
     * in response to a `PROPFIND` request, this callback will be called once for every found
     * member resource.
     *
     * @param response   the parsed response (including URL)
     * @param relation   relation of the response to the called resource
     */
    fun onResponse(response: Response, relation: Response.HrefRelation)
}

/**
 * Callback for HTTP responses.
 */
fun interface ResponseCallback {
    /**
     * Called for a HTTP response. Typically this is only called for successful/redirect
     * responses because HTTP errors throw an exception before this callback is called.
     */
    fun onResponse(response: okhttp3.Response)
}
+2 −2
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ class DavAddressBook @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error
     */
    fun addressbookQuery(callback: DavResponseCallback): List<Property> {
    fun addressbookQuery(callback: MultiResponseCallback): List<Property> {
        /* <!ELEMENT addressbook-query ((DAV:allprop |
                                         DAV:propname |
                                         DAV:prop)?, filter, limit?)>
@@ -97,7 +97,7 @@ class DavAddressBook @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error
     */
    fun multiget(urls: List<HttpUrl>, contentType: String? = null, version: String? = null, callback: DavResponseCallback): List<Property> {
    fun multiget(urls: List<HttpUrl>, contentType: String? = null, version: String? = null, callback: MultiResponseCallback): List<Property> {
        /* <!ELEMENT addressbook-multiget ((DAV:allprop |
                                            DAV:propname |
                                            DAV:prop)?,
+2 −2
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ class DavCalendar @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error
     */
    fun calendarQuery(component: String, start: Date?, end: Date?, callback: DavResponseCallback): List<Property> {
    fun calendarQuery(component: String, start: Date?, end: Date?, callback: MultiResponseCallback): List<Property> {
        /* <!ELEMENT calendar-query ((DAV:allprop |
                                      DAV:propname |
                                      DAV:prop)?, filter, timezone?)>
@@ -135,7 +135,7 @@ class DavCalendar @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error
     */
    fun multiget(urls: List<HttpUrl>, contentType: String? = null, version: String? = null, callback: DavResponseCallback): List<Property> {
    fun multiget(urls: List<HttpUrl>, contentType: String? = null, version: String? = null, callback: MultiResponseCallback): List<Property> {
        /* <!ELEMENT calendar-multiget ((DAV:allprop |
                                        DAV:propname |
                                        DAV:prop)?, DAV:href+)>
+7 −5
Original line number Diff line number Diff line
@@ -10,9 +10,11 @@ import at.bitfire.dav4jvm.XmlUtils.insertTag
import at.bitfire.dav4jvm.exception.DavException
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.property.SyncToken
import okhttp3.*
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import java.io.IOException
import java.io.StringWriter
import java.util.logging.Logger
@@ -37,7 +39,7 @@ open class DavCollection @JvmOverloads constructor(
     * Sends a POST request. Primarily intended to be used with an Add-Member URL (RFC 5995).
     */
    @Throws(IOException::class, HttpException::class)
    fun post(body: RequestBody, ifNoneMatch: Boolean = false, callback: (Response) -> Unit) {
    fun post(body: RequestBody, ifNoneMatch: Boolean = false, callback: ResponseCallback) {
        followRedirects {
            val builder = Request.Builder()
                    .post(body)
@@ -50,7 +52,7 @@ open class DavCollection @JvmOverloads constructor(
            httpClient.newCall(builder.build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -70,7 +72,7 @@ open class DavCollection @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error
     */
    fun reportChanges(syncToken: String?, infiniteDepth: Boolean, limit: Int?, vararg properties: Property.Name, callback: DavResponseCallback): List<Property> {
    fun reportChanges(syncToken: String?, infiniteDepth: Boolean, limit: Int?, vararg properties: Property.Name, callback: MultiResponseCallback): List<Property> {
        /* <!ELEMENT sync-collection (sync-token, sync-level, limit?, prop)>

           <!ELEMENT sync-token CDATA>       <!-- Text MUST be a URI -->
+29 −37
Original line number Diff line number Diff line
@@ -8,21 +8,10 @@ package at.bitfire.dav4jvm

import at.bitfire.dav4jvm.XmlUtils.insertTag
import at.bitfire.dav4jvm.XmlUtils.propertyName
import at.bitfire.dav4jvm.exception.ConflictException
import at.bitfire.dav4jvm.exception.DavException
import at.bitfire.dav4jvm.exception.ForbiddenException
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.exception.NotFoundException
import at.bitfire.dav4jvm.exception.PreconditionFailedException
import at.bitfire.dav4jvm.exception.ServiceUnavailableException
import at.bitfire.dav4jvm.exception.UnauthorizedException
import at.bitfire.dav4jvm.exception.*
import at.bitfire.dav4jvm.property.SyncToken
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.xmlpull.v1.XmlPullParser
@@ -152,7 +141,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class)
    fun options(callback: (davCapabilities: Set<String>, response: Response) -> Unit) {
    fun options(callback: CapabilitiesCallback) {
        httpClient.newCall(Request.Builder()
                .method("OPTIONS", null)
                .header("Content-Length", "0")
@@ -160,7 +149,10 @@ open class DavResource @JvmOverloads constructor(
                .header("Accept-Encoding", "identity")      // disable compression
                .build()).execute().use { response ->
            checkStatus(response)
            callback(HttpUtils.listHeader(response, "DAV").map { it.trim() }.toSet(), response)
            callback.onCapabilities(
                HttpUtils.listHeader(response, "DAV").map { it.trim() }.toSet(),
                response
            )
        }
    }

@@ -176,7 +168,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on WebDAV error or HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class, DavException::class)
    fun move(destination: HttpUrl, forceOverride: Boolean, callback: (response: Response) -> Unit) {
    fun move(destination: HttpUrl, forceOverride: Boolean, callback: ResponseCallback) {
        val requestBuilder = Request.Builder()
                .method("MOVE", null)
                .header("Content-Length", "0")
@@ -202,7 +194,7 @@ open class DavResource @JvmOverloads constructor(
                location = it
            }

            callback(response)
            callback.onResponse(response)
        }
    }

@@ -217,7 +209,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on WebDAV error or HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class, DavException::class)
    fun copy(destination:HttpUrl, forceOverride: Boolean, callback: (response: Response) -> Unit) {
    fun copy(destination:HttpUrl, forceOverride: Boolean, callback: ResponseCallback) {
        val requestBuilder = Request.Builder()
                .method("COPY", null)
                .header("Content-Length", "0")
@@ -239,7 +231,7 @@ open class DavResource @JvmOverloads constructor(
                [_] (RFC 4918 9.8.5. Status Codes for COPY Method) */
                throw HttpException(response)

            callback(response)
            callback.onResponse(response)
        }
    }

@@ -251,7 +243,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class)
    fun mkCol(xmlBody: String?, callback: (response: Response) -> Unit) {
    fun mkCol(xmlBody: String?, callback: ResponseCallback) {
        val rqBody = xmlBody?.toRequestBody(MIME_XML)

        followRedirects {
@@ -261,7 +253,7 @@ open class DavResource @JvmOverloads constructor(
                    .build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -276,7 +268,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on HTTPS -> HTTP redirect
     */
    fun head(callback: (response: Response) -> Unit) {
    fun head(callback: ResponseCallback) {
        followRedirects {
            httpClient.newCall(
                Request.Builder()
@@ -286,7 +278,7 @@ open class DavResource @JvmOverloads constructor(
            ).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -305,7 +297,7 @@ open class DavResource @JvmOverloads constructor(
     */
    @Deprecated("Use get(accept, headers, callback) with explicit Accept-Encoding instead")
    @Throws(IOException::class, HttpException::class)
    fun get(accept: String, callback: (response: Response) -> Unit) =
    fun get(accept: String, callback: ResponseCallback) =
        get(accept, Headers.headersOf("Accept-Encoding", "identity"), callback)

    /**
@@ -322,7 +314,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on HTTPS -> HTTP redirect
     */
    fun get(accept: String, headers: Headers?, callback: (response: Response) -> Unit) {
    fun get(accept: String, headers: Headers?, callback: ResponseCallback) {
        followRedirects {
            val request = Request.Builder()
                .get()
@@ -337,7 +329,7 @@ open class DavResource @JvmOverloads constructor(
            httpClient.newCall(request.build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -358,7 +350,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on high-level errors
     */
    @Throws(IOException::class, HttpException::class)
    fun getRange(accept: String, offset: Long, size: Int, headers: Headers? = null, callback: (response: Response) -> Unit) {
    fun getRange(accept: String, offset: Long, size: Int, headers: Headers? = null, callback: ResponseCallback) {
        followRedirects {
            val request = Request.Builder()
                .get()
@@ -375,7 +367,7 @@ open class DavResource @JvmOverloads constructor(
            httpClient.newCall(request.build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -395,7 +387,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class)
    fun put(body: RequestBody, ifETag: String? = null, ifScheduleTag: String? = null, ifNoneMatch: Boolean = false, callback: (Response) -> Unit) {
    fun put(body: RequestBody, ifETag: String? = null, ifScheduleTag: String? = null, ifNoneMatch: Boolean = false, callback: ResponseCallback) {
        followRedirects {
            val builder = Request.Builder()
                    .put(body)
@@ -414,7 +406,7 @@ open class DavResource @JvmOverloads constructor(
            httpClient.newCall(builder.build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
            callback.onResponse(response)
        }
    }

@@ -434,7 +426,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class)
    fun delete(ifETag: String? = null, ifScheduleTag: String? = null, callback: (Response) -> Unit) {
    fun delete(ifETag: String? = null, ifScheduleTag: String? = null, callback: ResponseCallback) {
        followRedirects {
            val builder = Request.Builder()
                    .delete()
@@ -454,7 +446,7 @@ open class DavResource @JvmOverloads constructor(
                   a 207 (Multi-Status). […] (RFC 4918 9.6.1. DELETE for Collections) */
                throw HttpException(response)

            callback(response)
            callback.onResponse(response)
        }
    }

@@ -472,7 +464,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws DavException on WebDAV error (like no 207 Multi-Status response) or HTTPS -> HTTP redirect
     */
    @Throws(IOException::class, HttpException::class, DavException::class)
    fun propfind(depth: Int, vararg reqProp: Property.Name, callback: DavResponseCallback) {
    fun propfind(depth: Int, vararg reqProp: Property.Name, callback: MultiResponseCallback) {
        // build XML request body
        val serializer = XmlUtils.newSerializer()
        val writer = StringWriter()
@@ -519,7 +511,7 @@ open class DavResource @JvmOverloads constructor(
    fun proppatch(
        setProperties: Map<Property.Name, String>,
        removeProperties: List<Property.Name>,
        callback: (at.bitfire.dav4jvm.Response, at.bitfire.dav4jvm.Response.HrefRelation) -> Unit
        callback: MultiResponseCallback
    ) {
        followRedirects {
            val rqBody = createProppatchXml(setProperties, removeProperties)
@@ -550,7 +542,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error (like no 207 Multi-Status response) or HTTPS -> HTTP redirect
     */
    fun search(search: String, callback: (at.bitfire.dav4jvm.Response, at.bitfire.dav4jvm.Response.HrefRelation) -> Unit) {
    fun search(search: String, callback: MultiResponseCallback) {
        followRedirects {
            httpClient.newCall(Request.Builder()
                .url(location)
@@ -686,7 +678,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error (for instance, when the response is not a Multi-Status response)
     */
    protected fun processMultiStatus(response: Response, callback: DavResponseCallback): List<Property> {
    protected fun processMultiStatus(response: Response, callback: MultiResponseCallback): List<Property> {
        checkStatus(response)
        assertMultiStatus(response)
        response.body!!.use {
@@ -707,7 +699,7 @@ open class DavResource @JvmOverloads constructor(
     * @throws HttpException on HTTP error
     * @throws DavException on WebDAV error (like an invalid XML response)
     */
    protected fun processMultiStatus(reader: Reader, callback: DavResponseCallback): List<Property> {
    protected fun processMultiStatus(reader: Reader, callback: MultiResponseCallback): List<Property> {
        val responseProperties = mutableListOf<Property>()
        val parser = XmlUtils.newPullParser()

Loading