use of org.eclipse.jetty.server.handler.HandlerList 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.HandlerList in project JsonPath by jayway.
the class Main method main.
public static void main(String[] args) throws Exception {
String configPort = "8080";
if (args.length > 0) {
configPort = args[0];
}
String port = System.getProperty("server.http.port", configPort);
System.out.println("Server started on port: " + port);
Server server = new Server();
server.setConnectors(new Connector[] { createConnector(server, Integer.parseInt(port)) });
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/api");
ServletHolder servletHolder = new ServletHolder(createJerseyServlet());
servletHolder.setInitOrder(1);
context.addServlet(servletHolder, "/*");
WebAppContext webAppContext = new WebAppContext();
webAppContext.setServer(server);
webAppContext.setContextPath("/");
String resourceBase = System.getProperty("resourceBase");
if (resourceBase != null) {
webAppContext.setResourceBase(resourceBase);
} else {
webAppContext.setResourceBase(Main.class.getResource("/webapp").toExternalForm());
}
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { context, webAppContext });
server.setHandler(handlers);
server.start();
server.join();
}
use of org.eclipse.jetty.server.handler.HandlerList in project alluxio by Alluxio.
the class WebServer method addHandler.
/**
* Adds a handler.
*
* @param handler the handler to add
*/
public void addHandler(AbstractHandler handler) {
HandlerList handlers = new HandlerList();
handlers.addHandler(handler);
for (Handler h : mServer.getHandlers()) {
handlers.addHandler(h);
}
mServer.setHandler(handlers);
}
use of org.eclipse.jetty.server.handler.HandlerList in project jvarkit by lindenb.
the class TViewServer method doWork.
@Override
public int doWork(final List<String> args) {
Server server = null;
try {
final List<File> samFiles = IOUtil.unrollFiles(args.stream().map(S -> new File(S)).collect(Collectors.toList()), ".bam");
if (samFiles.isEmpty()) {
LOG.error("No BAM file defined");
return -1;
}
if (this.optionalReferenceFile == null) {
LOG.warn("No reference file defined");
} else {
final SAMSequenceDictionary dict = SAMSequenceDictionaryExtractor.extractDictionary(this.optionalReferenceFile);
if (dict == null || dict.isEmpty()) {
LOG.error("Empty/No dict in " + this.optionalReferenceFile);
return -1;
}
samFiles.forEach(F -> {
final SAMSequenceDictionary samdict = SAMSequenceDictionaryExtractor.extractDictionary(F);
if (!SequenceUtil.areSequenceDictionariesEqual(samdict, dict)) {
throw new JvarkitException.DictionariesAreNotTheSame(samdict, dict);
}
});
}
samFiles.forEach(F -> {
IOUtil.assertFileIsReadable(F);
});
server = new Server(this.port);
final HandlerList handlers = new HandlerList();
handlers.addHandler(new SamViewHandler(samFiles));
server.setHandler(handlers);
LOG.info("Starting " + TViewServer.class.getName() + " on http://localhost:" + this.port);
server.start();
if (this.shutdownAferSeconds > 0) {
final Server theServer = server;
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
LOG.info("automatic shutdown after " + shutdownAferSeconds);
try {
theServer.stop();
} catch (final Throwable err2) {
LOG.error(err2);
}
}
}, 1000 * this.shutdownAferSeconds);
}
server.join();
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
} finally {
if (server != null) {
server.destroy();
}
}
}
use of org.eclipse.jetty.server.handler.HandlerList in project onebusaway-application-modules by camsys.
the class WebappBootstrapMain method run.
public static void run(URL warUrl, boolean consoleMode, String[] args) throws Exception {
if (args.length == 0 || isHelp(args)) {
BootstrapCommon.printUsage(WebappBootstrapMain.class, "usage-webapp.txt");
System.exit(-1);
}
Options options = createOptions();
Parser parser = new GnuParser();
CommandLine cli = parser.parse(options, args);
args = cli.getArgs();
if (args.length != 1) {
BootstrapCommon.printUsage(WebappBootstrapMain.class, "usage-webapp.txt");
System.exit(-1);
}
String bundlePath = args[0];
System.setProperty("bundlePath", bundlePath);
int port = 8080;
if (cli.hasOption(ARG_PORT))
port = Integer.parseInt(cli.getOptionValue(ARG_PORT));
Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(port);
server.setConnectors(new Connector[] { connector });
WebAppContext context = new WebAppContext();
context.setContextPath("/");
// context.setWelcomeFiles(new String[] {"index.action"});
context.setWar(warUrl.toExternalForm());
// We store the command line object as a webapp context attribute so it can
// be used by the context loader to inject additional beans as needed
context.setAttribute(WebappCommon.COMMAND_LINE_CONTEXT_ATTRIBUTE, cli);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { new WelcomeFileHandler(context), context });
server.setHandler(handlers);
/**
* We specify a Jetty override descriptor to inject additional definitions
* into our web.xml configuration. Specifically, we specify a
* <context-param> to specify the "contextClass" that will be used to create
* our Spring ApplicationContext. We create a custom version that will
* inject additional beans based on command-line arguments.
*/
context.addOverrideDescriptor("/WEB-INF/override-web.xml");
if (cli.hasOption(WebappCommon.ARG_BUILD)) {
System.setProperty("hibernate.hbm2ddl.auto", "update");
System.setProperty("bundleCacheDir", bundlePath + "/cache");
System.setProperty("gtfsPath", cli.getOptionValue(WebappCommon.ARG_GTFS_PATH));
context.addOverrideDescriptor("/WEB-INF/builder-override-web.xml");
}
try {
server.start();
System.err.println("=============================================================");
System.err.println("=");
System.err.println("= Your OneBusAway instance has started. Browse to:");
System.err.println("=");
System.err.println("= http://localhost:" + port + "/");
System.err.println("=");
System.err.println("= to see your instance in action.");
if (consoleMode) {
System.err.println("=");
System.err.println("= When you are finished, press return to exit gracefully...");
}
System.err.println("=============================================================");
if (consoleMode) {
System.in.read();
server.stop();
server.join();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
Aggregations