Search in sources :

Example 1 with ConnectionError

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.");
    }
}
Also used : InvalidRedirectUrlException(org.openecard.addons.cg.ex.InvalidRedirectUrlException) ConnectionError(org.openecard.addons.cg.ex.ConnectionError) ChipGatewayDataError(org.openecard.addons.cg.ex.ChipGatewayDataError) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RemotePinException(org.openecard.addons.cg.ex.RemotePinException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) HttpException(org.openecard.apache.http.HttpException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvalidRedirectUrlException(org.openecard.addons.cg.ex.InvalidRedirectUrlException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) JoseException(org.jose4j.lang.JoseException) AuthServerException(org.openecard.addons.cg.ex.AuthServerException) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CertificateException(java.security.cert.CertificateException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) ThreadTerminateException(org.openecard.common.ThreadTerminateException)

Example 2 with ConnectionError

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);
    }
}
Also used : StringEntity(org.openecard.apache.http.entity.StringEntity) ContentType(org.openecard.apache.http.entity.ContentType) HttpEntity(org.openecard.apache.http.HttpEntity) ConnectionError(org.openecard.addons.cg.ex.ConnectionError) HttpResponse(org.openecard.apache.http.HttpResponse) HttpException(org.openecard.apache.http.HttpException) IOException(java.io.IOException) BasicHttpEntityEnclosingRequest(org.openecard.apache.http.message.BasicHttpEntityEnclosingRequest)

Example 3 with ConnectionError

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);
    }
}
Also used : TlsClientProtocol(org.openecard.bouncycastle.tls.TlsClientProtocol) ConnectionError(org.openecard.addons.cg.ex.ConnectionError) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) StreamHttpClientConnection(org.openecard.transport.httpcore.StreamHttpClientConnection)

Aggregations

IOException (java.io.IOException)3 ConnectionError (org.openecard.addons.cg.ex.ConnectionError)3 URISyntaxException (java.net.URISyntaxException)2 HttpException (org.openecard.apache.http.HttpException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MalformedURLException (java.net.MalformedURLException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1 TimeoutException (java.util.concurrent.TimeoutException)1 JoseException (org.jose4j.lang.JoseException)1 AuthServerException (org.openecard.addons.cg.ex.AuthServerException)1 ChipGatewayDataError (org.openecard.addons.cg.ex.ChipGatewayDataError)1 InvalidRedirectUrlException (org.openecard.addons.cg.ex.InvalidRedirectUrlException)1 RemotePinException (org.openecard.addons.cg.ex.RemotePinException)1 HttpEntity (org.openecard.apache.http.HttpEntity)1 HttpResponse (org.openecard.apache.http.HttpResponse)1 ContentType (org.openecard.apache.http.entity.ContentType)1