Skip to content

Commit

Permalink
#29: Support Document Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Lundgren committed May 6, 2012
1 parent 0276365 commit d4052bf
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 9 deletions.
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.ektorp.http.HttpClient;
import org.ektorp.http.HttpCopyRequest;
import org.ektorp.http.HttpResponse;
import org.ektorp.http.IdleConnectionMonitor;
import org.ektorp.http.PreemptiveAuthRequestInterceptor;
Expand Down Expand Up @@ -113,6 +114,11 @@ public HttpResponse put(String uri, InputStream data, String contentType,
hp.setEntity(e);
return executeRequest(hp);
}

@Override
public HttpResponse copy(String sourceUri, String destination) {
return executeRequest(new HttpCopyRequest(sourceUri, destination), true);
}

private HttpResponse executePutPost(HttpEntityEnclosingRequestBase request,
String content, boolean useBackend) {
Expand Down
21 changes: 20 additions & 1 deletion org.ektorp/src/main/java/org/ektorp/CouchDbConnector.java
Expand Up @@ -71,7 +71,26 @@ public interface CouchDbConnector {
* if there was an update conflict.
*/
String delete(String id, String revision);

/**
* Copies a document to the target document id.
* Useful when you want to duplicate docs with large attachments.
*
* @param sourceDocId
* @param targetDocId
* @return revision of the target document.
*/
String copy(String sourceDocId, String targetDocId);
/**
*
* Copies a document and overwrites the target document.
* Useful when you want to duplicate docs with large attachments.
*
* @param sourceDocId
* @param targetDocId
* @param targetRevision
* @return revision of the target document.
*/
String copy(String sourceDocId, String targetDocId, String targetRevision);
/**
* Permanently removes the references to deleted documents from the database.
*
Expand Down
2 changes: 2 additions & 0 deletions org.ektorp/src/main/java/org/ektorp/http/HttpClient.java
Expand Up @@ -26,6 +26,8 @@ HttpResponse put(String uri, InputStream data, String contentType,

HttpResponse postUncached(String uri, String content);

HttpResponse copy(String sourceUri, String destination);

void shutdown();

}
20 changes: 20 additions & 0 deletions org.ektorp/src/main/java/org/ektorp/http/HttpCopyRequest.java
@@ -0,0 +1,20 @@
package org.ektorp.http;

import org.apache.http.client.methods.HttpRequestBase;

public class HttpCopyRequest extends HttpRequestBase {

public final static String METHOD_NAME = "COPY";

public HttpCopyRequest(String uri, String destinationUri) {
super();
setURI(java.net.URI.create(uri));
addHeader("Destination", destinationUri);
}

@Override
public String getMethod() {
return METHOD_NAME;
}

}
5 changes: 4 additions & 1 deletion org.ektorp/src/main/java/org/ektorp/http/RestTemplate.java
Expand Up @@ -43,7 +43,10 @@ public <T> T put(String path, String content, ResponseCallback<T> callback) {
return handleResponse(callback, client.put(path, content));
}


public <T> T copy(String path, String destinationUri, ResponseCallback<T> callback) {
return handleResponse(callback, client.copy(path, destinationUri));
}

public void put(String path, String content) {
handleVoidResponse(client.put(path, content));
}
Expand Down
8 changes: 7 additions & 1 deletion org.ektorp/src/main/java/org/ektorp/http/StdHttpClient.java
Expand Up @@ -18,6 +18,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
Expand Down Expand Up @@ -127,7 +128,7 @@ private HttpResponse executePutPost(HttpEntityEnclosingRequestBase request,
}
}

private HttpResponse executeRequest(HttpRequestBase request, boolean useBackend) {
private HttpResponse executeRequest(HttpUriRequest request, boolean useBackend) {
try {
org.apache.http.HttpResponse rsp;
if (useBackend) {
Expand All @@ -150,6 +151,11 @@ private HttpResponse executeRequest(HttpRequestBase request) {
return executeRequest(request, false);
}

@Override
public HttpResponse copy(String sourceUri, String destination) {
return executeRequest(new HttpCopyRequest(sourceUri, destination), true);
}

public void shutdown() {
client.getConnectionManager().shutdown();
}
Expand Down
8 changes: 4 additions & 4 deletions org.ektorp/src/main/java/org/ektorp/http/StdHttpResponse.java
Expand Up @@ -8,7 +8,7 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.ektorp.util.Exceptions;
Expand All @@ -26,13 +26,13 @@ public class StdHttpResponse implements HttpResponse {
private final HttpEntity entity;
private final StatusLine status;
private final String requestURI;
private final HttpRequestBase httpRequest;
private final HttpUriRequest httpRequest;

public static StdHttpResponse of(org.apache.http.HttpResponse rsp, HttpRequestBase httpRequest) {
public static StdHttpResponse of(org.apache.http.HttpResponse rsp, HttpUriRequest httpRequest) {
return new StdHttpResponse(rsp.getEntity(), rsp.getStatusLine(), httpRequest);
}

private StdHttpResponse(HttpEntity e, StatusLine status, HttpRequestBase httpRequest) {
private StdHttpResponse(HttpEntity e, StatusLine status, HttpUriRequest httpRequest) {
this.httpRequest = httpRequest;
this.entity = e != null ? e : NULL_ENTITY;
this.status = status;
Expand Down
Expand Up @@ -46,7 +46,7 @@ public static DbAccessException createDbAccessException(HttpResponse hr) {
}

private static String toPrettyString(JsonNode n) throws IOException {
return MAPPER.defaultPrettyPrintingWriter().writeValueAsString(n);
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(n);
}

private static JsonNode responseBodyAsNode(String s) throws IOException {
Expand Down
Expand Up @@ -374,6 +374,18 @@ public String delete(String id, String revision) {
dbURI.append(id).param("rev", revision).toString(),
revisionHandler).getRevision();
}

@Override
public String copy(String sourceDocId, String targetDocId) {
return copy(sourceDocId, targetDocId, null);
}

@Override
public String copy(String sourceDocId, String targetDocId,
String targetRevision) {
String destinationUri = targetRevision != null ? targetDocId + "?rev=" + targetRevision : targetDocId;
return restTemplate.copy(dbURI.append(sourceDocId).toString(), destinationUri, revisionHandler).getRevision();
}

@Override
public List<String> getAllDocIds() {
Expand Down Expand Up @@ -657,7 +669,7 @@ public int getRevisionLimit() {
@Override
public Integer success(HttpResponse hr) throws Exception {
JsonNode rlimit = objectMapper.readTree(hr.getContent());
return rlimit.getValueAsInt();
return rlimit.getIntValue();
}
});
}
Expand Down

0 comments on commit d4052bf

Please sign in to comment.