Search in sources :

Example 1 with WebSocketExtension

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

the class AbstractHandshakeHandler method doHandshake.

@Override
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders());
    if (logger.isTraceEnabled()) {
        logger.trace("Processing request " + request.getURI() + " with headers=" + headers);
    }
    try {
        if (HttpMethod.GET != request.getMethod()) {
            response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
            response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
            if (logger.isErrorEnabled()) {
                logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
            }
            return false;
        }
        if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
            handleInvalidUpgradeHeader(request, response);
            return false;
        }
        if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
            handleInvalidConnectHeader(request, response);
            return false;
        }
        if (!isWebSocketVersionSupported(headers)) {
            handleWebSocketVersionNotSupported(request, response);
            return false;
        }
        if (!isValidOrigin(request)) {
            response.setStatusCode(HttpStatus.FORBIDDEN);
            return false;
        }
        String wsKey = headers.getSecWebSocketKey();
        if (wsKey == null) {
            if (logger.isErrorEnabled()) {
                logger.error("Missing \"Sec-WebSocket-Key\" header");
            }
            response.setStatusCode(HttpStatus.BAD_REQUEST);
            return false;
        }
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
    }
    String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler);
    List<WebSocketExtension> requested = headers.getSecWebSocketExtensions();
    List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request);
    List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported);
    Principal user = determineUser(request, wsHandler, attributes);
    if (logger.isTraceEnabled()) {
        logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions);
    }
    this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes);
    return true;
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) IOException(java.io.IOException) Principal(java.security.Principal)

Example 2 with WebSocketExtension

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

the class JettyRequestUpgradeStrategy method buildWebSocketExtensions.

private List<WebSocketExtension> buildWebSocketExtensions() {
    Set<String> names = this.factory.getExtensionFactory().getExtensionNames();
    List<WebSocketExtension> result = new ArrayList<>(names.size());
    for (String name : names) {
        result.add(new WebSocketExtension(name));
    }
    return result;
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) ArrayList(java.util.ArrayList)

Example 3 with WebSocketExtension

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

the class AbstractStandardUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {
    HttpHeaders headers = request.getHeaders();
    InetSocketAddress localAddr = null;
    try {
        localAddr = request.getLocalAddress();
    } catch (Exception ex) {
    // Ignore
    }
    InetSocketAddress remoteAddr = null;
    try {
        remoteAddr = request.getRemoteAddress();
    } catch (Exception ex) {
    // Ignore
    }
    StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
    StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);
    List<Extension> extensions = new ArrayList<>();
    for (WebSocketExtension extension : selectedExtensions) {
        extensions.add(new WebSocketToStandardExtensionAdapter(extension));
    }
    upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
Also used : Extension(javax.websocket.Extension) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) StandardWebSocketHandlerAdapter(org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) WebSocketToStandardExtensionAdapter(org.springframework.web.socket.adapter.standard.WebSocketToStandardExtensionAdapter)

Example 4 with WebSocketExtension

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

the class JettyWebSocketSession method initializeNativeSession.

@Override
public void initializeNativeSession(Session session) {
    super.initializeNativeSession(session);
    this.id = ObjectUtils.getIdentityHexString(getNativeSession());
    this.uri = session.getUpgradeRequest().getRequestURI();
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(session.getUpgradeRequest().getHeaders());
    this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
    this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
    List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions();
    if (!CollectionUtils.isEmpty(jettyExtensions)) {
        List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size());
        for (ExtensionConfig jettyExtension : jettyExtensions) {
            extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters()));
        }
        this.extensions = Collections.unmodifiableList(extensions);
    } else {
        this.extensions = Collections.emptyList();
    }
    if (this.user == null) {
        this.user = session.getUpgradeRequest().getUserPrincipal();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) ArrayList(java.util.ArrayList)

Example 5 with WebSocketExtension

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

the class AbstractWebSocketClient method doHandshake.

@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, WebSocketHttpHeaders headers, URI uri) {
    Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
    assertUri(uri);
    if (logger.isDebugEnabled()) {
        logger.debug("Connecting to " + uri);
    }
    HttpHeaders headersToUse = new HttpHeaders();
    if (headers != null) {
        for (String header : headers.keySet()) {
            if (!specialHeaders.contains(header.toLowerCase())) {
                headersToUse.put(header, headers.get(header));
            }
        }
    }
    List<String> subProtocols = (headers != null && headers.getSecWebSocketProtocol() != null ? headers.getSecWebSocketProtocol() : Collections.emptyList());
    List<WebSocketExtension> extensions = (headers != null && headers.getSecWebSocketExtensions() != null ? headers.getSecWebSocketExtensions() : Collections.emptyList());
    return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions, Collections.emptyMap());
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketExtension(org.springframework.web.socket.WebSocketExtension)

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