Browse Source
Update methods available on WebSocketSession interface. Introduce DelegatingWebSocketSession interface.pull/333/merge
36 changed files with 733 additions and 588 deletions
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.socket.adapter; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.URI; |
||||
import java.security.Principal; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.util.ObjectUtils; |
||||
import org.springframework.web.socket.BinaryMessage; |
||||
import org.springframework.web.socket.CloseStatus; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
|
||||
/** |
||||
* A {@link WebSocketSession} for use with the Jetty 9 WebSocket API. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class JettyWebSocketSession extends AbstractWebSocketSesssion<org.eclipse.jetty.websocket.api.Session> { |
||||
|
||||
private HttpHeaders headers; |
||||
|
||||
private final Principal principal; |
||||
|
||||
|
||||
/** |
||||
* Class constructor. |
||||
* |
||||
* @param principal the user associated with the session, or {@code null} |
||||
*/ |
||||
public JettyWebSocketSession(Principal principal) { |
||||
this.principal = principal; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public String getId() { |
||||
checkDelegateSessionInitialized(); |
||||
return ObjectUtils.getIdentityHexString(getDelegateSession()); |
||||
} |
||||
|
||||
@Override |
||||
public URI getUri() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getUpgradeRequest().getRequestURI(); |
||||
} |
||||
|
||||
@Override |
||||
public HttpHeaders getHandshakeHeaders() { |
||||
checkDelegateSessionInitialized(); |
||||
if (this.headers == null) { |
||||
this.headers = new HttpHeaders(); |
||||
this.headers.putAll(getDelegateSession().getUpgradeRequest().getHeaders()); |
||||
this.headers = HttpHeaders.readOnlyHttpHeaders(headers); |
||||
} |
||||
return this.headers; |
||||
} |
||||
|
||||
@Override |
||||
public Principal getPrincipal() { |
||||
return this.principal; |
||||
} |
||||
|
||||
@Override |
||||
public InetSocketAddress getLocalAddress() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getLocalAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public InetSocketAddress getRemoteAddress() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getRemoteAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public String getAcceptedProtocol() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getUpgradeResponse().getAcceptedSubProtocol(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isOpen() { |
||||
return ((getDelegateSession() != null) && getDelegateSession().isOpen()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendTextMessage(TextMessage message) throws IOException { |
||||
getDelegateSession().getRemote().sendString(message.getPayload()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendBinaryMessage(BinaryMessage message) throws IOException { |
||||
getDelegateSession().getRemote().sendBytes(message.getPayload()); |
||||
} |
||||
|
||||
@Override |
||||
protected void closeInternal(CloseStatus status) throws IOException { |
||||
getDelegateSession().close(status.getCode(), status.getReason()); |
||||
} |
||||
|
||||
} |
||||
@ -1,144 +0,0 @@
@@ -1,144 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.socket.adapter; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.URI; |
||||
import java.security.Principal; |
||||
|
||||
import org.eclipse.jetty.websocket.api.Session; |
||||
import org.eclipse.jetty.websocket.api.UpgradeResponse; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ObjectUtils; |
||||
import org.springframework.web.socket.BinaryMessage; |
||||
import org.springframework.web.socket.CloseStatus; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
|
||||
/** |
||||
* Adapts a Jetty {@link org.eclipse.jetty.websocket.api.Session} to |
||||
* {@link WebSocketSession}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class JettyWebSocketSessionAdapter |
||||
extends AbstractWebSocketSesssionAdapter<org.eclipse.jetty.websocket.api.Session> { |
||||
|
||||
private Session session; |
||||
|
||||
private Principal principal; |
||||
|
||||
private String protocol; |
||||
|
||||
|
||||
@Override |
||||
public void initSession(Session session) { |
||||
Assert.notNull(session, "session must not be null"); |
||||
this.session = session; |
||||
|
||||
if (this.protocol == null) { |
||||
UpgradeResponse response = session.getUpgradeResponse(); |
||||
if ((response != null) && response.getAcceptedSubProtocol() != null) { |
||||
this.protocol = response.getAcceptedSubProtocol(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getId() { |
||||
return ObjectUtils.getIdentityHexString(this.session); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isSecure() { |
||||
return this.session.isSecure(); |
||||
} |
||||
|
||||
@Override |
||||
public URI getUri() { |
||||
return this.session.getUpgradeRequest().getRequestURI(); |
||||
} |
||||
|
||||
@Override |
||||
public void setUri(URI uri) { |
||||
} |
||||
|
||||
@Override |
||||
public Principal getPrincipal() { |
||||
return this.principal; |
||||
} |
||||
|
||||
@Override |
||||
public void setPrincipal(Principal principal) { |
||||
this.principal = principal; |
||||
} |
||||
|
||||
@Override |
||||
public String getRemoteHostName() { |
||||
return this.session.getRemoteAddress().getHostName(); |
||||
} |
||||
|
||||
@Override |
||||
public void setRemoteHostName(String address) { |
||||
// ignore
|
||||
} |
||||
|
||||
@Override |
||||
public String getRemoteAddress() { |
||||
InetSocketAddress address = this.session.getRemoteAddress(); |
||||
return address.isUnresolved() ? null : address.getAddress().getHostAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public void setRemoteAddress(String address) { |
||||
// ignore
|
||||
} |
||||
|
||||
@Override |
||||
public String getAcceptedProtocol() { |
||||
return this.protocol; |
||||
} |
||||
|
||||
@Override |
||||
public void setAcceptedProtocol(String protocol) { |
||||
this.protocol = protocol; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isOpen() { |
||||
return this.session.isOpen(); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendTextMessage(TextMessage message) throws IOException { |
||||
this.session.getRemote().sendString(message.getPayload()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendBinaryMessage(BinaryMessage message) throws IOException { |
||||
this.session.getRemote().sendBytes(message.getPayload()); |
||||
} |
||||
|
||||
@Override |
||||
protected void closeInternal(CloseStatus status) throws IOException { |
||||
this.session.close(status.getCode(), status.getReason()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,123 @@
@@ -0,0 +1,123 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.socket.adapter; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.URI; |
||||
import java.security.Principal; |
||||
|
||||
import javax.websocket.CloseReason; |
||||
import javax.websocket.CloseReason.CloseCodes; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.web.socket.BinaryMessage; |
||||
import org.springframework.web.socket.CloseStatus; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
|
||||
/** |
||||
* A {@link WebSocketSession} for use with the standard WebSocket for Java API. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class StandardWebSocketSession extends AbstractWebSocketSesssion<javax.websocket.Session> { |
||||
|
||||
private final HttpHeaders headers; |
||||
|
||||
private final InetSocketAddress localAddress; |
||||
|
||||
private final InetSocketAddress remoteAddress; |
||||
|
||||
|
||||
/** |
||||
* Class constructor. |
||||
* |
||||
* @param handshakeHeaders the headers of the handshake request |
||||
*/ |
||||
public StandardWebSocketSession(HttpHeaders handshakeHeaders, InetSocketAddress localAddress, |
||||
InetSocketAddress remoteAddress) { |
||||
|
||||
handshakeHeaders = (handshakeHeaders != null) ? handshakeHeaders : new HttpHeaders(); |
||||
this.headers = HttpHeaders.readOnlyHttpHeaders(handshakeHeaders); |
||||
this.localAddress = localAddress; |
||||
this.remoteAddress = remoteAddress; |
||||
} |
||||
|
||||
@Override |
||||
public String getId() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getId(); |
||||
} |
||||
|
||||
@Override |
||||
public URI getUri() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getRequestURI(); |
||||
} |
||||
|
||||
@Override |
||||
public HttpHeaders getHandshakeHeaders() { |
||||
return this.headers; |
||||
} |
||||
|
||||
@Override |
||||
public Principal getPrincipal() { |
||||
checkDelegateSessionInitialized(); |
||||
return getDelegateSession().getUserPrincipal(); |
||||
} |
||||
|
||||
@Override |
||||
public InetSocketAddress getLocalAddress() { |
||||
return this.localAddress; |
||||
} |
||||
|
||||
@Override |
||||
public InetSocketAddress getRemoteAddress() { |
||||
return this.remoteAddress; |
||||
} |
||||
|
||||
@Override |
||||
public String getAcceptedProtocol() { |
||||
checkDelegateSessionInitialized(); |
||||
String protocol = getDelegateSession().getNegotiatedSubprotocol(); |
||||
return StringUtils.isEmpty(protocol)? null : protocol; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isOpen() { |
||||
return ((getDelegateSession() != null) && getDelegateSession().isOpen()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendTextMessage(TextMessage message) throws IOException { |
||||
getDelegateSession().getBasicRemote().sendText(message.getPayload(), message.isLast()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendBinaryMessage(BinaryMessage message) throws IOException { |
||||
getDelegateSession().getBasicRemote().sendBinary(message.getPayload(), message.isLast()); |
||||
} |
||||
|
||||
@Override |
||||
protected void closeInternal(CloseStatus status) throws IOException { |
||||
getDelegateSession().close(new CloseReason(CloseCodes.getCloseCode(status.getCode()), status.getReason())); |
||||
} |
||||
|
||||
} |
||||
@ -1,145 +0,0 @@
@@ -1,145 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.socket.adapter; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.URI; |
||||
import java.security.Principal; |
||||
|
||||
import javax.websocket.CloseReason; |
||||
import javax.websocket.CloseReason.CloseCodes; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.web.socket.BinaryMessage; |
||||
import org.springframework.web.socket.CloseStatus; |
||||
import org.springframework.web.socket.TextMessage; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
|
||||
/** |
||||
* Adapts a standard {@link javax.websocket.Session} to {@link WebSocketSession}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class StandardWebSocketSessionAdapter extends AbstractWebSocketSesssionAdapter<javax.websocket.Session> { |
||||
|
||||
private javax.websocket.Session session; |
||||
|
||||
private URI uri; |
||||
|
||||
private String remoteHostName; |
||||
|
||||
private String remoteAddress; |
||||
|
||||
private String protocol; |
||||
|
||||
|
||||
@Override |
||||
public void initSession(javax.websocket.Session session) { |
||||
Assert.notNull(session, "session must not be null"); |
||||
this.session = session; |
||||
|
||||
if (this.protocol == null) { |
||||
if (StringUtils.hasText(session.getNegotiatedSubprotocol())) { |
||||
this.protocol = session.getNegotiatedSubprotocol(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getId() { |
||||
return this.session.getId(); |
||||
} |
||||
|
||||
@Override |
||||
public URI getUri() { |
||||
return this.uri; |
||||
} |
||||
|
||||
@Override |
||||
public void setUri(URI uri) { |
||||
this.uri = uri; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean isSecure() { |
||||
return this.session.isSecure(); |
||||
} |
||||
|
||||
@Override |
||||
public Principal getPrincipal() { |
||||
return this.session.getUserPrincipal(); |
||||
} |
||||
|
||||
@Override |
||||
public void setPrincipal(Principal principal) { |
||||
// ignore
|
||||
} |
||||
|
||||
@Override |
||||
public String getRemoteHostName() { |
||||
return this.remoteHostName; |
||||
} |
||||
|
||||
@Override |
||||
public void setRemoteHostName(String name) { |
||||
this.remoteHostName = name; |
||||
} |
||||
|
||||
@Override |
||||
public String getRemoteAddress() { |
||||
return this.remoteAddress; |
||||
} |
||||
|
||||
@Override |
||||
public void setRemoteAddress(String address) { |
||||
this.remoteAddress = address; |
||||
} |
||||
|
||||
@Override |
||||
public String getAcceptedProtocol() { |
||||
return this.protocol; |
||||
} |
||||
|
||||
@Override |
||||
public void setAcceptedProtocol(String protocol) { |
||||
this.protocol = protocol; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isOpen() { |
||||
return this.session.isOpen(); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendTextMessage(TextMessage message) throws IOException { |
||||
this.session.getBasicRemote().sendText(message.getPayload(), message.isLast()); |
||||
} |
||||
|
||||
@Override |
||||
protected void sendBinaryMessage(BinaryMessage message) throws IOException { |
||||
this.session.getBasicRemote().sendBinary(message.getPayload(), message.isLast()); |
||||
} |
||||
|
||||
@Override |
||||
protected void closeInternal(CloseStatus status) throws IOException { |
||||
this.session.close(new CloseReason(CloseCodes.getCloseCode(status.getCode()), status.getReason())); |
||||
} |
||||
|
||||
} |
||||
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.socket.server.support; |
||||
|
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.web.socket.WebSocketSession; |
||||
import org.springframework.web.socket.adapter.ConfigurableWebSocketSession; |
||||
|
||||
/** |
||||
* Copies information from the handshake HTTP request and response to a given |
||||
* {@link WebSocketSession}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class ServerWebSocketSessionInitializer { |
||||
|
||||
public void initialize(ServerHttpRequest request, ServerHttpResponse response, |
||||
String protocol, ConfigurableWebSocketSession session) { |
||||
|
||||
session.setUri(request.getURI()); |
||||
session.setRemoteHostName(request.getRemoteHostName()); |
||||
session.setRemoteAddress(request.getRemoteAddress()); |
||||
session.setPrincipal(request.getPrincipal()); |
||||
session.setAcceptedProtocol(protocol); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue