io.reactivex.netty.protocol.http.server.HttpServer Java Examples
The following examples show how to use
io.reactivex.netty.protocol.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: NettyNoResponseHttpITest.java From hawkular-apm with Apache License 2.0 | 6 votes |
@Override public void init() { server = HttpServer.newServer() .enableWireLogging(LogLevel.DEBUG) .start((req, resp) -> { if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) { return resp.setStatus(HttpResponseStatus.BAD_REQUEST); } if (req.getHttpMethod() == HttpMethod.POST || req.getHttpMethod() == HttpMethod.PUT) { req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString())); } resp.setStatus(HttpResponseStatus.OK); return resp; } ); super.init(); }
Example #2
Source File: NettyHttpITest.java From hawkular-apm with Apache License 2.0 | 6 votes |
@Override public void init() { server = HttpServer.newServer() .enableWireLogging(LogLevel.DEBUG) .start((req, resp) -> { if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) { return resp.setStatus(HttpResponseStatus.BAD_REQUEST); } if (req.getHttpMethod() == HttpMethod.POST || req.getHttpMethod() == HttpMethod.PUT) { req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString())); } return resp.writeString(Observable.just(HELLO_WORLD)); }); super.init(); }
Example #3
Source File: MesosClientIntegrationTest.java From mesos-rxjava with Apache License 2.0 | 6 votes |
@Test public void testStreamDoesNotRunWhenSubscribeFails_mesos5xxResponse() throws Throwable { final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); return response.close(); }; final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler); server.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort())); final MesosClient<String, String> client = createClient(uri); try { client.openStream().await(); fail("Expect an exception to be propagated up because subscribe will 500"); } catch (Mesos5xxException e) { // expected final MesosClientErrorContext ctx = e.getContext(); assertThat(ctx.getStatusCode()).isEqualTo(500); } finally { server.shutdown(); } }
Example #4
Source File: MesosClientIntegrationTest.java From mesos-rxjava with Apache License 2.0 | 6 votes |
@Test public void testStreamDoesNotRunWhenSubscribeFails_mismatchContentType() throws Throwable { final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> { response.setStatus(HttpResponseStatus.OK); response.getHeaders().setHeader("Content-Type", "application/json"); return response.close(); }; final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler); server.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort())); final MesosClient<String, String> client = createClient(uri); try { client.openStream().await(); fail("Expect an exception to be propagated up because of content type mismatch"); } catch (MesosException e) { // expected final MesosClientErrorContext ctx = e.getContext(); assertThat(ctx.getStatusCode()).isEqualTo(200); assertThat(ctx.getMessage()).isEqualTo("Response had Content-Type \"application/json\" expected \"text/plain;charset=utf-8\""); } finally { server.shutdown(); } }
Example #5
Source File: RealRequestBenchmarks.java From feign with Apache License 2.0 | 6 votes |
@Setup public void setup() { server = HttpServer.newServer(SERVER_PORT) .start((request, response) -> null); client = new OkHttpClient(); client.retryOnConnectionFailure(); okFeign = Feign.builder() .client(new feign.okhttp.OkHttpClient(client)) .logLevel(Level.NONE) .logger(new Logger.ErrorLogger()) .retryer(new Retryer.Default()) .target(FeignTestInterface.class, "http://localhost:" + SERVER_PORT); queryRequest = new Request.Builder() .url("http://localhost:" + SERVER_PORT + "/?Action=GetUser&Version=2010-05-08&limit=1") .build(); }
Example #6
Source File: RxMovieServer.java From ribbon with Apache License 2.0 | 6 votes |
public HttpServer<ByteBuf, ByteBuf> createServer() { HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { @Override public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { if (request.getPath().contains("/users")) { if (request.getHttpMethod().equals(HttpMethod.GET)) { return handleRecommendationsByUserId(request, response); } else { return handleUpdateRecommendationsForUser(request, response); } } if (request.getPath().contains("/recommendations")) { return handleRecommendationsBy(request, response); } if (request.getPath().contains("/movies")) { return handleRegisterMovie(request, response); } response.setStatus(HttpResponseStatus.NOT_FOUND); return response.close(); } }).pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).enableWireLogging(LogLevel.ERROR).build(); System.out.println("RxMovie server started..."); return server; }
Example #7
Source File: ClientServer.java From ReactiveLab with Apache License 2.0 | 6 votes |
public static HttpServer<ByteBuf, ByteBuf> startServer(int port) { /** * Creates an HTTP server which returns "Hello World!" responses. */ return RxNetty.createHttpServer(port, /* * HTTP Request handler for RxNetty where you control what you write as the * response for each and every request the server receives. */ (request, response) -> { /** * In a real server, you would be writing different responses based on the * URI of the request. * This example just returns a "Hello World!!" string unconditionally. */ return response.writeStringAndFlush("Hello World!!"); }) .start(); }
Example #8
Source File: ClientServer.java From ReactiveLab with Apache License 2.0 | 6 votes |
public static void main(String[] args) { /** * Start our HTTP server. */ HttpServer<ByteBuf, ByteBuf> server = startServer(8088); /** * Submit the request. */ createRequest("localhost", server.getServerPort()) /* Block till you get the response. In a real world application, one should not be blocked but chained * into a response to the caller. */ .toBlocking() /** * Print each content of the response. */ .forEach(System.out::println); }
Example #9
Source File: RxNettyRequestReplyServer.java From MarketData with Apache License 2.0 | 5 votes |
public HttpServer<ByteBuf, ByteBuf> createServer() { return RxNetty.createHttpServer(port, (request, response) -> { HttpRequest httpRequest = new HttpRequest(request.getQueryParameters()); String content = getResponseContent(httpRequest); return response.writeStringAndFlush(content); }); }
Example #10
Source File: Karyon.java From karyon with Apache License 2.0 | 5 votes |
/** * Creates a new {@link KaryonServer} that has a single HTTP server instance which delegates all request * handling to {@link RequestHandler}. * The {@link HttpServer} is created using {@link KaryonTransport#newHttpServer(int, HttpRequestHandler)} * * @param port Port for the server. * @param handler Request Handler * @param bootstrapModules Additional bootstrapModules if any. * * @return {@link KaryonServer} which is to be used to start the created server. */ public static KaryonServer forRequestHandler(int port, final RequestHandler<ByteBuf, ByteBuf> handler, BootstrapModule... bootstrapModules) { HttpServer<ByteBuf, ByteBuf> httpServer = KaryonTransport.newHttpServer(port, new RequestHandler<ByteBuf, ByteBuf>() { @Override public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) { return handler.handle(request, response); } }); return new RxNettyServerBackedServer(httpServer, bootstrapModules); }
Example #11
Source File: KaryonHttpModule.java From karyon with Apache License 2.0 | 5 votes |
@Override protected void configure() { configureServer(); bind(serverConfigKey).toInstance(serverConfigBuilder.build()); bind(interceptorSupportKey).toInstance(interceptorSupportInstance); MapBinder.newMapBinder(binder(), String.class, RxServer.class).addBinding(nameAnnotation.value()).toProvider( new HttpRxServerProvider<I, O, HttpServer<I, O>>(nameAnnotation.value(), iType, oType) ).asEagerSingleton(); }
Example #12
Source File: KaryonHttpModule.java From karyon with Apache License 2.0 | 5 votes |
protected KaryonHttpModule(String moduleName, Class<I> iType, Class<O> oType) { super(moduleName, iType, oType); routerKey = keyFor(RequestHandler.class, iType, oType, nameAnnotation); interceptorSupportKey = keyFor(GovernatorHttpInterceptorSupport.class, iType, oType, nameAnnotation); httpServerKey = keyFor(HttpServer.class, iType, oType, nameAnnotation); }
Example #13
Source File: AbstractMiddleTierService.java From ReactiveLab with Apache License 2.0 | 5 votes |
public HttpServer<ByteBuf, ServerSentEvent> createServer(int port) { System.out.println("Start " + getClass().getSimpleName() + " on port: " + port); // declare handler chain (wrapped in Hystrix) // TODO create a better way of chaining these (related https://github.com/ReactiveX/RxNetty/issues/232 and https://github.com/ReactiveX/RxNetty/issues/202) HystrixMetricsStreamHandler<ByteBuf, ServerSentEvent> handlerChain = new HystrixMetricsStreamHandler<>(metrics, "/hystrix.stream", 1000, (request, response) -> { try { long startTime = System.currentTimeMillis(); return handleRequest(request, response) .doOnCompleted(() -> System.out.println("Response => " + request.getPath() + " Time => " + (int) (System.currentTimeMillis() - startTime) + "ms")) .doOnCompleted(() -> metrics.getRollingPercentile().addValue((int) (System.currentTimeMillis() - startTime))) .doOnCompleted(() -> metrics.getRollingNumber().add(Metrics.EventType.SUCCESS, 1)) .doOnError(t -> metrics.getRollingNumber().add(Metrics.EventType.FAILURE, 1)); } catch (Throwable e) { e.printStackTrace(); System.err.println("Server => Error [" + request.getPath() + "] => " + e); response.setStatus(HttpResponseStatus.BAD_REQUEST); return response.writeStringAndFlush("data: Error 500: Bad Request\n" + e.getMessage() + "\n"); } }); return RxNetty.createHttpServer(port, (request, response) -> { // System.out.println("Server => Request: " + request.getPath()); return handlerChain.handle(request, response); }, PipelineConfigurators.<ByteBuf> serveSseConfigurator()); }
Example #14
Source File: NettyRxJavaServer.java From netty-cookbook with Apache License 2.0 | 5 votes |
public static void main(String... args) throws InterruptedException { HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(8080, (HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) -> { System.out.println("Server => Request: " + request.getPath()); try { if ("/error".equals(request.getPath())) { throw new RuntimeException("forced error"); } response.setStatus(HttpResponseStatus.OK); response.writeString("Path Requested =>: " + request.getPath() + '\n'); return response.close(); } catch (Throwable e) { System.err.println("Server => Error [" + request.getPath() + "] => " + e); response.setStatus(HttpResponseStatus.BAD_REQUEST); response.writeString("Error 500: Bad Request\n"); return response.close(); } }); server.startAndWait(); RxNetty.createHttpGet("http://localhost:8080/") .flatMap(response -> response.getContent()) .map(data -> "Client => " + data.toString(Charset.defaultCharset())) .toBlocking().forEach(System.out::println); RxNetty.createHttpGet("http://localhost:8080/error") .flatMap(response -> response.getContent()) .map(data -> "Client => " + data.toString(Charset.defaultCharset())) .toBlocking().forEach(System.out::println); RxNetty.createHttpGet("http://localhost:8080/data") .flatMap(response -> response.getContent()) .map(data -> "Client => " + data.toString(Charset.defaultCharset())) .toBlocking().forEach(System.out::println); //server.shutdown(); }
Example #15
Source File: HttpExampleUtils.java From ocelli with Apache License 2.0 | 5 votes |
protected static SocketAddress startServer(HttpResponseStatus cannedStatus) { return HttpServer.newServer() .start((request, response) -> { return response.addHeader("X-Instance", response.unsafeNettyChannel().localAddress()) .setStatus(cannedStatus); }) .getServerAddress(); }
Example #16
Source File: HttpExampleUtils.java From ocelli with Apache License 2.0 | 5 votes |
protected static SocketAddress startServer(long latencyMillis) { return HttpServer.newServer() .start((request, response) -> { return Observable.timer(latencyMillis, TimeUnit.MILLISECONDS) .flatMap(aTick -> response.addHeader("X-Instance", response.unsafeNettyChannel() .localAddress()) .setStatus(HttpResponseStatus.OK)); }) .getServerAddress(); }
Example #17
Source File: MesosClientBackpressureIntegrationTest.java From mesos-rxjava with Apache License 2.0 | 5 votes |
@Test public void testBurstyObservable_unboundedBufferSucceeds() throws Throwable { msgNo = 0; final int numMessages = 20000; final String subscribedMessage = "{\"type\": \"SUBSCRIBED\",\"subscribed\": {\"framework_id\": {\"value\":\"12220-3440-12532-2345\"},\"heartbeat_interval_seconds\":15.0}"; final String heartbeatMessage = "{\"type\":\"HEARTBEAT\"}"; final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> { response.setStatus(HttpResponseStatus.OK); response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8"); writeRecordIOMessage(response, subscribedMessage); for (int i = 0; i < numMessages; i++) { writeRecordIOMessage(response, heartbeatMessage); } return response.close(); }; final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler); server.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort())); final MesosClient<String, String> client = createClientForStreaming(uri) .onBackpressureBuffer() .build(); try { client.openStream().await(); } finally { // 20000 heartbeats PLUS 1 subscribe assertEquals("All heartbeats received (plus the subscribed)", 1 + numMessages, msgNo); server.shutdown(); } }
Example #18
Source File: MesosClientBackpressureIntegrationTest.java From mesos-rxjava with Apache License 2.0 | 5 votes |
@Test @Ignore public void testBurstyObservable_missingBackpressureException() throws Throwable { final String subscribedMessage = "{\"type\": \"SUBSCRIBED\",\"subscribed\": {\"framework_id\": {\"value\":\"12220-3440-12532-2345\"},\"heartbeat_interval_seconds\":15.0}"; final String heartbeatMessage = "{\"type\":\"HEARTBEAT\"}"; final byte[] hmsg = heartbeatMessage.getBytes(StandardCharsets.UTF_8); final byte[] hbytes = String.format("%d\n", heartbeatMessage.getBytes().length).getBytes(StandardCharsets.UTF_8); final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> { response.setStatus(HttpResponseStatus.OK); response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8"); writeRecordIOMessage(response, subscribedMessage); for (int i = 0; i < 20000; i++) { response.writeBytes(hbytes); response.writeBytes(hmsg); } return response.flush(); }; final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler); server.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort())); final MesosClient<String, String> client = createClientForStreaming(uri).build(); try { client.openStream().await(); fail("Expect an exception to be propagated up due to backpressure"); } catch (MissingBackpressureException e) { // expected e.printStackTrace(); assertThat(e.getMessage()).isNullOrEmpty(); } finally { server.shutdown(); } }
Example #19
Source File: MesosClientIntegrationTest.java From mesos-rxjava with Apache License 2.0 | 5 votes |
@Test public void testStreamDoesNotRunWhenSubscribeFails_mesos4xxResponse() throws Throwable { final String errorMessage = "Error message that should come from the server"; final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> { response.setStatus(HttpResponseStatus.BAD_REQUEST); final byte[] msgBytes = errorMessage.getBytes(StandardCharsets.UTF_8); response.getHeaders().setHeader("Content-Length", msgBytes.length); response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8"); response.writeBytes(msgBytes); return response.close(); }; final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler); server.start(); final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort())); final MesosClient<String, String> client = createClient(uri); try { client.openStream().await(); fail("Expect an exception to be propagated up because subscribe will 400"); } catch (Mesos4xxException e) { // expected final MesosClientErrorContext ctx = e.getContext(); assertThat(ctx.getStatusCode()).isEqualTo(400); assertThat(ctx.getMessage()).isEqualTo(errorMessage); } finally { server.shutdown(); } }
Example #20
Source File: RxNettyEventServer.java From MarketData with Apache License 2.0 | 5 votes |
public HttpServer<ByteBuf, ServerSentEvent> createServer() { HttpServer<ByteBuf, ServerSentEvent> server = RxNetty.createHttpServer(port, (request, response) -> { response.getHeaders().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); response.getHeaders().set(CACHE_CONTROL, "no-cache"); response.getHeaders().set(CONNECTION, "keep-alive"); response.getHeaders().set(CONTENT_TYPE, "text/event-stream"); return getIntervalObservable(request, response); }, PipelineConfigurators.<ByteBuf>serveSseConfigurator()); System.out.println("HTTP Server Sent Events server started..."); return server; }
Example #21
Source File: RxNettyEventBroadcaster.java From MarketData with Apache License 2.0 | 5 votes |
public HttpServer<ByteBuf, ServerSentEvent> createServer() { if (flaky) { events = SubscriptionLimiter .limitSubscriptions(1,initializeEventStream()); } else { events = initializeEventStream(); } return super.createServer(); }
Example #22
Source File: KaryonTransport.java From karyon with Apache License 2.0 | 4 votes |
public static <I, O> HttpServer<I, O> newHttpServer(int port, RequestHandler<I, O> router) { return newHttpServerBuilder(port, router).build(); }
Example #23
Source File: KaryonTransport.java From karyon with Apache License 2.0 | 4 votes |
public static <I, O> HttpServer<I, O> newHttpServer(int port, HttpRequestHandler<I, O> requestHandler) { return newHttpServerBuilder(port, requestHandler).build(); }
Example #24
Source File: Karyon.java From karyon with Apache License 2.0 | 2 votes |
/** * Creates a new {@link KaryonServer} which combines lifecycle of the passed {@link HttpServer} with * it's own lifecycle. * * @param server HTTP server * @param modules Additional bootstrapModules if any. * * @return {@link KaryonServer} which is to be used to start the created server. */ public static KaryonServer forHttpServer(HttpServer<?, ?> server, Module... modules) { return forHttpServer(server, toBootstrapModule(modules)); }
Example #25
Source File: Karyon.java From karyon with Apache License 2.0 | 2 votes |
/** * Creates a new {@link KaryonServer} which combines lifecycle of the passed {@link HttpServer} with * it's own lifecycle. * * @param server HTTP server * @param bootstrapModules Additional bootstrapModules if any. * * @return {@link KaryonServer} which is to be used to start the created server. */ public static KaryonServer forHttpServer(HttpServer<?, ?> server, BootstrapModule... bootstrapModules) { return new RxNettyServerBackedServer(server, bootstrapModules); }