Skip to content

Commit

Permalink
Merge pull request #180 from dice-group/fix/175-handle-2XX-responses
Browse files Browse the repository at this point in the history
fix accepting all 200-299 status codes on http responses
  • Loading branch information
frensing authored Sep 23, 2022
2 parents 1121c82 + 6e8e304 commit befe887
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.Instant;
import java.util.concurrent.*;

Expand Down Expand Up @@ -116,17 +118,6 @@ public void shutdownResultProcessor() {
}
}

boolean checkResponseStatus() {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 200) {
return true;
} else {
double duration = durationInMilliseconds(requestStartTime, Instant.now());
addResultsOnce(new QueryExecutionStats(queryId, COMMON.QUERY_HTTP_FAILURE, duration));
return false;
}
}

synchronized protected void addResultsOnce(QueryExecutionStats queryExecutionStats) {
if (!resultsSaved) {
this.addResults(queryExecutionStats);
Expand Down Expand Up @@ -183,18 +174,19 @@ private void handleException(String query, Long cause, Exception e) {
}

protected void processHttpResponse() {
// check if query execution took already longer than timeout
boolean responseCodeOK = checkResponseStatus();
int responseCode = response.getStatusLine().getStatusCode();
boolean responseCodeSuccess = responseCode >= 200 && responseCode < 300;
boolean responseCodeOK = responseCode == 200;

if (responseCodeOK) { // response status is OK (200)
// get content type header
HttpEntity httpResponse = response.getEntity();
Header contentTypeHeader = new BasicHeader(httpResponse.getContentType().getName(), httpResponse.getContentType().getValue());
// get content stream
try (InputStream inputStream = httpResponse.getContent()) {
// read content stream
//Stream in resultProcessor, return length, set string in StringBuilder.
try (InputStream contentStream = httpResponse.getContent()) {
// read content stream with resultProcessor, return length, set string in StringBuilder.
ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
long length = resultProcessor.readResponse(inputStream, responseBody);
long length = resultProcessor.readResponse(contentStream, responseBody);
tmpExecutedQueries++;
// check if such a result was already parsed and is cached
double duration = durationInMilliseconds(requestStartTime, Instant.now());
Expand All @@ -217,6 +209,12 @@ protected void processHttpResponse() {
double duration = durationInMilliseconds(requestStartTime, Instant.now());
addResultsOnce(new QueryExecutionStats(queryId, COMMON.QUERY_HTTP_FAILURE, duration));
}
} else if (responseCodeSuccess) { // response status is succeeded (2xx) but not OK (200)
double duration = durationInMilliseconds(requestStartTime, Instant.now());
addResultsOnce(new QueryExecutionStats(queryId, COMMON.QUERY_SUCCESS, duration, 0));
} else { // response status indicates that the query did not succeed (!= 2xx)
double duration = durationInMilliseconds(requestStartTime, Instant.now());
addResultsOnce(new QueryExecutionStats(queryId, COMMON.QUERY_HTTP_FAILURE, duration));
}
}

Expand Down

0 comments on commit befe887

Please sign in to comment.