public SentryWebServer(List<EventListener> listeners, int port, Configuration conf) { this.port = port; server = new Server(port); ServletContextHandler servletContextHandler = new ServletContextHandler(); ServletHolder servletHolder = new ServletHolder(AdminServlet.class); servletContextHandler.addServlet(servletHolder, "/*"); for(EventListener listener:listeners) { servletContextHandler.addEventListener(listener); } ServletHolder confServletHolder = new ServletHolder(ConfServlet.class); servletContextHandler.addServlet(confServletHolder, "/conf"); servletContextHandler.getServletContext() .setAttribute(ConfServlet.CONF_CONTEXT_ATTRIBUTE, conf); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setDirectoriesListed(true); URL url = this.getClass().getResource(RESOURCE_DIR); try { resourceHandler.setBaseResource(Resource.newResource(url.toString())); } catch (IOException e) { LOGGER.error("Got exception while setBaseResource for Sentry Service web UI", e); } resourceHandler.setWelcomeFiles(new String[]{WELCOME_PAGE}); ContextHandler contextHandler= new ContextHandler(); contextHandler.setHandler(resourceHandler); ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection(); contextHandlerCollection.setHandlers(new Handler[]{contextHandler, servletContextHandler}); String authMethod = conf.get(ServerConfig.SENTRY_WEB_SECURITY_TYPE); if (!ServerConfig.SENTRY_WEB_SECURITY_TYPE_NONE.equals(authMethod)) { /** * SentryAuthFilter is a subclass of AuthenticationFilter and * AuthenticationFilter tagged as private and unstable interface: * While there are not guarantees that this interface will not change, * it is fairly stable and used by other projects (ie - Oozie) */ FilterHolder filterHolder = servletContextHandler.addFilter(SentryAuthFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); filterHolder.setInitParameters(loadWebAuthenticationConf(conf)); } server.setHandler(contextHandlerCollection); }
/** * Starts Server * * @param port Server Port * @param contextPath Endpoint name */ public static void launch(int port, String endpoint) { // Initialize Guice Injector Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { binder().requireExplicitBindings(); install(new FishWorldModule()); bind(GuiceFilter.class); bind(MetricsRegistry.class).toInstance(Metrics.defaultRegistry()); } }); // Create Server s_server = new Server(port); String contextPath = endpoint; // Add default Servlet if (!contextPath.startsWith("/")) { contextPath = "/" + contextPath; } ServletContextHandler handler = new ServletContextHandler(); handler.setContextPath(contextPath); handler.addServlet(new ServletHolder(new BadRequestServlet()), "/*"); // Add Guice & Jersey Filter FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class)); handler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class)); s_server.setHandler(handler); // Start Server try { s_server.start(); } catch (Exception e) { m_log.error(MarkerManager.getMarker("EXCEPTION"), "Error while starting server.", e); } }