diff --git a/org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java b/org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java index 5d77995f..eb9f7213 100644 --- a/org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java +++ b/org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java @@ -9,6 +9,7 @@ import org.ektorp.changes.ChangesFeed; import org.ektorp.changes.DocumentChange; import org.ektorp.http.HttpClient; +import org.ektorp.impl.AttachmentCouchDbConnector; /** * Primary interface for working with Objects mapped as documents in CouchDb. @@ -18,7 +19,7 @@ * @author henrik lundgren * */ -public interface CouchDbConnector extends LocalBulkBuffer { +public interface CouchDbConnector extends LocalBulkBuffer, AttachmentCouchDbConnector { /** * * @param id diff --git a/org.ektorp/src/main/java/org/ektorp/impl/AttachmentCouchDbConnector.java b/org.ektorp/src/main/java/org/ektorp/impl/AttachmentCouchDbConnector.java new file mode 100644 index 00000000..0606ebda --- /dev/null +++ b/org.ektorp/src/main/java/org/ektorp/impl/AttachmentCouchDbConnector.java @@ -0,0 +1,20 @@ +package org.ektorp.impl; + +import org.apache.http.HttpEntity; +import org.ektorp.AttachmentInputStream; + +public interface AttachmentCouchDbConnector { + + String createAttachment(String docId, AttachmentInputStream data); + + String createAttachment(String docId, String revision, AttachmentInputStream data); + + String createAttachment(String docId, HttpEntity attachmentEntity, String attachmentName); + + String createAttachment(String docId, String revision, HttpEntity attachmentEntity, String attachmentName); + + AttachmentInputStream getAttachment(String id, String attachmentId); + + AttachmentInputStream getAttachment(String id, String attachmentId, String revision); + +} diff --git a/org.ektorp/src/main/java/org/ektorp/impl/StdAttachmentCouchDbConnector.java b/org.ektorp/src/main/java/org/ektorp/impl/StdAttachmentCouchDbConnector.java new file mode 100644 index 00000000..0e699c33 --- /dev/null +++ b/org.ektorp/src/main/java/org/ektorp/impl/StdAttachmentCouchDbConnector.java @@ -0,0 +1,86 @@ +package org.ektorp.impl; + +import org.apache.http.HttpEntity; +import org.ektorp.AttachmentInputStream; +import org.ektorp.http.HttpResponse; +import org.ektorp.http.RestTemplate; +import org.ektorp.http.URI; +import org.ektorp.util.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StdAttachmentCouchDbConnector implements AttachmentCouchDbConnector { + + private static final Logger LOG = LoggerFactory.getLogger(StdCouchDbConnector.class); + + protected final URI dbURI; + + protected final RevisionResponseHandler revisionHandler; + + protected final RestTemplate restTemplate; + + public StdAttachmentCouchDbConnector(URI dbURI, RestTemplate restTemplate, RevisionResponseHandler revisionHandler) { + this.dbURI = dbURI; + this.restTemplate = restTemplate; + this.revisionHandler = revisionHandler; + } + + @Override + public String createAttachment(String docId, AttachmentInputStream data) { + return createAttachment(docId, null, data); + } + + @Override + public String createAttachment(String docId, String revision, AttachmentInputStream data) { + assertDocIdHasValue(docId); + URI uri = dbURI.append(docId).append(data.getId()); + if (revision != null) { + uri.param("rev", revision); + } + return restTemplate.put(uri.toString(), data, data.getContentType(), data.getContentLength(), revisionHandler).getRevision(); + } + + @Override + public String createAttachment(String docId, HttpEntity attachmentEntity, String attachmentName) { + return createAttachment(docId, null, attachmentEntity, attachmentName); + } + + @Override + public String createAttachment(String docId, String revision, HttpEntity attachmentEntity, String attachmentName) { + assertDocIdHasValue(docId); + URI uri = dbURI.append(docId).append(attachmentName); + if (revision != null) { + uri.param("rev", revision); + } + return restTemplate.put(uri.toString(), attachmentEntity, revisionHandler).getRevision(); + } + + @Override + public AttachmentInputStream getAttachment(final String id, final String attachmentId) { + assertDocIdHasValue(id); + Assert.hasText(attachmentId, "attachmentId may not be null or empty"); + + LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); + return getAttachment(attachmentId, dbURI.append(id).append(attachmentId)); + } + + @Override + public AttachmentInputStream getAttachment(String id, String attachmentId, String revision) { + assertDocIdHasValue(id); + Assert.hasText(attachmentId, "attachmentId may not be null or empty"); + Assert.hasText(revision, "revision may not be null or empty"); + + LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); + return getAttachment(attachmentId, dbURI.append(id).append(attachmentId).param("rev", revision)); + } + + private AttachmentInputStream getAttachment(String attachmentId, URI uri) { + HttpResponse r = restTemplate.get(uri.toString()); + return new AttachmentInputStream(attachmentId, r.getContent(), r.getContentType(), r.getContentLength()); + } + + protected void assertDocIdHasValue(String docId) { + Assert.hasText(docId, "document id cannot be empty"); + } + +} diff --git a/org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbConnector.java b/org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbConnector.java index 129a6584..f8443099 100644 --- a/org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbConnector.java +++ b/org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbConnector.java @@ -14,6 +14,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.HttpEntity; import org.ektorp.*; import org.ektorp.changes.ChangesCommand; import org.ektorp.changes.ChangesFeed; @@ -58,6 +59,8 @@ public class StdCouchDbConnector implements CouchDbConnector { private BulkExecutor inputStreamBulkExecutor; + private AttachmentCouchDbConnector attachmentCouchDbConnector; + private final static Options EMPTY_OPTIONS = new Options(); public StdCouchDbConnector(String databaseName, CouchDbInstance dbInstance) { @@ -95,6 +98,8 @@ protected BulkExecutor getBulkExecutor() { } }; + attachmentCouchDbConnector = new StdAttachmentCouchDbConnector(dbURI, restTemplate, revisionHandler); + inputStreamBulkExecutor = new InputStreamWrapperBulkExecutor(dbURI, restTemplate, objectMapper); } @@ -172,47 +177,33 @@ public Boolean success(HttpResponse hr) throws Exception { } @Override - public String createAttachment(String docId, AttachmentInputStream data) { - return createAttachment(docId, null, data); + public AttachmentInputStream getAttachment(String id, String attachmentId) { + return attachmentCouchDbConnector.getAttachment(id, attachmentId); } @Override - public String createAttachment(String docId, String revision, - AttachmentInputStream data) { - assertDocIdHasValue(docId); - URI uri = dbURI.append(docId).append(data.getId()); - if (revision != null) { - uri.param("rev", revision); - } - return restTemplate.put(uri.toString(), data, data.getContentType(), - data.getContentLength(), revisionHandler).getRevision(); + public AttachmentInputStream getAttachment(String id, String attachmentId, String revision) { + return attachmentCouchDbConnector.getAttachment(id, attachmentId, revision); } @Override - public AttachmentInputStream getAttachment(final String id, - final String attachmentId) { - assertDocIdHasValue(id); - Assert.hasText(attachmentId, "attachmentId may not be null or empty"); - - LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); - return getAttachment(attachmentId, dbURI.append(id).append(attachmentId)); + public String createAttachment(String docId, AttachmentInputStream data) { + return attachmentCouchDbConnector.createAttachment(docId, data); } @Override - public AttachmentInputStream getAttachment(String id, String attachmentId, - String revision) { - assertDocIdHasValue(id); - Assert.hasText(attachmentId, "attachmentId may not be null or empty"); - Assert.hasText(revision, "revision may not be null or empty"); + public String createAttachment(String docId, String revision, AttachmentInputStream data) { + return attachmentCouchDbConnector.createAttachment(docId, revision, data); + } - LOG.trace("fetching attachment for doc: {} attachmentId: {}", id, attachmentId); - return getAttachment(attachmentId, dbURI.append(id).append(attachmentId).param("rev", revision)); + @Override + public String createAttachment(String docId, HttpEntity attachmentEntity, String attachmentName) { + return attachmentCouchDbConnector.createAttachment(docId, attachmentEntity, attachmentName); } - private AttachmentInputStream getAttachment(String attachmentId, URI uri) { - HttpResponse r = restTemplate.get(uri.toString()); - return new AttachmentInputStream(attachmentId, r.getContent(), - r.getContentType(), r.getContentLength()); + @Override + public String createAttachment(String docId, String revision, HttpEntity attachmentEntity, String attachmentName) { + return attachmentCouchDbConnector.createAttachment(docId, revision, attachmentEntity, attachmentName); } @Override