changeset 30:8e7f61e14e4d

as per Jackson recommendation, don't use Reader objects but instead use just plain input stream
author dirk
date Tue, 20 Sep 2011 16:18:42 +0200
parents fc8f3ac948df
children 56cf93ee85f4
files conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java conflict-editor/src/test/java/de/codedo/conflicteditor/ConflictsViewTestCase.java conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java
diffstat 6 files changed, 35 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java	Tue Sep 20 16:18:42 2011 +0200
@@ -2,7 +2,7 @@
 package de.codedo.conflicteditor;
 
 import java.io.IOException;
-import java.io.Reader;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -45,17 +45,17 @@
 
     public JsonNode getConflicts() throws IOException
     {
-        Reader reader = null;
+        InputStream input = null;
         try
         {
-            reader = getHttpAccess().post(JSON);
-            return rowsNodeFromJson(reader);
+            input = getHttpAccess().post(JSON);
+            return rowsNodeFromJson(input);
         }
         finally
         {
-            if (reader != null)
+            if (input != null)
             {
-                reader.close();
+                input.close();
             }
         }
     }
@@ -69,10 +69,10 @@
         return _httpAccess;
     }
 
-    private JsonNode rowsNodeFromJson(Reader reader) throws IOException
+    private JsonNode rowsNodeFromJson(InputStream input) throws IOException
     {
         ObjectMapper mapper = new ObjectMapper();
-        JsonNode rootNode = mapper.readTree(reader);
+        JsonNode rootNode = mapper.readTree(input);
         return rootNode.get("rows");
     }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java	Tue Sep 20 16:18:42 2011 +0200
@@ -2,15 +2,15 @@
 package de.codedo.conflicteditor;
 
 import java.io.IOException;
-import java.io.Reader;
+import java.io.InputStream;
 
 public interface HttpAccess
 {
-    Reader delete() throws IOException;
+    InputStream delete() throws IOException;
 
-    Reader get() throws IOException;
+    InputStream get() throws IOException;
 
-    Reader post(String content) throws IOException;
+    InputStream post(String content) throws IOException;
 
-    Reader put(String content) throws IOException;
+    InputStream put(String content) throws IOException;
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java	Tue Sep 20 16:18:42 2011 +0200
@@ -1,12 +1,9 @@
 
 package de.codedo.conflicteditor;
 
-import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.net.HttpURLConnection;
 import java.net.ProtocolException;
 import java.net.URL;
@@ -28,27 +25,27 @@
     }
 
     @Override
-    public Reader post(String content) throws IOException
+    public InputStream post(String content) throws IOException
     {
         HttpURLConnection connection = createUrlConnection(HTTP_METHOD_POST, content);
         send(connection, content);
-        return createReader(connection);
+        return connection.getInputStream();
     }
 
     @Override
-    public Reader put(String content) throws IOException
+    public InputStream put(String content) throws IOException
     {
         HttpURLConnection connection = createUrlConnection(HTTP_METHOD_PUT, content);
         send(connection, content);
-        return createReader(connection);
+        return connection.getInputStream();
     }
 
     @Override
-    public Reader delete() throws IOException
+    public InputStream delete() throws IOException
     {
         HttpURLConnection connection = createUrlConnection(HTTP_METHOD_DELETE);
         connection.connect();
-        return createReader(connection);
+        return connection.getInputStream();
     }
 
     private HttpURLConnection createUrlConnection(String method, String content) throws IOException, ProtocolException
@@ -76,17 +73,10 @@
         output.close();
     }
 
-    private Reader createReader(HttpURLConnection connection) throws IOException
-    {
-        InputStream input = connection.getInputStream();
-        return new BufferedReader(new InputStreamReader(input));
-    }
-
     @Override
-    public Reader get() throws IOException
+    public InputStream get() throws IOException
     {
         HttpURLConnection connection = (HttpURLConnection)_url.openConnection();
-        InputStream input = connection.getInputStream();
-        return new BufferedReader(new InputStreamReader(input));
+        return connection.getInputStream();
     }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Tue Sep 20 16:18:42 2011 +0200
@@ -9,7 +9,7 @@
 import de.codedo.conflicteditor.UrlConnectionHttpAccess;
 
 import java.io.IOException;
-import java.io.Reader;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
 
@@ -43,19 +43,19 @@
     {
         URL documentUrl = _database.urlForDocumentAndRevision(conflict.getId(),
             conflict.getConflictRevision());
-        Reader reader = new UrlConnectionHttpAccess(documentUrl).get();
-        return parseJson(reader);
+        InputStream input = new UrlConnectionHttpAccess(documentUrl).get();
+        return parseJson(input);
     }
 
-    private JsonNode parseJson(Reader reader) throws IOException
+    private JsonNode parseJson(InputStream input) throws IOException
     {
         try
         {
-            return new ObjectMapper().readTree(reader);
+            return new ObjectMapper().readTree(input);
         }
         finally
         {
-            reader.close();
+            input.close();
         }
     }
 
@@ -113,19 +113,19 @@
     {
         String id = _currentConflict.get("_id").getValueAsText();
         URL updateUrl = _database.urlForDocument(id);
-        Reader reader = new UrlConnectionHttpAccess(updateUrl).put(_currentConflict.toString());
+        InputStream input = new UrlConnectionHttpAccess(updateUrl).put(_currentConflict.toString());
 
         // TODO take care of the return value
-        System.out.println(new ObjectMapper().readTree(reader));
+        System.out.println(new ObjectMapper().readTree(input));
     }
 
     private void deleteConflictVersion() throws Exception
     {
         String id = _currentConflict.get("_id").getValueAsText();
         URL deleteUrl = _database.urlForDocumentAndRevision(id, _conflictRevision);
-        Reader reader = new UrlConnectionHttpAccess(deleteUrl).delete();
+        InputStream input = new UrlConnectionHttpAccess(deleteUrl).delete();
 
         // TODO take care of the return value
-        System.out.println(new ObjectMapper().readTree(reader));
+        System.out.println(new ObjectMapper().readTree(input));
     }
 }
--- a/conflict-editor/src/test/java/de/codedo/conflicteditor/ConflictsViewTestCase.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/ConflictsViewTestCase.java	Tue Sep 20 16:18:42 2011 +0200
@@ -9,8 +9,6 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 
 import org.codehaus.jackson.JsonNode;
 import org.junit.Test;
@@ -37,8 +35,7 @@
         assertNotNull(input);
 
         HttpAccess httpAccess = mock(HttpAccess.class);
-        Reader reader = new InputStreamReader(input);
-        when(httpAccess.post(anyString())).thenReturn(reader);
+        when(httpAccess.post(anyString())).thenReturn(input);
 
         ConflictsView conflictsView = new ConflictsView(httpAccess);
         return conflictsView.getConflicts();
--- a/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java	Sun Sep 18 19:06:59 2011 +0200
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java	Tue Sep 20 16:18:42 2011 +0200
@@ -2,9 +2,9 @@
 package de.codedo.conflicteditor;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
-import java.io.Reader;
 import java.net.URL;
 import java.util.List;
 
@@ -34,9 +34,9 @@
             URL documentUrl = database.urlForDocumentAndRevision(conflict.getId(),
                 conflict.getConflictRevision());
             System.out.printf("curl -X DELETE \"%s\"\n", documentUrl);
-            Reader reader = new UrlConnectionHttpAccess(documentUrl).get();
+            InputStream input = new UrlConnectionHttpAccess(documentUrl).get();
 
-            JsonNode conflictDocument = new ObjectMapper().readTree(reader);
+            JsonNode conflictDocument = new ObjectMapper().readTree(input);
             // System.out.println("\nconflict:");
             // prettyPrint(conflictDocument);