commit 24cb51fb61b564b2e2406dfd4a939c97f9772b97
parent 23dd547e76dd65f50cea9d61501e6b54cb55bc3f
Author: dankert <devnull@localhost>
Date: Thu, 27 Oct 2011 00:14:14 +0200
Umstellung der HTTP-Response auf Byte-Array.
Diffstat:
3 files changed, 72 insertions(+), 18 deletions(-)
diff --git a/src/de/openrat/client/HTTPRequest.java b/src/de/openrat/client/HTTPRequest.java
@@ -22,6 +22,7 @@ package de.openrat.client;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -322,7 +323,7 @@ public class HTTPRequest implements Serializable
* @throws IOException
* if server is unrechable or responds non-wellformed XML
*/
- public String performRequest() throws IOException
+ public byte[] performRequest() throws IOException
{
return performRequest(null);
}
@@ -335,7 +336,7 @@ public class HTTPRequest implements Serializable
* @throws IOException
* if server is unrechable or responds non-wellformed XML
*/
- public String performRequest(String body) throws IOException
+ public byte[] performRequest(String body) throws IOException
{
final Socket socket = new Socket();
@@ -468,10 +469,10 @@ public class HTTPRequest implements Serializable
outputStream.flush();
final InputStream inputStream = socket.getInputStream();
- final int available = inputStream.available();
+// final int available = inputStream.available();
final BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(socket.getInputStream()));
+ new MyStreamReader(inputStream),1);
final String httpResponse = bufferedReader.readLine().trim();
final String httpRetCode = httpResponse.substring(9, 12);
@@ -501,20 +502,27 @@ public class HTTPRequest implements Serializable
if (this.trace)
System.out.println(responseHeader);
}
+ //inputStreamReader.reset();
+ //inputStream.reset();
- StringBuffer response = new StringBuffer();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- while (bufferedReader.ready())
- {
- response.append(bufferedReader.readLine() + "\n");
+ int nRead;
+ byte[] data = new byte[1024];
+
+ while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
+ buffer.write(data, 0, nRead);
}
+ buffer.flush();
+ byte[] response = buffer.toByteArray();
+
if (this.trace)
System.out.println("--- response body ---");
if (this.trace)
System.out.println(response + "\n\n\n");
- return response.toString();
+ return response;
} finally
{
try
diff --git a/src/de/openrat/client/MyStreamReader.java b/src/de/openrat/client/MyStreamReader.java
@@ -0,0 +1,35 @@
+package de.openrat.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+public class MyStreamReader extends Reader
+{
+
+ private InputStream stream;
+
+ public MyStreamReader(InputStream inputStream)
+ {
+ this.stream = inputStream;
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ stream.close();
+ }
+
+ @Override
+ public int read(char[] buf, int offset, int count) throws IOException
+ {
+ if (count != 1 || buf.length != 1 || offset != 0)
+ throw new IOException("Buffer size must be 1");
+ byte[] b = new byte[1];
+ this.stream.read(b);
+ char c = (char) b[0];
+ buf[0] = c;
+ return 1;
+ }
+
+}
diff --git a/src/de/openrat/client/OpenRatClient.java b/src/de/openrat/client/OpenRatClient.java
@@ -2,6 +2,7 @@ package de.openrat.client;
import java.io.File;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -131,7 +132,7 @@ public class OpenRatClient extends CMSRequest
* @return
*/
public void setValue(String pageid, String elementid, String type,
- String value,boolean release,boolean publish) throws IOException
+ String value, boolean release, boolean publish) throws IOException
{
clearParameters();
setAction("pageelement");
@@ -139,8 +140,8 @@ public class OpenRatClient extends CMSRequest
setId(pageid);
setParameter("elementid", elementid);
setParameter("text", value);
- setParameter("release", release?"1":"0");
- setParameter("publish", publish?"1":"0");
+ setParameter("release", release ? "1" : "0");
+ setParameter("publish", publish ? "1" : "0");
setMethod("POST");
readJSON();
@@ -199,7 +200,7 @@ public class OpenRatClient extends CMSRequest
protected JSONObject readJSON() throws OpenRatClientException
{
- String response;
+ byte[] response;
try
{
response = super.performRequest();
@@ -217,7 +218,17 @@ public class OpenRatClient extends CMSRequest
try
{
- final JSONObject json = new JSONObject(response);
+ JSONObject json;
+ try
+ {
+ json = new JSONObject(
+ new String(response,"UTF-8"));
+ }
+ catch (UnsupportedEncodingException e1)
+ {
+ throw new OpenRatClientException(
+ "UTF-8 not supported?!", e1);
+ }
try
{
@@ -254,7 +265,7 @@ public class OpenRatClient extends CMSRequest
catch (JSONException e)
{
throw new OpenRatClientException(
- "JSON Parsing Error. Original repsonse was:\n" + response,
+ "JSON Parsing Error. Original response was:\n" + new String(response)+"\n\n",
e);
}
}
@@ -618,8 +629,8 @@ public class OpenRatClient extends CMSRequest
super.setAction("file");
super.setActionMethod("show");
super.setId(objectid);
-
- final String content = performRequest();
- return content.getBytes();
+
+ final byte[] content = performRequest();
+ return content;
}
}