Search in sources :

Example 6 with Header

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

the class Http11Response method copyHttpResponse.

/**
 * Copy the content of a HttpResponse to another instance.
 *
 * @param in HttpResponse
 * @param out HttpResponse
 */
public static void copyHttpResponse(HttpResponse in, HttpResponse out) {
    // remove and copy headers
    HeaderIterator headIt = out.headerIterator();
    while (headIt.hasNext()) {
        headIt.nextHeader();
        headIt.remove();
    }
    headIt = in.headerIterator();
    while (headIt.hasNext()) {
        Header next = headIt.nextHeader();
        out.addHeader(next);
    }
    // set entity stuff
    if (in.getEntity() != null) {
        HttpEntity entity = in.getEntity();
        out.setEntity(entity);
        if (entity.getContentType() != null) {
            out.setHeader(entity.getContentType());
        }
        if (entity.getContentEncoding() != null) {
            out.setHeader(entity.getContentEncoding());
        }
        if (entity.getContentLength() > 0) {
            out.setHeader(HeaderTypes.CONTENT_LENGTH.fieldName(), Long.toString(entity.getContentLength()));
        }
    // TODO: use chunked, repeatable and streaming attribute from entity
    }
    // copy rest
    Locale l = in.getLocale();
    if (l != null) {
        out.setLocale(l);
    }
    out.setStatusLine(in.getStatusLine());
}
Also used : Locale(java.util.Locale) Header(org.openecard.apache.http.Header) HttpEntity(org.openecard.apache.http.HttpEntity) HeaderIterator(org.openecard.apache.http.HeaderIterator)

Example 7 with Header

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

the class StreamHttpClientConnectionTest method consumeEntity.

private void consumeEntity(StreamHttpClientConnection conn, String hostName, int numIt) throws IOException, HttpException {
    HttpContext ctx = new BasicHttpContext();
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    HttpResponse response = null;
    DefaultConnectionReuseStrategy reuse = new DefaultConnectionReuseStrategy();
    int i = 0;
    while (i == 0 || (i < numIt && reuse.keepAlive(response, ctx))) {
        i++;
        // send request and receive response
        HttpRequest request = new BasicHttpRequest("GET", "/");
        HttpRequestHelper.setDefaultHeader(request, hostName);
        response = httpexecutor.execute(request, conn, ctx);
        conn.receiveResponseEntity(response);
        HttpEntity entity = response.getEntity();
        assertNotNull(entity);
        // consume entity
        byte[] content = FileUtils.toByteArray(entity.getContent());
        // read header and check if content size is correct
        Header lengthHeader = response.getFirstHeader("Content-Length");
        long length = Long.parseLong(lengthHeader.getValue());
        assertNotNull(lengthHeader);
        assertEquals(entity.getContentLength(), length);
        assertEquals(content.length, length);
        // consume everything from the entity and close stream
        EntityUtils.consume(entity);
    }
}
Also used : HttpRequest(org.openecard.apache.http.HttpRequest) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest) HttpRequestExecutor(org.openecard.apache.http.protocol.HttpRequestExecutor) HttpEntity(org.openecard.apache.http.HttpEntity) Header(org.openecard.apache.http.Header) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) HttpContext(org.openecard.apache.http.protocol.HttpContext) DefaultConnectionReuseStrategy(org.openecard.apache.http.impl.DefaultConnectionReuseStrategy) HttpResponse(org.openecard.apache.http.HttpResponse) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest)

Example 8 with Header

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

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

the class ErrorResponseInterceptor method process.

@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
    StatusLine statusLine = httpResponse.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (errorCodes.contains(statusCode)) {
        LOG.debug("HTTP response intercepted");
        Header contentType = httpResponse.getFirstHeader(HeaderTypes.CONTENT_TYPE.fieldName());
        if (contentType != null) {
            // Intercept response with the content type "text/plain"
            if (contentType.getValue().contains(MimeType.TEXT_PLAIN.getMimeType())) {
                // Remove old headers
                httpResponse.removeHeaders(HeaderTypes.CONTENT_TYPE.fieldName());
                httpResponse.removeHeaders(HeaderTypes.CONTENT_LENGTH.fieldName());
                // Read message body
                String content = readEntity(httpResponse.getEntity());
                // escape string to prevent script content to be injected into the template (XSS)
                content = HTMLUtils.escapeHtml(content);
                template.setProperty("%%%MESSAGE%%%", content);
            }
        } else {
            template.setProperty("%%%MESSAGE%%%", lang.translationForKey("http." + statusCode));
        }
        template.setProperty("%%%TITLE%%%", "Error");
        String reason = statusLine.getReasonPhrase();
        template.setProperty("%%%HEADLINE%%%", reason);
        // Add new content
        httpResponse.setEntity(new StringEntity(template.toString(), "UTF-8"));
        httpResponse.addHeader(HeaderTypes.CONTENT_TYPE.fieldName(), MimeType.TEXT_HTML.getMimeType() + "; charset=utf-8");
        httpResponse.addHeader(HeaderTypes.CONTENT_LENGTH.fieldName(), String.valueOf(template.getBytes().length));
    }
}
Also used : StatusLine(org.openecard.apache.http.StatusLine) StringEntity(org.openecard.apache.http.entity.StringEntity) Header(org.openecard.apache.http.Header)

Aggregations

Header (org.openecard.apache.http.Header)9 HttpEntity (org.openecard.apache.http.HttpEntity)3 StatusLine (org.openecard.apache.http.StatusLine)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HeaderIterator (org.openecard.apache.http.HeaderIterator)2 HttpResponse (org.openecard.apache.http.HttpResponse)2 BasicHttpContext (org.openecard.apache.http.protocol.BasicHttpContext)2 HttpContext (org.openecard.apache.http.protocol.HttpContext)2 HttpRequestExecutor (org.openecard.apache.http.protocol.HttpRequestExecutor)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Socket (java.net.Socket)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Locale (java.util.Locale)1 Nullable (javax.annotation.Nullable)1 Headers (org.openecard.addon.bind.Headers)1 HttpException (org.openecard.apache.http.HttpException)1 HttpRequest (org.openecard.apache.http.HttpRequest)1