Search in sources :

Example 1 with Configuration

use of org.eclipse.jetty.webapp.Configuration in project nifi by apache.

the class JettyServer method configureConnectors.

private void configureConnectors(final Server server) throws ServerConfigurationException {
    // create the http configuration
    final HttpConfiguration httpConfiguration = new HttpConfiguration();
    final int headerSize = DataUnit.parseDataSize(props.getWebMaxHeaderSize(), DataUnit.B).intValue();
    httpConfiguration.setRequestHeaderSize(headerSize);
    httpConfiguration.setResponseHeaderSize(headerSize);
    if (props.getPort() != null) {
        final Integer port = props.getPort();
        if (port < 0 || (int) Math.pow(2, 16) <= port) {
            throw new ServerConfigurationException("Invalid HTTP port: " + port);
        }
        logger.info("Configuring Jetty for HTTP on port: " + port);
        final List<Connector> serverConnectors = Lists.newArrayList();
        final Map<String, String> httpNetworkInterfaces = props.getHttpNetworkInterfaces();
        if (httpNetworkInterfaces.isEmpty() || httpNetworkInterfaces.values().stream().filter(value -> !Strings.isNullOrEmpty(value)).collect(Collectors.toList()).isEmpty()) {
            // create the connector
            final ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration));
            // set host and port
            if (StringUtils.isNotBlank(props.getProperty(NiFiProperties.WEB_HTTP_HOST))) {
                http.setHost(props.getProperty(NiFiProperties.WEB_HTTP_HOST));
            }
            http.setPort(port);
            serverConnectors.add(http);
        } else {
            // add connectors for all IPs from http network interfaces
            serverConnectors.addAll(Lists.newArrayList(httpNetworkInterfaces.values().stream().map(ifaceName -> {
                NetworkInterface iface = null;
                try {
                    iface = NetworkInterface.getByName(ifaceName);
                } catch (SocketException e) {
                    logger.error("Unable to get network interface by name {}", ifaceName, e);
                }
                if (iface == null) {
                    logger.warn("Unable to find network interface named {}", ifaceName);
                }
                return iface;
            }).filter(Objects::nonNull).flatMap(iface -> Collections.list(iface.getInetAddresses()).stream()).map(inetAddress -> {
                // create the connector
                final ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration));
                // set host and port
                http.setHost(inetAddress.getHostAddress());
                http.setPort(port);
                return http;
            }).collect(Collectors.toList())));
        }
        // add all connectors
        serverConnectors.forEach(server::addConnector);
    }
    if (props.getSslPort() != null) {
        final Integer port = props.getSslPort();
        if (port < 0 || (int) Math.pow(2, 16) <= port) {
            throw new ServerConfigurationException("Invalid HTTPs port: " + port);
        }
        logger.info("Configuring Jetty for HTTPs on port: " + port);
        final List<Connector> serverConnectors = Lists.newArrayList();
        final Map<String, String> httpsNetworkInterfaces = props.getHttpsNetworkInterfaces();
        if (httpsNetworkInterfaces.isEmpty() || httpsNetworkInterfaces.values().stream().filter(value -> !Strings.isNullOrEmpty(value)).collect(Collectors.toList()).isEmpty()) {
            final ServerConnector https = createUnconfiguredSslServerConnector(server, httpConfiguration);
            // set host and port
            if (StringUtils.isNotBlank(props.getProperty(NiFiProperties.WEB_HTTPS_HOST))) {
                https.setHost(props.getProperty(NiFiProperties.WEB_HTTPS_HOST));
            }
            https.setPort(port);
            serverConnectors.add(https);
        } else {
            // add connectors for all IPs from https network interfaces
            serverConnectors.addAll(Lists.newArrayList(httpsNetworkInterfaces.values().stream().map(ifaceName -> {
                NetworkInterface iface = null;
                try {
                    iface = NetworkInterface.getByName(ifaceName);
                } catch (SocketException e) {
                    logger.error("Unable to get network interface by name {}", ifaceName, e);
                }
                if (iface == null) {
                    logger.warn("Unable to find network interface named {}", ifaceName);
                }
                return iface;
            }).filter(Objects::nonNull).flatMap(iface -> Collections.list(iface.getInetAddresses()).stream()).map(inetAddress -> {
                final ServerConnector https = createUnconfiguredSslServerConnector(server, httpConfiguration);
                // set host and port
                https.setHost(inetAddress.getHostAddress());
                https.setPort(port);
                return https;
            }).collect(Collectors.toList())));
        }
        // add all connectors
        serverConnectors.forEach(server::addConnector);
    }
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) Enumeration(java.util.Enumeration) ServletException(javax.servlet.ServletException) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) LoggerFactory(org.slf4j.LoggerFactory) JarFile(java.util.jar.JarFile) FlowService(org.apache.nifi.services.FlowService) StringUtils(org.apache.commons.lang3.StringUtils) InetAddress(java.net.InetAddress) ContentAccess(org.apache.nifi.web.ContentAccess) Configuration(org.eclipse.jetty.webapp.Configuration) AnnotationConfiguration(org.eclipse.jetty.annotations.AnnotationConfiguration) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) UiExtension(org.apache.nifi.ui.extension.UiExtension) UiExtensionType(org.apache.nifi.web.UiExtensionType) Filter(javax.servlet.Filter) Server(org.eclipse.jetty.server.Server) EnumSet(java.util.EnumSet) Collection(java.util.Collection) NetworkInterface(java.net.NetworkInterface) Set(java.util.Set) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ServletResponse(javax.servlet.ServletResponse) UiExtensionMapping(org.apache.nifi.ui.extension.UiExtensionMapping) ExtensionManager(org.apache.nifi.nar.ExtensionManager) DataUnit(org.apache.nifi.processor.DataUnit) DocGenerator(org.apache.nifi.documentation.DocGenerator) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) KeyStoreUtils(org.apache.nifi.security.util.KeyStoreUtils) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashMap(java.util.HashMap) BundleDetails(org.apache.nifi.bundle.BundleDetails) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) JarEntry(java.util.jar.JarEntry) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) SocketException(java.net.SocketException) Lists(com.google.common.collect.Lists) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ServletRequest(javax.servlet.ServletRequest) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) Logger(org.slf4j.Logger) WebApplicationContextUtils(org.springframework.web.context.support.WebApplicationContextUtils) HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletResponse(javax.servlet.http.HttpServletResponse) ExtensionMapping(org.apache.nifi.nar.ExtensionMapping) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) NiFiServer(org.apache.nifi.NiFiServer) ServerConnector(org.eclipse.jetty.server.ServerConnector) FileFilter(java.io.FileFilter) NiFiProperties(org.apache.nifi.util.NiFiProperties) Paths(java.nio.file.Paths) Resource(org.eclipse.jetty.util.resource.Resource) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) ServletContext(javax.servlet.ServletContext) BufferedReader(java.io.BufferedReader) Connector(org.eclipse.jetty.server.Connector) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Collections(java.util.Collections) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) SocketException(java.net.SocketException) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) NetworkInterface(java.net.NetworkInterface) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector)

Example 2 with Configuration

use of org.eclipse.jetty.webapp.Configuration in project shiro by apache.

the class AbstractContainerIT method startContainer.

@BeforeClass
public static void startContainer() throws Exception {
    EmbeddedJettyConfiguration config = EmbeddedJettyConfiguration.builder().withWebapp(getWarDir()).build();
    jetty = new EmbeddedJetty(config) {

        /**
         * Overriding with contents of this pull request, to make fragment scanning work.
         * https://github.com/mjeanroy/junit-servers/pull/3
         */
        protected WebAppContext createdWebAppContext() throws Exception {
            final String path = configuration.getPath();
            final String webapp = configuration.getWebapp();
            final String classpath = configuration.getClasspath();
            WebAppContext ctx = new WebAppContext();
            ctx.setClassLoader(Thread.currentThread().getContextClassLoader());
            ctx.setContextPath(path);
            // Useful for WebXmlConfiguration
            ctx.setBaseResource(newResource(webapp));
            ctx.setConfigurations(new Configuration[] { new WebInfConfiguration(), new WebXmlConfiguration(), new AnnotationConfiguration(), new JettyWebXmlConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration() });
            if (isNotBlank(classpath)) {
                // Fix to scan Spring WebApplicationInitializer
                // This will add compiled classes to jetty classpath
                // See: http://stackoverflow.com/questions/13222071/spring-3-1-webapplicationinitializer-embedded-jetty-8-annotationconfiguration
                // And more precisely: http://stackoverflow.com/a/18449506/1215828
                File classes = new File(classpath);
                FileResource containerResources = new FileResource(classes.toURI());
                ctx.getMetaData().addContainerResource(containerResources);
            }
            Server server = getDelegate();
            ctx.setParentLoaderPriority(true);
            ctx.setWar(webapp);
            ctx.setServer(server);
            // Add server context
            server.setHandler(ctx);
            return ctx;
        }
    };
    jetty.start();
    assertTrue(jetty.isStarted());
}
Also used : JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) WebXmlConfiguration(org.eclipse.jetty.webapp.WebXmlConfiguration) FragmentConfiguration(org.eclipse.jetty.webapp.FragmentConfiguration) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) WebXmlConfiguration(org.eclipse.jetty.webapp.WebXmlConfiguration) Configuration(org.eclipse.jetty.webapp.Configuration) AnnotationConfiguration(org.eclipse.jetty.annotations.AnnotationConfiguration) WebInfConfiguration(org.eclipse.jetty.webapp.WebInfConfiguration) MetaInfConfiguration(org.eclipse.jetty.webapp.MetaInfConfiguration) EmbeddedJettyConfiguration(com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration) WebInfConfiguration(org.eclipse.jetty.webapp.WebInfConfiguration) Server(org.eclipse.jetty.server.Server) FileResource(org.eclipse.jetty.util.resource.FileResource) EmbeddedJettyConfiguration(com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) EmbeddedJetty(com.github.mjeanroy.junit.servers.jetty.EmbeddedJetty) MetaInfConfiguration(org.eclipse.jetty.webapp.MetaInfConfiguration) FragmentConfiguration(org.eclipse.jetty.webapp.FragmentConfiguration) AnnotationConfiguration(org.eclipse.jetty.annotations.AnnotationConfiguration) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 3 with Configuration

use of org.eclipse.jetty.webapp.Configuration in project spring-boot by spring-projects.

the class JettyServletWebServerFactoryTests method mockConfiguration.

Configuration mockConfiguration(Class<? extends Configuration> type) {
    Configuration mock = mock(type);
    ClassMatcher classMatcher = new ClassMatcher();
    given(mock.getSystemClasses()).willReturn(classMatcher);
    given(mock.getServerClasses()).willReturn(classMatcher);
    return mock;
}
Also used : ClassMatcher(org.eclipse.jetty.webapp.ClassMatcher) AbstractConfiguration(org.eclipse.jetty.webapp.AbstractConfiguration) Configuration(org.eclipse.jetty.webapp.Configuration)

Example 4 with Configuration

use of org.eclipse.jetty.webapp.Configuration in project spring-boot by spring-projects.

the class JettyServletWebServerFactoryTests method jettyConfigurations.

@Test
void jettyConfigurations() throws Exception {
    JettyServletWebServerFactory factory = getFactory();
    Configuration[] configurations = new Configuration[] { mockConfiguration(Configuration1.class), mockConfiguration(Configuration2.class), mockConfiguration(Configuration3.class), mockConfiguration(Configuration4.class) };
    factory.setConfigurations(Arrays.asList(configurations[0], configurations[1]));
    factory.addConfigurations(configurations[2], configurations[3]);
    this.webServer = factory.getWebServer();
    InOrder ordered = inOrder((Object[]) configurations);
    for (Configuration configuration : configurations) {
        ordered.verify(configuration).configure(any(WebAppContext.class));
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) InOrder(org.mockito.InOrder) AbstractConfiguration(org.eclipse.jetty.webapp.AbstractConfiguration) Configuration(org.eclipse.jetty.webapp.Configuration) Test(org.junit.jupiter.api.Test)

Aggregations

Configuration (org.eclipse.jetty.webapp.Configuration)4 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)3 File (java.io.File)2 AnnotationConfiguration (org.eclipse.jetty.annotations.AnnotationConfiguration)2 Server (org.eclipse.jetty.server.Server)2 JettyWebXmlConfiguration (org.eclipse.jetty.webapp.JettyWebXmlConfiguration)2 EmbeddedJetty (com.github.mjeanroy.junit.servers.jetty.EmbeddedJetty)1 EmbeddedJettyConfiguration (com.github.mjeanroy.junit.servers.jetty.EmbeddedJettyConfiguration)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 BufferedReader (java.io.BufferedReader)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1