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.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.springframework.web.socket.CloseStatus;
25  import org.springframework.web.socket.WebSocketHandler;
26  import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
27  import org.springframework.web.socket.sockjs.frame.SockJsFrame;
28  import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
29  
30  /**
31   * @author Rossen Stoyanchev
32   */
33  public class TestHttpSockJsSession extends AbstractHttpSockJsSession {
34  
35  	private boolean active;
36  
37  	private final List<SockJsFrame> sockJsFrames = new ArrayList<>();
38  
39  	private CloseStatus closeStatus;
40  
41  	private IOException exceptionOnWrite;
42  
43  	private int numberOfLastActiveTimeUpdates;
44  
45  	private boolean cancelledHeartbeat;
46  
47  	private String subProtocol;
48  
49  
50  	public TestHttpSockJsSession(String sessionId, SockJsServiceConfig config,
51  			WebSocketHandler wsHandler, Map<String, Object> attributes) {
52  
53  		super(sessionId, config, wsHandler, attributes);
54  	}
55  
56  	@Override
57  	protected boolean isStreaming() {
58  		return true;
59  	}
60  
61  	@Override
62  	public String getAcceptedProtocol() {
63  		return this.subProtocol;
64  	}
65  
66  	@Override
67  	public void setAcceptedProtocol(String protocol) {
68  		this.subProtocol = protocol;
69  	}
70  
71  	public CloseStatus getCloseStatus() {
72  		return this.closeStatus;
73  	}
74  
75  	@Override
76  	public boolean isActive() {
77  		return this.active;
78  	}
79  
80  	public void setActive(boolean active) {
81  		this.active = active;
82  	}
83  
84  	public List<SockJsFrame> getSockJsFramesWritten() {
85  		return this.sockJsFrames;
86  	}
87  
88  	public void setExceptionOnWrite(IOException exceptionOnWrite) {
89  		this.exceptionOnWrite = exceptionOnWrite;
90  	}
91  
92  	public int getNumberOfLastActiveTimeUpdates() {
93  		return this.numberOfLastActiveTimeUpdates;
94  	}
95  
96  	public boolean didCancelHeartbeat() {
97  		return this.cancelledHeartbeat;
98  	}
99  
100 	@Override
101 	protected void updateLastActiveTime() {
102 		this.numberOfLastActiveTimeUpdates++;
103 		super.updateLastActiveTime();
104 	}
105 
106 	@Override
107 	protected void cancelHeartbeat() {
108 		this.cancelledHeartbeat = true;
109 		super.cancelHeartbeat();
110 	}
111 
112 	@Override
113 	protected void writeFrameInternal(SockJsFrame frame) throws IOException {
114 		this.sockJsFrames.add(frame);
115 		if (this.exceptionOnWrite != null) {
116 			throw this.exceptionOnWrite;
117 		}
118 	}
119 
120 	@Override
121 	protected void disconnect(CloseStatus status) {
122 		this.closeStatus = status;
123 	}
124 
125 	@Override
126 	protected void flushCache() throws SockJsTransportFailureException {
127 	}
128 
129 }