Skip to content

Commit 466b940

Browse files
Merge pull request #31260 from anjumfatima90/websphereIntegrationWithSpring
Test websphere integration with spring framework websocket
2 parents f749104 + 3f5c18e commit 466b940

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

Diff for: dev/io.openliberty.springboot.fat30.websocket.app/build.gradle

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2023 IBM Corporation and others.
2+
* Copyright (c) 2017, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -13,7 +13,7 @@
1313

1414
// https://plugins.gradle.org/plugin/org.springframework.boot
1515
plugins {
16-
id 'org.springframework.boot' version '3.0.7'
16+
id 'org.springframework.boot' version '3.4.4'
1717
}
1818
apply from: '../wlp-gradle/subprojects/spring.gradle'
1919
def sv = springVersions[ '3.0' ]
@@ -28,5 +28,8 @@ sourceCompatibility = 17
2828
apply from: '../wlp-gradle/subprojects/maven-central-mirror.gradle'
2929

3030
dependencies {
31-
implementation('org.springframework.boot:spring-boot-starter-websocket' + ':' + sv['springBoot'])
31+
implementation('org.springframework.boot:spring-boot-starter-websocket') {
32+
exclude module: 'spring-boot-starter-tomcat' //Temporarily excluding tomcat starter till it is removed in SpringBootThinUtil
33+
}
34+
compileOnly('org.springframework.boot:spring-boot-starter-tomcat')
3235
}

Diff for: dev/io.openliberty.springboot.fat30.websocket.app/src/main/java/com/ibm/ws/springboot/fat30/websocket/app/TestApplication.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018,2023 IBM Corporation and others.
2+
* Copyright (c) 2018,2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -18,13 +18,20 @@
1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.web.socket.config.annotation.EnableWebSocket;
2020
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
21+
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
22+
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
23+
import org.springframework.web.socket.server.support.AbstractHandshakeHandler;
24+
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
25+
import org.springframework.web.socket.handler.TextWebSocketHandler;
26+
import org.springframework.web.socket.TextMessage;
27+
import org.springframework.web.socket.WebSocketSession;
2128

2229
import com.ibm.ws.springboot.fat30.websocket.echo.ServerEchoWebSocketEndpoint;
2330

2431

2532
@SpringBootApplication
2633
@EnableWebSocket
27-
public class TestApplication{
34+
public class TestApplication implements WebSocketConfigurer {
2835

2936
public static void main(String[] args) {
3037
SpringApplication.run(TestApplication.class, args);
@@ -40,4 +47,23 @@ public ServerEndpointExporter serverEndpointExporter() {
4047
return new ServerEndpointExporter();
4148
}
4249

50+
@Override
51+
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
52+
registry.addHandler(customWebSocketHandler(), "/customHandler").setHandshakeHandler(customHandshakeHandler());
53+
}
54+
55+
@Bean
56+
public TextWebSocketHandler customWebSocketHandler() {
57+
return new TextWebSocketHandler() {
58+
@Override
59+
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
60+
session.sendMessage(new TextMessage("Did you say: " + message.getPayload()));
61+
}
62+
};
63+
}
64+
65+
@Bean
66+
public AbstractHandshakeHandler customHandshakeHandler() {
67+
return new DefaultHandshakeHandler();
68+
}
4369
}

Diff for: dev/io.openliberty.springboot.fat30_fat/fat/src/com/ibm/ws/springboot/support/fat/WebSocketTests30.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018,2023 IBM Corporation and others.
2+
* Copyright (c) 2018,2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -27,6 +27,7 @@
2727
import jakarta.websocket.Session;
2828
import jakarta.websocket.WebSocketContainer;
2929

30+
import org.junit.AfterClass;
3031
import org.junit.Before;
3132
import org.junit.Test;
3233
import org.junit.runner.RunWith;
@@ -86,6 +87,28 @@ public void testEchoWebSocket30() throws Exception {
8687
assertEquals("Expected message from server not found", "Did you say: Hello World", clientEndpoint.getMessageFromServer());
8788
}
8889

90+
/**
91+
* Test websocket using a custom websocket configurer.
92+
*
93+
* The application registers a custom websocket handler and a abstract handshake handler.
94+
* The org.springframework.web.socket.server.support.AbstractHandshakeHandler looksup websphere websocket code which is now being removed in spring framework 7.x.
95+
* This test should fail for future Spring Boot versions using spring framework 7.x, we might have to use the websocket-2.2 feature and do some implementation changes if
96+
* required.
97+
*
98+
* @throws Exception
99+
*/
100+
@Test
101+
public void testEchoWithCustomWebsocketHandler() throws Exception {
102+
Log.info(getClass(), "testEchoWithCustomWebsocketHandler", wsContainer.toString());
103+
Session session = wsContainer.connectToServer(clientEndpoint, new URI("ws://" + server.getHostname() + ":" + server.getHttpDefaultPort() + "/customHandler"));
104+
assertNotNull("Session cannot be null", session);
105+
assertTrue("Session is not open", session.isOpen());
106+
CountDownLatch latch = new CountDownLatch(1);
107+
clientEndpoint.sendMessage("Hello Websocket", latch);
108+
assertTrue(latch.await(5, TimeUnit.SECONDS));
109+
assertEquals("Expected message from server not found", "Did you say: Hello Websocket", clientEndpoint.getMessageFromServer());
110+
}
111+
89112
@Override
90113
public Set<String> getFeatures() {
91114
return new HashSet<>(Arrays.asList("springBoot-3.0", "servlet-6.0", "websocket-2.1"));
@@ -96,4 +119,11 @@ public String getApplication() {
96119
return SPRING_BOOT_30_APP_WEBSOCKET;
97120
}
98121

122+
@AfterClass
123+
public static void stopTestServer() throws Exception {
124+
//[WARNING ] SRVE8094W: WARNING: Cannot set header. Response already committed. Stack trace of errant attempt to set header:
125+
//at com.ibm.ws.webcontainer.srt.SRTServletResponse.setHeader(SRTServletResponse.java:1845)
126+
server.stopServer(true, "SRVE8094W");
127+
}
128+
99129
}

0 commit comments

Comments
 (0)