Skip to content

Commit b672731

Browse files
committed
Fix error stream handling for HttpOpener. (#463)
Only read `errorStream` _after_ reading `inputStream` failed. (Thanks to @dr0i for the hint!) Drops use of response code range to determine failure handling. (864f0da)
1 parent 75c7ef5 commit b672731

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

metafacture-io/src/main/java/org/metafacture/io/HttpOpener.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public final class HttpOpener extends DefaultObjectPipe<String, ObjectReceiver<R
6767
private static final Pattern HEADER_FIELD_SEPARATOR_PATTERN = Pattern.compile(HEADER_FIELD_SEPARATOR);
6868
private static final Pattern HEADER_VALUE_SEPARATOR_PATTERN = Pattern.compile(HEADER_VALUE_SEPARATOR);
6969

70-
private static final int SUCCESS_CODE_MIN = 200;
71-
private static final int SUCCESS_CODE_MAX = 399;
72-
7370
private final Map<String, String> headers = new HashMap<>();
7471

7572
private Method method;
@@ -260,11 +257,9 @@ public void process(final String input) {
260257
connection.getOutputStream().write(requestBody.getBytes());
261258
}
262259

263-
final InputStream errorStream = connection.getErrorStream();
264-
final InputStream inputStream = errorStream != null ?
265-
getErrorStream(errorStream) : getInputStream(connection);
266-
260+
final InputStream inputStream = getInputStream(connection);
267261
final String contentEncoding = getEncoding(connection.getContentEncoding());
262+
268263
getReceiver().process(new InputStreamReader(inputStream, contentEncoding));
269264
}
270265
catch (final IOException e) {
@@ -294,30 +289,19 @@ private InputStream getInputStream(final HttpURLConnection connection) throws IO
294289
return connection.getInputStream();
295290
}
296291
catch (final IOException e) {
297-
final int responseCode = connection.getResponseCode();
298-
if (responseCode >= SUCCESS_CODE_MIN && responseCode <= SUCCESS_CODE_MAX) {
299-
throw e;
292+
final InputStream errorStream = connection.getErrorStream();
293+
if (errorStream != null) {
294+
return getErrorStream(errorStream);
300295
}
301296
else {
302-
final StringBuilder sb = new StringBuilder(String.valueOf(responseCode));
303-
304-
final String responseMessage = connection.getResponseMessage();
305-
if (responseMessage != null) {
306-
sb.append(" - ").append(responseMessage);
307-
}
308-
309-
return getErrorStream(getInputStream(sb.toString()));
297+
throw e;
310298
}
311299
}
312300
}
313301

314-
private InputStream getInputStream(final String string) {
315-
return new ByteArrayInputStream(string.getBytes());
316-
}
317-
318302
private InputStream getErrorStream(final InputStream errorStream) {
319303
if (errorPrefix != null) {
320-
final InputStream errorPrefixStream = getInputStream(errorPrefix);
304+
final InputStream errorPrefixStream = new ByteArrayInputStream(errorPrefix.getBytes());
321305
return new SequenceInputStream(errorPrefixStream, errorStream);
322306
}
323307
else {

metafacture-io/src/test/java/org/metafacture/io/HttpOpenerTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public final class HttpOpenerTest {
5858
private static final String TEST_STRING = "test string";
5959
private static final StringValuePattern TEST_VALUE = WireMock.equalTo(TEST_STRING);
6060

61-
private static final String TEST_ERROR = "400 - Bad Request";
62-
6361
private static final String REQUEST_BODY = "request body";
6462
private static final String RESPONSE_BODY = "response bödy"; // UTF-8
6563

@@ -263,19 +261,19 @@ public void shouldPerformPostRequestWithEncodingParameterAndContentEncodingRespo
263261
@Test
264262
public void shouldPerformGetRequestWithErrorResponse() throws IOException {
265263
shouldPerformRequest(TEST_URL, HttpOpener.Method.GET, (o, u) -> {},
266-
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), "ERROR: " + TEST_ERROR);
264+
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), "ERROR: " + RESPONSE_BODY);
267265
}
268266

269267
@Test
270268
public void shouldPerformGetRequestWithErrorResponseAndErrorPrefixParameter() throws IOException {
271269
shouldPerformRequest(TEST_URL, HttpOpener.Method.GET, (o, u) -> o.setErrorPrefix(TEST_STRING),
272-
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), TEST_STRING + TEST_ERROR);
270+
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), TEST_STRING + RESPONSE_BODY);
273271
}
274272

275273
@Test
276274
public void shouldPerformGetRequestWithErrorResponseAndWithoutErrorPrefixParameter() throws IOException {
277275
shouldPerformRequest(TEST_URL, HttpOpener.Method.GET, (o, u) -> o.setErrorPrefix(null),
278-
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), TEST_ERROR);
276+
null, null, WireMock.badRequest().withBody(RESPONSE_BODY), RESPONSE_BODY);
279277
}
280278

281279
private void shouldPerformRequest(final String input, final HttpOpener.Method method, final BiConsumer<HttpOpener, String> consumer, final String... headers) throws IOException {

0 commit comments

Comments
 (0)