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

The following examples show how to use reactor.netty.http.server.HttpServer#protocol() . 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: 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]);
}