Skip to content

Commit

Permalink
fix: Only map websockets to servlet path and not any sub paths (#14638)…
Browse files Browse the repository at this point in the history
… (#14676)

(cherry picked from commit f4c7497)

Co-authored-by: Artur <artur@vaadin.com>
  • Loading branch information
MarcinVaadin and Artur- committed Sep 29, 2022
1 parent 83d301f commit 30e23a9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
5 changes: 5 additions & 0 deletions flow-tests/vaadin-spring-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.vaadin.flow.spring.test;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class CustomWebSocket implements WebSocketConfigurer {

public static final String WEBSOCKET_URL = "/customWebSocket";

public static final String WEBSOCKET_RESPONSE_TEXT = "This is a response from Web Socket!";

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new WebSocketHandler() {

@Override
public void afterConnectionEstablished(WebSocketSession session) {
}

@Override
public void handleMessage(WebSocketSession session,
WebSocketMessage<?> message) throws Exception {
session.sendMessage(new TextMessage(WEBSOCKET_RESPONSE_TEXT));
}

@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) {

}

@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus) {

}

@Override
public boolean supportsPartialMessages() {
return false;
}
}, WEBSOCKET_URL);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.vaadin.flow.spring.test;

import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(SpringBootOnly.class)
public class CustomWebSocketIT extends AbstractSpringTest {

@ClientEndpoint
public class CustomWebSocketEndpoint {

private final CountDownLatch closureLatch = new CountDownLatch(1);

private String message;

@OnMessage
public void onMessage(Session session, String message)
throws IOException {
this.message = message;
session.close();
}

@OnClose
public void onClose(CloseReason reason) {
closureLatch.countDown();
}

public String waitForMessage() throws InterruptedException {
closureLatch.await(1, TimeUnit.SECONDS);
return message;
}
}

@Test
public void properWebsocketResponseIsReceived() throws Exception {
WebSocketContainer container = ContainerProvider
.getWebSocketContainer();

String testUrl = getTestURL().replace("http://", "ws://");
URI uri = URI.create(testUrl + CustomWebSocket.WEBSOCKET_URL);

try {
CustomWebSocketEndpoint clientEndpoint = new CustomWebSocketEndpoint();
Session session = container.connectToServer(clientEndpoint, uri);
session.getBasicRemote().sendText("Hello");
Assert.assertEquals(CustomWebSocket.WEBSOCKET_RESPONSE_TEXT,
clientEndpoint.waitForMessage());
session.close();
} catch (Exception ex) {
throw ex;
}
}

@Override
protected String getTestPath() {
return "";
}

}
28 changes: 28 additions & 0 deletions flow-tests/vaadin-spring-tests/test-spring-war/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@
</exclusions>
</dependency>
<!-- End Spring -->

<!-- Jetty Websocket client side dependencies -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-javax-client</artifactId>
<version>${jetty.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public ServletRegistrationBean<SpringServlet> servletRegistrationBean(
*/
initParameters.put(ApplicationConfig.JSR356_MAPPING_PATH,
pushRegistrationPath);
initParameters.put(ApplicationConfig.JSR356_PATH_MAPPING_LENGTH, "0");

ServletRegistrationBean<SpringServlet> registration = new ServletRegistrationBean<>(
new SpringServlet(context, rootMapping), mapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void onStartup(ServletContext servletContext)
*/
initParameters.put(ApplicationConfig.JSR356_MAPPING_PATH,
pushRegistrationPath);
initParameters.put(ApplicationConfig.JSR356_PATH_MAPPING_LENGTH, "0");

registration.setInitParameters(initParameters);

Expand Down

0 comments on commit 30e23a9

Please sign in to comment.