public void aroundRequest(HttpRequest req, RunnableWithException<IOException> continuation) throws IOException{ continuation.run(); }
@Override public Completable shutdown() { return Completable.defer(() -> { weld.shutdown(); return super.shutdown(); }); }
private void service(int i, Context context, HttpServerRequest req, HttpServerResponse resp, HttpRequest vertxReq, HttpResponse vertxResp, boolean handleNotFound) throws IOException { if(i < plugins.size()) plugins.get(i).aroundRequest(vertxReq, () -> service(i+1, context, req, resp, vertxReq, vertxResp, handleNotFound)); else super.service(context, req, resp, vertxReq, vertxResp, handleNotFound); } }
private Completable startVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { Router router = Router.router(vertx); AppGlobals globals = AppGlobals.get(); globals.setRouter(router); VertxPluginRequestHandler resteasyHandler = new VertxPluginRequestHandler(vertx, deployment, plugins); return doOnPlugins(plugin -> plugin.preRoute()) .doOnComplete(() -> { setupRoutes(router); router.route().handler(routingContext -> { ResteasyProviderFactory.pushContext(RoutingContext.class, routingContext); ResteasyProviderFactory.pushContext(io.vertx.rxjava.ext.web.RoutingContext.class, io.vertx.rxjava.ext.web.RoutingContext.newInstance(routingContext.getDelegate())); resteasyHandler.handle(routingContext.request()); }); }).concatWith(doOnPlugins(plugin -> plugin.postRoute())) .concatWith(Completable.defer(() -> { // Start the front end server using the Jax-RS controller int port = globals.getConfig().getInteger("http_port", 9000); String host = globals.getConfig().getString("http_host", NetServerOptions.DEFAULT_HOST); return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .doOnSuccess(server -> System.out.println("Server started on port " + server.actualPort())) .doOnError(t -> t.printStackTrace()) .ignoreElement(); })); }); }
@Override public Completable preRoute() { return Completable.defer(() -> { AppGlobals globals = AppGlobals.get(); SockJSHandler sockJSHandler = SockJSHandler.create(globals.getVertx()); BridgeOptions bridgeOptions = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("app.markdown")) .addOutboundPermitted(new PermittedOptions().setAddress("page.saved")); sockJSHandler.bridge(bridgeOptions); globals.getRouter().route("/eventbus/*").handler(sockJSHandler); return super.preRoute(); }); } });
@Override public Completable deployToResteasy(VertxResteasyDeployment deployment) { return Completable.defer(() -> { JsonArray packages = AppGlobals.get().getConfig().getJsonArray("scan"); if(packages == null) { System.err.println("Not scanning any packages, please specify the 'scan' array of packages in configuration"); }else { String[] packagesToScan = (String[]) packages.getList().toArray(new String[packages.size()]); new FastClasspathScanner(packagesToScan) .matchClassesWithAnnotation(Path.class, klass -> { if(!Modifier.isAbstract(klass.getModifiers())) deployment.getActualResourceClasses().add(klass); }) .matchClassesWithAnnotation(Provider.class, klass -> { if(!Modifier.isAbstract(klass.getModifiers())) deployment.getActualProviderClasses().add(klass); }) .scan(); } return super.deployToResteasy(deployment); }); } }
private Completable setupPlugins() { return Completable.defer(() -> { loadPlugins(); return doOnPlugins(plugin -> plugin.preInit()); }); }
private Completable setupVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { // Get a DB SQLClient dbClient = createDbClient(AppGlobals.get().getConfig()); Class<?> mainClass = null; for (Class<?> resourceClass : deployment.getActualResourceClasses()) { if(resourceClass.getAnnotation(MainResource.class) != null){ mainClass = resourceClass; break; } } // Save our injected globals AppGlobals globals = AppGlobals.get(); globals.setDbClient(dbClient); globals.setMainClass(mainClass); globals.setDeployment(deployment); return doOnPlugins(plugin -> plugin.init()) .concatWith(startVertx(deployment)); }); }
@Override public Completable preRoute() { return Completable.defer(() -> { AppGlobals globals = AppGlobals.get(); SockJSHandler sockJSHandler = SockJSHandler.create(globals.getVertx()); BridgeOptions bridgeOptions = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("app.markdown")) .addOutboundPermitted(new PermittedOptions().setAddress("page.saved")); sockJSHandler.bridge(bridgeOptions); globals.getRouter().route("/eventbus/*").handler(sockJSHandler); return super.preRoute(); }); } });
@Override public Completable deployToResteasy(VertxResteasyDeployment deployment) { return Completable.defer(() -> { JsonArray packages = AppGlobals.get().getConfig().getJsonArray("scan"); if(packages == null) { System.err.println("Not scanning any packages, please specify the 'scan' array of packages in configuration"); }else { String[] packagesToScan = (String[]) packages.getList().toArray(new String[packages.size()]); new FastClasspathScanner(packagesToScan) .matchClassesWithAnnotation(Path.class, klass -> { if(!Modifier.isAbstract(klass.getModifiers())) deployment.getActualResourceClasses().add(klass); }) .matchClassesWithAnnotation(Provider.class, klass -> { if(!Modifier.isAbstract(klass.getModifiers())) deployment.getActualProviderClasses().add(klass); }) .scan(); } return super.deployToResteasy(deployment); }); } }
public void aroundRequest(HttpRequest req, RunnableWithException<IOException> continuation) throws IOException{ continuation.run(); }
private Completable setupPlugins() { return Completable.defer(() -> { loadPlugins(); return doOnPlugins(plugin -> plugin.preInit()); }); }
public Completable close() { return doOnPlugins(plugin -> plugin.shutdown()) .concatWith(vertx.rxClose()); }
private Completable setupVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { // Get a DB SQLClient dbClient = createDbClient(AppGlobals.get().getConfig()); Class<?> mainClass = null; for (Class<?> resourceClass : deployment.getActualResourceClasses()) { if(resourceClass.getAnnotation(MainResource.class) != null){ mainClass = resourceClass; break; } } // Save our injected globals AppGlobals globals = AppGlobals.get(); globals.setDbClient(dbClient); globals.setMainClass(mainClass); globals.setDeployment(deployment); return doOnPlugins(plugin -> plugin.init()) .concatWith(startVertx(deployment)); }); }
private void service(int i, Context context, HttpServerRequest req, HttpServerResponse resp, HttpRequest vertxReq, HttpResponse vertxResp, boolean handleNotFound) throws IOException { if(i < plugins.size()) plugins.get(i).aroundRequest(vertxReq, () -> service(i+1, context, req, resp, vertxReq, vertxResp, handleNotFound)); else super.service(context, req, resp, vertxReq, vertxResp, handleNotFound); } }
protected Single<VertxResteasyDeployment> setupResteasy(Class<?>... resourceOrProviderClasses) { return Single.defer(() -> { // Build the Jax-RS hello world deployment VertxResteasyDeployment deployment = new VertxResteasyDeployment(); deployment.getDefaultContextObjects().put(Vertx.class, AppGlobals.get().getVertx()); deployment.getDefaultContextObjects().put(AppGlobals.class, AppGlobals.get()); return doOnPlugins(plugin -> plugin.deployToResteasy(deployment)).toSingle(() -> { for(Class<?> klass : resourceOrProviderClasses) { if(klass.isAnnotationPresent(Path.class)) deployment.getActualResourceClasses().add(klass); if(klass.isAnnotationPresent(Provider.class)) deployment.getActualProviderClasses().add(klass); } try { deployment.start(); }catch(ExceptionInInitializerError err) { // rxjava behaves badly on LinkageError RedpipeUtil.rethrow(err.getCause()); } return deployment; }).doOnError(t -> t.printStackTrace()); }); }
@Override public void aroundRequest(HttpRequest req, RunnableWithException<IOException> continuation) throws IOException { BoundRequestContext cdiContext = CDI.current().select(BoundRequestContext.class).get(); Map<String,Object> contextMap = new HashMap<String,Object>(); cdiContext.associate(contextMap); cdiContext.activate(); try { // FIXME: associate CDI thread context on thread change, like Resteasy context? continuation.run(); }finally { if(req.getAsyncContext().isSuspended()) { req.getAsyncContext().getAsyncResponse().register((CompletionCallback)(t) -> { cdiContext.invalidate(); cdiContext.deactivate(); cdiContext.dissociate(contextMap); }); }else { cdiContext.invalidate(); cdiContext.deactivate(); cdiContext.dissociate(contextMap); } } } }
return super.preInit(); });
public Completable close() { return doOnPlugins(plugin -> plugin.shutdown()) .concatWith(vertx.rxClose()); }
protected Single<VertxResteasyDeployment> setupResteasy(Class<?>... resourceOrProviderClasses) { return Single.defer(() -> { // Build the Jax-RS hello world deployment VertxResteasyDeployment deployment = new VertxResteasyDeployment(); deployment.getDefaultContextObjects().put(Vertx.class, AppGlobals.get().getVertx()); deployment.getDefaultContextObjects().put(AppGlobals.class, AppGlobals.get()); return doOnPlugins(plugin -> plugin.deployToResteasy(deployment)).toSingle(() -> { for(Class<?> klass : resourceOrProviderClasses) { if(klass.isAnnotationPresent(Path.class)) deployment.getActualResourceClasses().add(klass); if(klass.isAnnotationPresent(Provider.class)) deployment.getActualProviderClasses().add(klass); } try { deployment.start(); }catch(ExceptionInInitializerError err) { // rxjava behaves badly on LinkageError RedpipeUtil.rethrow(err.getCause()); } return deployment; }).doOnError(t -> t.printStackTrace()); }); }