use of org.apache.http.impl.execchain.RequestAbortedException in project SEPA by arces-wot.
the class SPARQL11Protocol method executeRequest.
/*
* http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.
* html#d5e279
*
* 1.5. Exception handling
*
* HTTP protocol processors can throw two types of exceptions:
*
* 1) java.io.IOException in case of an I/O failure such as socket timeout or an
* socket reset 2) HttpException that signals an HTTP failure such as a
* violation of the HTTP protocol.
*
* Usually I/O errors are considered non-fatal and recoverable, whereas HTTP
* protocol errors are considered fatal and cannot be automatically recovered
* from. Please note that HttpClient implementations re-throw HttpExceptions as
* ClientProtocolException, which is a subclass of java.io.IOException. This
* enables the users of HttpClient to handle both I/O errors and protocol
* violations from a single catch clause.
*/
/*
* {"error":"IOException","status_code":500,
* "error_description":"Connect to mml.arces.unibo.it:8666 [mml.arces.unibo.it/137.204.143.19] failed: Operation timed out"
* }
*
* extended by java.io.IOException extended by java.net.SocketException extended
* by java.net.ConnectException extended by
* org.apache.http.conn.HttpHostConnectException
*
*/
private Response executeRequest(HttpUriRequest req, Request request) {
CloseableHttpResponse httpResponse = null;
HttpEntity responseEntity = null;
int responseCode = 0;
String responseBody = null;
ErrorResponse errorResponse = null;
try {
// Execute HTTP request
logger.trace(req.toString() + " " + request.toString() + " (timeout: " + request.getTimeout() + " ms) ");
long start = Timings.getTime();
httpResponse = httpClient.execute(req);
long stop = Timings.getTime();
if (request.getClass().equals(UpdateRequest.class))
Timings.log("HTTP_UPDATE_TIME", start, stop);
else
Timings.log("HTTP_QUERY_TIME", start, stop);
// Status code
responseCode = httpResponse.getStatusLine().getStatusCode();
// Body
responseEntity = httpResponse.getEntity();
responseBody = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
logger.trace(String.format("Response code: %d", responseCode));
EntityUtils.consume(responseEntity);
} catch (Exception e) {
errorResponse = new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getClass().getName(), e.getMessage());
// Considered as *** TIMEOUTS ***
if (e instanceof HttpHostConnectException || e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException || e instanceof RequestAbortedException)
errorResponse = new ErrorResponse(HttpStatus.SC_REQUEST_TIMEOUT, e.getClass().getName(), e.getMessage() + " [timeout: " + request.getTimeout() + " ms retry: " + request.getNRetry() + "]");
} finally {
try {
if (httpResponse != null)
httpResponse.close();
} catch (IOException e) {
logger.error(e.getMessage());
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "IOException", e.getMessage());
}
responseEntity = null;
}
if (responseCode >= 400 && errorResponse == null) {
// http://mml.arces.unibo.it/TR/sparql11-se-protocol.html#ErrorResponses
try {
JsonObject ret = new JsonParser().parse(responseBody).getAsJsonObject();
errorResponse = new ErrorResponse(ret.get("status_code").getAsInt(), ret.get("error").getAsString(), ret.get("error_description").getAsString());
} catch (Exception e) {
// E.g. parsing response from a "common" SPARQL endpoint
logger.warn(e.getMessage() + " response code:" + responseCode + " response body: " + responseBody);
if (responseBody.equals(""))
responseBody = httpResponse.toString();
errorResponse = new ErrorResponse(responseCode, "sparql11_endpoint", responseBody);
}
}
if (errorResponse != null) {
logger.error(errorResponse + " Token expired: " + errorResponse.isTokenExpiredError() + " Security manager: " + (sm != null) + " nRetry: " + request.getNRetry());
// TOKEN EXPIRED
if (errorResponse.isTokenExpiredError()) {
try {
logger.info("Refresh token");
Response ret = sm.refreshToken();
if (ret.isError())
return ret;
JWTResponse token = (JWTResponse) ret;
logger.debug(token.getAccessToken());
req.setHeader("Authorization", token.getTokenType() + " " + token.getAccessToken());
// request.setAuthorizationHeader(token.getTokenType()+"
// "+token.getAccessToken());
} catch (SEPAPropertiesException | SEPASecurityException e) {
logger.error("Failed to refresh token. " + e.getMessage());
return errorResponse;
}
return executeRequest(req, request);
} else // TIMEOUT
if (errorResponse.isTimeout() && request.getNRetry() > 0) {
logger.warn(errorResponse);
logger.warn("*** TIMEOUT RETRY " + request.getNRetry() + " ***");
request.retry();
return executeRequest(req, request);
}
return errorResponse;
}
return (request.getClass().equals(UpdateRequest.class) ? new UpdateResponse(responseBody) : new QueryResponse(responseBody));
}
Aggregations