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.util.Assert; 024import org.springframework.util.concurrent.SettableListenableFuture; 025import org.springframework.web.socket.CloseStatus; 026import org.springframework.web.socket.TextMessage; 027import org.springframework.web.socket.WebSocketExtension; 028import org.springframework.web.socket.WebSocketHandler; 029import org.springframework.web.socket.WebSocketSession; 030import org.springframework.web.socket.adapter.NativeWebSocketSession; 031 032/** 033 * An extension of {@link AbstractClientSockJsSession} wrapping and delegating 034 * to an actual WebSocket session. 035 * 036 * @author Rossen Stoyanchev 037 * @since 4.1 038 */ 039public class WebSocketClientSockJsSession extends AbstractClientSockJsSession implements NativeWebSocketSession { 040 041 private WebSocketSession webSocketSession; 042 043 044 public WebSocketClientSockJsSession(TransportRequest request, WebSocketHandler handler, 045 SettableListenableFuture<WebSocketSession> connectFuture) { 046 047 super(request, handler, connectFuture); 048 } 049 050 051 @Override 052 public Object getNativeSession() { 053 return this.webSocketSession; 054 } 055 056 @SuppressWarnings("unchecked") 057 @Override 058 public <T> T getNativeSession(Class<T> requiredType) { 059 return (requiredType == null || requiredType.isInstance(this.webSocketSession) ? (T) this.webSocketSession : null); 060 } 061 062 @Override 063 public InetSocketAddress getLocalAddress() { 064 checkDelegateSessionInitialized(); 065 return this.webSocketSession.getLocalAddress(); 066 } 067 068 @Override 069 public InetSocketAddress getRemoteAddress() { 070 checkDelegateSessionInitialized(); 071 return this.webSocketSession.getRemoteAddress(); 072 } 073 074 @Override 075 public String getAcceptedProtocol() { 076 checkDelegateSessionInitialized(); 077 return this.webSocketSession.getAcceptedProtocol(); 078 } 079 080 @Override 081 public void setTextMessageSizeLimit(int messageSizeLimit) { 082 checkDelegateSessionInitialized(); 083 this.webSocketSession.setTextMessageSizeLimit(messageSizeLimit); 084 } 085 086 @Override 087 public int getTextMessageSizeLimit() { 088 checkDelegateSessionInitialized(); 089 return this.webSocketSession.getTextMessageSizeLimit(); 090 } 091 092 @Override 093 public void setBinaryMessageSizeLimit(int messageSizeLimit) { 094 checkDelegateSessionInitialized(); 095 this.webSocketSession.setBinaryMessageSizeLimit(messageSizeLimit); 096 } 097 098 @Override 099 public int getBinaryMessageSizeLimit() { 100 checkDelegateSessionInitialized(); 101 return this.webSocketSession.getBinaryMessageSizeLimit(); 102 } 103 104 @Override 105 public List<WebSocketExtension> getExtensions() { 106 checkDelegateSessionInitialized(); 107 return this.webSocketSession.getExtensions(); 108 } 109 110 private void checkDelegateSessionInitialized() { 111 Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); 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 this.webSocketSession.sendMessage(textMessage); 121 } 122 123 @Override 124 protected void disconnect(CloseStatus status) throws IOException { 125 if (this.webSocketSession != null) { 126 this.webSocketSession.close(status); 127 } 128 } 129 130}