1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.websockets.jsr;
20
21 import io.undertow.servlet.ServletExtension;
22 import io.undertow.servlet.Servlets;
23 import io.undertow.servlet.api.DeploymentInfo;
24 import io.undertow.servlet.api.ThreadSetupHandler;
25 import io.undertow.servlet.core.ContextClassLoaderSetupAction;
26 import io.undertow.servlet.spec.ServletContextImpl;
27 import io.undertow.connector.ByteBufferPool;
28 import io.undertow.websockets.extensions.ExtensionHandshake;
29 import org.xnio.XnioWorker;
30
31 import javax.servlet.DispatcherType;
32 import javax.servlet.FilterRegistration;
33 import javax.servlet.ServletContext;
34 import javax.servlet.ServletContextEvent;
35 import javax.servlet.ServletContextListener;
36 import javax.websocket.DeploymentException;
37 import javax.websocket.Extension;
38 import javax.websocket.server.ServerContainer;
39 import javax.websocket.server.ServerEndpointConfig;
40 import java.net.InetSocketAddress;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.EnumSet;
44 import java.util.List;
45 import java.util.function.Supplier;
46
47 /**
48  * @author Stuart Douglas
49  */

50 public class Bootstrap implements ServletExtension {
51
52     public static final String FILTER_NAME = "Undertow Web Socket Filter";
53
54     @Override
55     public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
56         WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
57
58         if (info == null) {
59             return;
60         }
61         Supplier<XnioWorker> worker = info.getWorker();
62         ByteBufferPool buffers = info.getBuffers();
63         if(buffers == null) {
64             ServerWebSocketContainer defaultContainer = UndertowContainerProvider.getDefaultContainer();
65             if(defaultContainer == null) {
66                 throw JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNullAndNoDefault();
67             }
68             JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNull();
69             buffers = defaultContainer.getBufferPool();
70         }
71
72         final List<ThreadSetupHandler> setup = new ArrayList<>();
73         setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
74         setup.addAll(deploymentInfo.getThreadSetupActions());
75
76         InetSocketAddress bind = null;
77         if(info.getClientBindAddress() != null) {
78             bind = new InetSocketAddress(info.getClientBindAddress(), 0);
79         }
80         List<Extension> extensions = new ArrayList<>();
81         for(ExtensionHandshake e: info.getExtensions()) {
82             extensions.add(new ExtensionImpl(e.getName(), Collections.emptyList()));
83         }
84         ServerWebSocketContainer container = new ServerWebSocketContainer(deploymentInfo.getClassIntrospecter(), servletContext.getClassLoader(), worker, buffers, setup, info.isDispatchToWorkerThread(), bind, info.getReconnectHandler(), extensions);
85         try {
86             for (Class<?> annotation : info.getAnnotatedEndpoints()) {
87                 container.addEndpoint(annotation);
88             }
89             for(ServerEndpointConfig programatic : info.getProgramaticEndpoints()) {
90                 container.addEndpoint(programatic);
91             }
92         } catch (DeploymentException e) {
93             throw new RuntimeException(e);
94         }
95         servletContext.setAttribute(ServerContainer.class.getName(), container);
96         info.containerReady(container);
97         SecurityActions.addContainer(deploymentInfo.getClassLoader(), container);
98
99         deploymentInfo.addListener(Servlets.listener(WebSocketListener.class));
100         deploymentInfo.addDeploymentCompleteListener(new ServletContextListener() {
101             @Override
102             public void contextInitialized(ServletContextEvent sce) {
103                 container.validateDeployment();
104             }
105
106             @Override
107             public void contextDestroyed(ServletContextEvent sce) {
108
109             }
110         });
111     }
112
113     private static final class WebSocketListener implements ServletContextListener {
114
115         private ServerWebSocketContainer container;
116
117         @Override
118         public void contextInitialized(ServletContextEvent sce) {
119             container = (ServerWebSocketContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
120             FilterRegistration.Dynamic filter = sce.getServletContext().addFilter(FILTER_NAME, JsrWebSocketFilter.class);
121             sce.getServletContext().addListener(JsrWebSocketFilter.LogoutListener.class);
122             filter.setAsyncSupported(true);
123             if(!container.getConfiguredServerEndpoints().isEmpty()){
124                 filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true"/*");
125             } else {
126                 container.setContextToAddFilter((ServletContextImpl) sce.getServletContext());
127             }
128         }
129
130         @Override
131         public void contextDestroyed(ServletContextEvent sce) {
132             SecurityActions.removeContainer(sce.getServletContext().getClassLoader());
133             container.close();
134         }
135     }
136
137 }
138