public void removeFromServer(Server server) { for (Connector connector : server.getConnectors()) { if (connector instanceof NetworkTrafficServerConnector) { NetworkTrafficServerConnector serverConnector = (NetworkTrafficServerConnector)connector; if (serverConnector.getPort() == port && serverConnector.getHost().equals(host)) { server.removeConnector(serverConnector); if (server.isStarted()) { try { serverConnector.stop(); } catch (Exception e) { throw new RuntimeException(e); } } } } } }
private static void initServer(int port, String contextPah) throws Exception { WebAppContext webApp = createWebApp(contextPah); NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(JETTY_SERVER); connector.setPort(port); connector.setSoLingerTime(-1); JETTY_SERVER.setStopAtShutdown(true); JETTY_SERVER.addConnector(connector); JETTY_SERVER.setHandler(webApp); }
/** * Launcher and program main method. * * @param args program arguments * @throws Exception thrown by Jetty */ public static void main(final String[] args) throws Exception { LOG.entry((Object[]) args); // Jetty server final Server server = new Server(); // Add Jetty Web XML and annotation configuration final ClassList classList = ClassList.setServerDefault(server); classList.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); //$NON-NLS-1$ //$NON-NLS-2$ // Add a connector to open a port try (final NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server)) { connector.setPort(8080); server.addConnector(connector); // Get the current class URL to add the current packaged war final ProtectionDomain mainDomain = Main.class.getProtectionDomain(); final URL location = mainDomain.getCodeSource().getLocation(); // Add the war location to the context, the default context path is / final WebAppContext webAppContext = new WebAppContext(); webAppContext.setWar(location.toExternalForm()); server.setHandler(webAppContext); // Start the Jetty server server.start(); server.join(); } LOG.exit(); }
public DistributionClientDownloadServer(String host, int port, String distributionClientBasePath, boolean directoriesListed) { server=new org.eclipse.jetty.server.Server(); connector = new NetworkTrafficServerConnector(server); connector.setPort(port); connector.setHost(host); server.setConnectors(new Connector[]{connector}); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setBaseResource(Resource.newResource(new File(distributionClientBasePath))); resourceHandler.setDirectoriesListed(directoriesListed); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] {resourceHandler, new DefaultHandler() }); server.setHandler(handlers); }
public void start() { System.setProperty("java.net.preferIPv4Stack", "true");//TODO optional? server = new org.eclipse.jetty.server.Server(); // HashSessionIdManager sessionIdManager = new HashSessionIdManager(); // server.setSessionIdManager(sessionIdManager); connector = new NetworkTrafficServerConnector(server); connector.setPort(httpPort); connector.setHost(host); server.addConnector(connector); ServletContextHandler contextHandler = new ServletContextHandler(); SessionHandler sessionHandler = new SessionHandler(); // HashSessionManager sessionManager = new HashSessionManager(); // sessionManager.setMaxInactiveInterval(sessionTimeoutS); // sessionManager.setSessionCookie(sessionManager.getSessionCookie()+httpPort); //avoid session mix up for 2 server running as localhost // sessionHandler.setSessionManager(sessionManager); contextHandler.setSessionHandler(sessionHandler); contextHandler.addServlet(new ServletHolder(new ServletContainer(jerseySetup(restResource))), "/applicationServer/*"); ErrorHandler errorHandler = new ErrorHandler(); errorHandler.setShowStacks(true); contextHandler.setErrorHandler(errorHandler); GzipHandler gzipHandler = new GzipHandler(); // HashSet<String> mimeTypes = new HashSet<>(); // mimeTypes.add("text/html"); // mimeTypes.add("text/plain"); // mimeTypes.add("text/css"); // mimeTypes.add("application/x-javascript"); // mimeTypes.add("application/json"); gzipHandler.setMinGzipSize(0); // gzipHandler.setMimeTypes(mimeTypes); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[]{resourceHandler, contextHandler}); gzipHandler.setHandler(handlers); server.setHandler(gzipHandler); try { server.start(); } catch (Exception e) { throw new RuntimeException(e); } }