From c4b3e68d3619e2d7db4a789f6538451230480184 Mon Sep 17 00:00:00 2001 From: "Trejkaz (pen name)" Date: Mon, 2 Feb 2015 16:07:34 +1100 Subject: [PATCH 1/2] put overloads accepting WriteLogic --- src/main/java/com/github/sardine/Sardine.java | 20 ++++++ .../java/com/github/sardine/WriteLogic.java | 14 ++++ .../com/github/sardine/impl/SardineImpl.java | 34 +++++++-- .../github/sardine/impl/WriteLogicEntity.java | 71 +++++++++++++++++++ 4 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/sardine/WriteLogic.java create mode 100644 src/main/java/com/github/sardine/impl/WriteLogicEntity.java diff --git a/src/main/java/com/github/sardine/Sardine.java b/src/main/java/com/github/sardine/Sardine.java index e31246d7..8d7e1104 100644 --- a/src/main/java/com/github/sardine/Sardine.java +++ b/src/main/java/com/github/sardine/Sardine.java @@ -157,6 +157,15 @@ List list(String url, int depth, boolean allProp) */ void put(String url, InputStream dataStream) throws IOException; + /** + * Uses PUT to send data to a server. Not repeatable on authentication failure. + * + * @param url Path to the resource including protocol and hostname + * @param writeLogic a callback executed executed to perform the write to the resource + * @throws IOException I/O error or HTTP response validation failure + */ + void put(String url, WriteLogic writeLogic) throws IOException; + /** * Uses PUT to send data to a server with a specific content type * header. Repeatable on authentication failure. @@ -214,6 +223,17 @@ List list(String url, int depth, boolean allProp) * @throws IOException I/O error or HTTP response validation failure */ void put(String url, InputStream dataStream, Map headers) throws IOException; + + /** + * Uses PUT to send data to a server with specific headers. Not repeatable + * on authentication failure. + * + * @param url Path to the resource including protocol and hostname + * @param writeLogic a callback executed executed to perform the write to the resource + * @param headers Additional HTTP headers to add to the request + * @throws IOException I/O error or HTTP response validation failure + */ + void put(String url, WriteLogic writeLogic, Map headers) throws IOException; /** * Uses PUT to upload file to a server with specific contentType. diff --git a/src/main/java/com/github/sardine/WriteLogic.java b/src/main/java/com/github/sardine/WriteLogic.java new file mode 100644 index 00000000..229fe162 --- /dev/null +++ b/src/main/java/com/github/sardine/WriteLogic.java @@ -0,0 +1,14 @@ +package com.github.sardine; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Callback to perform logic to write to an output stream. + * + * @author trejkaz + */ +public interface WriteLogic +{ + void withOutputStream(OutputStream stream) throws IOException; +} diff --git a/src/main/java/com/github/sardine/impl/SardineImpl.java b/src/main/java/com/github/sardine/impl/SardineImpl.java index 62a8d34d..8776e565 100644 --- a/src/main/java/com/github/sardine/impl/SardineImpl.java +++ b/src/main/java/com/github/sardine/impl/SardineImpl.java @@ -23,6 +23,7 @@ import com.github.sardine.DavResource; import com.github.sardine.Sardine; import com.github.sardine.Version; +import com.github.sardine.WriteLogic; import com.github.sardine.impl.handler.ExistsResponseHandler; import com.github.sardine.impl.handler.LockResponseHandler; import com.github.sardine.impl.handler.MultiStatusResponseHandler; @@ -744,6 +745,13 @@ public void put(String url, InputStream dataStream) throws IOException this.put(url, dataStream, (String) null); } + @Override + public void put(String url, WriteLogic writeLogic) throws IOException + { + WriteLogicEntity entity = new WriteLogicEntity(writeLogic); + this.put(url, entity, (String) null, true); + } + @Override public void put(String url, InputStream dataStream, String contentType) throws IOException { @@ -765,13 +773,25 @@ public void put(String url, InputStream dataStream, String contentType, boolean } @Override - public void put(String url, InputStream dataStream, Map headers) throws IOException { + public void put(String url, InputStream dataStream, Map headers) throws IOException + { + this.put(url, dataStream, headerMapToList(headers)); + } + + @Override + public void put(String url, WriteLogic writeLogic, Map headers) throws IOException + { + this.put(url, writeLogic, headerMapToList(headers)); + } + + private List
headerMapToList(Map map) + { List
list = new ArrayList
(); - for(Map.Entry h: headers.entrySet()) { + for(Map.Entry h: map.entrySet()) { list.add(new BasicHeader(h.getKey(), h.getValue())); } - this.put(url, dataStream, list); - } + return list; + } public void put(String url, InputStream dataStream, List
headers) throws IOException { @@ -780,6 +800,12 @@ public void put(String url, InputStream dataStream, List
headers) throws this.put(url, entity, headers); } + public void put(String url, WriteLogic writeLogic, List
headers) throws IOException + { + WriteLogicEntity entity = new WriteLogicEntity(writeLogic); + this.put(url, entity, headers); + } + /** * Upload the entity using PUT * diff --git a/src/main/java/com/github/sardine/impl/WriteLogicEntity.java b/src/main/java/com/github/sardine/impl/WriteLogicEntity.java new file mode 100644 index 00000000..bee6a9f6 --- /dev/null +++ b/src/main/java/com/github/sardine/impl/WriteLogicEntity.java @@ -0,0 +1,71 @@ +package com.github.sardine.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.http.entity.AbstractHttpEntity; +import org.apache.http.entity.ContentType; + +import com.github.sardine.WriteLogic; + +/** + *

An entity which performs writing using an instance of {@link WriteLogic}.

+ * + *

Calls to {@code getContent()} will fail and this class can only be used when making requests + * (but that's the only time that its use makes any kind of sense.)

+ * + * @author trejkaz + */ +class WriteLogicEntity extends AbstractHttpEntity +{ + private final WriteLogic writeLogic; + + WriteLogicEntity(WriteLogic writeLogic) + { + this(writeLogic, null); + } + + WriteLogicEntity(WriteLogic writeLogic, ContentType contentType) + { + this.writeLogic = writeLogic; + if (contentType != null) + { + this.setContentType(contentType.toString()); + } + } + + @Override + public boolean isRepeatable() + { + // Playing it safe. + return false; + } + + @Override + public long getContentLength() + { + // Because we can't possibly know. + return -1L; + } + + @Override + public InputStream getContent() throws IOException + { + // Supposedly this is OK, but the API hasn't formalised it yet: + // http://stackoverflow.com/questions/10146692/how-do-i-write-to-an-outputstream-using-defaulthttpclient + throw new UnsupportedOperationException("getContent() not supported, use writeTo(OutputStream) instead"); + } + + @Override + public void writeTo(OutputStream outputStream) throws IOException + { + writeLogic.withOutputStream(outputStream); + } + + @Override + public boolean isStreaming() + { + // Still not sure what the right value is here. + return false; + } +} From 521b77bd9d2f6c91b18fc256e9c29e95a01bd040 Mon Sep 17 00:00:00 2001 From: "Trejkaz (pen name)" Date: Mon, 16 Feb 2015 10:58:26 +1100 Subject: [PATCH 2/2] Corrected space indents to tabs. --- src/main/java/com/github/sardine/Sardine.java | 4 +- .../java/com/github/sardine/WriteLogic.java | 2 +- .../com/github/sardine/impl/SardineImpl.java | 37 ++++---- .../github/sardine/impl/WriteLogicEntity.java | 86 +++++++++---------- 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/github/sardine/Sardine.java b/src/main/java/com/github/sardine/Sardine.java index 8d7e1104..6f116ed9 100644 --- a/src/main/java/com/github/sardine/Sardine.java +++ b/src/main/java/com/github/sardine/Sardine.java @@ -234,10 +234,10 @@ List list(String url, int depth, boolean allProp) * @throws IOException I/O error or HTTP response validation failure */ void put(String url, WriteLogic writeLogic, Map headers) throws IOException; - + /** * Uses PUT to upload file to a server with specific contentType. - * Repeatable on authentication failure. + * Repeatable on authentication failure. * * @param url Path to the resource including protocol and hostname * @param localFile local file to send diff --git a/src/main/java/com/github/sardine/WriteLogic.java b/src/main/java/com/github/sardine/WriteLogic.java index 229fe162..d29b4a45 100644 --- a/src/main/java/com/github/sardine/WriteLogic.java +++ b/src/main/java/com/github/sardine/WriteLogic.java @@ -10,5 +10,5 @@ */ public interface WriteLogic { - void withOutputStream(OutputStream stream) throws IOException; + void withOutputStream(OutputStream stream) throws IOException; } diff --git a/src/main/java/com/github/sardine/impl/SardineImpl.java b/src/main/java/com/github/sardine/impl/SardineImpl.java index 8776e565..e373ee16 100644 --- a/src/main/java/com/github/sardine/impl/SardineImpl.java +++ b/src/main/java/com/github/sardine/impl/SardineImpl.java @@ -235,8 +235,8 @@ public void setCredentials(String username, String password) @Override public void setCredentials(String username, String password, String domain, String workstation) { - this.context.setCredentialsProvider(this.getCredentialsProvider(username, password, domain, workstation)); - this.context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState()); + this.context.setCredentialsProvider(this.getCredentialsProvider(username, password, domain, workstation)); + this.context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState()); } private CredentialsProvider getCredentialsProvider(String username, String password, String domain, String workstation) @@ -775,22 +775,22 @@ public void put(String url, InputStream dataStream, String contentType, boolean @Override public void put(String url, InputStream dataStream, Map headers) throws IOException { - this.put(url, dataStream, headerMapToList(headers)); - } + this.put(url, dataStream, headerMapToList(headers)); + } - @Override + @Override public void put(String url, WriteLogic writeLogic, Map headers) throws IOException { - this.put(url, writeLogic, headerMapToList(headers)); + this.put(url, writeLogic, headerMapToList(headers)); } private List
headerMapToList(Map map) { - List
list = new ArrayList
(); - for(Map.Entry h: map.entrySet()) { - list.add(new BasicHeader(h.getKey(), h.getValue())); - } - return list; + List
list = new ArrayList
(); + for(Map.Entry h: map.entrySet()) { + list.add(new BasicHeader(h.getKey(), h.getValue())); + } + return list; } public void put(String url, InputStream dataStream, List
headers) throws IOException @@ -870,12 +870,15 @@ public T put(String url, HttpEntity entity, List
headers, ResponseHa throw e; } } - @Override - public void put(String url, File localFile, String contentType) throws IOException { - FileEntity content = new FileEntity(localFile); - //don't use ExpectContinue for repetable FileEntity, some web server (IIS for exmaple) may return 400 bad request after retry - this.put(url, content, contentType, false); - } + + @Override + public void put(String url, File localFile, String contentType) throws IOException + { + FileEntity content = new FileEntity(localFile); + //don't use ExpectContinue for repetable FileEntity, some web server (IIS for exmaple) may return 400 bad request after retry + this.put(url, content, contentType, false); + } + @Override public void delete(String url) throws IOException { diff --git a/src/main/java/com/github/sardine/impl/WriteLogicEntity.java b/src/main/java/com/github/sardine/impl/WriteLogicEntity.java index bee6a9f6..c90ab0f2 100644 --- a/src/main/java/com/github/sardine/impl/WriteLogicEntity.java +++ b/src/main/java/com/github/sardine/impl/WriteLogicEntity.java @@ -18,54 +18,54 @@ */ class WriteLogicEntity extends AbstractHttpEntity { - private final WriteLogic writeLogic; + private final WriteLogic writeLogic; - WriteLogicEntity(WriteLogic writeLogic) - { - this(writeLogic, null); - } + WriteLogicEntity(WriteLogic writeLogic) + { + this(writeLogic, null); + } - WriteLogicEntity(WriteLogic writeLogic, ContentType contentType) - { - this.writeLogic = writeLogic; - if (contentType != null) - { - this.setContentType(contentType.toString()); - } - } + WriteLogicEntity(WriteLogic writeLogic, ContentType contentType) + { + this.writeLogic = writeLogic; + if (contentType != null) + { + this.setContentType(contentType.toString()); + } + } - @Override - public boolean isRepeatable() - { - // Playing it safe. - return false; - } + @Override + public boolean isRepeatable() + { + // Playing it safe. + return false; + } - @Override - public long getContentLength() - { - // Because we can't possibly know. - return -1L; - } + @Override + public long getContentLength() + { + // Because we can't possibly know. + return -1L; + } - @Override - public InputStream getContent() throws IOException - { - // Supposedly this is OK, but the API hasn't formalised it yet: - // http://stackoverflow.com/questions/10146692/how-do-i-write-to-an-outputstream-using-defaulthttpclient - throw new UnsupportedOperationException("getContent() not supported, use writeTo(OutputStream) instead"); - } + @Override + public InputStream getContent() throws IOException + { + // Supposedly this is OK, but the API hasn't formalised it yet: + // http://stackoverflow.com/questions/10146692/how-do-i-write-to-an-outputstream-using-defaulthttpclient + throw new UnsupportedOperationException("getContent() not supported, use writeTo(OutputStream) instead"); + } - @Override - public void writeTo(OutputStream outputStream) throws IOException - { - writeLogic.withOutputStream(outputStream); - } + @Override + public void writeTo(OutputStream outputStream) throws IOException + { + writeLogic.withOutputStream(outputStream); + } - @Override - public boolean isStreaming() - { - // Still not sure what the right value is here. - return false; - } + @Override + public boolean isStreaming() + { + // Still not sure what the right value is here. + return false; + } }