Search in sources :

Example 1 with HttpEntityEnclosingRequest

use of org.openecard.apache.http.HttpEntityEnclosingRequest 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 2 with HttpEntityEnclosingRequest

use of org.openecard.apache.http.HttpEntityEnclosingRequest in project open-ecard by ecsec.

the class HttpAppPluginActionHandler method getRequestBody.

private RequestBody getRequestBody(HttpRequest httpRequest, String resourceName) throws IOException {
    try {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpRequest;
        HttpEntity entity = entityRequest.getEntity();
        InputStream is = entity.getContent();
        // TODO: This assumes the content is UTF-8. Evaluate what is actually sent.
        String value = FileUtils.toString(is);
        String mimeType = ContentType.get(entity).getMimeType();
        // TODO: find out if we have a Base64 coded value
        boolean base64Content = false;
        RequestBody body = new RequestBody(resourceName, null);
        body.setValue(value, mimeType, base64Content);
        return body;
    } catch (UnsupportedCharsetException | ParseException e) {
        LOG.error("Failed to create request body.", e);
    }
    return null;
}
Also used : HttpEntity(org.openecard.apache.http.HttpEntity) InputStream(java.io.InputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) HttpEntityEnclosingRequest(org.openecard.apache.http.HttpEntityEnclosingRequest) ParseException(org.openecard.apache.http.ParseException) RequestBody(org.openecard.addon.bind.RequestBody)

Aggregations

RequestBody (org.openecard.addon.bind.RequestBody)2 HttpEntityEnclosingRequest (org.openecard.apache.http.HttpEntityEnclosingRequest)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 HashMap (java.util.HashMap)1 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)1 AppPluginAction (org.openecard.addon.bind.AppPluginAction)1 BindingResult (org.openecard.addon.bind.BindingResult)1 Headers (org.openecard.addon.bind.Headers)1 HttpEntity (org.openecard.apache.http.HttpEntity)1 HttpResponse (org.openecard.apache.http.HttpResponse)1 ParseException (org.openecard.apache.http.ParseException)1 DocumentRoot (org.openecard.control.binding.http.common.DocumentRoot)1