reactor.netty.http.server.HttpServer Java Examples
The following examples show how to use
reactor.netty.http.server.HttpServer.
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: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void noSubprotocolSelected() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString( Mono.just("SERVER:" + o.selectedSubprotocol())))) .wiretap(true) .bindNow(); List<String> res = HttpClient.create() .port(httpServer.port()) .headers(h -> h.add("Authorization", auth)) .websocket() .uri("/test") .handle((in, out) -> in.receive() .asString() .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv)) .log() .collectList() .block(Duration.ofSeconds(30)); Assert.assertNotNull(res); Assert.assertThat(res.get(0), is("CLIENT:null-SERVER:null")); }
Example #2
Source File: RSocketNettyReactiveWebServerFactory.java From spring-boot-rsocket with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private RSocketFactory.Start<CloseableChannel> createRSocketStarter(HttpHandler httpHandler) { RSocketFactory.ServerRSocketFactory rSocketFactory = applyCustomizers(RSocketFactory.receive()); HttpServer httpServer = createHttpServer(); ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler); return rSocketFactory .acceptor(socketAcceptor) .transport((ServerTransport) new WebsocketRouteTransport( httpServer, r -> r.route(hsr -> !("/" + hsr.path()).equals(path), handlerAdapter), path )); }
Example #3
Source File: FunctionEndpointInitializer.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Override public void onApplicationEvent(ApplicationEvent event) { ApplicationContext context = ((ContextRefreshedEvent) event).getApplicationContext(); if (context != this.context) { return; } if (!ClassUtils.isPresent("org.springframework.http.server.reactive.HttpHandler", null)) { logger.info("No web server classes found so no server to start"); return; } Integer port = Integer.valueOf(context.getEnvironment().resolvePlaceholders("${server.port:${PORT:8080}}")); String address = context.getEnvironment().resolvePlaceholders("${server.address:0.0.0.0}"); if (port >= 0) { HttpHandler handler = context.getBean(HttpHandler.class); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host(address).port(port).handle(adapter); Thread thread = new Thread( () -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback), "server-startup"); thread.setDaemon(false); thread.start(); } }
Example #4
Source File: HttpCompressionClientServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue282() { DisposableServer server = HttpServer.create() .compress(2048) .port(0) .handle((req, res) -> res.sendString(Mono.just("testtesttesttesttest"))) .bindNow(); Mono<String> response = HttpClient.create() .port(server.port()) .get() .uri("/") .responseContent() .aggregate() .asString(); StepVerifier.create(response) .expectNextMatches("testtesttesttesttest"::equals) .expectComplete() .verify(); server.disposeNow(); }
Example #5
Source File: HttpMetricsHandlerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Before public void setUp() { httpServer = customizeServerOptions( HttpServer.create() .host("127.0.0.1") .port(0) .metrics(true, s -> s) .route(r -> r.post("/1", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))) .post("/2", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))))); provider = ConnectionProvider.create("HttpMetricsHandlerTests", 1); httpClient = customizeClientOptions(HttpClient.create(provider) .remoteAddress(() -> disposableServer.address()) .metrics(true, s -> s)); registry = new SimpleMeterRegistry(); Metrics.addRegistry(registry); }
Example #6
Source File: RSocketNettyReactiveWebServerFactory.java From spring-boot-rsocket with Apache License 2.0 | 6 votes |
private HttpServer createHttpServer() { HttpServer server = HttpServer.create(); if (this.resourceFactory != null) { LoopResources resources = this.resourceFactory.getLoopResources(); Assert.notNull(resources, "No LoopResources: is ReactorResourceFactory not initialized yet?"); server = server.tcpConfiguration((tcpServer) -> tcpServer.runOn(resources) .addressSupplier(this::getListenAddress)); } else { server = server.tcpConfiguration( (tcpServer) -> tcpServer.addressSupplier(this::getListenAddress)); } if (getSsl() != null && getSsl().isEnabled()) { SslServerCustomizer sslServerCustomizer = new SslServerCustomizer(getSsl(), getHttp2(), getSslStoreProvider()); server = sslServerCustomizer.apply(server); } if (getCompression() != null && getCompression().getEnabled()) { CompressionCustomizer compressionCustomizer = new CompressionCustomizer( getCompression()); server = compressionCustomizer.apply(server); } server = server.protocol(listProtocols()).forwarded(this.useForwardHeaders); return applyCustomizers(server); }
Example #7
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testMaxFramePayloadLengthFailed() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(Mono.just("12345678901")))) .wiretap(true) .bindNow(); Mono<Void> response = HttpClient.create() .port(httpServer.port()) .websocket(WebsocketClientSpec.builder().maxFramePayloadLength(10).build()) .handle((in, out) -> in.receive() .asString() .map(srv -> srv)) .log() .then(); StepVerifier.create(response) .expectError(CorruptedFrameException.class) .verify(Duration.ofSeconds(30)); }
Example #8
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void simpleSubprotocolServerNoSubprotocol() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(Mono.just("test")))) .wiretap(true) .bindNow(); StepVerifier.create( HttpClient.create() .port(httpServer.port()) .headers(h -> h.add("Authorization", auth)) .websocket(WebsocketClientSpec.builder().protocols("SUBPROTOCOL,OTHER").build()) .uri("/test") .handle((i, o) -> i.receive().asString())) .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER"); }
Example #9
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue632() throws Exception { disposableServer = HttpServer.create() .port(0) .wiretap(true) .handle((req, res) -> res.header(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE + ", " + HttpHeaderValues.CLOSE)) .bindNow(); assertThat(disposableServer).isNotNull(); CountDownLatch latch = new CountDownLatch(1); createHttpClientForContextWithPort() .doOnConnected(conn -> conn.channel() .closeFuture() .addListener(future -> latch.countDown())) .get() .uri("/") .responseContent() .blockLast(Duration.ofSeconds(30)); assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue(); }
Example #10
Source File: HttpErrorTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void test() { DisposableServer server = HttpServer.create() .port(0) .route(httpServerRoutes -> httpServerRoutes.get( "/", (httpServerRequest, httpServerResponse) -> { return httpServerResponse.sendString( Mono.error(new IllegalArgumentException("test"))); })) .bindNow(Duration.ofSeconds(30)); HttpClient client = HttpClient.create() .port(server.port()); StepVerifier.create(client.get() .uri("/") .responseContent() .asString(StandardCharsets.UTF_8) .collectList()) .expectNextMatches(List::isEmpty) .verifyComplete(); server.disposeNow(); }
Example #11
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void simpleSubprotocolServerSupported() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket( (i, o) -> o.sendString(Mono.just("test")), WebsocketServerSpec.builder().protocols("SUBPROTOCOL").build())) .wiretap(true) .bindNow(); List<String> res = HttpClient.create() .port(httpServer.port()) .wiretap(true) .headers(h -> h.add("Authorization", auth)) .websocket(WebsocketClientSpec.builder().protocols("SUBPROTOCOL,OTHER").build()) .uri("/test") .handle((i, o) -> i.receive().asString()) .log() .collectList() .block(Duration.ofSeconds(30)); Assert.assertNotNull(res); Assert.assertThat(res.get(0), is("test")); }
Example #12
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testResourceUrlSetInResponse() { disposableServer = HttpServer.create() .port(0) .handle((req, res) -> res.send()) .wiretap(true) .bindNow(); final String requestUri = "http://localhost:" + disposableServer.port() + "/foo"; StepVerifier.create( createHttpClientForContextWithAddress() .get() .uri(requestUri) .responseConnection((res, conn) -> Mono.justOrEmpty(res.resourceUrl()))) .expectNext(requestUri) .expectComplete() .verify(Duration.ofSeconds(30)); }
Example #13
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
private void doTestIssue444(BiFunction<WebsocketInbound, WebsocketOutbound, Publisher<Void>> fn) { httpServer = HttpServer.create() .host("localhost") .port(0) .handle((req, res) -> res.sendWebsocket(fn)) .wiretap(true) .bindNow(); StepVerifier.create( HttpClient.create() .remoteAddress(httpServer::address) .wiretap(true) .websocket() .uri("/") .handle((i, o) -> i.receiveFrames() .then())) .expectComplete() .verify(Duration.ofSeconds(30)); }
Example #14
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void simpleSubprotocolServerNotSupported() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket( (i, o) -> { return o.sendString(Mono.just("test")); }, WebsocketServerSpec.builder().protocols("protoA,protoB").build())) .wiretap(true) .bindNow(); StepVerifier.create( HttpClient.create() .port(httpServer.port()) .headers(h -> h.add("Authorization", auth)) .websocket(WebsocketClientSpec.builder().protocols("SUBPROTOCOL,OTHER").build()) .uri("/test") .handle((i, o) -> i.receive().asString())) //the SERVER returned null which means that it couldn't select a protocol .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER"); }
Example #15
Source File: ReactorNettyHttpClientSpringBootTests.java From spring-cloud-sleuth with Apache License 2.0 | 6 votes |
@Test public void shouldUseInvocationContext() throws Exception { disposableServer = HttpServer.create().port(0) // this reads the trace context header, b3, returning it in the response .handle((in, out) -> out .sendString(Flux.just(in.requestHeaders().get("b3")))) .bindNow(); String b3SingleHeaderReadByServer; try (Scope ws = currentTraceContext.newScope(context)) { b3SingleHeaderReadByServer = httpClient.port(disposableServer.port()).get() .uri("/").responseContent().aggregate().asString().block(); } MutableSpan clientSpan = spanHandler.takeRemoteSpan(CLIENT); assertThat(b3SingleHeaderReadByServer).isEqualTo(context.traceIdString() + "-" + clientSpan.id() + "-1-" + context.spanIdString()); }
Example #16
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #17
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private void doTestConnectionAliveWhenTransformationErrors(BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> handler, Flux<String> expectation, int count) { httpServer = HttpServer.create() .port(0) .handle((req, res) -> res.sendWebsocket(handler)) .wiretap(true) .bindNow(); FluxIdentityProcessor<String> output = Processors.replayAll(); HttpClient.create() .port(httpServer.port()) .websocket() .uri("/") .handle((in, out) -> out.sendString(Flux.just("1", "text", "2")) .then(in.aggregateFrames() .receiveFrames() .map(WebSocketFrame::content) .map(byteBuf -> byteBuf.readCharSequence(byteBuf.readableBytes(), Charset.defaultCharset()).toString()) .take(count) .subscribeWith(output) .then())) .blockLast(Duration.ofSeconds(30)); assertThat(output.collectList().block(Duration.ofSeconds(30))) .isEqualTo(expectation.collectList().block(Duration.ofSeconds(30))); }
Example #18
Source File: HTTPConfigurationServer.java From james-project with Apache License 2.0 | 5 votes |
public RunningStage start() { return new RunningStage(HttpServer.create() .port(configuration.getPort() .map(Port::getValue) .orElse(RANDOM_PORT)) .route(routes -> routes .get(SMTP_BEHAVIORS, this::getBehaviors) .put(SMTP_BEHAVIORS, this::putBehaviors) .delete(SMTP_BEHAVIORS, this::deleteBehaviors) .get(SMTP_MAILS, this::getMails) .delete(SMTP_MAILS, this::deleteMails)) .bindNow()); }
Example #19
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #20
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) throws Exception { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( Application.class)) { context.getBean(HttpServer.class) .bindUntilJavaShutdown(Duration.ofSeconds(60), null); } }
Example #21
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void unidirectional() { int c = 10; httpServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendWebsocket( (i, o) -> o.sendString( Mono.just("test") .delayElement(Duration.ofMillis(100)) .repeat()))) .wiretap(true) .bindNow(); Flux<String> ws = HttpClient.create() .port(httpServer.port()) .wiretap(true) .websocket() .uri("/") .handle((in, out) -> in.aggregateFrames() .receive() .asString()); List<String> expected = Flux.range(1, c) .map(v -> "test") .collectList() .block(); Assert.assertNotNull(expected); StepVerifier.create(ws.take(c) .log()) .expectNextSequence(expected) .expectComplete() .verify(); }
Example #22
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void closePool() { ConnectionProvider pr = ConnectionProvider.create("closePool", 1); disposableServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendString(Mono.just("test") .delayElement(Duration.ofMillis(100)) .repeat())) .wiretap(true) .bindNow(); Flux<String> ws = createHttpClientForContextWithPort(pr) .get() .uri("/") .responseContent() .asString(); List<String> expected = Flux.range(1, 20) .map(v -> "test") .collectList() .block(); Assert.assertNotNull(expected); StepVerifier.create( Flux.range(1, 10) .concatMap(i -> ws.take(2) .log())) .expectNextSequence(expected) .expectComplete() .verify(); pr.dispose(); }
Example #23
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testExternalConnections_PullRequest1047() { String incorrectHostName = "incorrect-host.io"; httpServer = HttpServer.create() .port(0) .handle((req, res) -> { if (req.requestHeaders().get(HttpHeaderNames.HOST).contains(incorrectHostName)) { return res.status(418) .sendString(Mono.just("Incorrect Host header")); } return res.sendWebsocket((in, out) -> out.sendString(Mono.just("echo")) .sendObject(new CloseWebSocketFrame())); }) .wiretap(true) .bindNow(); Mono<Void> response = HttpClient.create() .port(httpServer.port()) .headers(h -> h.add(HttpHeaderNames.HOST, incorrectHostName)) .websocket() .uri("/") .handle((in, out) -> out.sendObject(in.receiveFrames() .doOnNext(WebSocketFrame::retain) .then())) .next(); StepVerifier.create(response) .expectComplete() .verify(Duration.ofSeconds(30)); }
Example #24
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #25
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #26
Source File: WebsocketTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private void doTestServerMaxFramePayloadLength(int maxFramePayloadLength, Flux<String> input, Flux<String> expectation, int count) { httpServer = HttpServer.create() .port(0) .handle((req, res) -> res.sendWebsocket((in, out) -> out.sendObject(in.aggregateFrames() .receiveFrames() .map(WebSocketFrame::content) .map(byteBuf -> byteBuf.readCharSequence(byteBuf.readableBytes(), Charset.defaultCharset()).toString()) .map(TextWebSocketFrame::new)), WebsocketServerSpec.builder().maxFramePayloadLength(maxFramePayloadLength).build())) .wiretap(true) .bindNow(); FluxIdentityProcessor<String> output = Processors.replayAll(); HttpClient.create() .port(httpServer.port()) .websocket() .uri("/") .handle((in, out) -> out.sendString(input) .then(in.aggregateFrames() .receiveFrames() .map(WebSocketFrame::content) .map(byteBuf -> byteBuf.readCharSequence(byteBuf.readableBytes(), Charset.defaultCharset()).toString()) .take(count) .subscribeWith(output) .then())) .blockLast(Duration.ofSeconds(30)); assertThat(output.collectList().block(Duration.ofSeconds(30))) .isEqualTo(expectation.collectList().block(Duration.ofSeconds(30))); }
Example #27
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #28
Source File: WebsocketRouteTransportTest.java From rsocket-java with Apache License 2.0 | 5 votes |
@DisplayName("constructor throw NullPointer with null routesBuilder") @Test void constructorNullRoutesBuilder() { assertThatNullPointerException() .isThrownBy(() -> new WebsocketRouteTransport(HttpServer.create(), null, "/test-path")) .withMessage("routesBuilder must not be null"); }
Example #29
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }
Example #30
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
@Profile("default") @Bean public HttpServer nettyHttpServer(ApplicationContext context) { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port(this.port); return httpServer.handle(adapter); }