private List<Connector> buildAdminConnectors(MetricRegistry metricRegistry, Server server) { // threadpool is shared between all the connectors, so it should be managed by the server instead of the // individual connectors final QueuedThreadPool threadPool = new InstrumentedQueuedThreadPool(metricRegistry, adminMaxThreads, adminMinThreads); threadPool.setName("dw-admin"); server.addBean(threadPool); final List<Connector> connectors = new ArrayList<>(); for (ConnectorFactory factory : adminConnectors) { final Connector connector = factory.build(server, metricRegistry, "admin", threadPool); if (connector instanceof ContainerLifeCycle) { ((ContainerLifeCycle) connector).unmanage(threadPool); } connectors.add(connector); } return connectors; }
@Test public void shouldSetErrorHandlerForServer() throws Exception { jetty9Server.configure(); verify(server).addBean(any(JettyCustomErrorPageHandler.class)); }
private void initialize() { synchronized (this.monitor) { try { // Cache the connectors and then remove them to prevent requests being // handled before the application context is ready. this.connectors = this.server.getConnectors(); this.server.addBean(new AbstractLifeCycle() { @Override protected void doStart() throws Exception { for (Connector connector : JettyWebServer.this.connectors) { Assert.state(connector.isStopped(), () -> "Connector " + connector + " has been started prematurely"); } JettyWebServer.this.server.setConnectors(null); } }); // Start the server so that the ServletContext is available this.server.start(); this.server.setStopAtShutdown(false); } catch (Throwable ex) { // Ensure process isn't left running stopSilently(); throw new WebServerException("Unable to start embedded Jetty web server", ex); } } }
@Override public Connector build(Server server, MetricRegistry metrics, String name, @Nullable ThreadPool threadPool) { final HttpConfiguration httpConfig = buildHttpConfiguration(); final HttpConnectionFactory httpConnectionFactory = buildHttpConnectionFactory(httpConfig); final SslContextFactory sslContextFactory = configureSslContextFactory(new SslContextFactory()); sslContextFactory.addLifeCycleListener(logSslInfoOnStart(sslContextFactory)); server.addBean(sslContextFactory); server.addBean(new SslReload(sslContextFactory, this::configureSslContextFactory)); final SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()); final Scheduler scheduler = new ScheduledExecutorScheduler(); final ByteBufferPool bufferPool = buildBufferPool(); return buildConnector(server, scheduler, bufferPool, name, threadPool, new Jetty93InstrumentedConnectionFactory( sslConnectionFactory, metrics.timer(httpConnections())), httpConnectionFactory); }
/** Get/Set/Create the server default Configuration ClassList. * <p>Get the class list from: a Server bean; or the attribute (which can * either be a ClassList instance or an String[] of class names); or a new instance * with default configuration classes.</p> * <p>This method also adds the obtained ClassList instance as a dependent bean * on the server and clears the attribute</p> * @param server The server the default is for * @return the server default ClassList instance of the configuration classes for this server. Changes to this list will change the server default instance. */ public static ClassList setServerDefault(Server server) { ClassList cl=server.getBean(ClassList.class); if (cl!=null) return cl; cl=serverDefault(server); server.addBean(cl); server.setAttribute(ATTR,null); return cl; }
/** * Secure the web server with PAM. */ void setupPam(Builder b, Handler handler) { LoginService loginService = new PamLoginService(); webServer.addBean(loginService); ConstraintSecurityHandler security = new ConstraintSecurityHandler(); Constraint constraint = new PamConstraint(); ConstraintMapping mapping = new PamConstraintMapping(constraint); security.setConstraintMappings(Collections.singletonList(mapping)); security.setAuthenticator(b.pamAuthenticator); security.setLoginService(loginService); security.setHandler(handler); webServer.setHandler(security); }
protected Server buildServer(LifecycleEnvironment lifecycle, ThreadPool threadPool) { final Server server = new Server(threadPool); server.addLifeCycleListener(buildSetUIDListener()); lifecycle.attach(server); final ErrorHandler errorHandler = new ErrorHandler(); errorHandler.setServer(server); errorHandler.setShowStacks(false); server.addBean(errorHandler); server.setStopAtShutdown(true); server.setStopTimeout(shutdownGracePeriod.toMilliseconds()); return server; }
@Override public void configure() throws Exception { server.addEventListener(mbeans()); server.addConnector(plainConnector()); server.addConnector(sslConnector()); ContextHandlerCollection handlers = new ContextHandlerCollection(); deploymentManager.setContexts(handlers); createWebAppContext(); JettyCustomErrorPageHandler errorHandler = new JettyCustomErrorPageHandler(); webAppContext.setErrorHandler(errorHandler); webAppContext.setGzipHandler(gzipHandler()); server.addBean(errorHandler); server.addBean(deploymentManager); HandlerCollection serverLevelHandlers = new HandlerCollection(); serverLevelHandlers.setHandlers(new Handler[]{handlers}); server.setHandler(serverLevelHandlers); performCustomConfiguration(); server.setStopAtShutdown(true); }
@Override public Connector build(Server server, MetricRegistry metrics, String name, @Nullable ThreadPool threadPool) { // HTTP/2 requires that a server MUST support TLSv1.2 and TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 cipher // See http://http2.github.io/http2-spec/index.html#rfc.section.9.2.2 setSupportedProtocols(Collections.singletonList("TLSv1.2")); checkSupportedCipherSuites(); // Setup connection factories final HttpConfiguration httpConfig = buildHttpConfiguration(); final HttpConnectionFactory http1 = buildHttpConnectionFactory(httpConfig); final HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(httpConfig); http2.setMaxConcurrentStreams(maxConcurrentStreams); http2.setInitialStreamRecvWindow(initialStreamRecvWindow); final NegotiatingServerConnectionFactory alpn = new ALPNServerConnectionFactory(H2, H2_17); alpn.setDefaultProtocol(HTTP_1_1); // Speak HTTP 1.1 over TLS if negotiation fails final SslContextFactory sslContextFactory = configureSslContextFactory(new SslContextFactory()); sslContextFactory.addLifeCycleListener(logSslInfoOnStart(sslContextFactory)); server.addBean(sslContextFactory); server.addBean(new SslReload(sslContextFactory, this::configureSslContextFactory)); // We should use ALPN as a negotiation protocol. Old clients that don't support it will be served // via HTTPS. New clients, however, that want to use HTTP/2 will use TLS with ALPN extension. // If negotiation succeeds, the client and server switch to HTTP/2 protocol. final SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, "alpn"); return buildConnector(server, new ScheduledExecutorScheduler(), buildBufferPool(), name, threadPool, new Jetty93InstrumentedConnectionFactory(sslConnectionFactory, metrics.timer(httpConnections())), alpn, http2, http1); }
srv.addBean(mbContainer);
protected Handler addRequestLog(Server server, Handler handler, String name) { if (getRequestLogFactory().isEnabled()) { final RequestLogHandler requestLogHandler = new RequestLogHandler(); requestLogHandler.setRequestLog(getRequestLogFactory().build(name)); // server should own the request log's lifecycle since it's already started, // the handler might not become managed in case of an error which would leave // the request log stranded server.addBean(requestLogHandler.getRequestLog(), true); requestLogHandler.setHandler(handler); return requestLogHandler; } return handler; }
m_server.addBean(errorHandler);
new Password(properties.getProperty("eventhubhandler.password")), new String[]{"user"}); server.addBean(loginService);
if (JMXManager.isEnabled()) { JMXManager jmx = JMXManager.getInstance(); httpBindServer.addBean(jmx.getContainer());
deploymentManager.setContextAttribute(CONTAINER_INCLUDE_PATTERN_KEY, CONTAINER_INCLUDE_PATTERN_VALUE); deploymentManager.setContexts(contextHandlers); server.addBean(deploymentManager);
webServer.addBean(low);
private static void addAuthHandler(Server server, String auth, LoginAuthenticator authenticator, Handler handler) { server.addBean(LOGIN_SERVICE); Constraint constraint = new Constraint(); constraint.setName(auth); constraint.setRoles(new String[]{USER, ADMIN}); constraint.setAuthenticate(true); ConstraintMapping mapping = new ConstraintMapping(); mapping.setConstraint(constraint); mapping.setPathSpec("/*"); Set<String> knownRoles = new HashSet<>(); knownRoles.add(USER); knownRoles.add(ADMIN); List<ConstraintMapping> cm = new ArrayList<>(); cm.add(mapping); ConstraintSecurityHandler security = new ConstraintSecurityHandler(); security.setConstraintMappings(cm, knownRoles); security.setAuthenticator(authenticator); security.setLoginService(LOGIN_SERVICE); security.setHandler(handler); server.setHandler(security); }
server.addBean(new ScheduledExecutorScheduler("JettyScheduler", true), true);
server.addBean(loginService); securityHandler.setLoginService(loginService);