public ServletHolder addServlet(Servlet servlet, String subpath) { ServletHolder holderEvents = new ServletHolder(servlet); String fullPath = "/" + subpath + "/*"; synchronized (this.context) { this.context.addServlet(holderEvents, fullPath); } System.out.println( "new servlet launched at " + server.getURI().toString().substring(0, server.getURI().toString().length() - 1) + fullPath); return holderEvents; }
public static String run() throws Exception { int port = PORT; // the port thex server will listen to for HTTP requests Server server = new Server(port); // (1) Use a ServletContextHandler to hold a "context" (our application) // that will be deployed on the server. // The parameter is a bitwise "or" of options, defined in ServletContextHandler. // Options are: SESSIONS, NO_SESSIONS, SECURITY, NO_SECURITY ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); // (2) Define the path that server should map to our context. // If you use "/" it means the server root. // context.setContextPath("/ske"); // (3) Add servlets and mapping of requests to requests to servlets to the ContextHandler. // The ServletContextHandler class gives you several ways to do this: // To add a Servlet class and its pathspec: // context.addServlet( Class<? extends Servlet> clazz, String pathspec ) // To add an object (a ServletHolder): // context.addServlet( ServletHolder servletHolder, String pathspec ) // A Jetty ServletHolder holds a javax.servlet.Servlet instance along with a name, // initialization parameters, and state information. It implements the ServletConfig interface. // Here we use a ServletHolder to hold a Jersey ServletContainer. ServletHolder holder = new ServletHolder(org.glassfish.jersey.servlet.ServletContainer.class); // (4) Configure the Jersey ServletContainer so it will manage our resource // classes and pass HTTP requests to our resources. // Do this by setting initialization parameters. // The ServletContainer javadoc tells you what the initialization parameter are. // This initialization parameter tells Jersey to auto-configure all resource classes // in the named package(s). holder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "contact.resource"); context.addServlet(holder, "/*"); // (5) Add the context (our application) to the Jetty server server.setHandler(context); System.out.println("Starting Jetty server on port " + port); server.start(); return server.getURI().toString(); }
private int getServerPort(Server server) { return server.getURI().getPort(); }
public WebServer(final int port, String uiPath, String tasksPath, final Simulator simulator) throws Exception { this.server = new Server(); final ServerConnector connector = new ServerConnector(server); connector.setPort(port); this.server.addConnector(connector); this.uiPath = uiPath; this.tasksPath = tasksPath; this.context = new ServletContextHandler(ServletContextHandler.SESSIONS); // serve UI webpage (after dynamically setting server ip) HttpServlet ui_servlet = new IPTokenHTTPServlet(port, UI_PATH); this.context.addServlet(new ServletHolder(ui_servlet), "/"); // serve UI Process webpage (after dynamically setting server ip) HttpServlet ui_process_servlet = new IPTokenHTTPServlet(port, UI_PROCESS_PATH); this.context.addServlet(new ServletHolder(ui_process_servlet), "/uiprocess"); // related static resources ContextHandler resourcesContext = new ContextHandler(); resourcesContext.setContextPath("/resources"); ResourceHandler rh = new ResourceHandler(); rh.setBaseResource(Resource.newClassPathResource(STATIC_RESOURCES_PATH)); resourcesContext.setHandler(rh); ContextHandler webjarsContext = new ContextHandler(); webjarsContext.setContextPath("/webjars/"); rh = new ResourceHandler() { @Override public Resource getResource(String path) throws MalformedURLException { Resource resource = Resource.newClassPathResource(path); if (resource == null || !resource.exists()) { resource = Resource.newClassPathResource(WEBJARS_RESOURCES_PATH + path); } return resource; } }; webjarsContext.setHandler(rh); ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[] {webjarsContext, resourcesContext, context}); server.setHandler(contexts); context.setContextPath("/"); // add REST bridge servlet String apiBasePath = eu.learnpad.sim.BridgeInterface.class.getAnnotation(Path.class).value(); context.addServlet( new SimulatorBridgeServletHolder(simulator, context, apiBasePath), apiBasePath + "*"); // start server this.server.start(); // set chat servlet ServletHolder holder = new ServletHolder(new DummyChatServlet()); String fullPath = "/chat/*"; this.context.addServlet(holder, fullPath); System.out.println( "chat servlet launched at " + server.getURI().toString().substring(0, server.getURI().toString().length() - 1) + fullPath); }