View Javadoc
1   /*
2    * Copyright 2002-2014 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.adapter.jetty;
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.eclipse.jetty.websocket.api.Session;
28  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
29  
30  import org.springframework.http.HttpHeaders;
31  import org.springframework.util.ObjectUtils;
32  import org.springframework.web.socket.BinaryMessage;
33  import org.springframework.web.socket.CloseStatus;
34  import org.springframework.web.socket.PingMessage;
35  import org.springframework.web.socket.PongMessage;
36  import org.springframework.web.socket.TextMessage;
37  import org.springframework.web.socket.WebSocketExtension;
38  import org.springframework.web.socket.WebSocketSession;
39  import org.springframework.web.socket.adapter.AbstractWebSocketSession;
40  
41  /**
42   * A {@link WebSocketSession} for use with the Jetty 9 WebSocket API.
43   *
44   * @author Phillip Webb
45   * @author Rossen Stoyanchev
46   * @since 4.0
47   */
48  public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
49  
50  	private String id;
51  
52  	private URI uri;
53  
54  	private HttpHeaders headers;
55  
56  	private String acceptedProtocol;
57  
58  	private List<WebSocketExtension> extensions;
59  
60  	private Principal user;
61  
62  
63  	/**
64  	 * Create a new {@link JettyWebSocketSession} instance.
65  	 *
66  	 * @param attributes attributes from the HTTP handshake to associate with the WebSocket session
67  	 */
68  	public JettyWebSocketSession(Map<String, Object> attributes) {
69  		this(attributes, null);
70  	}
71  
72  	/**
73  	 * Create a new {@link JettyWebSocketSession} instance associated with the given user.
74  	 *
75  	 * @param attributes attributes from the HTTP handshake to associate with the WebSocket
76  	 * session; the provided attributes are copied, the original map is not used.
77  	 * @param user the user associated with the session; if {@code null} we'll fallback on the user
78  	 *  available via {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()}
79  	 */
80  	public JettyWebSocketSession(Map<String, Object> attributes, Principal user) {
81  		super(attributes);
82  		this.user = user;
83  	}
84  
85  
86  	@Override
87  	public String getId() {
88  		checkNativeSessionInitialized();
89  		return this.id;
90  	}
91  
92  	@Override
93  	public URI getUri() {
94  		checkNativeSessionInitialized();
95  		return this.uri;
96  	}
97  
98  	@Override
99  	public HttpHeaders getHandshakeHeaders() {
100 		checkNativeSessionInitialized();
101 		return this.headers;
102 	}
103 
104 	@Override
105 	public String getAcceptedProtocol() {
106 		checkNativeSessionInitialized();
107 		return this.acceptedProtocol;
108 	}
109 
110 	@Override
111 	public List<WebSocketExtension> getExtensions() {
112 		checkNativeSessionInitialized();
113 		return this.extensions;
114 	}
115 
116 	@Override
117 	public Principal getPrincipal() {
118 		return this.user;
119 	}
120 
121 	@Override
122 	public InetSocketAddress getLocalAddress() {
123 		checkNativeSessionInitialized();
124 		return getNativeSession().getLocalAddress();
125 	}
126 
127 	@Override
128 	public InetSocketAddress getRemoteAddress() {
129 		checkNativeSessionInitialized();
130 		return getNativeSession().getRemoteAddress();
131 	}
132 
133 	@Override
134 	public void setTextMessageSizeLimit(int messageSizeLimit) {
135 		checkNativeSessionInitialized();
136 		getNativeSession().getPolicy().setMaxTextMessageSize(messageSizeLimit);
137 	}
138 
139 	@Override
140 	public int getTextMessageSizeLimit() {
141 		checkNativeSessionInitialized();
142 		return getNativeSession().getPolicy().getMaxTextMessageSize();
143 	}
144 
145 	@Override
146 	public void setBinaryMessageSizeLimit(int messageSizeLimit) {
147 		checkNativeSessionInitialized();
148 		getNativeSession().getPolicy().setMaxBinaryMessageSize(messageSizeLimit);
149 	}
150 
151 	@Override
152 	public int getBinaryMessageSizeLimit() {
153 		checkNativeSessionInitialized();
154 		return getNativeSession().getPolicy().getMaxBinaryMessageSize();
155 	}
156 
157 	@Override
158 	public boolean isOpen() {
159 		return ((getNativeSession() != null) && getNativeSession().isOpen());
160 	}
161 
162 	@Override
163 	public void initializeNativeSession(Session session) {
164 		super.initializeNativeSession(session);
165 
166 		this.id = ObjectUtils.getIdentityHexString(getNativeSession());
167 		this.uri = session.getUpgradeRequest().getRequestURI();
168 
169 		this.headers = new HttpHeaders();
170 		this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
171 		this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
172 
173 		this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
174 
175 		List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
176 		this.extensions = new ArrayList<WebSocketExtension>(source.size());
177 		for (ExtensionConfig ec : source) {
178 			this.extensions.add(new WebSocketExtension(ec.getName(), ec.getParameters()));
179 		}
180 
181 		if (this.user == null) {
182 			this.user = session.getUpgradeRequest().getUserPrincipal();
183 		}
184 	}
185 
186 	@Override
187 	protected void sendTextMessage(TextMessage message) throws IOException {
188 		getNativeSession().getRemote().sendString(message.getPayload());
189 	}
190 
191 	@Override
192 	protected void sendBinaryMessage(BinaryMessage message) throws IOException {
193 		getNativeSession().getRemote().sendBytes(message.getPayload());
194 	}
195 
196 	@Override
197 	protected void sendPingMessage(PingMessage message) throws IOException {
198 		getNativeSession().getRemote().sendPing(message.getPayload());
199 	}
200 
201 	@Override
202 	protected void sendPongMessage(PongMessage message) throws IOException {
203 		getNativeSession().getRemote().sendPong(message.getPayload());
204 	}
205 
206 	@Override
207 	protected void closeInternal(CloseStatus status) throws IOException {
208 		getNativeSession().close(status.getCode(), status.getReason());
209 	}
210 
211 }