Search in sources :

Example 1 with RequestLine

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

the class HttpUtils method dumpHttpRequest.

/**
 * Dump the given HTTP request and log it with the given logger instance.
 * An optional message can be given wich will be printed in the head of the log entry to define the context of the
 * message. The request message is not modified by this method.
 *
 * @param logger Logger to dump HTTP request to.
 * @param msg Message qualifying the context of the request.
 * @param req Request to dump.
 */
public static void dumpHttpRequest(@Nonnull Logger logger, @Nullable String msg, @Nonnull HttpRequest req) {
    if (logger.isDebugEnabled()) {
        StringWriter w = new StringWriter();
        PrintWriter pw = new PrintWriter(w);
        pw.print("HTTP Request");
        if (msg != null) {
            pw.format(" (%s)", msg);
        }
        pw.println(":");
        RequestLine rl = req.getRequestLine();
        pw.format("  %s %s %s%n", rl.getMethod(), rl.getUri(), rl.getProtocolVersion().toString());
        for (Header h : req.getAllHeaders()) {
            pw.format("  %s: %s%n", h.getName(), h.getValue());
        }
        pw.flush();
        logger.debug(w.toString());
    }
}
Also used : RequestLine(org.openecard.apache.http.RequestLine) StringWriter(java.io.StringWriter) Header(org.openecard.apache.http.Header) PrintWriter(java.io.PrintWriter)

Example 2 with RequestLine

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

the class FileHandler method handle.

@Override
public HttpResponse handle(HttpRequest httpRequest) throws HttpException, Exception {
    // Return 404 Not Found in the default case
    Http11Response httpResponse = new Http11Response(HttpStatus.SC_NOT_FOUND);
    RequestLine requestLine = httpRequest.getRequestLine();
    if (requestLine.getMethod().equals("GET")) {
        URI requestURI = URI.create(requestLine.getUri());
        URL filePath = documentRoot.getFile(URLDecoder.decode(requestURI.getPath(), "UTF-8"));
        if (filePath != null) {
            // Handle file
            _logger.debug("Handle file request");
            handleFile(httpResponse, filePath);
        } else {
            _logger.debug("The DocumentRoot does not contain the URI: {}", requestURI.getPath());
        }
    } else {
        // Return 405 Method Not Allowed
        httpResponse.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED);
    }
    return httpResponse;
}
Also used : RequestLine(org.openecard.apache.http.RequestLine) URI(java.net.URI) URL(java.net.URL) Http11Response(org.openecard.control.binding.http.common.Http11Response)

Aggregations

RequestLine (org.openecard.apache.http.RequestLine)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 URL (java.net.URL)1 Header (org.openecard.apache.http.Header)1 Http11Response (org.openecard.control.binding.http.common.Http11Response)1