use of org.openecard.addons.cg.ex.ConnectionError in project open-ecard by ecsec.
the class ChipGateway method sendMessageInterruptable.
private <T> T sendMessageInterruptable(final String resource, final String msg, final Class<T> resClass) throws ConnectionError, InvalidRedirectUrlException, ChipGatewayDataError, ThreadTerminateException {
FutureTask<T> task = new FutureTask<>(new Callable<T>() {
@Override
public T call() throws Exception {
return sendMessage(resource, msg, resClass);
}
});
new Thread(task, "HTTP-Client-" + HTTP_THREAD_NUM.getAndIncrement()).start();
try {
return task.get();
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof ConnectionError) {
throw (ConnectionError) cause;
} else if (cause instanceof InvalidRedirectUrlException) {
throw (InvalidRedirectUrlException) cause;
} else if (cause instanceof ChipGatewayDataError) {
throw (ChipGatewayDataError) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new RuntimeException("Unexpected exception raised by HTTP message sending thread.", cause);
}
} catch (InterruptedException ex) {
LOG.debug("Sending HTTP message interrupted.");
task.cancel(true);
// force new connection because this one may be unfinished and thus unusable
try {
conn.shutdown();
} catch (IOException ignore) {
}
throw new ThreadTerminateException("Interrupt received while sending HTTP message.");
}
}
use of org.openecard.addons.cg.ex.ConnectionError in project open-ecard by ecsec.
the class ChipGateway method sendMessage.
private <T> T sendMessage(String resource, String msg, Class<T> resClass, boolean tryAgain) throws ConnectionError, InvalidRedirectUrlException, ChipGatewayDataError {
try {
// open initial connection
if (conn == null || !canReuse || (!conn.isOpen() && canReuse)) {
openHttpStream();
}
// prepare request
BasicHttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("POST", resource);
HttpRequestHelper.setDefaultHeader(req, tlsHandler.getServerAddress());
req.setHeader("Accept", "application/json");
ContentType reqContentType = ContentType.create("application/json", "UTF-8");
if (LOG_HTTP_MESSAGES) {
HttpUtils.dumpHttpRequest(LOG, "before adding content", req);
}
StringEntity reqMsg = new StringEntity(msg, reqContentType);
req.setEntity(reqMsg);
req.setHeader(reqMsg.getContentType());
req.setHeader("Content-Length", Long.toString(reqMsg.getContentLength()));
if (LOG_HTTP_MESSAGES) {
LOG.debug(msg);
}
// send request and receive response
LOG.debug("Sending HTTP request.");
HttpResponse response = httpExecutor.execute(req, conn, httpCtx);
canReuse = reuseStrategy.keepAlive(response, httpCtx);
LOG.debug("HTTP response received.");
int statusCode = response.getStatusLine().getStatusCode();
checkHTTPStatusCode(statusCode);
conn.receiveResponseEntity(response);
HttpEntity entity = response.getEntity();
byte[] entityData = FileUtils.toByteArray(entity.getContent());
if (LOG_HTTP_MESSAGES) {
HttpUtils.dumpHttpResponse(LOG, response, entityData);
}
// convert entity and return it
T resultObj = parseResultObj(entityData, resClass);
return resultObj;
} catch (IOException ex) {
if (!Thread.currentThread().isInterrupted() && tryAgain) {
String errorMsg = "ChipGateway server closed the connection. Trying to connect again.";
if (LOG.isDebugEnabled()) {
LOG.debug(errorMsg, ex);
} else {
LOG.info(errorMsg);
}
canReuse = false;
return sendMessage(resource, msg, resClass, false);
} else {
throw new ConnectionError(token.finalizeErrorAddress(ResultMinor.COMMUNICATION_ERROR), CONNECTION_OPEN_FAILED, ex);
}
} catch (HttpException ex) {
throw new ConnectionError(token.finalizeErrorAddress(ResultMinor.SERVER_ERROR), HTTP_ERROR, ex);
}
}
use of org.openecard.addons.cg.ex.ConnectionError in project open-ecard by ecsec.
the class ChipGateway method openHttpStream.
private void openHttpStream() throws ConnectionError, InvalidRedirectUrlException {
try {
LOG.debug("Opening connection to ChipGateway server.");
TlsClientProtocol handler = tlsHandler.createTlsConnection();
conn = new StreamHttpClientConnection(handler.getInputStream(), handler.getOutputStream());
LOG.debug("Connection to ChipGateway server established.");
} catch (IOException | URISyntaxException ex) {
throw new ConnectionError(token.finalizeErrorAddress(ResultMinor.COMMUNICATION_ERROR), CONNECTION_OPEN_FAILED, ex);
}
}
Aggregations