Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only map websockets to servlet path and not any sub paths #14638

Merged
merged 5 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -151,7 +151,7 @@
<configuration>
<excludes>
<!-- The proxy does not let push through -->
<exclude>*PushIT*</exclude>
<exclude>*PushIT*,*CustomWebSocketIT*</exclude>
</excludes>
</configuration>
</plugin>
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