use of org.eclipse.jetty.server.handler.ResourceHandler in project stagen by wiztools.
the class RunnerRun method run.
@Override
public void run(final File baseDir) throws IOException, ExecutorException {
// Generate site:
clean.run(baseDir);
gen.run(baseDir);
// Start monitoring service:
startMonitoring(baseDir);
// Start server:
try {
LOG.log(Level.INFO, "Starting HTTP server at port: {0}", cmd.port);
Server server = new Server(cmd.port);
ResourceHandler rh = new ResourceHandler();
rh.setDirectoriesListed(true);
rh.setWelcomeFiles(new String[] { "index.html" });
rh.setResourceBase(Constants.getOutDir(baseDir).getPath());
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { rh, new DefaultHandler() });
server.setHandler(handlers);
server.start();
server.join();
} catch (Exception ex) {
throw new ExecutorException(ex);
}
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project EventHub by Codecademy.
the class EventHubHandler method main.
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(EventHub.class.getClassLoader().getResourceAsStream("hub.properties"));
properties.load(EventHubHandler.class.getClassLoader().getResourceAsStream("web.properties"));
properties.putAll(System.getProperties());
Injector injector = Guice.createInjector(Modules.override(new DmaIdListModule(), new DatedEventIndexModule(), new ShardedEventIndexModule(), new PropertiesIndexModule(), new UserEventIndexModule(), new EventStorageModule(), new UserStorageModule(), new EventHubModule(properties)).with(new Module()));
final EventHubHandler eventHubHandler = injector.getInstance(EventHubHandler.class);
int port = injector.getInstance(Key.get(Integer.class, Names.named("eventhubhandler.port")));
final Server server = new Server(port);
@SuppressWarnings("ConstantConditions") String webDir = EventHubHandler.class.getClassLoader().getResource("frontend").toExternalForm();
HashLoginService loginService = new HashLoginService();
loginService.putUser(properties.getProperty("eventhubhandler.username"), new Password(properties.getProperty("eventhubhandler.password")), new String[] { "user" });
server.addBean(loginService);
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
Constraint constraint = new Constraint();
constraint.setName("auth");
constraint.setAuthenticate(true);
constraint.setRoles(new String[] { "user", "admin" });
ConstraintMapping mapping = new ConstraintMapping();
mapping.setPathSpec("/*");
mapping.setConstraint(constraint);
securityHandler.setConstraintMappings(Collections.singletonList(mapping));
securityHandler.setAuthenticator(new BasicAuthenticator());
securityHandler.setLoginService(loginService);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(false);
resourceHandler.setWelcomeFiles(new String[] { "main.html" });
resourceHandler.setResourceBase(webDir);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { new JsonpCallbackHandler(eventHubHandler), securityHandler });
server.setHandler(handlers);
securityHandler.setHandler(resourceHandler);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (server.isStarted()) {
try {
server.stop();
eventHubHandler.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, "Stop Jetty Hook"));
server.join();
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project saga by timurstrekalov.
the class FileServer method start.
public int start() {
final SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(0);
server.addConnector(connector);
final HandlerCollection handlers = new HandlerCollection();
final ResourceHandler resourceHandler = new ResourceHandler() {
@Override
protected void doResponseHeaders(final HttpServletResponse response, final Resource resource, final String mimeType) {
response.addDateHeader("Expires", 0L);
}
};
resourceHandler.setDirectoriesListed(true);
resourceHandler.setResourceBase(resourceBase);
handlers.addHandler(resourceHandler);
server.setHandler(handlers);
try {
server.start();
logger.info("File server started on port {}", connector.getLocalPort());
} catch (final Exception e) {
throw new RuntimeException(e);
}
return connector.getLocalPort();
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project zeppelin by apache.
the class TerminalThread method run.
public void run() {
ServerConnector connector = new ServerConnector(jettyServer);
connector.setPort(port);
jettyServer.addConnector(connector);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/terminal/");
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
ClassLoader clazz = TerminalThread.class.getClassLoader();
URL url = clazz.getResource("html");
if (url == null) {
throw new RuntimeException("Unable to find resource directory");
}
ResourceHandler resourceHandler = new ResourceHandler();
// Resolve file to directory
String webRootUri = url.toExternalForm();
LOGGER.info("WebRoot is " + webRootUri);
// debug
// webRootUri = "/home/hadoop/zeppelin-current/interpreter/sh";
resourceHandler.setResourceBase(webRootUri);
HandlerCollection handlers = new HandlerCollection(context, resourceHandler);
jettyServer.setHandler(handlers);
try {
ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
container.addEndpoint(TerminalSocket.class);
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.eclipse.jetty.server.handler.ResourceHandler in project gravel by gravel-st.
the class StartJetty method main.
public static void main(String[] args) throws Exception {
File fn;
int port = 8080;
if (args.length == 0) {
fn = ImageBootstrapper.defaultSourceFolder();
} else {
fn = new File(args[0]);
if (args.length != 1) {
port = Integer.parseInt(args[1]);
}
}
ImageBootstrapper.bootstrap(fn);
Server server = new Server(port);
ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContext.setContextPath("/browser");
ResourceHandler staticFilesHandler = new ResourceHandler();
staticFilesHandler.setResourceBase("src/main/html");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { servletContext, staticFilesHandler, new DefaultHandler() });
Object stServlet = getStServlet();
servletContext.addServlet(new ServletHolder(new JettyToStHttpServletConverter(stServlet)), "/*");
server.setHandler(handlers);
server.start();
server.join();
}
Aggregations