Search in sources :

Example 1 with GatewayWebsocketHandler

use of org.apache.knox.gateway.websockets.GatewayWebsocketHandler in project knox by apache.

the class GatewayServer method createHandlers.

private static HandlerCollection createHandlers(final GatewayConfig config, final GatewayServices services, final ContextHandlerCollection contexts, final Map<String, Integer> topologyPortMap) {
    final Map<String, Handler> contextToHandlerMap = new HashMap<>();
    if (contexts.getHandlers() != null) {
        Arrays.asList(contexts.getHandlers()).stream().filter(h -> h instanceof WebAppContext).forEach(h -> contextToHandlerMap.put(((WebAppContext) h).getContextPath(), h));
    }
    HandlerCollection handlers = new HandlerCollection();
    RequestLogHandler logHandler = new RequestLogHandler();
    logHandler.setRequestLog(new AccessHandler());
    TraceHandler traceHandler = new TraceHandler();
    traceHandler.setHandler(contexts);
    traceHandler.setTracedBodyFilter(System.getProperty("org.apache.knox.gateway.trace.body.status.filter"));
    CorrelationHandler correlationHandler = new CorrelationHandler();
    correlationHandler.setHandler(traceHandler);
    // Used to correct the {target} part of request with Topology Port Mapping feature
    final PortMappingHelperHandler portMappingHandler = new PortMappingHelperHandler(config);
    portMappingHandler.setHandler(correlationHandler);
    if (config.isGatewayPortMappingEnabled()) {
        /* Do the virtual host bindings for all the defined topology port mapped
      *  contexts except for the one that has gateway port to prevent issues
      *  with context deployment */
        topologyPortMap.entrySet().stream().filter(e -> !e.getValue().equals(config.getGatewayPort())).forEach(entry -> {
            log.createJettyHandler(entry.getKey());
            final Handler context = contextToHandlerMap.get("/" + config.getGatewayPath() + "/" + entry.getKey());
            if (context != null) {
                ((WebAppContext) context).setVirtualHosts(new String[] { "@" + entry.getKey().toLowerCase(Locale.ROOT) });
            } else {
                // no topology found for mapping entry.getKey()
                log.noMappedTopologyFound(entry.getKey());
            }
        });
    }
    handlers.addHandler(logHandler);
    if (config.isWebsocketEnabled()) {
        final GatewayWebsocketHandler websocketHandler = new GatewayWebsocketHandler(config, services);
        websocketHandler.setHandler(portMappingHandler);
        handlers.addHandler(websocketHandler);
    } else {
        handlers.addHandler(portMappingHandler);
    }
    return handlers;
}
Also used : Arrays(java.util.Arrays) DeploymentException(org.apache.knox.gateway.deploy.DeploymentException) SessionCookieConfig(javax.servlet.SessionCookieConfig) KeyStoreException(java.security.KeyStoreException) AuditServiceFactory(org.apache.knox.gateway.audit.api.AuditServiceFactory) StringUtils(org.apache.commons.lang3.StringUtils) ServerSocket(java.net.ServerSocket) URLClassLoader(java.net.URLClassLoader) Configuration(org.eclipse.jetty.webapp.Configuration) Document(org.w3c.dom.Document) Map(java.util.Map) KnoxErrorHandler(org.apache.knox.gateway.trace.KnoxErrorHandler) GatewayConfigurationException(org.apache.knox.gateway.config.GatewayConfigurationException) Server(org.eclipse.jetty.server.Server) Path(java.nio.file.Path) DeploymentFactory(org.apache.knox.gateway.deploy.DeploymentFactory) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) PortMappingHelperHandler(org.apache.knox.gateway.filter.PortMappingHelperHandler) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) SSLService(org.apache.knox.gateway.services.security.SSLService) AccessHandler(org.apache.knox.gateway.trace.AccessHandler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) GatewayWebsocketHandler(org.apache.knox.gateway.websockets.GatewayWebsocketHandler) XmlUtils(org.apache.knox.gateway.util.XmlUtils) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) FilenameFilter(java.io.FilenameFilter) TransformerException(javax.xml.transform.TransformerException) MessagesFactory(org.apache.knox.gateway.i18n.messages.MessagesFactory) ArrayList(java.util.ArrayList) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Properties(java.util.Properties) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Files(java.nio.file.Files) Auditor(org.apache.knox.gateway.audit.api.Auditor) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) File(java.io.File) Paths(java.nio.file.Paths) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) GatewayServices(org.apache.knox.gateway.services.GatewayServices) AdvancedServiceDiscoveryConfigurationMonitor(org.apache.knox.gateway.topology.discovery.advanced.AdvancedServiceDiscoveryConfigurationMonitor) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Urls(org.apache.knox.gateway.util.Urls) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) TopologyListener(org.apache.knox.gateway.topology.TopologyListener) HadoopXmlResourceMonitor(org.apache.knox.gateway.topology.hadoop.xml.HadoopXmlResourceMonitor) TopologyEvent(org.apache.knox.gateway.topology.TopologyEvent) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Locale(java.util.Locale) ResourcesFactory(org.apache.knox.gateway.i18n.resources.ResourcesFactory) TraceHandler(org.apache.knox.gateway.trace.TraceHandler) CorrelationHandler(org.apache.knox.gateway.filter.CorrelationHandler) Topology(org.apache.knox.gateway.topology.Topology) URI(java.net.URI) HadoopXmlResourceParser(org.apache.knox.gateway.topology.hadoop.xml.HadoopXmlResourceParser) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceLoader(java.util.ServiceLoader) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ParseException(org.apache.commons.cli.ParseException) SAXException(org.xml.sax.SAXException) Pattern(java.util.regex.Pattern) ActionOutcome(org.apache.knox.gateway.audit.api.ActionOutcome) ServiceType(org.apache.knox.gateway.services.ServiceType) Socket(java.net.Socket) GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Application(org.apache.knox.gateway.topology.Application) ServiceDefinitionRegistry(org.apache.knox.gateway.services.registry.ServiceDefinitionRegistry) Node(org.w3c.dom.Node) CommandLine(org.apache.commons.cli.CommandLine) OutputStreamWriter(java.io.OutputStreamWriter) OutputStream(java.io.OutputStream) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) ResourceType(org.apache.knox.gateway.audit.api.ResourceType) CertificateException(java.security.cert.CertificateException) AuditConstants(org.apache.knox.gateway.audit.log4j.audit.AuditConstants) ExplodedExporter(org.jboss.shrinkwrap.api.exporter.ExplodedExporter) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Action(org.apache.knox.gateway.audit.api.Action) AliasServiceException(org.apache.knox.gateway.services.security.AliasServiceException) Connector(org.eclipse.jetty.server.Connector) Comparator(java.util.Comparator) InputStream(java.io.InputStream) CorrelationHandler(org.apache.knox.gateway.filter.CorrelationHandler) TraceHandler(org.apache.knox.gateway.trace.TraceHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PortMappingHelperHandler(org.apache.knox.gateway.filter.PortMappingHelperHandler) KnoxErrorHandler(org.apache.knox.gateway.trace.KnoxErrorHandler) PortMappingHelperHandler(org.apache.knox.gateway.filter.PortMappingHelperHandler) AccessHandler(org.apache.knox.gateway.trace.AccessHandler) GatewayWebsocketHandler(org.apache.knox.gateway.websockets.GatewayWebsocketHandler) Handler(org.eclipse.jetty.server.Handler) TraceHandler(org.apache.knox.gateway.trace.TraceHandler) CorrelationHandler(org.apache.knox.gateway.filter.CorrelationHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) AccessHandler(org.apache.knox.gateway.trace.AccessHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) GatewayWebsocketHandler(org.apache.knox.gateway.websockets.GatewayWebsocketHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Aggregations

File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Serializable (java.io.Serializable)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1