001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.sockjs.client;
018
019import java.io.IOException;
020import java.net.InetSocketAddress;
021import java.util.List;
022
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025import org.springframework.util.concurrent.SettableListenableFuture;
026import org.springframework.web.socket.CloseStatus;
027import org.springframework.web.socket.TextMessage;
028import org.springframework.web.socket.WebSocketExtension;
029import org.springframework.web.socket.WebSocketHandler;
030import org.springframework.web.socket.WebSocketSession;
031import org.springframework.web.socket.adapter.NativeWebSocketSession;
032
033/**
034 * An extension of {@link AbstractClientSockJsSession} wrapping and delegating
035 * to an actual WebSocket session.
036 *
037 * @author Rossen Stoyanchev
038 * @since 4.1
039 */
040public class WebSocketClientSockJsSession extends AbstractClientSockJsSession implements NativeWebSocketSession {
041
042        @Nullable
043        private WebSocketSession webSocketSession;
044
045
046        public WebSocketClientSockJsSession(TransportRequest request, WebSocketHandler handler,
047                        SettableListenableFuture<WebSocketSession> connectFuture) {
048
049                super(request, handler, connectFuture);
050        }
051
052
053        @Override
054        public Object getNativeSession() {
055                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
056                return this.webSocketSession;
057        }
058
059        @SuppressWarnings("unchecked")
060        @Override
061        @Nullable
062        public <T> T getNativeSession(@Nullable Class<T> requiredType) {
063                return (requiredType == null || requiredType.isInstance(this.webSocketSession) ? (T) this.webSocketSession : null);
064        }
065
066        @Override
067        public InetSocketAddress getLocalAddress() {
068                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
069                return this.webSocketSession.getLocalAddress();
070        }
071
072        @Override
073        public InetSocketAddress getRemoteAddress() {
074                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
075                return this.webSocketSession.getRemoteAddress();
076        }
077
078        @Override
079        public String getAcceptedProtocol() {
080                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
081                return this.webSocketSession.getAcceptedProtocol();
082        }
083
084        @Override
085        public void setTextMessageSizeLimit(int messageSizeLimit) {
086                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
087                this.webSocketSession.setTextMessageSizeLimit(messageSizeLimit);
088        }
089
090        @Override
091        public int getTextMessageSizeLimit() {
092                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
093                return this.webSocketSession.getTextMessageSizeLimit();
094        }
095
096        @Override
097        public void setBinaryMessageSizeLimit(int messageSizeLimit) {
098                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
099                this.webSocketSession.setBinaryMessageSizeLimit(messageSizeLimit);
100        }
101
102        @Override
103        public int getBinaryMessageSizeLimit() {
104                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
105                return this.webSocketSession.getBinaryMessageSizeLimit();
106        }
107
108        @Override
109        public List<WebSocketExtension> getExtensions() {
110                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
111                return this.webSocketSession.getExtensions();
112        }
113
114        public void initializeDelegateSession(WebSocketSession session) {
115                this.webSocketSession = session;
116        }
117
118        @Override
119        protected void sendInternal(TextMessage textMessage) throws IOException {
120                Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
121                this.webSocketSession.sendMessage(textMessage);
122        }
123
124        @Override
125        protected void disconnect(CloseStatus status) throws IOException {
126                if (this.webSocketSession != null) {
127                        this.webSocketSession.close(status);
128                }
129        }
130
131}