Skip to content

Support for prodiving a HttpEntity to save a couchdb attachement #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,7 +19,7 @@
* @author henrik lundgren
*
*/
public interface CouchDbConnector extends LocalBulkBuffer {
public interface CouchDbConnector extends LocalBulkBuffer, AttachmentCouchDbConnector {
/**
*
* @param id
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
49 changes: 20 additions & 29 deletions org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class StdCouchDbConnector implements CouchDbConnector {

private BulkExecutor<InputStream> inputStreamBulkExecutor;

private AttachmentCouchDbConnector attachmentCouchDbConnector;

private final static Options EMPTY_OPTIONS = new Options();

public StdCouchDbConnector(String databaseName, CouchDbInstance dbInstance) {
Expand Down Expand Up @@ -95,6 +98,8 @@ protected BulkExecutor getBulkExecutor() {
}
};

attachmentCouchDbConnector = new StdAttachmentCouchDbConnector(dbURI, restTemplate, revisionHandler);

inputStreamBulkExecutor = new InputStreamWrapperBulkExecutor(dbURI, restTemplate, objectMapper);
}

Expand Down Expand Up @@ -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
Expand Down