Search in sources :

Example 11 with BasicHttpResponse

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

the class TestBasicMessages method testSetResponseStatus.

@Test
public void testSetResponseStatus() {
    final HttpResponse response1 = new BasicHttpResponse(200, "OK");
    Assertions.assertNotNull(response1.getCode());
    Assertions.assertEquals(200, response1.getCode());
    final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
    Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, response2.getCode());
    final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
    Assertions.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response3.getCode());
    Assertions.assertEquals("whatever", response3.getReasonPhrase());
    final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
    Assertions.assertThrows(IllegalArgumentException.class, () -> response4.setCode(-23));
}
Also used : HttpResponse(org.apache.hc.core5.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 12 with BasicHttpResponse

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

the class TestBasicMessageBuilders method testResponseBasics.

@Test
public void testResponseBasics() throws Exception {
    final BasicResponseBuilder builder = BasicResponseBuilder.create(200);
    Assertions.assertEquals(200, builder.getStatus());
    Assertions.assertNull(builder.getHeaders());
    Assertions.assertNull(builder.getVersion());
    final BasicHttpResponse r1 = builder.build();
    Assertions.assertNotNull(r1);
    Assertions.assertEquals(200, r1.getCode());
    Assertions.assertNull(r1.getVersion());
    builder.setStatus(500);
    builder.setVersion(HttpVersion.HTTP_1_0);
    Assertions.assertEquals(500, builder.getStatus());
    Assertions.assertEquals(HttpVersion.HTTP_1_0, builder.getVersion());
    final BasicHttpResponse r2 = builder.build();
    Assertions.assertEquals(500, r2.getCode());
    Assertions.assertEquals(HttpVersion.HTTP_1_0, r2.getVersion());
    builder.addHeader("h1", "v1");
    builder.addHeader("h1", "v2");
    builder.addHeader("h2", "v2");
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(builder.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(builder.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    final BasicHttpResponse r3 = builder.build();
    assertThat(r3.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(r3.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(r3.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(r3.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    builder.removeHeader(new BasicHeader("h1", "v2"));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    final BasicHttpResponse r4 = builder.build();
    assertThat(r4.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(r4.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    builder.removeHeaders("h1");
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same());
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
    final BasicHttpResponse r5 = builder.build();
    assertThat(r5.getHeaders("h1"), HeadersMatcher.same());
    assertThat(r5.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 13 with BasicHttpResponse

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

the class TestBasicMessageBuilders method testResponseCopy.

@Test
public void testResponseCopy() throws Exception {
    final HttpResponse response = new BasicHttpResponse(400);
    response.addHeader("h1", "v1");
    response.addHeader("h1", "v2");
    response.addHeader("h2", "v2");
    response.setVersion(HttpVersion.HTTP_2);
    final BasicResponseBuilder builder = BasicResponseBuilder.copy(response);
    Assertions.assertEquals(400, builder.getStatus());
    Assertions.assertEquals(HttpVersion.HTTP_2, builder.getVersion());
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 14 with BasicHttpResponse

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

the class AbstractAsyncServerAuthFilter method handle.

@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
    final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
    final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
    final URIAuthority authority = request.getAuthority();
    final String requestUri = request.getRequestUri();
    final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
    final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
    final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
    if (authenticated) {
        if (expectContinue) {
            responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
        }
        return chain.proceed(request, entityDetails, context, responseTrigger);
    }
    final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
    unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
    final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
    if (respondImmediately || expectContinue || entityDetails == null) {
        responseTrigger.submitResponse(unauthorized, responseContentProducer);
        return null;
    }
    return new AsyncDataConsumer() {

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

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

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

        @Override
        public void releaseResources() {
            if (responseContentProducer != null) {
                responseContentProducer.releaseResources();
            }
        }
    };
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) ByteBuffer(java.nio.ByteBuffer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) Header(org.apache.hc.core5.http.Header) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) List(java.util.List) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 15 with BasicHttpResponse

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

the class TerminalAsyncServerFilter method handle.

@Override
public AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
    final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
    if (exchangeHandler != null) {
        exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {

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

            @Override
            public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {

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

                    @Override
                    public boolean isRepeatable() {
                        return false;
                    }

                    @Override
                    public long getContentLength() {
                        return entityDetails.getContentLength();
                    }

                    @Override
                    public String getContentType() {
                        return entityDetails.getContentType();
                    }

                    @Override
                    public String getContentEncoding() {
                        return entityDetails.getContentEncoding();
                    }

                    @Override
                    public boolean isChunked() {
                        return entityDetails.isChunked();
                    }

                    @Override
                    public Set<String> getTrailerNames() {
                        return entityDetails.getTrailerNames();
                    }

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

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

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

            @Override
            public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
                responseTrigger.pushPromise(promise, pushProducer);
            }
        }, context);
        return exchangeHandler;
    }
    responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
    return null;
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) Set(java.util.Set) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Aggregations

BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)60 HttpResponse (org.apache.hc.core5.http.HttpResponse)57 Test (org.junit.jupiter.api.Test)40 HttpRequest (org.apache.hc.core5.http.HttpRequest)15 Header (org.apache.hc.core5.http.Header)14 HttpException (org.apache.hc.core5.http.HttpException)11 IOException (java.io.IOException)10 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)9 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)9 InetSocketAddress (java.net.InetSocketAddress)8 EntityDetails (org.apache.hc.core5.http.EntityDetails)8 ByteBuffer (java.nio.ByteBuffer)7 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)7 Test (org.junit.Test)7 ProtocolException (org.apache.hc.core5.http.ProtocolException)6 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)6 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)6 Message (org.apache.hc.core5.http.Message)5 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)5 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)5