mirror of https://github.com/shred/acme4j
DefaultConnection stream reading fix
parent
41e9e177be
commit
21092fc647
|
@ -178,11 +178,9 @@ public class DefaultConnection implements Connection {
|
|||
try {
|
||||
InputStream in = (conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream());
|
||||
if (in != null) {
|
||||
try (BufferedReader r = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
|
||||
sb.append(r.readLine());
|
||||
}
|
||||
final String response = readStream(in);
|
||||
|
||||
result = JsonUtil.parseJson(sb.toString());
|
||||
result = JsonUtil.parseJson(response);
|
||||
LOG.debug("Result JSON: {}", sb);
|
||||
}
|
||||
|
||||
|
@ -193,6 +191,21 @@ public class DefaultConnection implements Connection {
|
|||
return result;
|
||||
}
|
||||
|
||||
private String readStream(final InputStream in) throws IOException {
|
||||
final 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
|
||||
public X509Certificate readCertificate() throws IOException {
|
||||
assertConnectionIsOpen();
|
||||
|
@ -220,14 +233,12 @@ public class DefaultConnection implements Connection {
|
|||
}
|
||||
|
||||
EnumMap<Resource, URI> resourceMap = new EnumMap<>(Resource.class);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String response = "";
|
||||
|
||||
try {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
|
||||
sb.append(reader.readLine());
|
||||
}
|
||||
response = readStream(conn.getInputStream());
|
||||
|
||||
Map<String, Object> result = JsonUtil.parseJson(sb.toString());
|
||||
Map<String, Object> result = JsonUtil.parseJson(response);
|
||||
for (Map.Entry<String, Object> entry : result.entrySet()) {
|
||||
Resource res = Resource.parse(entry.getKey());
|
||||
if (res != null) {
|
||||
|
@ -238,7 +249,7 @@ public class DefaultConnection implements Connection {
|
|||
|
||||
LOG.debug("Resource directory: {}", resourceMap);
|
||||
} catch (JoseException | URISyntaxException ex) {
|
||||
throw new AcmeProtocolException("Failed to read directory: " + sb, ex);
|
||||
throw new AcmeProtocolException("Failed to read directory: " + response, ex);
|
||||
}
|
||||
|
||||
return resourceMap;
|
||||
|
|
|
@ -366,7 +366,7 @@ public class DefaultConnectionTest {
|
|||
*/
|
||||
@Test
|
||||
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.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
|
||||
|
@ -417,11 +417,11 @@ public class DefaultConnectionTest {
|
|||
@Test
|
||||
public void testReadDirectory() throws Exception {
|
||||
StringBuilder jsonData = new StringBuilder();
|
||||
jsonData.append('{');
|
||||
jsonData.append("\"new-reg\":\"http://example.com/acme/newreg\",");
|
||||
jsonData.append("\"new-authz\":\"http://example.com/acme/newauthz\",");
|
||||
jsonData.append("\"old-foo\":\"http://example.com/acme/oldfoo\"");
|
||||
jsonData.append('}');
|
||||
jsonData.append("{\n");
|
||||
jsonData.append("\"new-reg\":\"http://example.com/acme/newreg\",\n");
|
||||
jsonData.append("\"new-authz\":\"http://example.com/acme/newauthz\",\n");
|
||||
jsonData.append("\"old-foo\":\"http://example.com/acme/oldfoo\"\n");
|
||||
jsonData.append("}\n");
|
||||
|
||||
when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/json");
|
||||
when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(jsonData.toString().getBytes("utf-8")));
|
||||
|
|
Loading…
Reference in New Issue