org.apache.hc.core5.http.ClassicHttpResponse Java Examples

The following examples show how to use org.apache.hc.core5.http.ClassicHttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HijackingHttpRequestExecutor.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
public ClassicHttpResponse execute(
    ClassicHttpRequest request,
    HttpClientConnection conn,
    HttpResponseInformationCallback informationCallback,
    HttpContext context
) throws IOException, HttpException {
    Objects.requireNonNull(request, "HTTP request");
    Objects.requireNonNull(conn, "Client connection");
    Objects.requireNonNull(context, "HTTP context");

    InputStream hijackedInput = (InputStream) context.getAttribute(HIJACKED_INPUT_ATTRIBUTE);
    if (hijackedInput != null) {
        return executeHijacked(request, conn, context, hijackedInput);
    }

    return super.execute(request, conn, informationCallback, context);
}
 
Example #2
Source File: Http5FileObject.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected InputStream doGetInputStream(final int bufferSize) throws Exception {
    final HttpGet getRequest = new HttpGet(getInternalURI());
    final ClassicHttpResponse httpResponse = executeHttpUriRequest(getRequest);
    final int status = httpResponse.getCode();

    if (status == HttpStatus.SC_NOT_FOUND) {
        throw new FileNotFoundException(getName());
    }

    if (status != HttpStatus.SC_OK) {
        throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status));
    }

    return new MonitoredHttpResponseContentInputStream(httpResponse, bufferSize);
}
 
Example #3
Source File: SocketServerHttp.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
protected void doSendMessage(MsgHttp msgHttp) throws Exception
{
    GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL, "SocketServerHttp: doSendMessage() ", msgHttp);

    synchronized(this)
    {
        ClassicHttpResponse response = (ClassicHttpResponse) msgHttp.getMessage();
        defaultHttpServerConnection.sendResponseHeader(response);
        defaultHttpServerConnection.sendResponseEntity(response);
        defaultHttpServerConnection.flush();
    }
}
 
Example #4
Source File: HttpLoaderServer.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public void receiveRequest( HttpRequest request,
        HttpContext context, ClassicHttpResponse response
        )throws HttpException, IOException
{
    String method = request.getMethod().toUpperCase();
    String target = request.getPath();
    sendResponse(response,context,request);
}
 
Example #5
Source File: HttpLoaderServer.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public void sendResponse( ClassicHttpResponse response,HttpContext context, HttpRequest request)throws HttpException, IOException
{

    response.setCode(HttpStatus.SC_OK);
    File file = new File("D:/XMLloader/testPileHttp/test/fileTestHttp.txt");
    //FileEntity body = new FileEntity(file, "text/html");
    HttpEntity body = new StringEntity("");
    response.setEntity(body);
}
 
Example #6
Source File: HttpLoaderServer.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public void handle(final HttpRequest request,
        final ClassicHttpResponse response,
        final HttpContext context)throws HttpException, IOException
{
    receiveRequest(request,context,response);
}
 
Example #7
Source File: HttpLoaderServer.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context)
		throws HttpException, IOException {
	receiveRequest(request,context,response);
	
}
 
Example #8
Source File: NIOSocketClientHttp.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean handle(HybridSocket hybridSocket)
{
    if(false == init) return true;
    
    try
    {
        if(clientConnection.isOpen())
        {
            ClassicHttpResponse response = clientConnection.receiveResponseHeader();
            clientConnection.receiveResponseEntity(response);

            Stack stack = StackFactory.getStack(StackFactory.PROTOCOL_HTTP);
            
            MsgHttp msgResponse = new MsgHttp(stack, response);

            //
            // Get corresponding msgRequest to read transactionId
            //
            if(isValid)
            {
                MsgHttp msgRequest = requestsSent.take();
                msgResponse.setTransactionId(msgRequest.getTransactionId());
                msgResponse.setChannel(this.connHttp);
                msgResponse.setType(msgRequest.getType());
            }

            //
            // Callback vers la Stack generic
            //
            stack.receiveMessage(msgResponse);
        }
        else
        {
            synchronized(this.connHttp)
            {
                restoreConnection();
            }
        }
    }
    catch(Exception e)
    {
        if(requestsSent.isEmpty())
        {
            GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL, e, "Exception in SocketClientHttp without pending messages");
        }
        else
        {
            GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL, e, "Exception in SocketClientHttp with pending messages");
        }
        synchronized(this.connHttp)
        {
            try
            {
                restoreConnection();
            }
            catch(Exception ignore) {}
        }
    }

    return !isShutdown;
}
 
Example #9
Source File: HijackingHttpRequestExecutor.java    From docker-java with Apache License 2.0 4 votes vote down vote up
private ClassicHttpResponse executeHijacked(
    ClassicHttpRequest request,
    HttpClientConnection conn,
    HttpContext context,
    InputStream hijackedInput
) throws HttpException, IOException {
    try {
        context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
        final ProtocolVersion transportVersion = request.getVersion();
        if (transportVersion != null) {
            context.setProtocolVersion(transportVersion);
        }

        conn.sendRequestHeader(request);
        conn.sendRequestEntity(request);
        conn.flush();

        ClassicHttpResponse response = conn.receiveResponseHeader();
        if (response.getCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
            conn.terminateRequest(request);
            throw new ProtocolException("Expected 101 Switching Protocols, got: " + new StatusLine(response));
        }

        Thread thread = new Thread(() -> {
            try {
                BasicClassicHttpRequest fakeRequest = new BasicClassicHttpRequest("POST", "/");
                fakeRequest.setHeader(HttpHeaders.CONTENT_LENGTH, Long.MAX_VALUE);
                fakeRequest.setEntity(new HijackedEntity(hijackedInput));
                conn.sendRequestEntity(fakeRequest);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        thread.setName("docker-java-httpclient5-hijacking-stream-" + System.identityHashCode(request));
        thread.setDaemon(true);
        thread.start();

        // 101 -> 200
        response.setCode(200);
        conn.receiveResponseEntity(response);
        return response;

    } catch (final HttpException | IOException | RuntimeException ex) {
        Closer.closeQuietly(conn);
        throw ex;
    }
}
 
Example #10
Source File: MonitoredHttpResponseContentInputStream.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public MonitoredHttpResponseContentInputStream(final ClassicHttpResponse httpResponse) throws IOException {
    super(httpResponse.getEntity().getContent());
    this.httpResponse = httpResponse;
}
 
Example #11
Source File: MonitoredHttpResponseContentInputStream.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public MonitoredHttpResponseContentInputStream(final ClassicHttpResponse httpResponse, final int bufferSize) throws IOException {
    super(httpResponse.getEntity().getContent(), bufferSize);
    this.httpResponse = httpResponse;
}
 
Example #12
Source File: Http5FileObject.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Execute the request using the given {@code httpRequest} and return a {@code ClassicHttpResponse} from the execution.
 *
 * @param httpRequest {@code HttpUriRequest} object
 * @return {@code ClassicHttpResponse} from the execution
 * @throws IOException if IO error occurs
 */
protected ClassicHttpResponse executeHttpUriRequest(final HttpUriRequest httpRequest) throws IOException {
    final CloseableHttpClient httpClient = (CloseableHttpClient) getAbstractFileSystem().getHttpClient();
    final HttpClientContext httpClientContext = getAbstractFileSystem().getHttpClientContext();
    return httpClient.execute(httpRequest, httpClientContext);
}