Search in sources :

Example 6 with RequestLine

use of org.apache.hc.core5.http.message.RequestLine in project httpcomponents-core by apache.

the class TestBasicLineFormatter method testRLFormattingInvalidInput.

@Test
public void testRLFormattingInvalidInput() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    final RequestLine requestline = new RequestLine(Method.GET.name(), "/stuff", HttpVersion.HTTP_1_1);
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatRequestLine(null, requestline));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatRequestLine(buf, null));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with RequestLine

use of org.apache.hc.core5.http.message.RequestLine in project httpcomponents-core by apache.

the class DefaultHttpRequestWriter method writeHeadLine.

@Override
protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException {
    lineBuf.clear();
    final ProtocolVersion transportVersion = message.getVersion();
    getLineFormatter().formatRequestLine(lineBuf, new RequestLine(message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1));
}
Also used : RequestLine(org.apache.hc.core5.http.message.RequestLine) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 8 with RequestLine

use of org.apache.hc.core5.http.message.RequestLine in project httpcomponents-core by apache.

the class DefaultHttpRequestParser method createMessage.

@Override
protected ClassicHttpRequest createMessage(final CharArrayBuffer buffer) throws IOException, HttpException {
    final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
    final ClassicHttpRequest request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
    request.setVersion(requestLine.getProtocolVersion());
    return request;
}
Also used : RequestLine(org.apache.hc.core5.http.message.RequestLine) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest)

Example 9 with RequestLine

use of org.apache.hc.core5.http.message.RequestLine in project httpcomponents-core by apache.

the class DefaultHttpRequestWriter method writeHeadLine.

@Override
protected void writeHeadLine(final ClassicHttpRequest message, final CharArrayBuffer lineBuf) throws IOException {
    final ProtocolVersion transportVersion = message.getVersion();
    getLineFormatter().formatRequestLine(lineBuf, new RequestLine(message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1));
}
Also used : RequestLine(org.apache.hc.core5.http.message.RequestLine) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 10 with RequestLine

use of org.apache.hc.core5.http.message.RequestLine in project httpcomponents-core by apache.

the class AsyncFullDuplexClientExample method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(5, TimeUnit.SECONDS).build();
    // Create and start requester
    // Disable 'Expect: Continue' handshake some servers cannot handle well
    final HttpAsyncRequester requester = AsyncRequesterBootstrap.bootstrap().setIOReactorConfig(ioReactorConfig).setHttpProcessor(HttpProcessors.customClient(null).addLast((HttpRequestInterceptor) (request, entity, context) -> request.removeHeaders(HttpHeaders.EXPECT)).build()).setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
            System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
            System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            if (keepAlive) {
                System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
            } else {
                System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
            }
        }
    }).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.out.println("HTTP requester shutting down");
        requester.close(CloseMode.GRACEFUL);
    }));
    requester.start();
    final URI requestUri = new URI("http://httpbin.org/post");
    final AsyncRequestProducer requestProducer = AsyncRequestBuilder.post(requestUri).setEntity("stuff").build();
    final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new StringAsyncEntityConsumer());
    final CountDownLatch latch = new CountDownLatch(1);
    requester.execute(new AsyncClientExchangeHandler() {

        @Override
        public void releaseResources() {
            requestProducer.releaseResources();
            responseConsumer.releaseResources();
            latch.countDown();
        }

        @Override
        public void cancel() {
            System.out.println(requestUri + " cancelled");
        }

        @Override
        public void failed(final Exception cause) {
            System.out.println(requestUri + "->" + cause);
        }

        @Override
        public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException {
            requestProducer.sendRequest(channel, httpContext);
        }

        @Override
        public int available() {
            return requestProducer.available();
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            requestProducer.produce(channel);
        }

        @Override
        public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
            System.out.println(requestUri + "->" + response.getCode());
        }

        @Override
        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
            System.out.println(requestUri + "->" + response.getCode());
            responseConsumer.consumeResponse(response, entityDetails, httpContext, null);
        }

        @Override
        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
            responseConsumer.updateCapacity(capacityChannel);
        }

        @Override
        public void consume(final ByteBuffer src) throws IOException {
            responseConsumer.consume(src);
        }

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            responseConsumer.streamEnd(trailers);
        }
    }, Timeout.ofSeconds(30), HttpCoreContext.create());
    latch.await(1, TimeUnit.MINUTES);
    System.out.println("Shutting down I/O reactor");
    requester.initiateShutdown();
}
Also used : HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) AsyncRequesterBootstrap(org.apache.hc.core5.http.impl.bootstrap.AsyncRequesterBootstrap) RequestLine(org.apache.hc.core5.http.message.RequestLine) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) ByteBuffer(java.nio.ByteBuffer) EntityDetails(org.apache.hc.core5.http.EntityDetails) StatusLine(org.apache.hc.core5.http.message.StatusLine) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpProcessors(org.apache.hc.core5.http.impl.HttpProcessors) CloseMode(org.apache.hc.core5.io.CloseMode) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel) HttpResponse(org.apache.hc.core5.http.HttpResponse) URI(java.net.URI) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) HttpException(org.apache.hc.core5.http.HttpException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Header(org.apache.hc.core5.http.Header) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) IOException(java.io.IOException) Timeout(org.apache.hc.core5.util.Timeout) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeaders(org.apache.hc.core5.http.HttpHeaders) List(java.util.List) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpConnection(org.apache.hc.core5.http.HttpConnection) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) HttpConnection(org.apache.hc.core5.http.HttpConnection) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) URI(java.net.URI) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) StatusLine(org.apache.hc.core5.http.message.StatusLine) RequestLine(org.apache.hc.core5.http.message.RequestLine) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Aggregations

RequestLine (org.apache.hc.core5.http.message.RequestLine)16 HttpConnection (org.apache.hc.core5.http.HttpConnection)9 HttpRequest (org.apache.hc.core5.http.HttpRequest)9 HttpResponse (org.apache.hc.core5.http.HttpResponse)9 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)9 StatusLine (org.apache.hc.core5.http.message.StatusLine)9 Header (org.apache.hc.core5.http.Header)7 HttpHost (org.apache.hc.core5.http.HttpHost)5 HttpAsyncRequester (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)4 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)4 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)3 HttpException (org.apache.hc.core5.http.HttpException)3 Message (org.apache.hc.core5.http.Message)3 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)3 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2