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/"); }
private void addAccessLog(HttpServer httpServer) { try { String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation(); accessLogLocation = accessLogLocation + "/" + replaceSlash(serverData.getModule().getContext()) + "-access.log"; final AccessLogBuilder builder = new AccessLogBuilder(accessLogLocation); builder.rotatedDaily(); builder.rotationPattern("yyyy-MM-dd"); builder.instrument(httpServer.getServerConfiguration()); } catch (Exception e) { logger.error(InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getMessage()); if (e.getCause() != null) logger.error("CAUSED BY: " + InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getCause().getMessage()); } }
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(); }
URI uri = new URI("http://0.0.0.0:8080/"); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri); ServerConfiguration config = server.getServerConfiguration(); config.addHttpHandler(handler, "/");
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; }
/** * 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); } }
final ServerConfiguration config = server.getServerConfiguration(); if (handler != null) { final String path = uri.getPath().replaceAll("/{2,}", "/");
httpServer.getServerConfiguration().addHttpHandler(dynamicHandler, "/otp/"); staticHandler.setFileCacheEnabled(false); httpServer.getServerConfiguration().addHttpHandler(staticHandler, "/"); params.clientDirectory.getAbsolutePath()); localHandler.setFileCacheEnabled(false); httpServer.getServerConfiguration().addHttpHandler(localHandler, "/local");
/** * * @param server */ private void destoryServlets(HttpServer server) { if (servletHandlers != null && !servletHandlers.isEmpty()) { ServerConfiguration config = server.getServerConfiguration(); for (final ServletHandler handler : servletHandlers) { config.removeHttpHandler(handler); } } }
/** * * @param server */ private void destoryServlets(HttpServer server) { if (servletHandlers != null && !servletHandlers.isEmpty()) { ServerConfiguration config = server.getServerConfiguration(); for (final ServletHandler handler : servletHandlers) { config.removeHttpHandler(handler); } } }
/** * * @param server */ private void destoryServlets(HttpServer server) { if (servletHandlers != null && !servletHandlers.isEmpty()) { ServerConfiguration config = server.getServerConfiguration(); for (final ServletHandler handler : servletHandlers) { config.removeHttpHandler(handler); } } }
ResourceConfig rc = new ResourceConfig(); rc.packages("com.danny.resources"); rc.registerInstances(new StatusModule(useFake), new NetworkModule(useFake)); GrizzlyHttpContainer resourceConfigContainer = ContainerFactory .createContainer(GrizzlyHttpContainer.class, rc); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri); server.getServerConfiguration().addHttpHandler(resourceConfigContainer, "/");
@Override protected void startUp() throws Exception { httpServer = GrizzlyHttpServerFactory.createHttpServer( uri, createResourceConfig(), false); httpServer.getServerConfiguration().setDefaultErrorPageGenerator(ERROR_PAGE_GENERATOR); httpServer.start(); }
private CometHttpHandler addHttpHandler(String alias, boolean resume) { final CometHttpHandler c = new CometHttpHandler(cometContext, resume); httpServer.getServerConfiguration().addHttpHandler(c, alias); return c; }
/** * 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"); }
private void startServer(HttpHandler httpHandler) throws IOException { httpServer = new HttpServer(); NetworkListener networkListener = new NetworkListener("jaxws-listener", "0.0.0.0", PORT); httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler(), "/add"); // make sure JAX-WS Handler is not default httpServer.getServerConfiguration().addHttpHandler(httpHandler, HttpHandlerRegistration.bulder() .contextPath("/add/a/b") .urlPattern("/") .build()); httpServer.addListener(networkListener); httpServer.start(); }