Loading src/main/kotlin/at/bitfire/dav4jvm/DavCalendar.kt +14 −6 Original line number Diff line number Diff line Loading @@ -8,11 +8,16 @@ package at.bitfire.dav4jvm import at.bitfire.dav4jvm.exception.DavException import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.property.CalendarData import at.bitfire.dav4jvm.property.GetContentType import at.bitfire.dav4jvm.property.GetETag import at.bitfire.dav4jvm.property.ScheduleTag import okhttp3.HttpUrl import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.xmlpull.v1.XmlSerializer import java.io.IOException import java.io.StringWriter import java.text.SimpleDateFormat Loading Loading @@ -119,6 +124,11 @@ class DavCalendar @JvmOverloads constructor( * @throws DavException on WebDAV error */ fun multiget(urls: List<HttpUrl>, callback: DavResponseCallback): List<Property> { fun XmlSerializer.emptyTag(propertyName: Property.Name) { startTag(propertyName.namespace, propertyName.name) endTag(propertyName.namespace, propertyName.name) } /* <!ELEMENT calendar-multiget ((DAV:allprop | DAV:propname | DAV:prop)?, DAV:href+)> Loading @@ -131,12 +141,10 @@ class DavCalendar @JvmOverloads constructor( serializer.setPrefix("CAL", XmlUtils.NS_CALDAV) serializer.startTag(XmlUtils.NS_CALDAV, "calendar-multiget") serializer.startTag(XmlUtils.NS_WEBDAV, "prop") serializer.startTag(XmlUtils.NS_WEBDAV, "getcontenttype") // to determine the character set serializer.endTag(XmlUtils.NS_WEBDAV, "getcontenttype") serializer.startTag(XmlUtils.NS_WEBDAV, "getetag") serializer.endTag(XmlUtils.NS_WEBDAV, "getetag") serializer.startTag(XmlUtils.NS_CALDAV, "calendar-data") serializer.endTag(XmlUtils.NS_CALDAV, "calendar-data") serializer.emptyTag(GetContentType.NAME) // to determine the character set serializer.emptyTag(GetETag.NAME) serializer.emptyTag(ScheduleTag.NAME) serializer.emptyTag(CalendarData.NAME) serializer.endTag(XmlUtils.NS_WEBDAV, "prop") for (url in urls) { serializer.startTag(XmlUtils.NS_WEBDAV, "href") Loading src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt +11 −7 Original line number Diff line number Diff line Loading @@ -220,7 +220,8 @@ open class DavResource @JvmOverloads constructor( * When the server returns an ETag, it is stored in response properties. * * @param body new resource body to upload * @param ifMatchETag value of `If-Match` header to set, or null to omit * @param ifMatch value of `If-Match` header to set, or null to omit * @param ifScheduleTag value of `If-Schedule-Tag` header to set, or null to omit * @param ifNoneMatch indicates whether `If-None-Match: *` ("don't overwrite anything existing") header shall be sent * @param callback called with server response unless an exception is thrown * Loading @@ -228,15 +229,18 @@ open class DavResource @JvmOverloads constructor( * @throws HttpException on HTTP error */ @Throws(IOException::class, HttpException::class) fun put(body: RequestBody, ifMatchETag: String?, ifNoneMatch: Boolean, callback: (Response) -> Unit) { fun put(body: RequestBody, ifMatch: String? = null, ifScheduleTag: String? = null, ifNoneMatch: Boolean = false, callback: (Response) -> Unit) { followRedirects { val builder = Request.Builder() .put(body) .url(location) if (ifMatchETag != null) if (ifMatch != null) // only overwrite specific version builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag)) builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatch)) if (ifScheduleTag != null) // only overwrite specific version builder.header("If-Schedule-Tag-Match", QuotedStringUtils.asQuotedString(ifScheduleTag)) if (ifNoneMatch) // don't overwrite anything existing builder.header("If-None-Match", "*") Loading src/test/kotlin/at/bitfire/dav4jvm/DavResourceTest.kt +3 −3 Original line number Diff line number Diff line Loading @@ -270,7 +270,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_CREATED) .setHeader("ETag", "W/\"Weak PUT ETag\"")) var called = false dav.put(sampleText.toRequestBody("text/plain".toMediaType()), null, false) { response -> dav.put(sampleText.toRequestBody("text/plain".toMediaType())) { response -> called = true assertEquals("Weak PUT ETag", GetETag.fromResponse(response)!!.eTag) assertEquals(response.request.url, dav.location) Loading @@ -290,7 +290,7 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)) called = false dav.put(sampleText.toRequestBody("text/plain".toMediaType()), null, true) { response -> dav.put(sampleText.toRequestBody("text/plain".toMediaType()), ifNoneMatch = true) { response -> called = true assertEquals(url.resolve("/target"), response.request.url) assertNull("Weak PUT ETag", GetETag.fromResponse(response)?.eTag) Loading @@ -307,7 +307,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_PRECON_FAILED)) called = false try { dav.put(sampleText.toRequestBody("text/plain".toMediaType()), "ExistingETag", false) { dav.put(sampleText.toRequestBody("text/plain".toMediaType()), "ExistingETag") { called = true } fail("Expected PreconditionFailedException") Loading Loading
src/main/kotlin/at/bitfire/dav4jvm/DavCalendar.kt +14 −6 Original line number Diff line number Diff line Loading @@ -8,11 +8,16 @@ package at.bitfire.dav4jvm import at.bitfire.dav4jvm.exception.DavException import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.property.CalendarData import at.bitfire.dav4jvm.property.GetContentType import at.bitfire.dav4jvm.property.GetETag import at.bitfire.dav4jvm.property.ScheduleTag import okhttp3.HttpUrl import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.xmlpull.v1.XmlSerializer import java.io.IOException import java.io.StringWriter import java.text.SimpleDateFormat Loading Loading @@ -119,6 +124,11 @@ class DavCalendar @JvmOverloads constructor( * @throws DavException on WebDAV error */ fun multiget(urls: List<HttpUrl>, callback: DavResponseCallback): List<Property> { fun XmlSerializer.emptyTag(propertyName: Property.Name) { startTag(propertyName.namespace, propertyName.name) endTag(propertyName.namespace, propertyName.name) } /* <!ELEMENT calendar-multiget ((DAV:allprop | DAV:propname | DAV:prop)?, DAV:href+)> Loading @@ -131,12 +141,10 @@ class DavCalendar @JvmOverloads constructor( serializer.setPrefix("CAL", XmlUtils.NS_CALDAV) serializer.startTag(XmlUtils.NS_CALDAV, "calendar-multiget") serializer.startTag(XmlUtils.NS_WEBDAV, "prop") serializer.startTag(XmlUtils.NS_WEBDAV, "getcontenttype") // to determine the character set serializer.endTag(XmlUtils.NS_WEBDAV, "getcontenttype") serializer.startTag(XmlUtils.NS_WEBDAV, "getetag") serializer.endTag(XmlUtils.NS_WEBDAV, "getetag") serializer.startTag(XmlUtils.NS_CALDAV, "calendar-data") serializer.endTag(XmlUtils.NS_CALDAV, "calendar-data") serializer.emptyTag(GetContentType.NAME) // to determine the character set serializer.emptyTag(GetETag.NAME) serializer.emptyTag(ScheduleTag.NAME) serializer.emptyTag(CalendarData.NAME) serializer.endTag(XmlUtils.NS_WEBDAV, "prop") for (url in urls) { serializer.startTag(XmlUtils.NS_WEBDAV, "href") Loading
src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt +11 −7 Original line number Diff line number Diff line Loading @@ -220,7 +220,8 @@ open class DavResource @JvmOverloads constructor( * When the server returns an ETag, it is stored in response properties. * * @param body new resource body to upload * @param ifMatchETag value of `If-Match` header to set, or null to omit * @param ifMatch value of `If-Match` header to set, or null to omit * @param ifScheduleTag value of `If-Schedule-Tag` header to set, or null to omit * @param ifNoneMatch indicates whether `If-None-Match: *` ("don't overwrite anything existing") header shall be sent * @param callback called with server response unless an exception is thrown * Loading @@ -228,15 +229,18 @@ open class DavResource @JvmOverloads constructor( * @throws HttpException on HTTP error */ @Throws(IOException::class, HttpException::class) fun put(body: RequestBody, ifMatchETag: String?, ifNoneMatch: Boolean, callback: (Response) -> Unit) { fun put(body: RequestBody, ifMatch: String? = null, ifScheduleTag: String? = null, ifNoneMatch: Boolean = false, callback: (Response) -> Unit) { followRedirects { val builder = Request.Builder() .put(body) .url(location) if (ifMatchETag != null) if (ifMatch != null) // only overwrite specific version builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag)) builder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatch)) if (ifScheduleTag != null) // only overwrite specific version builder.header("If-Schedule-Tag-Match", QuotedStringUtils.asQuotedString(ifScheduleTag)) if (ifNoneMatch) // don't overwrite anything existing builder.header("If-None-Match", "*") Loading
src/test/kotlin/at/bitfire/dav4jvm/DavResourceTest.kt +3 −3 Original line number Diff line number Diff line Loading @@ -270,7 +270,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_CREATED) .setHeader("ETag", "W/\"Weak PUT ETag\"")) var called = false dav.put(sampleText.toRequestBody("text/plain".toMediaType()), null, false) { response -> dav.put(sampleText.toRequestBody("text/plain".toMediaType())) { response -> called = true assertEquals("Weak PUT ETag", GetETag.fromResponse(response)!!.eTag) assertEquals(response.request.url, dav.location) Loading @@ -290,7 +290,7 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)) called = false dav.put(sampleText.toRequestBody("text/plain".toMediaType()), null, true) { response -> dav.put(sampleText.toRequestBody("text/plain".toMediaType()), ifNoneMatch = true) { response -> called = true assertEquals(url.resolve("/target"), response.request.url) assertNull("Weak PUT ETag", GetETag.fromResponse(response)?.eTag) Loading @@ -307,7 +307,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_PRECON_FAILED)) called = false try { dav.put(sampleText.toRequestBody("text/plain".toMediaType()), "ExistingETag", false) { dav.put(sampleText.toRequestBody("text/plain".toMediaType()), "ExistingETag") { called = true } fail("Expected PreconditionFailedException") Loading