From b9127c237a5eb6dfa8a7b535d416cec12b429459 Mon Sep 17 00:00:00 2001 From: Andrei Churyla Date: Wed, 5 Feb 2020 21:26:03 +0300 Subject: [PATCH] Sardine: Add additional "put" method (with lock token) --- .../com/thegrizzlylabs/sardineandroid/Sardine.java | 14 ++++++++++++++ .../sardineandroid/impl/OkHttpSardine.java | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/com/thegrizzlylabs/sardineandroid/Sardine.java b/src/main/java/com/thegrizzlylabs/sardineandroid/Sardine.java index 97ea98e..cce7492 100755 --- a/src/main/java/com/thegrizzlylabs/sardineandroid/Sardine.java +++ b/src/main/java/com/thegrizzlylabs/sardineandroid/Sardine.java @@ -280,6 +280,20 @@ public interface Sardine */ void put(String url, File localFile, String contentType, boolean expectContinue) throws IOException; + + /** + * Uses PUT to upload file to a server with specific contentType. + * Repeatable on authentication failure. + * + * @param url Path to the resource including protocol and hostname + * @param localFile local file to send + * @param contentType MIME type to add to the HTTP request header + * @param expectContinue Enable Expect: continue header for PUT requests. + * @param lockToken A lock token is a type of state token that identifies a particular lock. + * @throws IOException I/O error or HTTP response validation failure + */ + void put(String url, File localFile, String contentType, boolean expectContinue, String lockToken) throws IOException; + /** * Delete a resource using HTTP DELETE at the specified url * diff --git a/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java b/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java index d95c32b..46b9424 100644 --- a/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java +++ b/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java @@ -1,5 +1,7 @@ package com.thegrizzlylabs.sardineandroid.impl; +import android.text.TextUtils; + import com.thegrizzlylabs.sardineandroid.DavAce; import com.thegrizzlylabs.sardineandroid.DavAcl; import com.thegrizzlylabs.sardineandroid.DavPrincipal; @@ -302,12 +304,20 @@ public void put(String url, File localFile, String contentType) throws IOExcepti @Override public void put(String url, File localFile, String contentType, boolean expectContinue) throws IOException { + put(url, localFile, contentType, expectContinue, null); + } + + @Override + public void put(String url, File localFile, String contentType, boolean expectContinue, String lockToken) throws IOException { MediaType mediaType = contentType == null ? null : MediaType.parse(contentType); RequestBody requestBody = RequestBody.create(mediaType, localFile); Headers.Builder headersBuilder = new Headers.Builder(); if (expectContinue) { headersBuilder.add("Expect", "100-Continue"); } + if (!TextUtils.isEmpty(lockToken)) { + headersBuilder.add("If", "<" + url + "> (<" + lockToken + ">)"); + } put(url, requestBody, headersBuilder.build()); }