7 changed files with 328 additions and 74 deletions
@ -0,0 +1,111 @@ |
|||||||
|
/* |
||||||
|
* 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.client; |
||||||
|
|
||||||
|
import java.net.URI; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
import org.springframework.http.HttpHeaders; |
||||||
|
import org.springframework.util.Assert; |
||||||
|
import org.springframework.web.socket.WebSocketHandler; |
||||||
|
import org.springframework.web.socket.WebSocketSession; |
||||||
|
import org.springframework.web.util.UriComponentsBuilder; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract base class for {@link WebSocketClient} implementations. |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 4.0 |
||||||
|
*/ |
||||||
|
public abstract class AbstractWebSocketClient implements WebSocketClient { |
||||||
|
|
||||||
|
protected final Log logger = LogFactory.getLog(getClass()); |
||||||
|
|
||||||
|
private static final Set<String> disallowedHeaders = new HashSet<String>(); |
||||||
|
|
||||||
|
static { |
||||||
|
disallowedHeaders.add("cache-control"); |
||||||
|
disallowedHeaders.add("cookie"); |
||||||
|
disallowedHeaders.add("connection"); |
||||||
|
disallowedHeaders.add("host"); |
||||||
|
disallowedHeaders.add("sec-websocket-extensions"); |
||||||
|
disallowedHeaders.add("sec-websocket-key"); |
||||||
|
disallowedHeaders.add("sec-websocket-protocol"); |
||||||
|
disallowedHeaders.add("sec-websocket-version"); |
||||||
|
disallowedHeaders.add("pragma"); |
||||||
|
disallowedHeaders.add("upgrade"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, |
||||||
|
Object... uriVars) throws WebSocketConnectFailureException { |
||||||
|
|
||||||
|
Assert.notNull(uriTemplate, "uriTemplate must not be null"); |
||||||
|
URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); |
||||||
|
return doHandshake(webSocketHandler, null, uri); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public final WebSocketSession doHandshake(WebSocketHandler webSocketHandler, |
||||||
|
HttpHeaders headers, URI uri) throws WebSocketConnectFailureException { |
||||||
|
|
||||||
|
Assert.notNull(webSocketHandler, "webSocketHandler must not be null"); |
||||||
|
Assert.notNull(uri, "uri must not be null"); |
||||||
|
|
||||||
|
if (logger.isDebugEnabled()) { |
||||||
|
logger.debug("Connecting to " + uri); |
||||||
|
} |
||||||
|
|
||||||
|
HttpHeaders headersToUse = new HttpHeaders(); |
||||||
|
if (headers != null) { |
||||||
|
for (String header : headers.keySet()) { |
||||||
|
if (!disallowedHeaders.contains(header.toLowerCase())) { |
||||||
|
headersToUse.put(header, headers.get(header)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
List<String> subProtocols = new ArrayList<String>(); |
||||||
|
if ((headers != null) && (headers.getSecWebSocketProtocol() != null)) { |
||||||
|
subProtocols.addAll(headers.getSecWebSocketProtocol()); |
||||||
|
} |
||||||
|
|
||||||
|
return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* |
||||||
|
* @param webSocketHandler the client-side handler for WebSocket messages |
||||||
|
* @param headers HTTP headers to use for the handshake, with unwanted (forbidden) |
||||||
|
* headers filtered out, never {@code null} |
||||||
|
* @param uri the target URI for the handshake, never {@code null} |
||||||
|
* @param subProtocols requested sub-protocols, or an empty list |
||||||
|
* @return the established WebSocket session |
||||||
|
* @throws WebSocketConnectFailureException |
||||||
|
*/ |
||||||
|
protected abstract WebSocketSession doHandshakeInternal(WebSocketHandler webSocketHandler, |
||||||
|
HttpHeaders headers, URI uri, List<String> subProtocols) throws WebSocketConnectFailureException; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* 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.client; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Test fixture for {@link AbstractWebSocketClient}. |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
*/ |
||||||
|
public class AbstractWebSocketClientTests { |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,133 @@ |
|||||||
|
/* |
||||||
|
* 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.client.jetty; |
||||||
|
|
||||||
|
import java.net.URI; |
||||||
|
import java.util.Arrays; |
||||||
|
|
||||||
|
import org.eclipse.jetty.server.Server; |
||||||
|
import org.eclipse.jetty.server.ServerConnector; |
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeRequest; |
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeResponse; |
||||||
|
import org.eclipse.jetty.websocket.servlet.WebSocketCreator; |
||||||
|
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; |
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.http.HttpHeaders; |
||||||
|
import org.springframework.util.CollectionUtils; |
||||||
|
import org.springframework.util.SocketUtils; |
||||||
|
import org.springframework.web.socket.WebSocketHandler; |
||||||
|
import org.springframework.web.socket.WebSocketSession; |
||||||
|
import org.springframework.web.socket.adapter.JettyWebSocketListenerAdapter; |
||||||
|
import org.springframework.web.socket.adapter.JettyWebSocketSessionAdapter; |
||||||
|
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link JettyWebSocketClient}. |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
*/ |
||||||
|
public class JettyWebSocketClientTests { |
||||||
|
|
||||||
|
private JettyWebSocketClient client; |
||||||
|
|
||||||
|
private TestJettyWebSocketServer server; |
||||||
|
|
||||||
|
private String wsUrl; |
||||||
|
|
||||||
|
private WebSocketSession wsSession; |
||||||
|
|
||||||
|
|
||||||
|
@Before |
||||||
|
public void setup() throws Exception { |
||||||
|
|
||||||
|
int port = SocketUtils.findAvailableTcpPort(); |
||||||
|
|
||||||
|
this.server = new TestJettyWebSocketServer(port, new TextWebSocketHandlerAdapter()); |
||||||
|
this.server.start(); |
||||||
|
|
||||||
|
this.client = new JettyWebSocketClient(); |
||||||
|
this.client.start(); |
||||||
|
|
||||||
|
this.wsUrl = "ws://localhost:" + port + "/test"; |
||||||
|
} |
||||||
|
|
||||||
|
@After |
||||||
|
public void teardown() throws Exception { |
||||||
|
this.wsSession.close(); |
||||||
|
this.client.stop(); |
||||||
|
this.server.stop(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void doHandshake() throws Exception { |
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders(); |
||||||
|
headers.setSecWebSocketProtocol(Arrays.asList("echo")); |
||||||
|
|
||||||
|
this.wsSession = this.client.doHandshake(new TextWebSocketHandlerAdapter(), headers, new URI(this.wsUrl)); |
||||||
|
|
||||||
|
assertEquals(this.wsUrl, this.wsSession.getUri().toString()); |
||||||
|
assertEquals("echo", this.wsSession.getAcceptedProtocol()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static class TestJettyWebSocketServer { |
||||||
|
|
||||||
|
private final Server server; |
||||||
|
|
||||||
|
|
||||||
|
public TestJettyWebSocketServer(int port, final WebSocketHandler webSocketHandler) { |
||||||
|
|
||||||
|
this.server = new Server(); |
||||||
|
ServerConnector connector = new ServerConnector(this.server); |
||||||
|
connector.setPort(port); |
||||||
|
|
||||||
|
this.server.addConnector(connector); |
||||||
|
this.server.setHandler(new org.eclipse.jetty.websocket.server.WebSocketHandler() { |
||||||
|
@Override |
||||||
|
public void configure(WebSocketServletFactory factory) { |
||||||
|
factory.setCreator(new WebSocketCreator() { |
||||||
|
@Override |
||||||
|
public Object createWebSocket(UpgradeRequest req, UpgradeResponse resp) { |
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(req.getSubProtocols())) { |
||||||
|
resp.setAcceptedSubProtocol(req.getSubProtocols().get(0)); |
||||||
|
} |
||||||
|
|
||||||
|
JettyWebSocketSessionAdapter session = new JettyWebSocketSessionAdapter(); |
||||||
|
return new JettyWebSocketListenerAdapter(webSocketHandler, session); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public void start() throws Exception { |
||||||
|
this.server.start(); |
||||||
|
} |
||||||
|
|
||||||
|
public void stop() throws Exception { |
||||||
|
this.server.stop(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue