Search in sources :

Example 1 with TCToken

use of org.openecard.addons.cg.tctoken.TCToken in project open-ecard by ecsec.

the class ActivateCGAction method execute.

@Override
public BindingResult execute(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> att) {
    BindingResult response;
    boolean aquired = false;
    try {
        checkMethod(headers);
        final TCToken token = TCToken.generateToken(params);
        Runnable cgAction = new Runnable() {

            @Override
            public void run() {
                try {
                    tokenHandler.handleNoCardActivate(token);
                    // run a full GC to free some heap memory
                    System.gc();
                    System.runFinalization();
                    System.gc();
                } catch (ThreadTerminateException ex) {
                    LOG.debug("Activation task terminated by an interrupt.", ex);
                } catch (RuntimeException ex) {
                    LOG.error("Unhandled exception in activation process.", ex);
                } finally {
                    currentTaskThread = null;
                    // in some cases an error does not lead to a removal of the dynamic context so remove it here
                    DynamicContext.remove();
                }
            }
        };
        // guard thread creation
        MUTEX.acquire();
        aquired = true;
        Thread t = currentTaskThread;
        if (t != null) {
            if (token.isForceProcessing()) {
                LOG.info("Stopping already running ChipGateway Protocol instance.");
                t.interrupt();
                // wait for other task to complete
                t.join();
            } else {
                LOG.info("Another ChipGateway Protocol instance is already running, return status=busy.");
                response = new BindingResult(BindingResultCode.REDIRECT);
                response.getAuxResultData().put(AuxDataKeys.REDIRECT_LOCATION, token.finalizeBusyAddress());
                return response;
            }
        }
        // perform ChipGateway Protocol in background thread, so that we can return directly
        currentTaskThread = new Thread(cgAction);
        currentTaskThread.setDaemon(true);
        currentTaskThread.setName("ChipGateway-Activation-" + THREAD_NUM.getAndIncrement());
        currentTaskThread.start();
        // create redirect
        response = new BindingResult(BindingResultCode.REDIRECT);
        response.getAuxResultData().put(AuxDataKeys.REDIRECT_LOCATION, token.finalizeOkAddress());
    } catch (WrongMethodException ex) {
        LOG.warn(ex.getMessage());
        response = new BindingResult(BindingResultCode.WRONG_PARAMETER);
        response.setResultMessage(ex.getMessage());
    } catch (NoMethodException ex) {
        LOG.error("No method given in headers, maybe wrong binging.", ex);
        response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
        response.setResultMessage(ex.getMessage());
    } catch (InvalidRedirectUrlException | InvalidTCTokenElement ex) {
        LOG.error("Failed to create TCToken.", ex);
        response = ex.getBindingResult();
    } catch (InterruptedException ex) {
        LOG.info("ChipGateway activation interrupted.");
        response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
        response.setResultMessage(ex.getMessage());
    } finally {
        if (aquired) {
            MUTEX.release();
        }
    }
    return response;
}
Also used : InvalidRedirectUrlException(org.openecard.addons.cg.ex.InvalidRedirectUrlException) BindingResult(org.openecard.addon.bind.BindingResult) InvalidTCTokenElement(org.openecard.addons.cg.ex.InvalidTCTokenElement) TCToken(org.openecard.addons.cg.tctoken.TCToken) ThreadTerminateException(org.openecard.common.ThreadTerminateException)

Aggregations

BindingResult (org.openecard.addon.bind.BindingResult)1 InvalidRedirectUrlException (org.openecard.addons.cg.ex.InvalidRedirectUrlException)1 InvalidTCTokenElement (org.openecard.addons.cg.ex.InvalidTCTokenElement)1 TCToken (org.openecard.addons.cg.tctoken.TCToken)1 ThreadTerminateException (org.openecard.common.ThreadTerminateException)1