Merge pull request #15 from wsz87/master

DefaultConnection response stream reading fix
pull/18/head
Richard Körber 2016-06-03 20:09:08 +02:00
commit 97fb036bf8
2 changed files with 27 additions and 16 deletions

View File

@ -178,11 +178,9 @@ public class DefaultConnection implements Connection {
try { try {
InputStream in = (conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream()); InputStream in = (conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream());
if (in != null) { if (in != null) {
try (BufferedReader r = new BufferedReader(new InputStreamReader(in, "utf-8"))) { String response = readStream(in);
sb.append(r.readLine());
}
result = JsonUtil.parseJson(sb.toString()); result = JsonUtil.parseJson(response);
LOG.debug("Result JSON: {}", sb); LOG.debug("Result JSON: {}", sb);
} }
@ -193,6 +191,21 @@ public class DefaultConnection implements Connection {
return result; return result;
} }
private String readStream(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
}
return sb.toString();
}
@Override @Override
public X509Certificate readCertificate() throws IOException { public X509Certificate readCertificate() throws IOException {
assertConnectionIsOpen(); assertConnectionIsOpen();
@ -220,14 +233,12 @@ public class DefaultConnection implements Connection {
} }
EnumMap<Resource, URI> resourceMap = new EnumMap<>(Resource.class); EnumMap<Resource, URI> resourceMap = new EnumMap<>(Resource.class);
StringBuilder sb = new StringBuilder(); String response = "";
try { try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) { response = readStream(conn.getInputStream());
sb.append(reader.readLine());
}
Map<String, Object> result = JsonUtil.parseJson(sb.toString()); Map<String, Object> result = JsonUtil.parseJson(response);
for (Map.Entry<String, Object> entry : result.entrySet()) { for (Map.Entry<String, Object> entry : result.entrySet()) {
Resource res = Resource.parse(entry.getKey()); Resource res = Resource.parse(entry.getKey());
if (res != null) { if (res != null) {
@ -238,7 +249,7 @@ public class DefaultConnection implements Connection {
LOG.debug("Resource directory: {}", resourceMap); LOG.debug("Resource directory: {}", resourceMap);
} catch (JoseException | URISyntaxException ex) { } catch (JoseException | URISyntaxException ex) {
throw new AcmeProtocolException("Failed to read directory: " + sb, ex); throw new AcmeProtocolException("Failed to read directory: " + response, ex);
} }
return resourceMap; return resourceMap;

View File

@ -366,7 +366,7 @@ public class DefaultConnectionTest {
*/ */
@Test @Test
public void testReadJsonResponse() throws Exception { public void testReadJsonResponse() throws Exception {
String jsonData = "{\"foo\":123,\"bar\":\"a-string\"}"; String jsonData = "{\n\"foo\":123,\n\"bar\":\"a-string\"\n}\n";
when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/json"); when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/json");
when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
@ -417,11 +417,11 @@ public class DefaultConnectionTest {
@Test @Test
public void testReadDirectory() throws Exception { public void testReadDirectory() throws Exception {
StringBuilder jsonData = new StringBuilder(); StringBuilder jsonData = new StringBuilder();
jsonData.append('{'); jsonData.append("{\n");
jsonData.append("\"new-reg\":\"http://example.com/acme/newreg\","); jsonData.append("\"new-reg\":\"http://example.com/acme/newreg\",\n");
jsonData.append("\"new-authz\":\"http://example.com/acme/newauthz\","); jsonData.append("\"new-authz\":\"http://example.com/acme/newauthz\",\n");
jsonData.append("\"old-foo\":\"http://example.com/acme/oldfoo\""); jsonData.append("\"old-foo\":\"http://example.com/acme/oldfoo\"\n");
jsonData.append('}'); jsonData.append("}\n");
when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/json"); when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/json");
when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(jsonData.toString().getBytes("utf-8"))); when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(jsonData.toString().getBytes("utf-8")));