Mercurial > hg > ConflictEditor
changeset 31:56cf93ee85f4
Implement HttpAccess using Apache's HTTP components. This works much better in conjunction with UTF-8 encoded JSON
author | dirk |
---|---|
date | Tue, 20 Sep 2011 17:00:45 +0200 |
parents | 8e7f61e14e4d |
children | a6388599b106 |
files | conflict-editor/pom.xml conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java conflict-editor/src/main/java/de/codedo/conflicteditor/HttpClientHttpAccess.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java |
diffstat | 5 files changed, 95 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.1.2</version> + </dependency> <!-- test dependencies --> <dependency>
--- 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; }
--- /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(); + } +}
--- 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));
--- 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 {