Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix accepting all 200-299 status codes on http responses #180

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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