Skip to content

Commit f4c7497

Browse files
authored
fix: Only map websockets to servlet path and not any sub paths (#14638)
* fix: Only map websockets to servlet path and not any sub paths
1 parent 3bf1753 commit f4c7497

7 files changed

Lines changed: 162 additions & 1 deletion

File tree

‎flow-tests/vaadin-spring-tests/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128
<artifactId>jaxb-api</artifactId>
129129
<version>2.3.1</version>
130130
</dependency>
131+
<dependency>
132+
<groupId>javax.websocket</groupId>
133+
<artifactId>javax.websocket-api</artifactId>
134+
<version>1.1</version>
135+
</dependency>
131136
<dependency>
132137
<groupId>org.glassfish.jaxb</groupId>
133138
<artifactId>jaxb-runtime</artifactId>

‎flow-tests/vaadin-spring-tests/test-spring-boot-reverseproxy/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
<configuration>
152152
<excludes>
153153
<!-- The proxy does not let push through -->
154-
<exclude>*PushIT*</exclude>
154+
<exclude>*PushIT*,*CustomWebSocketIT*</exclude>
155155
</excludes>
156156
</configuration>
157157
</plugin>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.vaadin.flow.spring.test;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.socket.CloseStatus;
5+
import org.springframework.web.socket.TextMessage;
6+
import org.springframework.web.socket.WebSocketHandler;
7+
import org.springframework.web.socket.WebSocketMessage;
8+
import org.springframework.web.socket.WebSocketSession;
9+
import org.springframework.web.socket.config.annotation.EnableWebSocket;
10+
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
11+
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
12+
13+
@Configuration
14+
@EnableWebSocket
15+
public class CustomWebSocket implements WebSocketConfigurer {
16+
17+
public static final String WEBSOCKET_URL = "/customWebSocket";
18+
19+
public static final String WEBSOCKET_RESPONSE_TEXT = "This is a response from Web Socket!";
20+
21+
@Override
22+
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
23+
registry.addHandler(new WebSocketHandler() {
24+
25+
@Override
26+
public void afterConnectionEstablished(WebSocketSession session) {
27+
}
28+
29+
@Override
30+
public void handleMessage(WebSocketSession session,
31+
WebSocketMessage<?> message) throws Exception {
32+
session.sendMessage(new TextMessage(WEBSOCKET_RESPONSE_TEXT));
33+
}
34+
35+
@Override
36+
public void handleTransportError(WebSocketSession session,
37+
Throwable exception) {
38+
39+
}
40+
41+
@Override
42+
public void afterConnectionClosed(WebSocketSession session,
43+
CloseStatus closeStatus) {
44+
45+
}
46+
47+
@Override
48+
public boolean supportsPartialMessages() {
49+
return false;
50+
}
51+
}, WEBSOCKET_URL);
52+
}
53+
54+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.vaadin.flow.spring.test;
2+
3+
import javax.websocket.ClientEndpoint;
4+
import javax.websocket.CloseReason;
5+
import javax.websocket.ContainerProvider;
6+
import javax.websocket.OnClose;
7+
import javax.websocket.OnMessage;
8+
import javax.websocket.Session;
9+
import javax.websocket.WebSocketContainer;
10+
import java.io.IOException;
11+
import java.net.URI;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import org.junit.Assert;
16+
import org.junit.Test;
17+
import org.junit.experimental.categories.Category;
18+
19+
@Category(SpringBootOnly.class)
20+
public class CustomWebSocketIT extends AbstractSpringTest {
21+
22+
@ClientEndpoint
23+
public class CustomWebSocketEndpoint {
24+
25+
private final CountDownLatch closureLatch = new CountDownLatch(1);
26+
27+
private String message;
28+
29+
@OnMessage
30+
public void onMessage(Session session, String message)
31+
throws IOException {
32+
this.message = message;
33+
session.close();
34+
}
35+
36+
@OnClose
37+
public void onClose(CloseReason reason) {
38+
closureLatch.countDown();
39+
}
40+
41+
public String waitForMessage() throws InterruptedException {
42+
closureLatch.await(1, TimeUnit.SECONDS);
43+
return message;
44+
}
45+
}
46+
47+
@Test
48+
public void properWebsocketResponseIsReceived() throws Exception {
49+
WebSocketContainer container = ContainerProvider
50+
.getWebSocketContainer();
51+
52+
String testUrl = getTestURL().replace("http://", "ws://");
53+
URI uri = URI.create(testUrl + CustomWebSocket.WEBSOCKET_URL);
54+
55+
try {
56+
CustomWebSocketEndpoint clientEndpoint = new CustomWebSocketEndpoint();
57+
Session session = container.connectToServer(clientEndpoint, uri);
58+
session.getBasicRemote().sendText("Hello");
59+
Assert.assertEquals(CustomWebSocket.WEBSOCKET_RESPONSE_TEXT,
60+
clientEndpoint.waitForMessage());
61+
session.close();
62+
} catch (Exception ex) {
63+
throw ex;
64+
}
65+
}
66+
67+
@Override
68+
protected String getTestPath() {
69+
return "";
70+
}
71+
72+
}

‎flow-tests/vaadin-spring-tests/test-spring-war/pom.xml‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@
5959
</exclusions>
6060
</dependency>
6161
<!-- End Spring -->
62+
63+
<!-- Jetty Websocket client side dependencies -->
64+
<dependency>
65+
<groupId>org.eclipse.jetty</groupId>
66+
<artifactId>jetty-client</artifactId>
67+
<version>${jetty.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.eclipse.jetty</groupId>
71+
<artifactId>jetty-util</artifactId>
72+
<version>${jetty.version}</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.eclipse.jetty</groupId>
76+
<artifactId>jetty-http</artifactId>
77+
<version>${jetty.version}</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.eclipse.jetty</groupId>
81+
<artifactId>jetty-io</artifactId>
82+
<version>${jetty.version}</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.eclipse.jetty.websocket</groupId>
86+
<artifactId>websocket-javax-client</artifactId>
87+
<version>${jetty.version}</version>
88+
</dependency>
89+
6290
</dependencies>
6391

6492
<build>

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringBootAutoConfiguration.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public ServletRegistrationBean<SpringServlet> servletRegistrationBean(
101101
*/
102102
initParameters.put(ApplicationConfig.JSR356_MAPPING_PATH,
103103
pushRegistrationPath);
104+
initParameters.put(ApplicationConfig.JSR356_PATH_MAPPING_LENGTH, "0");
104105

105106
ServletRegistrationBean<SpringServlet> registration = new ServletRegistrationBean<>(
106107
new SpringServlet(context, rootMapping), mapping);

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinMVCWebAppInitializer.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void onStartup(ServletContext servletContext)
8585
*/
8686
initParameters.put(ApplicationConfig.JSR356_MAPPING_PATH,
8787
pushRegistrationPath);
88+
initParameters.put(ApplicationConfig.JSR356_PATH_MAPPING_LENGTH, "0");
8889

8990
registration.setInitParameters(initParameters);
9091

0 commit comments

Comments
 (0)