View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.sockjs.transport.session;
18  
19  import java.io.IOException;
20  import java.net.InetSocketAddress;
21  import java.net.URI;
22  import java.security.Principal;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.springframework.http.HttpHeaders;
28  import org.springframework.web.socket.CloseStatus;
29  import org.springframework.web.socket.WebSocketExtension;
30  import org.springframework.web.socket.WebSocketHandler;
31  import org.springframework.web.socket.sockjs.frame.SockJsFrame;
32  import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
33  
34  /**
35   * @author Rossen Stoyanchev
36   */
37  public class TestSockJsSession extends AbstractSockJsSession {
38  
39  	private URI uri;
40  
41  	private HttpHeaders headers;
42  
43  	private Principal principal;
44  
45  	private InetSocketAddress localAddress;
46  
47  	private InetSocketAddress remoteAddress;
48  
49  	private boolean active;
50  
51  	private final List<SockJsFrame> sockJsFrames = new ArrayList<>();
52  
53  	private CloseStatus closeStatus;
54  
55  	private IOException exceptionOnWrite;
56  
57  	private int numberOfLastActiveTimeUpdates;
58  
59  	private boolean cancelledHeartbeat;
60  
61  	private String subProtocol;
62  
63  	private List<WebSocketExtension> extensions = new ArrayList<>();
64  
65  
66  	public TestSockJsSession(String sessionId, SockJsServiceConfig config,
67  			WebSocketHandler wsHandler, Map<String, Object> attributes) {
68  
69  		super(sessionId, config, wsHandler, attributes);
70  	}
71  
72  
73  	public void setUri(URI uri) {
74  		this.uri = uri;
75  	}
76  
77  	@Override
78  	public URI getUri() {
79  		return this.uri;
80  	}
81  
82  	@Override
83  	public HttpHeaders getHandshakeHeaders() {
84  		return this.headers;
85  	}
86  
87  	public HttpHeaders getHeaders() {
88  		return this.headers;
89  	}
90  
91  	public void setHeaders(HttpHeaders headers) {
92  		this.headers = headers;
93  	}
94  
95  	@Override
96  	public Principal getPrincipal() {
97  		return this.principal;
98  	}
99  
100 	public void setPrincipal(Principal principal) {
101 		this.principal = principal;
102 	}
103 
104 	@Override
105 	public InetSocketAddress getLocalAddress() {
106 		return this.localAddress;
107 	}
108 
109 	public void setLocalAddress(InetSocketAddress localAddress) {
110 		this.localAddress = localAddress;
111 	}
112 
113 	@Override
114 	public InetSocketAddress getRemoteAddress() {
115 		return this.remoteAddress;
116 	}
117 
118 	public void setRemoteAddress(InetSocketAddress remoteAddress) {
119 		this.remoteAddress = remoteAddress;
120 	}
121 
122 	@Override
123 	public String getAcceptedProtocol() {
124 		return this.subProtocol;
125 	}
126 
127 	public void setAcceptedProtocol(String protocol) {
128 		this.subProtocol = protocol;
129 	}
130 
131 	@Override
132 	public void setTextMessageSizeLimit(int messageSizeLimit) {
133 	}
134 
135 	@Override
136 	public int getTextMessageSizeLimit() {
137 		return 0;
138 	}
139 
140 	@Override
141 	public void setBinaryMessageSizeLimit(int messageSizeLimit) {
142 	}
143 
144 	@Override
145 	public int getBinaryMessageSizeLimit() {
146 		return 0;
147 	}
148 
149 	@Override
150 	public List<WebSocketExtension> getExtensions() {
151 		return this.extensions;
152 	}
153 
154 	public void setExtensions(List<WebSocketExtension> extensions) {
155 		this.extensions = extensions;
156 	}
157 
158 	public CloseStatus getCloseStatus() {
159 		return this.closeStatus;
160 	}
161 
162 	@Override
163 	public boolean isActive() {
164 		return this.active;
165 	}
166 
167 	public void setActive(boolean active) {
168 		this.active = active;
169 	}
170 
171 	public List<SockJsFrame> getSockJsFramesWritten() {
172 		return this.sockJsFrames;
173 	}
174 
175 	public void setExceptionOnWrite(IOException exceptionOnWrite) {
176 		this.exceptionOnWrite = exceptionOnWrite;
177 	}
178 
179 	public int getNumberOfLastActiveTimeUpdates() {
180 		return this.numberOfLastActiveTimeUpdates;
181 	}
182 
183 	public boolean didCancelHeartbeat() {
184 		return this.cancelledHeartbeat;
185 	}
186 
187 	@Override
188 	protected void updateLastActiveTime() {
189 		this.numberOfLastActiveTimeUpdates++;
190 		super.updateLastActiveTime();
191 	}
192 
193 	@Override
194 	protected void cancelHeartbeat() {
195 		this.cancelledHeartbeat = true;
196 		super.cancelHeartbeat();
197 	}
198 
199 	@Override
200 	protected void sendMessageInternal(String message) {
201 	}
202 
203 	@Override
204 	protected void writeFrameInternal(SockJsFrame frame) throws IOException {
205 		this.sockJsFrames.add(frame);
206 		if (this.exceptionOnWrite != null) {
207 			throw this.exceptionOnWrite;
208 		}
209 	}
210 
211 	@Override
212 	protected void disconnect(CloseStatus status) throws IOException {
213 		this.closeStatus = status;
214 	}
215 
216 }