use of org.apache.hc.core5.reactor.ListenerEndpoint in project httpcomponents-core by apache.
the class Http1AuthenticationTest method testGetRequestAuthentication.
@Test
public void testGetRequestAuthentication() throws Exception {
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
final ListenerEndpoint listener = future.get();
final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
requester.start();
final HttpHost target = new HttpHost("localhost", address.getPort());
final HttpRequest request1 = new BasicHttpRequest(Method.GET, target, "/stuff");
final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message1, CoreMatchers.notNullValue());
final HttpResponse response1 = message1.getHead();
assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
final String body1 = message1.getBody();
assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
final HttpRequest request2 = new BasicHttpRequest(Method.GET, target, "/stuff");
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message2, CoreMatchers.notNullValue());
final HttpResponse response2 = message2.getHead();
assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body2 = message2.getBody();
assertThat(body2, CoreMatchers.equalTo(""));
}
use of org.apache.hc.core5.reactor.ListenerEndpoint in project commons-vfs by apache.
the class NHttpFileServer method start.
private NHttpFileServer start() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, InterruptedException, ExecutionException {
final AsyncServerBootstrap bootstrap = AsyncServerBootstrap.bootstrap();
SSLContext sslContext = null;
if (port == 8443 || port == 443) {
// Initialize SSL context
final URL url = NHttpFileServer.class.getResource("/test.keystore");
if (url == null) {
println("Keystore not found");
System.exit(1);
}
println("Loading keystore " + url);
sslContext = SSLContexts.custom().loadKeyMaterial(url, "nopassword".toCharArray(), "nopassword".toCharArray()).build();
bootstrap.setTlsStrategy(new BasicServerTlsStrategy(sslContext, new FixedPortStrategy(port)));
}
// @formatter:off
final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
// @formatter:on
server = bootstrap.setIOReactorConfig(config).register("*", new HttpFileHandler(docRoot)).create();
Runtime.getRuntime().addShutdownHook(new Thread(() -> close()));
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port));
listenerEndpoint = future.get();
println("Serving " + docRoot + " on " + listenerEndpoint.getAddress() + (sslContext == null ? "" : " with " + sslContext.getProvider() + " " + sslContext.getProtocol()));
return this;
}
use of org.apache.hc.core5.reactor.ListenerEndpoint in project californium by eclipse.
the class HttpServer method start.
/**
* Start http server.
*/
public void start() {
if (proxyServerFilter != null && server == null) {
bootstrap.addFilterBefore(StandardFilter.MAIN_HANDLER.name(), "proxy", new AsyncFilterHandler() {
@Override
public AsyncDataConsumer handle(HttpRequest request, EntityDetails entityDetails, HttpContext context, org.apache.hc.core5.http.nio.AsyncFilterChain.ResponseTrigger responseTrigger, AsyncFilterChain chain) throws HttpException, IOException {
try {
URI uri = request.getUri();
if (uri.getScheme() != null && !virtualHosts.contains(uri.getHost())) {
LOGGER.warn("proxy filter {}", uri);
return proxyServerFilter.handle(request, entityDetails, context, responseTrigger, chain);
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
return chain.proceed(request, entityDetails, context, responseTrigger);
}
});
}
server = bootstrap.create();
LOGGER.info("HttpServer listening on {} started.", StringUtil.toLog(httpInterface));
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOGGER.info("HTTP server shutting down");
HttpServer.this.stop();
}
});
server.start();
final Future<ListenerEndpoint> future = server.listen(httpInterface, URIScheme.HTTP);
try {
final ListenerEndpoint listenerEndpoint = future.get();
LOGGER.info("Listening on {}", listenerEndpoint.getAddress());
} catch (InterruptedException ex) {
LOGGER.info("interrupted", ex);
} catch (ExecutionException ex) {
LOGGER.error("unexpected error:", ex);
}
}
use of org.apache.hc.core5.reactor.ListenerEndpoint in project httpcomponents-core by apache.
the class HttpAsyncServer method listen.
/**
* @since 5.1
*/
public Future<ListenerEndpoint> listen(final SocketAddress address, final URIScheme scheme, final Object attachment, final FutureCallback<ListenerEndpoint> callback) {
final InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
final EndpointParameters parameters = new EndpointParameters(scheme.id, canonicalName != null ? canonicalName : "localhost", inetSocketAddress.getPort(), attachment);
return super.listen(address, parameters, callback);
}
use of org.apache.hc.core5.reactor.ListenerEndpoint in project httpcomponents-core by apache.
the class TestDefaultListeningIOReactor method testEndpointUpAndDown.
@Test
public void testEndpointUpAndDown() throws Exception {
ioReactor.start();
Set<ListenerEndpoint> endpoints = ioReactor.getEndpoints();
Assertions.assertNotNull(endpoints);
Assertions.assertEquals(0, endpoints.size());
final Future<ListenerEndpoint> future1 = ioReactor.listen(new InetSocketAddress(0));
final ListenerEndpoint endpoint1 = future1.get();
final Future<ListenerEndpoint> future2 = ioReactor.listen(new InetSocketAddress(0));
final ListenerEndpoint endpoint2 = future2.get();
final int port = ((InetSocketAddress) endpoint2.getAddress()).getPort();
endpoints = ioReactor.getEndpoints();
Assertions.assertNotNull(endpoints);
Assertions.assertEquals(2, endpoints.size());
endpoint1.close();
endpoints = ioReactor.getEndpoints();
Assertions.assertNotNull(endpoints);
Assertions.assertEquals(1, endpoints.size());
final ListenerEndpoint endpoint = endpoints.iterator().next();
Assertions.assertEquals(port, ((InetSocketAddress) endpoint.getAddress()).getPort());
ioReactor.close(CloseMode.GRACEFUL);
ioReactor.awaitShutdown(TimeValue.ofSeconds(5));
Assertions.assertEquals(IOReactorStatus.SHUT_DOWN, ioReactor.getStatus());
}
Aggregations