Skip to content

Test websphere integration with spring framework websocket #31260

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

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2023 IBM Corporation and others.
* Copyright (c) 2017, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -13,7 +13,7 @@

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

dependencies {
implementation('org.springframework.boot:spring-boot-starter-websocket' + ':' + sv['springBoot'])
implementation('org.springframework.boot:spring-boot-starter-websocket') {
exclude module: 'spring-boot-starter-tomcat' //Temporarily excluding tomcat starter till it is removed in SpringBootThinUtil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issue to fix the thinning for the recent versions of spring boot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #31262.

}
compileOnly('org.springframework.boot:spring-boot-starter-tomcat')
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018,2023 IBM Corporation and others.
* Copyright (c) 2018,2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -18,13 +18,20 @@
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.AbstractHandshakeHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

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


@SpringBootApplication
@EnableWebSocket
public class TestApplication{
public class TestApplication implements WebSocketConfigurer {

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

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(customWebSocketHandler(), "/customHandler").setHandshakeHandler(customHandshakeHandler());
}

@Bean
public TextWebSocketHandler customWebSocketHandler() {
return new TextWebSocketHandler() {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
session.sendMessage(new TextMessage("Did you say: " + message.getPayload()));
}
};
}

@Bean
public AbstractHandshakeHandler customHandshakeHandler() {
return new DefaultHandshakeHandler();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018,2023 IBM Corporation and others.
* Copyright (c) 2018,2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -27,6 +27,7 @@
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;

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

/**
* Test websocket using a custom websocket configurer.
*
* The application registers a custom websocket handler and a abstract handshake handler.
* The org.springframework.web.socket.server.support.AbstractHandshakeHandler looksup websphere websocket code which is now being removed in spring framework 7.x.
* 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
* required.
*
* @throws Exception
*/
@Test
public void testEchoWithCustomWebsocketHandler() throws Exception {
Log.info(getClass(), "testEchoWithCustomWebsocketHandler", wsContainer.toString());
Session session = wsContainer.connectToServer(clientEndpoint, new URI("ws://" + server.getHostname() + ":" + server.getHttpDefaultPort() + "/customHandler"));
assertNotNull("Session cannot be null", session);
assertTrue("Session is not open", session.isOpen());
CountDownLatch latch = new CountDownLatch(1);
clientEndpoint.sendMessage("Hello Websocket", latch);
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals("Expected message from server not found", "Did you say: Hello Websocket", clientEndpoint.getMessageFromServer());
}

@Override
public Set<String> getFeatures() {
return new HashSet<>(Arrays.asList("springBoot-3.0", "servlet-6.0", "websocket-2.1"));
Expand All @@ -96,4 +119,11 @@ public String getApplication() {
return SPRING_BOOT_30_APP_WEBSOCKET;
}

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

}