Example usage for org.apache.http.impl DefaultBHttpClientConnection receiveResponseEntity

List of usage examples for org.apache.http.impl DefaultBHttpClientConnection receiveResponseEntity

Introduction

In this page you can find the example usage for org.apache.http.impl DefaultBHttpClientConnection receiveResponseEntity.

Prototype

public void receiveResponseEntity(HttpResponse httpResponse) throws HttpException, IOException 

Source Link

Usage

From source file:com.lion328.xenonlauncher.minecraft.api.authentication.yggdrasil.YggdrasilMinecraftAuthenticator.java

private ResponseState sendRequest(String endpoint, String data) throws IOException, YggdrasilAPIException {
    URL url = new URL(serverURL, endpoint);

    // HttpURLConnection can only handle 2xx response code for headers
    // so it need to use HttpCore instead
    // maybe I could use an alternative like HttpClient
    // but for lightweight, I think is not a good idea

    BasicHttpEntity entity = new BasicHttpEntity();

    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
    entity.setContent(new ByteArrayInputStream(dataBytes));
    entity.setContentLength(dataBytes.length);

    HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", url.getFile(),
            HttpVersion.HTTP_1_1);/*w  w w.j  av  a 2 s  .c om*/
    request.setHeader(new BasicHeader("Host", url.getHost()));
    request.setHeader(new BasicHeader("Content-Type", "application/json"));
    request.setHeader(new BasicHeader("Content-Length", Integer.toString(dataBytes.length)));

    request.setEntity(entity);

    Socket s;
    int port = url.getPort();

    if (url.getProtocol().equals("https")) {
        if (port == -1) {
            port = 443;
        }

        s = SSLSocketFactory.getDefault().createSocket(url.getHost(), port);
    } else {
        if (port == -1) {
            port = 80;
        }

        s = new Socket(url.getHost(), port);
    }

    DefaultBHttpClientConnection connection = new DefaultBHttpClientConnection(8192);
    connection.bind(s);

    try {
        connection.sendRequestHeader(request);
        connection.sendRequestEntity(request);

        HttpResponse response = connection.receiveResponseHeader();
        connection.receiveResponseEntity(response);

        if (!response.getFirstHeader("Content-Type").getValue().startsWith("application/json")) {
            throw new InvalidImplementationException("Invalid content type");
        }

        InputStream stream = response.getEntity().getContent();
        StringBuilder sb = new StringBuilder();
        int b;

        while ((b = stream.read()) != -1) {
            sb.append((char) b);
        }

        return new ResponseState(response.getStatusLine().getStatusCode(), sb.toString());
    } catch (HttpException e) {
        throw new IOException(e);
    }
}

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 a  v  a2  s.  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;
    }
}