Skip to content

Commit

Permalink
[#1802] Switch pax-web-undertow-websocket from javax to jakarta
Browse files Browse the repository at this point in the history
  • Loading branch information
grgrzybek committed Jun 16, 2023
1 parent c826a66 commit 29f2c1f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 57 deletions.
37 changes: 27 additions & 10 deletions pax-web-undertow-websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
<configuration>
<instructions>
<Import-Package>
javax.servlet;version="[3.1,5)",
javax.servlet.annotation;version="[3.1,5)",
javax.servlet.http;version="[3.1,5)",
javax.websocket;version="[1.1,2)",
javax.websocket.server;version="[1.1,2)",
<!-- ranges indicate Servlet API 6.0+ (JakartaEE 10+) -->
jakarta.servlet;version="[6,7)",
jakarta.servlet.annotation;version="[6,7)",
jakarta.servlet.http;version="[6,7)",

jakarta.websocket;version="[2.1,3)",
jakarta.websocket.server;version="[2.1,3)",

<!-- from pax-logging-api -->
org.slf4j;version="[1.7,2)",
org.slf4j;version="[2,3)",

org.ops4j.pax.web.service.spi.model,
org.ops4j.pax.web.service.spi.servlet,
Expand Down Expand Up @@ -83,7 +85,7 @@
<artifactId>pax-web-undertow</artifactId>
</dependency>

<!-- JavaEE -->
<!-- JakartaEE -->

<dependency>
<groupId>jakarta.servlet</groupId>
Expand All @@ -95,6 +97,11 @@
<artifactId>jakarta.websocket-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-client-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Undertow -->

Expand Down Expand Up @@ -144,7 +151,7 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>test</scope>
</dependency>

Expand All @@ -159,8 +166,18 @@
<!-- Testing -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import java.util.EnumSet;
import java.util.EventListener;
import java.util.List;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.websocket.Extension;
import javax.websocket.server.ServerContainer;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.websocket.Extension;
import jakarta.websocket.server.ServerContainer;

import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.HandlesTypes;
import jakarta.websocket.DeploymentException;
import jakarta.websocket.Endpoint;
import jakarta.websocket.server.ServerApplicationConfig;
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpoint;
import jakarta.websocket.server.ServerEndpointConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,13 +49,13 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
ServerContainer wsContainer = (ServerContainer) ctx.getAttribute(ServerContainer.class.getName());

if (wsContainer == null) {
LOG.warn("[dev error] No javax.websocket.server.ServerContainer available in servlet context." +
LOG.warn("[dev error] No jakarta.websocket.server.ServerContainer available in servlet context." +
" Skipping WebSocket registration.");
return;
}

// inspired by Tomcat's org.apache.tomcat.websocket.server.WsSci and
// Jetty's org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketServletContainerInitializer
// Jetty's org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer

Set<Class<?>> annotatedEndpointClasses = new HashSet<>();
Set<Class<? extends Endpoint>> conigurableEndpoints = new HashSet<>();
Expand All @@ -67,15 +67,16 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
if (!Modifier.isPublic(modifiers) || Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)) {
continue;
}
if (potentialEndpointClass.getPackage().getName().startsWith("javax.")) {
if (potentialEndpointClass.getPackage().getName().startsWith("javax.")
|| potentialEndpointClass.getPackage().getName().startsWith("jakarta.")) {
continue;
}
if (Endpoint.class.isAssignableFrom(potentialEndpointClass)) {
// a class to be processed by javax.websocket.server.ServerApplicationConfig
// a class to be processed by jakarta.websocket.server.ServerApplicationConfig
conigurableEndpoints.add((Class<? extends Endpoint>) potentialEndpointClass);
}
if (ServerApplicationConfig.class.isAssignableFrom(potentialEndpointClass)) {
// a class that processes javax.websocket.Endpoints
// a class that processes jakarta.websocket.Endpoints
try {
configs.add((ServerApplicationConfig) potentialEndpointClass.getConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
Expand All @@ -93,7 +94,7 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
Set<Class<?>> configuredAnnotatedEndpointClasses = new HashSet<>();

if (configs.isEmpty()) {
// no javax.websocket.server.ServerApplicationConfigs available - consider only annotated endpoints
// no jakarta.websocket.server.ServerApplicationConfigs available - consider only annotated endpoints
configuredAnnotatedEndpointClasses.addAll(annotatedEndpointClasses);
} else {
for (ServerApplicationConfig config : configs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import javax.servlet.http.HttpServlet;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.ContainerProvider;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.websocket.ClientEndpointConfig;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.RemoteEndpoint;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import jakarta.websocket.server.ServerApplicationConfig;
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpoint;
import jakarta.websocket.server.ServerEndpointConfig;

import io.undertow.Handlers;
import io.undertow.Undertow;
Expand All @@ -52,13 +52,12 @@
import io.undertow.servlet.util.ImmediateInstanceFactory;
import io.undertow.websockets.jsr.ServerWebSocketContainer;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class EmbeddedUndertowWebSocketsTest {

Expand Down Expand Up @@ -115,7 +114,7 @@ public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationExc

// we can add more endpoints only before io.undertow.websockets.jsr.JsrWebSocketFilter.init() is called
ServerContainer sc = (ServerContainer) dm.getDeployment().getServletContext().getAttribute(ServerContainer.class.getName());
assertThat(sc.getClass().getName(), equalTo("io.undertow.websockets.jsr.ServerWebSocketContainer"));
assertThat(sc.getClass().getName()).isEqualTo("io.undertow.websockets.jsr.ServerWebSocketContainer");
sc.addEndpoint(MyAnnotatedEndpoint.class);

ClientEndpointConfig config = ClientEndpointConfig.Builder.create()
Expand Down
10 changes: 5 additions & 5 deletions pax-web-undertow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@
<artifactId>jul-to-slf4j</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
Expand Down Expand Up @@ -272,6 +267,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down

0 comments on commit 29f2c1f

Please sign in to comment.