@Override public EmbeddedServletContainer getEmbeddedServletContainer( ServletContextInitializer... initializers) { JettyEmbeddedWebAppContext context = new JettyEmbeddedWebAppContext(); int port = (getPort() >= 0 ? getPort() : 0); InetSocketAddress address = new InetSocketAddress(getAddress(), port); Server server = createServer(address); configureWebAppContext(context, initializers); server.setHandler(addHandlerWrappers(context)); this.logger.info("Server initialized with port: " + port); if (getSsl() != null && getSsl().isEnabled()) { SslContextFactory sslContextFactory = new SslContextFactory(); configureSsl(sslContextFactory, getSsl()); AbstractConnector connector = getSslServerConnectorFactory() .getConnector(server, sslContextFactory, port); server.setConnectors(new Connector[] { connector }); } for (JettyServerCustomizer customizer : getServerCustomizers()) { customizer.customize(server); } if (this.useForwardHeaders) { new ForwardHeadersCustomizer().customize(server); } return getJettyEmbeddedServletContainer(server); }
@Override public AbstractConnector getConnector(Server server, SslContextFactory sslContextFactory, int port) { try { Class<?> connectorClass = Class .forName("org.eclipse.jetty.server.ssl.SslSocketConnector"); AbstractConnector connector = (AbstractConnector) connectorClass .getConstructor(SslContextFactory.class) .newInstance(sslContextFactory); connector.getClass().getMethod("setPort", int.class).invoke(connector, port); return connector; } catch (Exception ex) { throw new IllegalStateException(ex); } }
private void customizeConnectionTimeout( JettyEmbeddedServletContainerFactory factory, final int connectionTimeout) { factory.addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { for (org.eclipse.jetty.server.Connector connector : server .getConnectors()) { if (connector instanceof AbstractConnector) { ((AbstractConnector) connector) .setIdleTimeout(connectionTimeout); } } } }); }
private static Server setupJettyServer(VarOneConfiguration conf) { AbstractConnector connector = new SelectChannelConnector(); // Set some timeout options to make debugging easier. int timeout = 1000 * 30; connector.setMaxIdleTime(timeout); connector.setSoLingerTime(-1); connector.setHost(conf.getServerAddress()); connector.setPort(conf.getServerPort()); final Server server = new Server(); server.addConnector(connector); return server; }
protected void applyConfiguration(AbstractConnector connector) { connector.setAcceptorPriorityOffset(configuration.getAcceptorThreadPriorityOffset()); connector.setAcceptors(configuration.getAcceptorThreads()); connector.setAcceptQueueSize(configuration.getAcceptQueueSize()); connector.setLowResourcesMaxIdleTime(configuration.getLowResourcesMaxIdleTime()); connector.setMaxBuffers(configuration.getMaxBufferCount()); connector.setMaxIdleTime(configuration.getMaxIdleTime()); connector.setRequestBufferSize(configuration.getRequestBufferSize()); connector.setRequestHeaderSize(configuration.getRequestHeaderBufferSize()); connector.setResponseBufferSize(configuration.getResponseBufferSize()); connector.setResponseHeaderSize(configuration.getResponseHeaderBufferSize()); connector.setReuseAddress(configuration.isReuseAddress()); // Use X-Forwarded-For header for origin IP connector.setForwarded(true); }
@Override public Set<? extends AbstractConnector> build() { final Set<AbstractConnector> connectors = new HashSet<AbstractConnector>(); if (getConfiguration().isMixedMode()) { connectors.add(configureExternalHttpsConnector()); connectors.add(configureExternalHttpConnector()); } else if (getConfiguration().isSsl()) { connectors.add(configureExternalHttpsConnector()); } else { connectors.add(configureExternalHttpConnector()); } // apply the configured values to each connector for (AbstractConnector connector : connectors) { applyConfiguration(connector); } return connectors; }
private AbstractConnector configureExternalHttpsConnector() { logger.info("Creating https connector"); final SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setCertAlias(getConfiguration().getSslConfiguration().getKeyStoreAlias()); sslContextFactory.setKeyStorePath(getConfiguration().getSslConfiguration().getKeyStorePath()); sslContextFactory.setKeyStorePassword(getConfiguration().getSslConfiguration().getKeyStorePassword()); final Integer port = getConfiguration().getSslConfiguration().getPort() != null ? getConfiguration().getSslConfiguration().getPort() : getConfiguration().getPort(); final InstrumentedSslSocketConnector connector = new InstrumentedSslSocketConnector( this.metricRegistry, port, sslContextFactory, Clock.defaultClock()); connector.setName(EXTERNAL_HTTPS_CONNECTOR_NAME); return connector; }
private AbstractConnector createConnector(InetSocketAddress address, Server server) { if (ClassUtils.isPresent(CONNECTOR_JETTY_8, getClass().getClassLoader())) { return new Jetty8ConnectorFactory().createConnector(server, address, this.acceptors, this.selectors); } return new Jetty9ConnectorFactory().createConnector(server, address, this.acceptors, this.selectors); }
@Override public AbstractConnector createConnector(Server server, InetSocketAddress address, int acceptors, int selectors) { try { Class<?> connectorClass = ClassUtils.forName(CONNECTOR_JETTY_8, getClass().getClassLoader()); AbstractConnector connector = (AbstractConnector) connectorClass .newInstance(); ReflectionUtils.findMethod(connectorClass, "setPort", int.class) .invoke(connector, address.getPort()); ReflectionUtils.findMethod(connectorClass, "setHost", String.class) .invoke(connector, address.getHostName()); if (acceptors > 0) { ReflectionUtils.findMethod(connectorClass, "setAcceptors", int.class) .invoke(connector, acceptors); } if (selectors > 0) { Object selectorManager = ReflectionUtils .findMethod(connectorClass, "getSelectorManager") .invoke(connector); ReflectionUtils.findMethod(selectorManager.getClass(), "setSelectSets", int.class) .invoke(selectorManager, selectors); } return connector; } catch (Exception ex) { throw new RuntimeException("Failed to configure Jetty 8 connector", ex); } }
@Override public AbstractConnector createConnector(Server server, InetSocketAddress address, int acceptors, int selectors) { ServerConnector connector = new ServerConnector(server, acceptors, selectors); connector.setHost(address.getHostName()); connector.setPort(address.getPort()); for (ConnectionFactory connectionFactory : connector .getConnectionFactories()) { if (connectionFactory instanceof HttpConfiguration.ConnectionFactory) { ((HttpConfiguration.ConnectionFactory) connectionFactory) .getHttpConfiguration().setSendServerVersion(false); } } return connector; }
@Override public AbstractConnector createConnector(Server server, InetSocketAddress address, int acceptors, int selectors) { ServerConnector connector = new ServerConnector(server, acceptors, selectors); connector.setHost(address.getHostName()); connector.setPort(address.getPort()); return connector; }
@Override public EmbeddedServletContainer getEmbeddedServletContainer( ServletContextInitializer... initializers) { JettyEmbeddedWebAppContext context = new JettyEmbeddedWebAppContext(); int port = (getPort() >= 0 ? getPort() : 0); Server server = new Server(new InetSocketAddress(getAddress(), port)); configureWebAppContext(context, initializers); if (getCompression() != null && getCompression().getEnabled()) { HandlerWrapper gzipHandler = createGzipHandler(); gzipHandler.setHandler(context); server.setHandler(gzipHandler); } else { server.setHandler(context); } this.logger.info("Server initialized with port: " + port); if (getSsl() != null && getSsl().isEnabled()) { SslContextFactory sslContextFactory = new SslContextFactory(); configureSsl(sslContextFactory, getSsl()); AbstractConnector connector = getSslServerConnectorFactory() .getConnector(server, sslContextFactory, port); server.setConnectors(new Connector[] { connector }); } for (JettyServerCustomizer customizer : getServerCustomizers()) { customizer.customize(server); } if (this.useForwardHeaders) { new ForwardHeadersCustomizer().customize(server); } return getJettyEmbeddedServletContainer(server); }
private void configureProxiedConnector(AbstractConnector connector) { // Check to see if we are deployed behind a proxy // Refer to http://docs.codehaus.org/display/JETTY/Configuring+Connectors if (isXFFEnabled()) { connector.setForwarded(true); // default: "X-Forwarded-For" String forwardedForHeader = getXFFHeader(); if (forwardedForHeader != null) { connector.setForwardedForHeader(forwardedForHeader); } // default: "X-Forwarded-Server" String forwardedServerHeader = getXFFServerHeader(); if (forwardedServerHeader != null) { connector.setForwardedServerHeader(forwardedServerHeader); } // default: "X-Forwarded-Host" String forwardedHostHeader = getXFFHostHeader(); if (forwardedHostHeader != null) { connector.setForwardedHostHeader(forwardedHostHeader); } // default: none String hostName = getXFFHostName(); if (hostName != null) { connector.setHostHeader(hostName); } } connector.setStatsOn(JMXManager.isEnabled()); }
@Override public Set<? extends AbstractConnector> build() { final SocketConnector connector = new SocketConnector(); connector.setPort(getConfiguration().getAdminPort()); connector.setThreadPool(new QueuedThreadPool(8)); connector.setName(INTERNAL_CONNECTOR_NAME); return Sets.newHashSet(connector); }
private AbstractConnector configureExternalHttpConnector() { logger.info("Creating http connector"); final InstrumentedSelectChannelConnector connector = new InstrumentedSelectChannelConnector( this.metricRegistry, getConfiguration().getPort(), Clock.defaultClock()); connector.setName(EXTERNAL_HTTP_CONNECTOR_NAME); connector.setUseDirectBuffers(true); return connector; }
AbstractConnector getConnector(Server server, SslContextFactory sslContextFactory, int port);
AbstractConnector createConnector(Server server, InetSocketAddress address, int acceptors, int selectors);
protected AbstractConnector createConnectorJettyInternal(Server server, JettyHttpEndpoint endpoint, SslContextFactory sslcf) { try { String hosto = endpoint.getHttpUri().getHost(); int porto = endpoint.getPort(); org.eclipse.jetty.server.HttpConfiguration httpConfig = new org.eclipse.jetty.server.HttpConfiguration(); httpConfig.setSendServerVersion(endpoint.isSendServerVersion()); httpConfig.setSendDateHeader(endpoint.isSendDateHeader()); httpConfig.setSendDateHeader(endpoint.isSendDateHeader()); if (requestBufferSize != null) { // Does not work //httpConfig.setRequestBufferSize(requestBufferSize); } if (requestHeaderSize != null) { httpConfig.setRequestHeaderSize(requestHeaderSize); } if (responseBufferSize != null) { httpConfig.setOutputBufferSize(responseBufferSize); } if (responseHeaderSize != null) { httpConfig.setResponseHeaderSize(responseHeaderSize); } HttpConnectionFactory httpFactory = new org.eclipse.jetty.server.HttpConnectionFactory(httpConfig); ArrayList<ConnectionFactory> connectionFactories = new ArrayList<ConnectionFactory>(); ServerConnector result = new org.eclipse.jetty.server.ServerConnector(server); if (sslcf != null) { httpConfig.addCustomizer(new org.eclipse.jetty.server.SecureRequestCustomizer()); SslConnectionFactory scf = new org.eclipse.jetty.server.SslConnectionFactory(sslcf, "HTTP/1.1"); connectionFactories.add(scf); result.setDefaultProtocol("SSL-HTTP/1.1"); } connectionFactories.add(httpFactory); result.setConnectionFactories(connectionFactories); result.setPort(porto); if (hosto != null) { result.setHost(hosto); } /* if (getSocketConnectorProperties() != null && !"https".equals(endpoint.getProtocol())) { // must copy the map otherwise it will be deleted Map<String, Object> properties = new HashMap<String, Object>(getSocketConnectorProperties()); IntrospectionSupport.setProperties(httpConfig, properties); if (properties.size() > 0) { throw new IllegalArgumentException("There are " + properties.size() + " parameters that couldn't be set on the SocketConnector." + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector." + " Unknown parameters=[" + properties + "]"); } } else*/ if (getSslSocketConnectorProperties() != null && "https".equals(endpoint.getProtocol())) { // must copy the map otherwise it will be deleted Map<String, Object> properties = new HashMap<String, Object>(getSslSocketConnectorProperties()); IntrospectionSupport.setProperties(sslcf, properties); if (properties.size() > 0) { throw new IllegalArgumentException("There are " + properties.size() + " parameters that couldn't be set on the SocketConnector." + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector." + " Unknown parameters=[" + properties + "]"); } } return result; } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } }
/** * Adds a listener to the <code>httpServer</code>. The listener is created and configured * according to the Jetty configuration. * @param httpServer the server to add the listener to * @param jettyConfig The configuraton for the server. Do not read the port at this point. * @param port The port to listen on. The configuration may have been overriden by this value. * @throws UnknownHostException if an invalid hostname was specified in the Mulgara server configuration */ @SuppressWarnings("deprecation") private void addConnector(Server httpServer, JettyConnector jettyConfig, int port) throws UnknownHostException { if (httpServer == null) throw new IllegalArgumentException("Null \"httpServer\" parameter"); if (logger.isDebugEnabled()) logger.debug("Adding socket listener"); // create and configure a listener AbstractConnector connector = new BlockingChannelConnector(); if ((hostName != null) && !hostName.equals("")) { connector.setHost(hostName); if (logger.isDebugEnabled()) logger.debug("Servlet container listening on host " + hostName); } else { hostName = EmbeddedMulgaraServer.getResolvedLocalHost(); if (logger.isDebugEnabled()) logger.debug("Servlet container listening on all host interfaces"); } // Each connector will get its own thread pool, so that they may be configured separately. // If a connector does not have its own thread pool, it inherits one from the server that it might // share with other connectors. QueuedThreadPool threadPool = new QueuedThreadPool(); if (jettyConfig.hasMaxThreads()) { threadPool.setMaxThreads(jettyConfig.getMaxThreads()); } connector.setThreadPool(threadPool); connector.setPort(port); if (jettyConfig.hasMaxIdleTimeMs()) connector.setMaxIdleTime(jettyConfig.getMaxIdleTimeMs()); if (jettyConfig.hasLowResourceMaxIdleTimeMs()) connector.setLowResourceMaxIdleTime(jettyConfig.getLowResourceMaxIdleTimeMs()); if (jettyConfig.hasAcceptors()) { int acceptors = jettyConfig.getAcceptors(); // Acceptors are part of the thread pool, but they delegate handling of servlet // requests to another thread in the pool. Therefore, the number of acceptors // must be strictly less than the maximum number of threads in the pool. int acceptorLimit = threadPool.getMaxThreads() - 1; if (acceptors > acceptorLimit) { logger.warn("Acceptor threads set beyond HTTP Server limits. Reducing from" + acceptors + " to " + acceptorLimit); acceptors = acceptorLimit; } connector.setAcceptors(acceptors); } // add the listener to the http server httpServer.addConnector(connector); }
protected abstract AbstractConnector createConnectorJettyInternal(Server server, JettyHttpEndpoint endpoint, SslContextFactory sslcf);
public abstract Set<? extends AbstractConnector> build();