Search in sources :

Example 6 with WebSocketExtension

use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.

the class JettyWebSocketClient method doHandshakeInternal.

@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    final ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(protocols);
    for (WebSocketExtension e : extensions) {
        request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
    }
    for (String header : headers.keySet()) {
        request.setHeader(header, headers.get(header));
    }
    Principal user = getUser();
    final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
    final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            Future<Session> future = client.connect(listener, uri, request);
            future.get();
            return wsSession;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) WebSocketToJettyExtensionConfigAdapter(org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Principal(java.security.Principal) WebSocketSession(org.springframework.web.socket.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)

Example 7 with WebSocketExtension

use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.

the class DefaultHandshakeHandlerTests method supportedExtensions.

@Test
public void supportedExtensions() throws Exception {
    WebSocketExtension extension1 = new WebSocketExtension("ext1");
    WebSocketExtension extension2 = new WebSocketExtension("ext2");
    given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] { "13" });
    given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1));
    this.servletRequest.setMethod("GET");
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
    headers.setUpgrade("WebSocket");
    headers.setConnection("Upgrade");
    headers.setSecWebSocketVersion("13");
    headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
    headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));
    WebSocketHandler handler = new TextWebSocketHandler();
    Map<String, Object> attributes = Collections.<String, Object>emptyMap();
    this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
    verify(this.upgradeStrategy).upgrade(this.request, this.response, null, Arrays.asList(extension1), null, handler, attributes);
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Aggregations

WebSocketExtension (org.springframework.web.socket.WebSocketExtension)7 ArrayList (java.util.ArrayList)3 HttpHeaders (org.springframework.http.HttpHeaders)3 WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)3 Principal (java.security.Principal)2 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Callable (java.util.concurrent.Callable)1 Extension (javax.websocket.Extension)1 Session (org.eclipse.jetty.websocket.api.Session)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)1 Test (org.junit.Test)1 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)1 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 JettyWebSocketHandlerAdapter (org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter)1 JettyWebSocketSession (org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)1 WebSocketToJettyExtensionConfigAdapter (org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter)1