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 package io.undertow.websockets.jsr;
19
20 import io.undertow.servlet.api.InstanceFactory;
21 import io.undertow.servlet.api.InstanceHandle;
22 import io.undertow.servlet.handlers.ServletRequestContext;
23 import io.undertow.servlet.util.ImmediateInstanceHandle;
24 import io.undertow.websockets.WebSocketConnectionCallback;
25 import io.undertow.websockets.core.WebSocketChannel;
26 import io.undertow.websockets.jsr.annotated.AnnotatedEndpoint;
27 import io.undertow.websockets.jsr.handshake.HandshakeUtil;
28 import io.undertow.websockets.spi.WebSocketHttpExchange;
29 import org.xnio.IoUtils;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.websocket.Endpoint;
33 import javax.websocket.Extension;
34 import javax.websocket.server.ServerEndpointConfig;
35 import java.net.URI;
36 import java.security.Principal;
37 import java.util.Collections;
38
39 /**
40  * {@link WebSocketConnectionCallback} implementation which will setuo the {@link UndertowSession} and notify
41  * the {@link Endpoint} about the new session.
42  *
43  * @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
44  */

45 public final class EndpointSessionHandler implements WebSocketConnectionCallback {
46     private final ServerWebSocketContainer container;
47
48     public EndpointSessionHandler(ServerWebSocketContainer container) {
49         this.container = container;
50     }
51
52     /**
53      * Returns the {@link ServerWebSocketContainer} which was used for this {@link WebSocketConnectionCallback}.
54      */

55     ServerWebSocketContainer getContainer() {
56         return container;
57     }
58
59     @Override
60     public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
61         ConfiguredServerEndpoint config = HandshakeUtil.getConfig(channel);
62         try {
63             if(container.isClosed()) {
64                 //if the underlying container is closed we just reject
65                 channel.sendClose();
66                 channel.resumeReceives();
67                 return;
68             }
69             InstanceFactory<?> endpointFactory = config.getEndpointFactory();
70             ServerEndpointConfig.Configurator configurator = config.getEndpointConfiguration().getConfigurator();
71             final InstanceHandle<?> instance;
72             DefaultContainerConfigurator.setCurrentInstanceFactory(endpointFactory);
73             final Object instanceFromConfigurator = configurator.getEndpointInstance(config.getEndpointConfiguration().getEndpointClass());
74             final InstanceHandle<?> factoryInstance = DefaultContainerConfigurator.clearCurrentInstanceFactory();
75             if (factoryInstance == null) {
76                 instance = new ImmediateInstanceHandle<>(instanceFromConfigurator);
77             } else if (factoryInstance.getInstance() == instanceFromConfigurator) {
78                 instance = factoryInstance;
79             } else {
80                 //the default instance has been wrapped
81                 instance = new InstanceHandle<Object>() {
82                     @Override
83                     public Object getInstance() {
84                         return instanceFromConfigurator;
85                     }
86
87                     @Override
88                     public void release() {
89                         factoryInstance.release();
90                     }
91                 };
92             }
93
94             ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
95             Principal principal = exchange.getAttachment(HandshakeUtil.PRINCIPAL);
96             if(principal == null) {
97                 if(src.getServletRequest() instanceof HttpServletRequest) {
98                     principal = ((HttpServletRequest)src.getServletRequest()).getUserPrincipal();
99                 } else {
100                     principal = src.getOriginalRequest().getUserPrincipal();
101                 }
102             }
103             final InstanceHandle<Endpoint> endpointInstance;
104             if(config.getAnnotatedEndpointFactory() != null) {
105                 final AnnotatedEndpoint annotated = config.getAnnotatedEndpointFactory().createInstance(instance);
106                 endpointInstance = new InstanceHandle<Endpoint>() {
107                     @Override
108                     public Endpoint getInstance() {
109                         return annotated;
110                     }
111
112                     @Override
113                     public void release() {
114                         instance.release();
115                     }
116                 };
117             } else {
118                 endpointInstance = (InstanceHandle<Endpoint>) instance;
119             }
120
121             UndertowSession session = new UndertowSession(channel, URI.create(exchange.getRequestURI()), exchange.getAttachment(HandshakeUtil.PATH_PARAMS), exchange.getRequestParameters(), this, principal, endpointInstance, config.getEndpointConfiguration(), exchange.getQueryString(), config.getEncodingFactory().createEncoding(config.getEndpointConfiguration()), config, channel.getSubProtocol(), Collections.<Extension>emptyList(), null);
122             config.addOpenSession(session);
123
124             session.setMaxBinaryMessageBufferSize(getContainer().getDefaultMaxBinaryMessageBufferSize());
125             session.setMaxTextMessageBufferSize(getContainer().getDefaultMaxTextMessageBufferSize());
126             session.setMaxIdleTimeout(getContainer().getDefaultMaxSessionIdleTimeout());
127             session.getAsyncRemote().setSendTimeout(getContainer().getDefaultAsyncSendTimeout());
128             try {
129                 endpointInstance.getInstance().onOpen(session, config.getEndpointConfiguration());
130             } catch (Exception e) {
131                 endpointInstance.getInstance().onError(session, e);
132                 IoUtils.safeClose(session);
133             }
134             channel.resumeReceives();
135         } catch (Exception e) {
136             JsrWebSocketLogger.REQUEST_LOGGER.endpointCreationFailed(e);
137             IoUtils.safeClose(channel);
138         }
139     }
140 }
141