Search in sources :

Example 6 with BindingResult

use of org.openecard.addon.bind.BindingResult 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)

Example 7 with BindingResult

use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.

the class StatusAction method execute.

@Override
public BindingResult execute(RequestBody body, Map<String, String> parameters, Headers headers, List<Attachment> attachments) {
    BindingResult response;
    try {
        StatusRequest statusRequest = StatusRequest.convert(parameters);
        response = statusHandler.handleRequest(statusRequest);
    } catch (Exception e) {
        response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
        LOG.error(e.getMessage(), e);
    }
    return response;
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult)

Example 8 with BindingResult

use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.

the class ActivateAction method processRequest.

/**
 * Process the request.
 *
 * @param body Body of the request.
 * @param params Query parameters of the request.
 * @param attachments Attachments of the request.
 * @param tokenUrl {@code TRUE} if {@code params} contains a TCTokenURL.
 * @param showUI {@code TRUE} if {@code params} contains a ShowUI parameter.
 * @param status {@code TRUE} if {@code params} contains a Status parameter.
 * @return A {@link BindingResult} representing the result of the request processing.
 */
private BindingResult processRequest(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> attachments, boolean tokenUrl, boolean showUI, boolean status) {
    BindingResult response = null;
    if (tokenUrl) {
        response = processTcToken(params);
        return response;
    }
    if (status) {
        response = processStatus(body, params, headers, attachments);
        return response;
    }
    if (showUI) {
        String requestedUI = params.get("ShowUI");
        response = processShowUI(requestedUI);
        return response;
    }
    return response;
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult)

Example 9 with BindingResult

use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.

the class ActivateAction method checkRequestParameters.

/**
 * Check the request for correct parameters and invoke their processing if they are ok.
 *
 * @param body The body of the request.
 * @param params The query parameters and their values.
 * @param attachments Attachments of the request.
 * @return A {@link BindingResult} with an error if the parameters are not correct or one depending on the processing
 * of the parameters.
 */
private BindingResult checkRequestParameters(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> attachments) {
    BindingResult response;
    boolean emptyParms, tokenUrl, status, showUI;
    emptyParms = tokenUrl = status = showUI = false;
    if (params.isEmpty()) {
        emptyParms = true;
    }
    if (params.containsKey("tcTokenURL")) {
        tokenUrl = true;
    }
    if (params.containsKey("Status")) {
        status = true;
    }
    if (params.containsKey("ShowUI")) {
        showUI = true;
    }
    // only continue, when there are known parameters in the request
    if (emptyParms || !(tokenUrl || status || showUI)) {
        response = new BindingResult(BindingResultCode.MISSING_PARAMETER);
        response.setResultMessage(lang.translationForKey(NO_ACTIVATION_PARAMETERS));
        showErrorMessage(lang.translationForKey(NO_ACTIVATION_PARAMETERS));
        return response;
    }
    // check illegal parameter combination
    if ((tokenUrl && showUI) || (tokenUrl && status) || (showUI && status)) {
        response = new BindingResult(BindingResultCode.WRONG_PARAMETER);
        response.setResultMessage(lang.translationForKey(NO_PARAMS));
        showErrorMessage(lang.translationForKey(NO_PARAMS));
        return response;
    }
    return processRequest(body, params, headers, attachments, tokenUrl, showUI, status);
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult)

Aggregations

BindingResult (org.openecard.addon.bind.BindingResult)9 URI (java.net.URI)2 HashMap (java.util.HashMap)2 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)2 AppPluginAction (org.openecard.addon.bind.AppPluginAction)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AddonManager (org.openecard.addon.AddonManager)1 AddonSelector (org.openecard.addon.AddonSelector)1 Headers (org.openecard.addon.bind.Headers)1 RequestBody (org.openecard.addon.bind.RequestBody)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 HttpEntityEnclosingRequest (org.openecard.apache.http.HttpEntityEnclosingRequest)1 HttpResponse (org.openecard.apache.http.HttpResponse)1 TCTokenRequest (org.openecard.binding.tctoken.TCTokenRequest)1 ActivationError (org.openecard.binding.tctoken.ex.ActivationError)1 FatalActivationError (org.openecard.binding.tctoken.ex.FatalActivationError)1 NonGuiException (org.openecard.binding.tctoken.ex.NonGuiException)1 DynamicContext (org.openecard.common.DynamicContext)1