Search in sources :

Example 1 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsTETrailerHeader.

@Test
public void testConvertFromFieldsTETrailerHeader() throws Exception {
    final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("te", "trailers"));
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    final HttpRequest request = converter.convert(headers);
    Assertions.assertNotNull(request);
    Assertions.assertEquals("GET", request.getMethod());
    Assertions.assertEquals("http", request.getScheme());
    Assertions.assertEquals(new URIAuthority("www.example.com"), request.getAuthority());
    Assertions.assertEquals("/", request.getPath());
    final Header[] allHeaders = request.getHeaders();
    Assertions.assertEquals(1, allHeaders.length);
    Assertions.assertEquals("te", allHeaders[0].getName());
    Assertions.assertEquals("trailers", allHeaders[0].getValue());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 2 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsBasic.

@Test
public void testConvertFromFieldsBasic() throws Exception {
    final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom123", "value"));
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    final HttpRequest request = converter.convert(headers);
    Assertions.assertNotNull(request);
    Assertions.assertEquals("GET", request.getMethod());
    Assertions.assertEquals("http", request.getScheme());
    Assertions.assertEquals(new URIAuthority("www.example.com"), request.getAuthority());
    Assertions.assertEquals("/", request.getPath());
    final Header[] allHeaders = request.getHeaders();
    Assertions.assertEquals(1, allHeaders.length);
    Assertions.assertEquals("custom123", allHeaders[0].getName());
    Assertions.assertEquals("value", allHeaders[0].getValue());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 3 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class ClassicTestClient method execute.

public ClassicHttpResponse execute(final HttpHost targetHost, final ClassicHttpRequest request, final HttpContext context) throws HttpException, IOException {
    final HttpRequester requester = this.requesterRef.get();
    if (requester == null) {
        throw new IllegalStateException("Requester has not been started");
    }
    if (request.getAuthority() == null) {
        request.setAuthority(new URIAuthority(targetHost));
    }
    request.setScheme(targetHost.getSchemeName());
    return requester.execute(targetHost, request, socketConfig.getSoTimeout(), context);
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) HttpRequester(org.apache.hc.core5.http.impl.bootstrap.HttpRequester)

Example 4 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class H2MultiplexingRequester method execute.

private void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final CancellableDependency cancellableDependency, final Timeout timeout, final HttpContext context) {
    Args.notNull(exchangeHandler, "Exchange handler");
    Args.notNull(timeout, "Timeout");
    Args.notNull(context, "Context");
    try {
        exchangeHandler.produceRequest((request, entityDetails, httpContext) -> {
            final String scheme = request.getScheme();
            final URIAuthority authority = request.getAuthority();
            if (authority == null) {
                throw new ProtocolException("Request authority not specified");
            }
            final HttpHost target = new HttpHost(scheme, authority);
            connPool.getSession(target, timeout, new FutureCallback<IOSession>() {

                @Override
                public void completed(final IOSession ioSession) {
                    ioSession.enqueue(new RequestExecutionCommand(new AsyncClientExchangeHandler() {

                        @Override
                        public void releaseResources() {
                            exchangeHandler.releaseResources();
                        }

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

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

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

                        @Override
                        public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeInformation(response, httpContext);
                        }

                        @Override
                        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeResponse(response, entityDetails, httpContext);
                        }

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

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

                        @Override
                        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                            exchangeHandler.streamEnd(trailers);
                        }

                        @Override
                        public void cancel() {
                            exchangeHandler.cancel();
                        }

                        @Override
                        public void failed(final Exception cause) {
                            exchangeHandler.failed(cause);
                        }
                    }, pushHandlerFactory, cancellableDependency, context), Command.Priority.NORMAL);
                    if (!ioSession.isOpen()) {
                        exchangeHandler.failed(new ConnectionClosedException());
                    }
                }

                @Override
                public void failed(final Exception ex) {
                    exchangeHandler.failed(ex);
                }

                @Override
                public void cancelled() {
                    exchangeHandler.cancel();
                }
            });
        }, context);
    } catch (final IOException | HttpException ex) {
        exchangeHandler.failed(ex);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) URIAuthority(org.apache.hc.core5.net.URIAuthority) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) IOException(java.io.IOException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) HttpHost(org.apache.hc.core5.http.HttpHost) EntityDetails(org.apache.hc.core5.http.EntityDetails) IOSession(org.apache.hc.core5.reactor.IOSession) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Example 5 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class ClassicRequestBuilderTest method builder.

@Test
public void builder() {
    final Header header = new BasicHeader("header2", "blah");
    final ClassicHttpRequest classicHttpRequest = ClassicRequestBuilder.get().setVersion(HttpVersion.HTTP_1_1).setCharset(StandardCharsets.US_ASCII).setAuthority(new URIAuthority("host")).setEntity("<html><body><h1>Access denied</h1></body></html>", ContentType.TEXT_HTML).setHeader(new BasicHeader("header2", "blah")).setHeader("X-Test-Filter", "active").setHeader(header).setPath("path/").setScheme("http").addHeader(header).addHeader("header", ".addHeader(header)").addParameter(new BasicHeader("header2", "blah")).addParameter("param1", "value1").addParameters(new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null)).setAbsoluteRequestUri(true).setEntity(new StringEntity("requestBody")).setEntity(new ByteArrayEntity(new byte[10240], ContentType.TEXT_PLAIN)).setEntity(new byte[10240], ContentType.TEXT_HTML).setEntity("requestBody").setUri("theUri").setHttpHost(new HttpHost("httpbin.org")).build();
    assertAll("Should return address of Oracle's headquarter", () -> assertNotNull(classicHttpRequest.getEntity()), () -> assertEquals(Method.GET.name(), classicHttpRequest.getMethod()), () -> assertEquals("http", classicHttpRequest.getScheme()), () -> assertEquals("httpbin.org", classicHttpRequest.getAuthority().getHostName()), () -> assertEquals(HttpVersion.HTTP_1_1, classicHttpRequest.getVersion()), () -> assertEquals(4, classicHttpRequest.getHeaders().length), () -> assertNotNull(ClassicRequestBuilder.get().toString()), () -> assertEquals("http://httpbin.org/theUri?header2=blah&param1=value1&param3=value3&param4", new String(classicHttpRequest.getRequestUri().getBytes())));
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) HttpHost(org.apache.hc.core5.http.HttpHost) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Aggregations

URIAuthority (org.apache.hc.core5.net.URIAuthority)41 Test (org.junit.jupiter.api.Test)26 HttpRequest (org.apache.hc.core5.http.HttpRequest)19 URI (java.net.URI)14 Header (org.apache.hc.core5.http.Header)11 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)9 HttpHost (org.apache.hc.core5.http.HttpHost)8 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)6 IOException (java.io.IOException)5 HttpException (org.apache.hc.core5.http.HttpException)5 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)5 HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)4 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 ProtocolException (org.apache.hc.core5.http.ProtocolException)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 URISyntaxException (java.net.URISyntaxException)3 ByteBuffer (java.nio.ByteBuffer)3 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)3 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)3