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);
}
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);
}
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));
}
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));
}
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;
}
Aggregations