Search in sources :

Example 1 with AppPluginAction

use of org.openecard.addon.bind.AppPluginAction 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 2 with AppPluginAction

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

Example 3 with AppPluginAction

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

the class AddonManager method getAppPluginAction.

/**
 * Get a specific AppPluginAction.
 *
 * @param addonSpec {@link AddonSpecification} which contains the description of the {@link AppPluginAction}.
 * @param resourceName The {@link AppPluginSpecification#resourceName} to identify the @{@link AppPluginAction} to
 * return.
 * @return A AppPluginAction which corresponds to the {@link AddonSpecification} and the {@code resourceName}. If no
 * such AppPluginAction exists NULL is returned.
 */
public AppPluginAction getAppPluginAction(@Nonnull AddonSpecification addonSpec, @Nonnull String resourceName) {
    AppPluginAction appPluginAction = cache.getAppPluginAction(addonSpec, resourceName);
    if (appPluginAction != null) {
        // AppExtensionAction cached so return it
        return appPluginAction;
    }
    AppPluginSpecification protoSpec = addonSpec.searchByResourceName(resourceName);
    if (protoSpec == null) {
        LOG.error("Plugin for resource {} does not exist in Add-on {}.", resourceName, addonSpec.getId());
    } else {
        String className = protoSpec.getClassName();
        try {
            ClassLoader cl = registry.downloadAddon(addonSpec);
            AppPluginActionProxy protoFactory = new AppPluginActionProxy(className, cl);
            Context aCtx = createContext(addonSpec);
            protoFactory.init(aCtx);
            cache.addAppPluginAction(addonSpec, resourceName, protoFactory);
            return protoFactory;
        } catch (ActionInitializationException e) {
            LOG.error("Initialization of AppPluginAction failed", e);
        } catch (AddonException ex) {
            LOG.error("Failed to download Add-on.", ex);
        }
    }
    return null;
}
Also used : AppPluginSpecification(org.openecard.addon.manifest.AppPluginSpecification) AppPluginActionProxy(org.openecard.addon.bind.AppPluginActionProxy) AppPluginAction(org.openecard.addon.bind.AppPluginAction)

Example 4 with AppPluginAction

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

the class CacheTest method addAppPluginAction.

@Test
public void addAppPluginAction() {
    AppPluginActionProxy proxy = new AppPluginActionProxy(null, null);
    int initialHash = proxy.hashCode();
    AddonSpecification spec = new AddonSpecification();
    String id = "test";
    spec.setId(id);
    spec.setVersion("1.0.0");
    cache.addAppPluginAction(spec, id, proxy);
    // now get it back from the cache
    AppPluginAction proto = cache.getAppPluginAction(spec, id);
    Assert.assertEquals(proto.hashCode(), initialHash);
}
Also used : AppPluginActionProxy(org.openecard.addon.bind.AppPluginActionProxy) AddonSpecification(org.openecard.addon.manifest.AddonSpecification) AppPluginAction(org.openecard.addon.bind.AppPluginAction) Test(org.testng.annotations.Test)

Example 5 with AppPluginAction

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

the class CacheTest method removeMulti.

@Test
public void removeMulti() {
    SALProtocolProxy proxy4 = new SALProtocolProxy(null, null);
    AddonSpecification spec4 = new AddonSpecification();
    String id4 = "test4";
    spec4.setId(id4);
    spec4.setVersion("1.0.0");
    cache.addSALProtocol(spec4, id4, proxy4);
    IFDProtocolProxy proxy3 = new IFDProtocolProxy(null, null);
    AddonSpecification spec2 = new AddonSpecification();
    String id1 = "test1";
    spec2.setId(id1);
    spec2.setVersion("1.0.0");
    cache.addIFDProtocol(spec2, id1, proxy3);
    AppPluginActionProxy proxy = new AppPluginActionProxy(null, null);
    AddonSpecification spec1 = new AddonSpecification();
    String id0 = "test2";
    spec1.setId(id0);
    spec1.setVersion("1.0.0");
    cache.addAppPluginAction(spec1, id0, proxy);
    AppExtensionActionProxy proxy2 = new AppExtensionActionProxy(null, null);
    AddonSpecification spec = new AddonSpecification();
    String id = "test";
    spec.setId(id);
    spec.setVersion("1.0.0");
    cache.addAppExtensionAction(spec, id, proxy2);
    cache.removeCacheEntry(spec, id);
    cache.removeCacheEntry(spec1, id0);
    // now get it back from the cache
    AppExtensionAction proto = cache.getAppExtensionAction(spec, id);
    AppPluginAction action = cache.getAppPluginAction(spec1, id0);
    Assert.assertNull(proto);
    Assert.assertNull(action);
}
Also used : AppExtensionAction(org.openecard.addon.bind.AppExtensionAction) AppPluginActionProxy(org.openecard.addon.bind.AppPluginActionProxy) IFDProtocolProxy(org.openecard.addon.ifd.IFDProtocolProxy) SALProtocolProxy(org.openecard.addon.sal.SALProtocolProxy) AddonSpecification(org.openecard.addon.manifest.AddonSpecification) AppExtensionActionProxy(org.openecard.addon.bind.AppExtensionActionProxy) AppPluginAction(org.openecard.addon.bind.AppPluginAction) Test(org.testng.annotations.Test)

Aggregations

AppPluginAction (org.openecard.addon.bind.AppPluginAction)6 AppPluginActionProxy (org.openecard.addon.bind.AppPluginActionProxy)4 AddonSpecification (org.openecard.addon.manifest.AddonSpecification)3 Test (org.testng.annotations.Test)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)2 BindingResult (org.openecard.addon.bind.BindingResult)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AddonManager (org.openecard.addon.AddonManager)1 AddonSelector (org.openecard.addon.AddonSelector)1 AppExtensionAction (org.openecard.addon.bind.AppExtensionAction)1 AppExtensionActionProxy (org.openecard.addon.bind.AppExtensionActionProxy)1 Headers (org.openecard.addon.bind.Headers)1 RequestBody (org.openecard.addon.bind.RequestBody)1 IFDProtocolProxy (org.openecard.addon.ifd.IFDProtocolProxy)1 AppPluginSpecification (org.openecard.addon.manifest.AppPluginSpecification)1 SALProtocolProxy (org.openecard.addon.sal.SALProtocolProxy)1 HttpEntityEnclosingRequest (org.openecard.apache.http.HttpEntityEnclosingRequest)1 HttpResponse (org.openecard.apache.http.HttpResponse)1