# HG changeset patch
# User dirk
# Date 1316530845 -7200
# Node ID 56cf93ee85f40d020b972578cca253df5a1501d2
# Parent 8e7f61e14e4dd85540c80b45f8844cba2e1c7f14
Implement HttpAccess using Apache's HTTP components. This works much better in conjunction with UTF-8 encoded JSON
diff -r 8e7f61e14e4d -r 56cf93ee85f4 conflict-editor/pom.xml
--- a/conflict-editor/pom.xml Tue Sep 20 16:18:42 2011 +0200
+++ b/conflict-editor/pom.xml Tue Sep 20 17:00:45 2011 +0200
@@ -19,6 +19,11 @@
jackson-mapper-asl
1.8.5
+
+ org.apache.httpcomponents
+ httpclient
+ 4.1.2
+
diff -r 8e7f61e14e4d -r 56cf93ee85f4 conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java Tue Sep 20 16:18:42 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java Tue Sep 20 17:00:45 2011 +0200
@@ -64,7 +64,7 @@
{
if (_httpAccess == null)
{
- _httpAccess = new UrlConnectionHttpAccess(_viewUrl);
+ _httpAccess = new HttpClientHttpAccess(_viewUrl);
}
return _httpAccess;
}
diff -r 8e7f61e14e4d -r 56cf93ee85f4 conflict-editor/src/main/java/de/codedo/conflicteditor/HttpClientHttpAccess.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/HttpClientHttpAccess.java Tue Sep 20 17:00:45 2011 +0200
@@ -0,0 +1,83 @@
+
+package de.codedo.conflicteditor;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.HTTP;
+
+public class HttpClientHttpAccess extends Object implements HttpAccess
+{
+ private String _url;
+ private HttpClient _httpClient;
+
+ public HttpClientHttpAccess(URL url)
+ {
+ super();
+ _url = url.toExternalForm();
+ _httpClient = new DefaultHttpClient();
+ }
+
+ @Override
+ public InputStream delete() throws IOException
+ {
+ HttpDelete delete = new HttpDelete(_url);
+ return execute(delete);
+ }
+
+ @Override
+ public InputStream get() throws IOException
+ {
+ HttpGet get = new HttpGet(_url);
+ return execute(get);
+ }
+
+ @Override
+ public InputStream post(String content) throws IOException
+ {
+ HttpEntity entity = new StringEntity(content, HTTP.UTF_8);
+
+ HttpPost post = new HttpPost(_url);
+ post.setEntity(entity);
+ addJsonContentTypeHeader(post);
+
+ return execute(post);
+ }
+
+ @Override
+ public InputStream put(String content) throws IOException
+ {
+ HttpEntity entity = new StringEntity(content, HTTP.UTF_8);
+
+ HttpPut put = new HttpPut(_url);
+ put.setEntity(entity);
+ addJsonContentTypeHeader(put);
+
+ return execute(put);
+ }
+
+ private void addJsonContentTypeHeader(HttpMessage message)
+ {
+ message.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
+ }
+
+ private InputStream execute(HttpUriRequest request) throws IOException
+ {
+ HttpResponse response = _httpClient.execute(request);
+ HttpEntity entity = response.getEntity();
+ return entity.getContent();
+ }
+}
diff -r 8e7f61e14e4d -r 56cf93ee85f4 conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java Tue Sep 20 16:18:42 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java Tue Sep 20 17:00:45 2011 +0200
@@ -6,7 +6,7 @@
import de.codedo.conflicteditor.CouchDb;
import de.codedo.conflicteditor.Difference;
import de.codedo.conflicteditor.DocumentMatcher;
-import de.codedo.conflicteditor.UrlConnectionHttpAccess;
+import de.codedo.conflicteditor.HttpClientHttpAccess;
import java.io.IOException;
import java.io.InputStream;
@@ -43,7 +43,7 @@
{
URL documentUrl = _database.urlForDocumentAndRevision(conflict.getId(),
conflict.getConflictRevision());
- InputStream input = new UrlConnectionHttpAccess(documentUrl).get();
+ InputStream input = new HttpClientHttpAccess(documentUrl).get();
return parseJson(input);
}
@@ -113,7 +113,7 @@
{
String id = _currentConflict.get("_id").getValueAsText();
URL updateUrl = _database.urlForDocument(id);
- InputStream input = new UrlConnectionHttpAccess(updateUrl).put(_currentConflict.toString());
+ InputStream input = new HttpClientHttpAccess(updateUrl).put(_currentConflict.toString());
// TODO take care of the return value
System.out.println(new ObjectMapper().readTree(input));
@@ -123,7 +123,7 @@
{
String id = _currentConflict.get("_id").getValueAsText();
URL deleteUrl = _database.urlForDocumentAndRevision(id, _conflictRevision);
- InputStream input = new UrlConnectionHttpAccess(deleteUrl).delete();
+ InputStream input = new HttpClientHttpAccess(deleteUrl).delete();
// TODO take care of the return value
System.out.println(new ObjectMapper().readTree(input));
diff -r 8e7f61e14e4d -r 56cf93ee85f4 conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java
--- a/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java Tue Sep 20 16:18:42 2011 +0200
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java Tue Sep 20 17:00:45 2011 +0200
@@ -13,7 +13,8 @@
public class Playground extends Object
{
- private static final String BASE_URL = "http://localhost:5984/conflicts";
+ // private static final String BASE_URL = "http://localhost:5984/conflicts";
+ private static final String BASE_URL = "http://xanthippe:5984/feedworm";
public static void main(String[] args) throws Exception
{