Java Code Examples for reactor.netty.http.server.HttpServer#secure()

The following examples show how to use reactor.netty.http.server.HttpServer#secure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelloWorldServer.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	HttpServer server =
			HttpServer.create()
			          .port(PORT)
			          .wiretap(WIRETAP)
			          .compress(COMPRESS)
			          .route(r -> r.get("/hello",
			                  (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
			                                   .sendString(Mono.just("Hello World!"))));

	if (SECURE) {
		SelfSignedCertificate ssc = new SelfSignedCertificate();
		server = server.secure(
				spec -> spec.sslContext(SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())));
	}

	if (HTTP2) {
		server = server.protocol(HttpProtocol.H2);
	}

	server.bindNow()
	      .onDispose()
	      .block();
}
 
Example 2
Source File: EchoServer.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	HttpServer server =
			HttpServer.create()
			          .port(PORT)
			          .wiretap(WIRETAP)
			          .compress(COMPRESS)
			          .route(r -> r.post("/echo",
			                  (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
			                                   .send(req.receive().retain())));

	if (SECURE) {
		SelfSignedCertificate ssc = new SelfSignedCertificate();
		server = server.secure(
				spec -> spec.sslContext(SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())));
	}

	if (HTTP2) {
		server = server.protocol(HttpProtocol.H2);
	}

	server.bindNow()
	      .onDispose()
	      .block();
}
 
Example 3
Source File: HttpsMetricsHandlerTests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpServer customizeServerOptions(HttpServer server) {
	try {
		SslContext ctx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
		                                  .sslProvider(SslProvider.JDK)
		                                  .build();
		return server.secure(ssl -> ssl.sslContext(ctx));
	}
	catch (SSLException e) {
		throw new RuntimeException(e);
	}
}
 
Example 4
Source File: ByteBufFluxTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
private void doTestByteBufFluxFromPath(boolean withSecurity) throws Exception {
	final int serverPort = SocketUtils.findAvailableTcpPort();
	HttpServer server = HttpServer.create()
	                              .port(serverPort)
	                              .wiretap(true);
	HttpClient client = HttpClient.create()
	                              .port(serverPort)
	                              .wiretap(true);
	if (withSecurity) {
		SelfSignedCertificate ssc = new SelfSignedCertificate();
		SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
		SslContext sslClient = SslContextBuilder.forClient()
		                                        .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
		server = server.secure(ssl -> ssl.sslContext(sslServer));
		client = client.secure(ssl -> ssl.sslContext(sslClient));
	}

	Path path = Paths.get(getClass().getResource("/largeFile.txt").toURI());
	DisposableServer c = server.handle((req, res) ->
	                                      res.send(ByteBufFlux.fromPath(path))
	                                         .then())
	                           .bindNow();

	AtomicLong counter = new AtomicLong(0);
	client.get()
	      .uri("/download")
	      .responseContent()
	      .doOnNext(b -> counter.addAndGet(b.readableBytes()))
	      .blockLast(Duration.ofSeconds(30));

	assertEquals(1245, counter.get());

	c.disposeNow();
}
 
Example 5
Source File: HttpProtocolsTests.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@Parameters(name = "{index}: {0}, {1}")
public static Object[][] data() throws Exception {
	SelfSignedCertificate cert = new SelfSignedCertificate();
	SslContextBuilder serverCtx = SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
	SslContextBuilder clientCtx = SslContextBuilder.forClient()
	                                               .trustManager(InsecureTrustManagerFactory.INSTANCE);

	HttpServer _server = HttpServer.create()
	                               .wiretap(true)
	                               .httpRequestDecoder(spec -> spec.h2cMaxContentLength(256));
	HttpServer securedServer = _server.secure(spec -> spec.sslContext(serverCtx));

	HttpServer[] servers = new HttpServer[]{
			_server, // by default protocol is HTTP/1.1
			_server.protocol(HttpProtocol.H2C),
			_server.protocol(HttpProtocol.HTTP11, HttpProtocol.H2C),
			securedServer, // by default protocol is HTTP/1.1
			securedServer.protocol(HttpProtocol.H2),
			securedServer.protocol(HttpProtocol.HTTP11, HttpProtocol.H2)
	};

	HttpClient _client = HttpClient.create()
	                               .wiretap(true);
	HttpClient securedClient = _client.secure(spec -> spec.sslContext(clientCtx));

	HttpClient[] clients = new HttpClient[]{
			_client, // by default protocol is HTTP/1.1
			_client.protocol(HttpProtocol.H2C),
			_client.protocol(HttpProtocol.HTTP11, HttpProtocol.H2C),
			securedClient, // by default protocol is HTTP/1.1
			securedClient.protocol(HttpProtocol.H2),
			securedClient.protocol(HttpProtocol.HTTP11, HttpProtocol.H2)
	};

	Flux<HttpServer> f1 = Flux.fromArray(servers).concatMap(o -> Flux.just(o).repeat(clients.length - 1));
	Flux<HttpClient> f2 = Flux.fromArray(clients).repeat(servers.length - 1);

	return Flux.zip(f1, f2)
	           .map(Tuple2::toArray)
	           .collectList()
	           .block(Duration.ofSeconds(30))
	           .toArray(new Object[servers.length * clients.length][2]);
}
 
Example 6
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
private void doTestIssue719(Publisher<ByteBuf> clientSend,
		Consumer<HttpHeaders> clientSendHeaders, boolean ssl) throws Exception {
	HttpServer server =
			HttpServer.create()
			          .port(0)
			          .wiretap(true)
			          .handle((req, res) -> req.receive()
			                                   .then(res.sendString(Mono.just("test"))
			                                            .then()));

	if (ssl) {
		SelfSignedCertificate cert = new SelfSignedCertificate();
		server = server.secure(spec -> spec.sslContext(
				SslContextBuilder.forServer(cert.certificate(), cert.privateKey())));
	}

	disposableServer = server.bindNow();

	HttpClient client = createHttpClientForContextWithAddress();
	if (ssl) {
		client = client.secure(spec ->
				spec.sslContext(SslContextBuilder.forClient()
				                                 .trustManager(InsecureTrustManagerFactory.INSTANCE)));
	}

	StepVerifier.create(
			client.headers(clientSendHeaders)
			      .post()
			      .uri("/")
			      .send(clientSend)
			      .responseContent()
			      .aggregate()
			      .asString())
	            .expectNext("test")
	            .expectComplete()
	            .verify(Duration.ofSeconds(30));

	StepVerifier.create(
			client.headers(clientSendHeaders)
			      .post()
			      .uri("/")
			      .send(clientSend)
			      .responseContent()
			      .aggregate()
			      .asString())
	            .expectNext("test")
	            .expectComplete()
	            .verify(Duration.ofSeconds(30));
}
 
Example 7
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
private void doTestConcurrentStreams(HttpClient baseClient, boolean isSecured,
		HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols) throws Exception {
	SelfSignedCertificate cert = new SelfSignedCertificate();
	SslContextBuilder serverCtx = SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
	SslContextBuilder clientCtx = SslContextBuilder.forClient()
	                                               .trustManager(InsecureTrustManagerFactory.INSTANCE);

	HttpServer httpServer =
			HttpServer.create()
			          .port(0)
			          .protocol(serverProtocols).wiretap(true)
			          .handle((req, res) -> res.sendString(Mono.just("test")));
	if (isSecured) {
		httpServer = httpServer.secure(spec -> spec.sslContext(serverCtx));
	}

	disposableServer = httpServer.bindNow();

	HttpClient client;
	if (isSecured) {
		client = baseClient.port(disposableServer.port()).wiretap(true)
		                   .protocol(clientProtocols)
		                   .secure(spec -> spec.sslContext(clientCtx));
	}
	else {
		client = baseClient.port(disposableServer.port())
		                   .protocol(clientProtocols);
	}

	CountDownLatch latch = new CountDownLatch(1);
	List<String> responses =
			Flux.range(0, 10)
			    .flatMapDelayError(i ->
			        client.get()
			              .uri("/")
			              .responseContent()
			              .aggregate()
			              .asString(),
			        256, 32)
			        .collectList()
			        .doFinally(fin -> latch.countDown())
			        .block(Duration.ofSeconds(30));

	assertThat(responses).isNotNull();
	assertThat(responses.size()).isEqualTo(10);
}