Search in sources :

Example 1 with BindingResult

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

the class WaitForChangeAction method execute.

@Override
public BindingResult execute(RequestBody body, Map<String, String> parameters, Headers headers, List<Attachment> attachments) {
    BindingResult response;
    try {
        WaitForChangeRequest statusRequest = WaitForChangeRequest.convert(parameters);
        StatusChange status = eventHandler.next(statusRequest.getSessionIdentifier());
        response = new WaitForChangeResponse(status);
    } catch (StatusException e) {
        response = new BindingResult(BindingResultCode.WRONG_PARAMETER);
        response.setResultMessage(e.getMessage());
    } catch (Exception e) {
        response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
        LOG.error(e.getMessage(), e);
    }
    return response;
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult) StatusChange(org.openecard.ws.schema.StatusChange)

Example 2 with BindingResult

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

the class ActivateAction method execute.

@Override
public BindingResult execute(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> attachments) {
    BindingResult response;
    try {
        if (SEMAPHORE.tryAcquire()) {
            response = checkRequestParameters(body, params, headers, attachments);
        } else {
            response = new BindingResult(BindingResultCode.RESOURCE_LOCKED);
            response.setResultMessage("An authentication process is already running.");
        }
    } finally {
        SEMAPHORE.release();
        // in some cases an error does not lead to a removal of the dynamic context so remove it here
        DynamicContext.remove();
    }
    return response;
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult)

Example 3 with BindingResult

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

the class ActivateAction method processTcToken.

/**
 * Process the tcTokenURL or the activation object and perform a authentication.
 *
 * @param params Parameters of the request.
 * @return A {@link BindingResult} representing the result of the authentication.
 */
private BindingResult processTcToken(Map<String, String> params) {
    BindingResult response;
    DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
    dynCtx.put(TR03112Keys.COOKIE_MANAGER, new CookieManager());
    try {
        TCTokenRequest tcTokenRequest = null;
        try {
            tcTokenRequest = TCTokenRequest.convert(params, ctx);
            response = tokenHandler.handleActivate(tcTokenRequest);
            // Show success message. If we get here we have a valid StartPAOSResponse and a valid refreshURL
            showFinishMessage((TCTokenResponse) response);
        } catch (ActivationError ex) {
            if (ex instanceof NonGuiException) {
            // error already displayed to the user so do not repeat it here
            } else {
                if (ex.getMessage().equals("Invalid HTTP message received.")) {
                    showErrorMessage(lang.translationForKey(ACTIVATION_INVALID_REFRESH_ADDRESS));
                } else {
                    showErrorMessage(ex.getLocalizedMessage());
                }
            }
            LOG.error(ex.getMessage());
            // stack trace only in debug level
            LOG.debug(ex.getMessage(), ex);
            LOG.debug("Returning result: \n{}", ex.getBindingResult());
            if (ex instanceof FatalActivationError) {
                LOG.info("Authentication failed, displaying error in Browser.");
            } else {
                LOG.info("Authentication failed, redirecting to with errors attached to the URL.");
            }
            response = ex.getBindingResult();
        } finally {
            if (tcTokenRequest != null && tcTokenRequest.getTokenContext() != null) {
                // close connection to tctoken server in case PAOS didn't already perform this action
                tcTokenRequest.getTokenContext().closeStream();
            }
        }
    } catch (RuntimeException e) {
        response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
        LOG.error(e.getMessage(), e);
    }
    return response;
}
Also used : FatalActivationError(org.openecard.binding.tctoken.ex.FatalActivationError) BindingResult(org.openecard.addon.bind.BindingResult) TCTokenRequest(org.openecard.binding.tctoken.TCTokenRequest) NonGuiException(org.openecard.binding.tctoken.ex.NonGuiException) CookieManager(org.openecard.transport.httpcore.cookies.CookieManager) DynamicContext(org.openecard.common.DynamicContext) FatalActivationError(org.openecard.binding.tctoken.ex.FatalActivationError) ActivationError(org.openecard.binding.tctoken.ex.ActivationError)

Example 4 with BindingResult

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

the class ActivationController method activate.

/**
 * Performs an activation according to BSI TR-03124-1, but does not perform the return to web session part.
 * A result containing the outcome of the
 *
 * @param url
 * @return
 */
public ActivationResult activate(String url) {
    // create request uri and extract query strings
    URI requestURI = URI.create(url);
    String path = requestURI.getPath();
    // remove leading '/'
    String resourceName = path.substring(1, path.length());
    // find suitable addon
    String failureMessage;
    AddonManager manager = sctx.getManager();
    AddonSelector selector = new AddonSelector(manager);
    try {
        if (manager == null || selector == null) {
            throw new IllegalStateException("Addon initialization failed.");
        } else {
            AppPluginAction action = selector.getAppPluginAction(resourceName);
            String rawQuery = requestURI.getRawQuery();
            Map<String, String> queries = new HashMap<>(0);
            if (rawQuery != null) {
                queries = HttpRequestLineUtils.transform(rawQuery);
            }
            BindingResult result = action.execute(null, queries, null, null);
            return createActivationResult(result);
        }
    } catch (AddonNotFoundException ex) {
        failureMessage = ex.getMessage();
        LOG.info("Addon not found.", ex);
    } catch (UnsupportedEncodingException ex) {
        failureMessage = "Unsupported encoding.";
        LOG.warn(failureMessage, ex);
    } catch (Exception ex) {
        failureMessage = ex.getMessage();
        LOG.warn(ex.getMessage(), ex);
    }
    LOG.info("Returning error as INTERRUPTED result.");
    return new ActivationResult(INTERRUPTED, failureMessage);
}
Also used : AddonSelector(org.openecard.addon.AddonSelector) BindingResult(org.openecard.addon.bind.BindingResult) HashMap(java.util.HashMap) AddonNotFoundException(org.openecard.addon.AddonNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AppPluginAction(org.openecard.addon.bind.AppPluginAction) URI(java.net.URI) AddonNotFoundException(org.openecard.addon.AddonNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AddonManager(org.openecard.addon.AddonManager)

Example 5 with BindingResult

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

the class HttpAppPluginActionHandler method handle.

@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext context) throws HttpException, IOException {
    LOG.debug("HTTP request: {}", httpRequest.toString());
    CORSFilter corsFilter = new CORSFilter();
    HttpResponse corsResp = corsFilter.preProcess(httpRequest, context);
    if (corsResp != null) {
        // CORS Response created, return it to the caller
        // This is either a preflight response, or a block, because the Origin mismatched
        LOG.debug("HTTP response: {}", corsResp);
        Http11Response.copyHttpResponse(corsResp, httpResponse);
        return;
    }
    // deconstruct request uri
    String uri = httpRequest.getRequestLine().getUri();
    URI requestURI = URI.create(uri);
    String path = requestURI.getPath();
    // remove leading '/'
    String resourceName = path.substring(1, path.length());
    // find suitable addon
    try {
        AppPluginAction action = selector.getAppPluginAction(resourceName);
        String rawQuery = requestURI.getRawQuery();
        Map<String, String> queries = new HashMap<>(0);
        if (rawQuery != null) {
            queries = HttpRequestLineUtils.transform(rawQuery);
        }
        RequestBody body = null;
        if (httpRequest instanceof HttpEntityEnclosingRequest) {
            LOG.debug("Request contains an entity.");
            body = getRequestBody(httpRequest, resourceName);
        }
        Headers headers = readReqHeaders(httpRequest);
        // and add some special values to the header section
        headers.setHeader(METHOD_HDR, httpRequest.getRequestLine().getMethod());
        BindingResult bindingResult = action.execute(body, queries, headers, null);
        HttpResponse response = createHTTPResponseFromBindingResult(bindingResult);
        response.setParams(httpRequest.getParams());
        LOG.debug("HTTP response: {}", response);
        Http11Response.copyHttpResponse(response, httpResponse);
        // CORS post processing
        corsFilter.postProcess(httpRequest, httpResponse, context);
    } catch (AddonNotFoundException ex) {
        if (path.equals("/")) {
            new IndexHandler().handle(httpRequest, httpResponse, context);
        } else if (path.startsWith("/")) {
            new FileHandler(new DocumentRoot("/www", "/www-files")).handle(httpRequest, httpResponse, context);
        } else {
            new DefaultHandler().handle(httpRequest, httpResponse, context);
        }
    }
}
Also used : BindingResult(org.openecard.addon.bind.BindingResult) HashMap(java.util.HashMap) AddonNotFoundException(org.openecard.addon.AddonNotFoundException) Headers(org.openecard.addon.bind.Headers) DocumentRoot(org.openecard.control.binding.http.common.DocumentRoot) HttpResponse(org.openecard.apache.http.HttpResponse) AppPluginAction(org.openecard.addon.bind.AppPluginAction) URI(java.net.URI) HttpEntityEnclosingRequest(org.openecard.apache.http.HttpEntityEnclosingRequest) RequestBody(org.openecard.addon.bind.RequestBody)

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