public boolean start(int httpPort) { // ideally greater than reserved port but then port 80 is also valid Preconditions.checkArgument(httpPort > 0); baseUri = URI.create("http://0.0.0.0:" + Integer.toString(httpPort) + "/"); httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, this); setupSwagger(httpServer); ClassLoader classLoader = ControllerAdminApiApplication.class.getClassLoader(); // This is ugly from typical patterns to setup static resources but all our APIs are // at path "/". So, configuring static handler for path "/" does not work well. // Configuring this as a default servlet is an option but that is still ugly if we evolve // So, we setup specific handlers for static resource directory. index.html is served directly // by a jersey handler httpServer.getServerConfiguration() .addHttpHandler(new CLStaticHttpHandler(classLoader, "/static/query/"), "/query/"); httpServer.getServerConfiguration().addHttpHandler(new CLStaticHttpHandler(classLoader, "/static/css/"), "/css/"); httpServer.getServerConfiguration().addHttpHandler(new CLStaticHttpHandler(classLoader, "/static/js/"), "/js/"); // without this explicit request to /index.html will not work httpServer.getServerConfiguration().addHttpHandler(new CLStaticHttpHandler(classLoader, "/static/"), "/index.html"); started = true; LOGGER.info("Start jersey admin API on port: {}", httpPort); return true; }
private void setupSwagger(HttpServer httpServer) { BeanConfig beanConfig = new BeanConfig(); beanConfig.setTitle("Pinot Server API"); beanConfig.setDescription("APIs for accessing Pinot server information"); beanConfig.setContact("https://github.com/apache/incubator-pinot"); beanConfig.setVersion("1.0"); beanConfig.setSchemes(new String[]{"http"}); beanConfig.setBasePath(baseUri.getPath()); beanConfig.setResourcePackage(RESOURCE_PACKAGE); beanConfig.setScan(true); CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(AdminApiApplication.class.getClassLoader(), "/api/"); // map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/api/"); httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/help/"); URL swaggerDistLocation = AdminApiApplication.class.getClassLoader().getResource("META-INF/resources/webjars/swagger-ui/2.2.2/"); CLStaticHttpHandler swaggerDist = new CLStaticHttpHandler(new URLClassLoader(new URL[]{swaggerDistLocation})); httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/"); }
private void setupSwagger() { BeanConfig beanConfig = new BeanConfig(); beanConfig.setTitle("Pinot Broker API"); beanConfig.setDescription("APIs for accessing Pinot broker information"); beanConfig.setContact("https://github.com/apache/incubator-pinot"); beanConfig.setVersion("1.0"); beanConfig.setSchemes(new String[]{"http"}); beanConfig.setBasePath(_baseUri.getPath()); beanConfig.setResourcePackage(RESOURCE_PACKAGE); beanConfig.setScan(true); HttpHandler httpHandler = new CLStaticHttpHandler(BrokerAdminApiApplication.class.getClassLoader(), "/api/"); // map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility _httpServer.getServerConfiguration().addHttpHandler(httpHandler, "/api/", "/help/"); URL swaggerDistLocation = BrokerAdminApiApplication.class.getClassLoader().getResource("META-INF/resources/webjars/swagger-ui/2.2.2/"); CLStaticHttpHandler swaggerDist = new CLStaticHttpHandler(new URLClassLoader(new URL[]{swaggerDistLocation})); _httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/"); }
public static HttpServer startServer(String webRootPath) { final HttpServer server = new HttpServer(); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { server.shutdownNow(); } })); final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT); server.addListener(listener); final ServerConfiguration config = server.getServerConfiguration(); // add handler for serving static content config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH); // add handler for serving JAX-RS resources config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH); try { // Start the server. server.start(); } catch (Exception ex) { throw new ProcessingException("Exception thrown when trying to start grizzly server", ex); } return server; }
public static HttpServer startServer(String webRootPath) { final HttpServer server = new HttpServer(); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { server.shutdownNow(); } })); final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT); server.addListener(listener); final ServerConfiguration config = server.getServerConfiguration(); // add handler for serving static content config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH); // add handler for serving JAX-RS resources config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH); try { // Start the server. server.start(); } catch (Exception ex) { throw new ProcessingException("Exception thrown when trying to start grizzly server", ex); } return server; }
private void setupSwagger(HttpServer httpServer) { BeanConfig beanConfig = new BeanConfig(); beanConfig.setTitle("Pinot Controller API"); beanConfig.setDescription("APIs for accessing Pinot Controller information"); beanConfig.setContact("https://github.com/apache/incubator-pinot"); beanConfig.setVersion("1.0"); if (_useHttps) { beanConfig.setSchemes(new String[]{"https"}); } else { beanConfig.setSchemes(new String[]{"http"}); } beanConfig.setBasePath(baseUri.getPath()); beanConfig.setResourcePackage(RESOURCE_PACKAGE); beanConfig.setScan(true); ClassLoader loader = this.getClass().getClassLoader(); CLStaticHttpHandler apiStaticHttpHandler = new CLStaticHttpHandler(loader, "/api/"); // map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility httpServer.getServerConfiguration().addHttpHandler(apiStaticHttpHandler, "/api/"); httpServer.getServerConfiguration().addHttpHandler(apiStaticHttpHandler, "/help/"); URL swaggerDistLocation = loader.getResource("META-INF/resources/webjars/swagger-ui/2.2.2/"); CLStaticHttpHandler swaggerDist = new CLStaticHttpHandler(new URLClassLoader(new URL[]{swaggerDistLocation})); httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/"); }
public void run() { int port = config.getProperty("port") != null ? Integer.parseInt(config.getProperty("port")) : DEFAULT_PORT; String addr = config.getProperty("bind-address") != null ? config.getProperty("bind-address") : DEFAULT_BIND_ADDRESS; LOG.info("Starting analyst broker on port {} of interface {}", port, addr); HttpServer httpServer = new HttpServer(); NetworkListener networkListener = new NetworkListener("broker", addr, port); // We avoid blocking IO, and the following line allows us to see closed connections. networkListener.getTransport().setIOStrategy(SameThreadIOStrategy.getInstance()); httpServer.addListener(networkListener); // Bypass Jersey etc. and add a low-level Grizzly handler. // As in servlets, * is needed in base path to identify the "rest" of the path. broker = new Broker(config, addr, port); httpServer.getServerConfiguration().addHttpHandler(new BrokerHttpHandler(broker), "/*"); try { httpServer.start(); LOG.info("Broker running."); broker.run(); // run queue broker task pump in this thread Thread.currentThread().join(); } catch (BindException be) { LOG.error("Cannot bind to port {}. Is it already in use?", port); } catch (IOException ioe) { LOG.error("IO exception while starting server."); } catch (InterruptedException ie) { LOG.info("Interrupted, shutting down."); } httpServer.shutdown(); }
/** * Starts Grizzly HTTP server exposing static content, JAX-RS resources * and web sockets defined in this application. * * @param webRootPath static content root path. */ private static void startServer(String webRootPath) { final HttpServer server = new HttpServer(); Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow)); final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT); server.addListener(listener); final ServerConfiguration config = server.getServerConfiguration(); // add handler for serving static content config.addHttpHandler(new StaticContentHandler(webRootPath), APP_PATH); // add handler for serving JAX-RS resources config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), API_PATH); try { // Start the server. server.start(); } catch (Exception ex) { throw new ProcessingException("Exception thrown when trying to start grizzly server", ex); } }
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri); ServerConfiguration config = server.getServerConfiguration(); config.addHttpHandler(handler, "/");
config.addHttpHandler(handler, HttpHandlerRegistration.bulder().contextPath(contextPath).build());
httpServer.getServerConfiguration().addHttpHandler(dynamicHandler, "/otp/"); staticHandler.setFileCacheEnabled(false); httpServer.getServerConfiguration().addHttpHandler(staticHandler, "/"); params.clientDirectory.getAbsolutePath()); localHandler.setFileCacheEnabled(false); httpServer.getServerConfiguration().addHttpHandler(localHandler, "/local");
/** * Adds the specified {@link HttpHandler} as a root handler. * * @param httpHandler a {@link HttpHandler} */ public void addHttpHandler(final HttpHandler httpHandler) { addHttpHandler(httpHandler, ROOT_MAPPING); }
/** * Adds the specified {@link HttpHandler} as a root handler. * * @param httpHandler a {@link HttpHandler} */ public void addHttpHandler(final HttpHandler httpHandler) { addHttpHandler(httpHandler, ROOT_MAPPING); }
/** * Adds the specified {@link HttpHandler} as a root handler. * * @param httpHandler a {@link HttpHandler} */ public void addHttpHandler(final HttpHandler httpHandler) { addHttpHandler(httpHandler, ROOT_MAPPING); }
/** * Adds the specified {@link HttpHandler} as a root handler. * * @param httpHandler a {@link HttpHandler} */ public void addHttpHandler(final HttpHandler httpHandler) { addHttpHandler(httpHandler, ROOT_MAPPING); }
/** * Adds the specified {@link HttpHandler} as a root handler. * * @param httpHandler a {@link HttpHandler} */ public void addHttpHandler(final HttpHandler httpHandler) { addHttpHandler(httpHandler, ROOT_MAPPING); }
public HttpServiceFactory(HttpServer httpServer, Logger logger, Bundle bundle) { this.logger = logger; mainHttpHandler = new OSGiMainHandler(logger, bundle); httpServer.getServerConfiguration().addHttpHandler(mainHttpHandler, "/"); }
@Override public void addRestHandler(Application application, String rootURI) { if (enabled) { final ServerConfiguration config = httpServer.getServerConfiguration(); HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class, application); config.addHttpHandler(handler, rootURI); } } }
private void addJerseyHandler(ResourceConfig resourceConfig, String mapping) { HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class, resourceConfig); httpServer.getServerConfiguration().addHttpHandler(handler, HttpHandlerRegistration.bulder().contextPath(mapping).build()); LOG.log(Level.INFO, I18NBundle.getBundle().getMessage( LocalStringKeys.HTTP_REST_REGISTER_APPLICATION, resourceConfig.getApplication().getClass().getName(), mapping)); }
/** * Add a handler for temporaty files. * * Server Java temporary files on /temp */ private void addTempFileHandler() { StaticHttpHandler staticHttpHandler = new StaticHttpHandler(System.getProperty("java.io.tmpdir")); staticHttpHandler.setFileCacheEnabled(false); httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/temp"); }