Skip to content

Commit 3a9dbba

Browse files
committed
Use streams when deserializing objects from http response
1 parent a73e507 commit 3a9dbba

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

src/main/java/com/github/kaklakariada/fritzbox/http/HttpTemplate.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,10 @@ private <T> T parse(final Response response, Class<T> resultType) {
9494
if (!response.isSuccessful()) {
9595
throw new FritzBoxException("Request failed: " + response);
9696
}
97-
final String body = getBodyAsString(response);
98-
if (body.startsWith("HTTP/1.0 500 Internal Server Error")) {
99-
throw new FritzBoxException("Request failed: " + body);
100-
}
101-
if (LOG.isTraceEnabled()) {
102-
LOG.trace("Got response {} with body\n'{}'", response, body.trim());
103-
}
104-
return deserializer.parse(body.trim(), resultType);
105-
}
106-
107-
private String getBodyAsString(final Response response) {
108-
try {
109-
return response.body().string();
110-
} catch (final IOException e) {
111-
throw new HttpException("Error getting body from response " + response, e);
97+
if (response.code() == 500) {
98+
throw new FritzBoxException("Request failed: " + deserializer.getStringFromStream(response.body().byteStream()));
11299
}
100+
return deserializer.parse(response.body().byteStream(), resultType);
113101
}
114102

115103
private HttpUrl createUrl(String path, QueryParameters parameters) {

src/main/java/com/github/kaklakariada/fritzbox/mapping/Deserializer.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
package com.github.kaklakariada.fritzbox.mapping;
1919

20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.Scanner;
24+
2025
import org.simpleframework.xml.Serializer;
2126
import org.simpleframework.xml.core.Persister;
2227
import org.slf4j.Logger;
@@ -38,25 +43,43 @@ public Deserializer() {
3843
this.xmlSerializer = xmlSerializer;
3944
}
4045

41-
public <T> T parse(String data, Class<T> resultType) {
42-
if (resultType == String.class) {
43-
return resultType.cast(data);
44-
}
45-
if (resultType == Boolean.class) {
46-
return resultType.cast("1".equals(data));
46+
public <T> T parse(InputStream data, Class<T> resultType) {
47+
try {
48+
final T resultObject;
49+
if (resultType == String.class
50+
|| resultType == Boolean.class
51+
|| resultType == Integer.class) {
52+
resultObject = parseSimpleType(resultType, data);
53+
} else {
54+
resultObject = xmlSerializer.read(resultType, data);
55+
}
56+
LOG.trace("Parsed response: {}", resultObject);
57+
return resultObject;
58+
} catch (final Exception e) {
59+
throw new DeserializerException("Error parsing response body", e);
4760
}
48-
if (resultType == Integer.class) {
49-
if (data.isEmpty() || "inval".equals(data)) {
61+
}
62+
63+
private <T> T parseSimpleType(Class<T> resultType, InputStream data) throws IOException {
64+
final String string = getStringFromStream(data);
65+
if (resultType == String.class) {
66+
return resultType.cast(string);
67+
} else if (resultType == Boolean.class) {
68+
return resultType.cast("1".equals(string));
69+
} else if (resultType == Integer.class) {
70+
if (string.isEmpty() || "inval".equals(string)) {
5071
return null;
5172
}
52-
return resultType.cast(Integer.parseInt(data));
53-
}
54-
try {
55-
final T object = xmlSerializer.read(resultType, data);
56-
LOG.trace("Parsed response: {}", object);
57-
return object;
58-
} catch (final Exception e) {
59-
throw new DeserializerException("Error parsing response body '" + data + "'", e);
73+
return resultType.cast(Integer.parseInt(string));
6074
}
75+
throw new IOException("Type '" + resultType + "' is not supported: " + string);
6176
}
77+
78+
public String getStringFromStream(InputStream data) {
79+
final Scanner scanner = new Scanner(data, StandardCharsets.UTF_8.toString());
80+
final String string = scanner.next();
81+
scanner.close();
82+
return string;
83+
}
84+
6285
}

src/test/java/com/github/kaklakariada/fritzbox/mapping/DeserializerTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
package com.github.kaklakariada.fritzbox.mapping;
1919

2020
import static com.github.kaklakariada.fritzbox.assertions.HomeAutomationAssertions.assertThat;
21-
import static java.util.stream.Collectors.joining;
2221
import static org.assertj.core.api.Assertions.assertThat;
2322
import static org.junit.jupiter.api.Assertions.assertEquals;
2423
import static org.junit.jupiter.api.Assertions.assertFalse;
2524
import static org.junit.jupiter.api.Assertions.assertNotNull;
2625
import static org.junit.jupiter.api.Assertions.assertTrue;
2726

2827
import java.io.IOException;
28+
import java.io.InputStream;
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
3131
import java.nio.file.Paths;
@@ -110,16 +110,13 @@ void parseDeviceListAllTogetherWithBlind() throws IOException {
110110
}
111111

112112
DeviceList parseDeviceList(final Path file) throws IOException {
113-
final String content = Files.readString(file);
113+
final InputStream content = Files.newInputStream(file);
114114
return new Deserializer().parse(content, DeviceList.class);
115115
}
116116

117117
@Test
118118
void parseDeviceStatsFritzDect200() throws IOException {
119-
final String fileContent = Files
120-
.readAllLines(Paths.get("src/test/resources/FritzOS29/devicestatsFritzDect200.xml"))
121-
.stream()
122-
.collect(joining("\n"));
119+
final InputStream fileContent = Files.newInputStream(Paths.get("src/test/resources/FritzOS29/devicestatsFritzDect200.xml"));
123120
final DeviceStats stats = new Deserializer().parse(fileContent, DeviceStats.class);
124121
assertEquals(1, stats.getTemperature().get().getStats().size(), "Temperature has just one statistics Element");
125122
assertEquals(Double.valueOf(0.1),
@@ -133,8 +130,7 @@ void parseDeviceStatsFritzDect200() throws IOException {
133130

134131
@Test
135132
void parseSessionInfo() throws IOException {
136-
final String fileContent = Files.readAllLines(Paths.get("src/test/resources/sessionInfo.xml")).stream()
137-
.collect(joining("\n"));
133+
final InputStream fileContent = Files.newInputStream(Paths.get("src/test/resources/sessionInfo.xml"));
138134
final SessionInfo sessionInfo = new Deserializer().parse(fileContent, SessionInfo.class);
139135
assertNotNull(sessionInfo.getUsers());
140136
assertEquals(3, sessionInfo.getUsers().size());

0 commit comments

Comments
 (0)