Search in sources :

Example 86 with BasicHttpRequest

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

the class HttpRequestWrapperTest method testRequestWithRelativeURI.

@Test
public void testRequestWithRelativeURI() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("/stuff"));
    final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
    Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
    Assertions.assertEquals("/stuff", httpRequestWrapper.getPath());
    Assertions.assertNull(httpRequestWrapper.getAuthority());
    Assertions.assertEquals(new URI("/stuff"), httpRequestWrapper.getUri());
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 87 with BasicHttpRequest

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

the class HttpRequestWrapperTest method testRequestWithAbsoluteURI.

@Test
public void testRequestWithAbsoluteURI() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("https://host:9443/stuff?param=value"));
    final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
    Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
    Assertions.assertEquals("/stuff?param=value", httpRequestWrapper.getPath());
    Assertions.assertEquals(new URIAuthority("host", 9443), httpRequestWrapper.getAuthority());
    Assertions.assertEquals("https", httpRequestWrapper.getScheme());
    Assertions.assertEquals(new URI("https://host:9443/stuff?param=value"), httpRequestWrapper.getUri());
    httpRequestWrapper.setScheme((URIScheme.HTTP.id));
    Assertions.assertEquals("http", httpRequestWrapper.getScheme());
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 88 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project mercury by yellow013.

the class AsyncClientH2FullDuplexExchange method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.FORCE_HTTP_2, H2Config.DEFAULT, null, ioReactorConfig);
    client.start();
    final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
    final BasicRequestProducer requestProducer = new BasicRequestProducer(request, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
    final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new StringAsyncEntityConsumer());
    System.out.println("Executing request " + request);
    final CountDownLatch latch = new CountDownLatch(1);
    client.execute(new AsyncClientExchangeHandler() {

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

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

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

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

        @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 context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
        }

        @Override
        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
            responseConsumer.consumeResponse(response, entityDetails, context, 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);
        }
    });
    latch.await(1, TimeUnit.MINUTES);
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) MinimalHttpAsyncClient(org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) 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) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Example 89 with BasicHttpRequest

use of org.apache.hc.core5.http.message.BasicHttpRequest in project skywalking-java by apache.

the class IOSessionImplPollInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Command command = (Command) ret;
    if (!(command instanceof RequestExecutionCommand)) {
        return ret;
    }
    HttpContext httpContext = ((RequestExecutionCommand) command).getContext();
    ContextSnapshot snapshot = (ContextSnapshot) httpContext.getAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
    if (snapshot == null) {
        return ret;
    }
    httpContext.removeAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
    AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local");
    localSpan.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    localSpan.setLayer(SpanLayer.HTTP);
    ContextManager.continued(snapshot);
    final ContextCarrier contextCarrier = new ContextCarrier();
    BasicHttpRequest request = (BasicHttpRequest) httpContext.getAttribute(HttpClientContext.HTTP_REQUEST);
    URI uri = request.getUri();
    String operationName = uri.getPath();
    int port = uri.getPort();
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, uri.getHost() + ":" + (port == -1 ? 80 : port));
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    Tags.URL.set(span, uri.toURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        request.setHeader(next.getHeadKey(), next.getHeadValue());
    }
    return ret;
}
Also used : ContextSnapshot(org.apache.skywalking.apm.agent.core.context.ContextSnapshot) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Command(org.apache.hc.core5.reactor.Command) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

HttpRequest (org.apache.hc.core5.http.HttpRequest)75 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)68 Test (org.junit.jupiter.api.Test)45 HttpResponse (org.apache.hc.core5.http.HttpResponse)41 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)37 Message (org.apache.hc.core5.http.Message)34 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)33 InetSocketAddress (java.net.InetSocketAddress)30 HttpHost (org.apache.hc.core5.http.HttpHost)30 Test (org.junit.Test)30 URI (java.net.URI)23 HttpException (org.apache.hc.core5.http.HttpException)21 IOException (java.io.IOException)20 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)19 URIAuthority (org.apache.hc.core5.net.URIAuthority)17 InterruptedIOException (java.io.InterruptedIOException)16 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)15 Header (org.apache.hc.core5.http.Header)14 URISyntaxException (java.net.URISyntaxException)8 ContentType (org.apache.hc.core5.http.ContentType)8