Example usage for org.apache.http.impl DefaultBHttpServerConnection receiveRequestEntity

List of usage examples for org.apache.http.impl DefaultBHttpServerConnection receiveRequestEntity

Introduction

In this page you can find the example usage for org.apache.http.impl DefaultBHttpServerConnection receiveRequestEntity.

Prototype

public void receiveRequestEntity(HttpEntityEnclosingRequest httpEntityEnclosingRequest)
            throws HttpException, IOException 

Source Link

Usage

From source file:jscover.server.PersistentStaticHttpServer.java

public void run() {
    DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
    try {/* ww w  .java 2 s  .  c  om*/
        conn.bind(socket);
        try {
            boolean keepAlive = true;
            while (keepAlive && !socket.isClosed()) {
                // fully read the request, whatever it is
                HttpRequest request = conn.receiveRequestHeader();
                logger.log(FINE, "Received request: {0}", request);
                keepAlive = isKeepAlive(request);

                if (request instanceof HttpEntityEnclosingRequest) {
                    conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                    if (entity != null) {
                        // consume all content to allow reuse
                        EntityUtils.consume(entity);
                    }
                }

                // send static content or reject the method
                String method = request.getRequestLine().getMethod();
                if (method.matches("(?i)get|post|put"))
                    sendOkContent(conn);
                else
                    rejectMethod(conn);
            }
        } finally {
            IOUtils.closeQuietly(conn);
            IOUtils.closeQuietly(socket);
        }
    } catch (HttpException e) {
        e.printStackTrace();
        IOUtils.closeQuietly(socket);
    } catch (IOException e) {
        e.printStackTrace();
        IOUtils.closeQuietly(socket);
    }
}

From source file:com.lion328.xenonlauncher.proxy.HttpDataHandler.java

@Override
public boolean process(Socket client, Socket server) throws Exception {
    InputStream clientIn = client.getInputStream();
    clientIn.mark(65536);//  w  ww. j av  a2s.co m

    try {
        DefaultBHttpServerConnection httpClient = new DefaultBHttpServerConnection(8192);
        httpClient.bind(client);
        httpClient.setSocketTimeout(timeout);

        DefaultBHttpClientConnection httpServer = new DefaultBHttpClientConnection(8192);
        httpServer.bind(server);

        HttpCoreContext context = HttpCoreContext.create();
        context.setAttribute("client.socket", client);
        context.setAttribute("server.socket", server);

        HttpEntityEnclosingRequest request;

        do {
            HttpRequest rawRequest = httpClient.receiveRequestHeader();

            if (rawRequest instanceof HttpEntityEnclosingRequest) {
                request = (HttpEntityEnclosingRequest) rawRequest;
            } else {
                request = new BasicHttpEntityEnclosingRequest(rawRequest.getRequestLine());
                request.setHeaders(rawRequest.getAllHeaders());
            }

            httpClient.receiveRequestEntity(request);

            HttpResponse response = new BasicHttpResponse(
                    new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));

            boolean sent = false;

            for (Map.Entry<Integer, HttpRequestHandler> entry : handlers.entrySet()) {
                entry.getValue().handle(request, response, context);

                if (context.getAttribute("response.set") instanceof HttpResponse) {
                    response = (HttpResponse) context.getAttribute("response.set");
                }

                if (context.getAttribute("pipeline.end") == Boolean.TRUE) {
                    break;
                }

                if (context.getAttribute("response.need-original") == Boolean.TRUE && !sent) {
                    httpServer.sendRequestHeader(request);
                    httpServer.sendRequestEntity(request);
                    response = httpServer.receiveResponseHeader();
                    httpServer.receiveResponseEntity(response);

                    entry.getValue().handle(request, response, context);

                    context.removeAttribute("response.need-original");
                    context.setAttribute("request.sent", true);

                    sent = true;
                }
            }

            if (context.getAttribute("response.sent") != Boolean.TRUE) {
                httpClient.sendResponseHeader(response);

                if (response.getEntity() != null) {
                    httpClient.sendResponseEntity(response);
                }
            }
        } while (request.getFirstHeader("Connection").getValue().equals("keep-alive"));

        return true;
    } catch (ProtocolException e) {
        clientIn.reset();
        return false;
    } catch (ConnectionClosedException e) {
        return true;
    }
}