Search in sources :

Example 16 with BasicClassicHttpResponse

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

the class ClassicServerFilterExample method main.

public static void main(final String[] args) throws Exception {
    int port = 8080;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
    final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setSocketConfig(socketConfig).replaceFilter(StandardFilter.EXPECT_CONTINUE.name(), new AbstractHttpServerAuthFilter<String>(false) {

        @Override
        protected String parseChallengeResponse(final String authorizationValue, final HttpContext context) throws HttpException {
            return authorizationValue;
        }

        @Override
        protected boolean authenticate(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
            return "let me pass".equals(challengeResponse);
        }

        @Override
        protected String generateChallenge(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
            return "who goes there?";
        }
    }).addFilterFirst("my-filter", (request, responseTrigger, context, chain) -> {
        if (request.getRequestUri().equals("/back-door")) {
            final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
            response.setEntity(new StringEntity("Welcome", ContentType.TEXT_PLAIN));
            responseTrigger.submitResponse(response);
        } else {
            chain.proceed(request, new HttpFilterChain.ResponseTrigger() {

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

                @Override
                public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
                    response.addHeader("X-Filter", "My-Filter");
                    responseTrigger.submitResponse(response);
                }
            }, context);
        }
    }).register("*", (request, response, context) -> {
        // do something useful
        response.setCode(HttpStatus.SC_OK);
        response.setEntity(new StringEntity("Hello"));
    }).create();
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> server.close(CloseMode.GRACEFUL)));
    System.out.println("Listening on port " + port);
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) TimeValue(org.apache.hc.core5.util.TimeValue) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) IOException(java.io.IOException) SocketConfig(org.apache.hc.core5.http.io.SocketConfig) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) TimeUnit(java.util.concurrent.TimeUnit) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) AbstractHttpServerAuthFilter(org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter) URIAuthority(org.apache.hc.core5.net.URIAuthority) ServerBootstrap(org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap) ContentType(org.apache.hc.core5.http.ContentType) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpFilterChain(org.apache.hc.core5.http.io.HttpFilterChain) CloseMode(org.apache.hc.core5.io.CloseMode) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) HttpStatus(org.apache.hc.core5.http.HttpStatus) StandardFilter(org.apache.hc.core5.http.impl.bootstrap.StandardFilter) URIAuthority(org.apache.hc.core5.net.URIAuthority) SocketConfig(org.apache.hc.core5.http.io.SocketConfig) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) IOException(java.io.IOException) AbstractHttpServerAuthFilter(org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) HttpException(org.apache.hc.core5.http.HttpException) HttpFilterChain(org.apache.hc.core5.http.io.HttpFilterChain) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 17 with BasicClassicHttpResponse

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

the class TestDefaultBHttpServerConnection method testWriteResponseHead.

@Test
public void testWriteResponseHead() throws Exception {
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Mockito.when(socket.getOutputStream()).thenReturn(outStream);
    conn.bind(socket);
    Assertions.assertEquals(0, conn.getEndpointDetails().getResponseCount());
    final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
    response.addHeader("User-Agent", "test");
    conn.sendResponseHeader(response);
    conn.flush();
    Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
    final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n", s);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 18 with BasicClassicHttpResponse

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

the class TestBHttpConnectionBase method testCreateEntityInputChunked.

@Test
public void testCreateEntityInputChunked() throws Exception {
    final InputStream inStream = Mockito.mock(InputStream.class);
    final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
    final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.CHUNKED);
    Assertions.assertNotNull(entity);
    Assertions.assertTrue(entity.isChunked());
    Assertions.assertEquals(-1, entity.getContentLength());
    final InputStream content = entity.getContent();
    Assertions.assertNotNull(content);
    Assertions.assertTrue((content instanceof ChunkedInputStream));
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 19 with BasicClassicHttpResponse

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

the class TestBHttpConnectionBase method testCreateEntityLengthDelimited.

@Test
public void testCreateEntityLengthDelimited() throws Exception {
    final InputStream inStream = Mockito.mock(InputStream.class);
    final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
    message.addHeader("Content-Length", "10");
    message.addHeader("Content-Type", "stuff");
    message.addHeader("Content-Encoding", "chunked");
    final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, 10);
    Assertions.assertNotNull(entity);
    Assertions.assertFalse(entity.isChunked());
    Assertions.assertEquals(10, entity.getContentLength());
    Assertions.assertEquals("stuff", entity.getContentType());
    Assertions.assertEquals("chunked", entity.getContentEncoding());
    final InputStream content = entity.getContent();
    Assertions.assertNotNull(content);
    Assertions.assertTrue((content instanceof ContentLengthInputStream));
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 20 with BasicClassicHttpResponse

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

the class ClassicResponseBuilder method build.

@Override
public ClassicHttpResponse build() {
    final BasicClassicHttpResponse result = new BasicClassicHttpResponse(getStatus());
    result.setVersion(getVersion());
    result.setHeaders(getHeaders());
    result.setEntity(entity);
    return result;
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Aggregations

BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)69 Test (org.junit.jupiter.api.Test)57 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)56 Header (org.apache.hc.core5.http.Header)34 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)19 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)14 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpEntity (org.apache.hc.core5.http.HttpEntity)12 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)11 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)10 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)10 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)9 TimeValue (org.apache.hc.core5.util.TimeValue)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 Timeout (org.apache.hc.core5.util.Timeout)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3