From 8e67b0fbfe7a4af18cb0a0382b1b7782a0396fdb Mon Sep 17 00:00:00 2001 From: tomco Date: Wed, 26 Apr 2023 23:53:13 +0200 Subject: [PATCH] Introduce support of Dropwizard 3.0.8 and JEE7 --- pom.xml | 49 +++++++++++++------ .../dropwizard/websocket/WebsocketBundle.java | 2 +- .../websocket/WebsocketHandler.java | 19 ++++--- .../handling/WebsocketContainer.java | 16 +++--- .../WebsocketContainerInitializer.java | 7 ++- .../registration/EndpointRegistration.java | 4 +- .../EndpointProgrammaticJava.java | 3 +- .../WebsocketHandlerFactoryTest.java | 2 +- .../websocket/WebsocketHandlerTest.java | 11 +++-- .../handling/WebsocketContainerTest.java | 5 +- .../IntegrationConfiguration.java | 5 +- .../IntegrationTestApplication.java | 5 +- .../PingPongClientEndpoint.java | 4 +- .../PingPongServerEndpoint.java | 4 +- .../ProgrammaticServerEndpoint.java | 10 ++-- .../EndpointRegistrationTest.java | 17 ++++--- 16 files changed, 91 insertions(+), 72 deletions(-) diff --git a/pom.xml b/pom.xml index 6c014dd..5df6073 100644 --- a/pom.xml +++ b/pom.xml @@ -3,11 +3,11 @@ 4.0.0 be.tomcools - dropwizard-websocket-jsr356-bundle - 4.0.1-SNAPSHOT + dropwizard-websocket-jee7-bundle + 3.0.8 jar - dropwizard-websocket-jsr356-bundle + dropwizard-websocket-jee7-bundle Dropwizard bundle to enable the use of Java Websockets. https://github.com/TomCools/dropwizard-websocket-jee7-bundle @@ -28,13 +28,14 @@ https://github.com/TomCools/dropwizard-websocket-jee7-bundle scm:git:https://github.com/TomCools/dropwizard-websocket-jee7-bundle.git scm:git:https://github.com/TomCools/dropwizard-websocket-jee7-bundle.git - dropwizard-websocket-jsr356-bundle-0.0.104 + dropwizard-websocket-jee7-bundle-0.0.104 UTF-8 UTF-8 - 4.0.0 + 3.0.8 + 9.4.56.v20240826 11 @@ -51,10 +52,16 @@ + + javax.websocket + javax.websocket-api + 1.1 + + org.projectlombok lombok - 1.18.26 + 1.18.34 provided @@ -79,7 +86,7 @@ org.mockito mockito-junit-jupiter - 4.8.1 + 5.13.0 test @@ -98,7 +105,7 @@ nl.jqno.equalsverifier equalsverifier - 3.14.1 + 3.16.2 test @@ -106,13 +113,23 @@ org.eclipse.jetty.websocket - websocket-jakarta-server + javax-websocket-server-impl + ${jetty.version} + org.eclipse.jetty.websocket - websocket-jakarta-client + javax-websocket-client-impl + ${jetty.version} + @@ -131,12 +148,12 @@ org.apache.maven.plugins maven-deploy-plugin - 3.0.0 + 3.1.3 org.apache.maven.plugins maven-compiler-plugin - 3.10.1 + 3.13.0 ${java.version} ${java.version} @@ -145,21 +162,21 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.4.1 + 3.10.0 org.apache.maven.plugins maven-sources-plugin - 3.2.1 + 3.3.1 org.jreleaser jreleaser-maven-plugin - 1.5.1 + 1.14.0 maven-surefire-plugin - 3.0.0 + 3.5.0 diff --git a/src/main/java/be/tomcools/dropwizard/websocket/WebsocketBundle.java b/src/main/java/be/tomcools/dropwizard/websocket/WebsocketBundle.java index 32c27d6..2586ea6 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/WebsocketBundle.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/WebsocketBundle.java @@ -5,7 +5,7 @@ import io.dropwizard.core.ConfiguredBundle; import io.dropwizard.core.server.ServerFactory; import io.dropwizard.core.setup.Environment; -import jakarta.websocket.server.ServerEndpointConfig; +import javax.websocket.server.ServerEndpointConfig;; public class WebsocketBundle implements ConfiguredBundle { private final WebsocketHandlerFactory handlerFactory = new WebsocketHandlerFactory(); diff --git a/src/main/java/be/tomcools/dropwizard/websocket/WebsocketHandler.java b/src/main/java/be/tomcools/dropwizard/websocket/WebsocketHandler.java index c97d1cc..0132037 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/WebsocketHandler.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/WebsocketHandler.java @@ -4,13 +4,14 @@ import be.tomcools.dropwizard.websocket.handling.WebsocketContainerInitializer; import be.tomcools.dropwizard.websocket.registration.EndpointRegistration; import io.dropwizard.core.setup.Environment; -import jakarta.servlet.ServletContext; -import jakarta.websocket.DeploymentException; -import jakarta.websocket.server.ServerContainer; -import jakarta.websocket.server.ServerEndpointConfig; -import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; +import org.eclipse.jetty.websocket.jsr356.server.ServerContainer; +import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; -public class WebsocketHandler implements JakartaWebSocketServletContainerInitializer.Configurator { +import javax.servlet.ServletContext; +import javax.websocket.DeploymentException; +import javax.websocket.server.ServerEndpointConfig; + +public class WebsocketHandler implements WebSocketServerContainerInitializer.Configurator { private final EndpointRegistration endpointRegistration; private final WebsocketConfiguration configuration; private final Environment environment; @@ -35,15 +36,13 @@ public void addEndpoint(ServerEndpointConfig serverEndpointConfig) { this.endpointRegistration.add(serverEndpointConfig); } - public void initialize() { + public void initialize() { containerInitializer.initialize(environment.getApplicationContext(), this); } @Override - public void accept(ServletContext servletContext, ServerContainer serverContainer) { + public void accept(ServletContext servletContext, ServerContainer serverContainer) throws DeploymentException { WebsocketContainer container = new WebsocketContainer(configuration, serverContainer); container.registerEndpoints(endpointRegistration.getRegisteredEndpoints()); } - - } diff --git a/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainer.java b/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainer.java index 039765d..042cc1b 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainer.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainer.java @@ -4,10 +4,10 @@ import be.tomcools.dropwizard.websocket.registration.Endpoint; import be.tomcools.dropwizard.websocket.registration.Endpoints; import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointProgrammaticJava; -import jakarta.websocket.DeploymentException; -import jakarta.websocket.server.ServerContainer; import lombok.extern.slf4j.Slf4j; +import javax.websocket.DeploymentException; +import javax.websocket.server.ServerContainer; import java.util.Optional; @Slf4j @@ -32,28 +32,28 @@ public WebsocketContainer(WebsocketConfiguration configuration, ServerContainer } public void registerEndpoints(final Endpoints endpoints) { - final Endpoints succesfullyAdded = new Endpoints(); + final Endpoints successfullyAdded = new Endpoints(); for (Endpoint endpoint : endpoints) { try { register(endpoint); - succesfullyAdded.add(endpoint); + successfullyAdded.add(endpoint); } catch (DeploymentException e) { log.error("Could not add websocket endpoint {} to the deployment.", endpoint, e); } } - logRegisteredEndpoints(succesfullyAdded); + logRegisteredEndpoints(successfullyAdded); } - private void logRegisteredEndpoints(Endpoints succesfullyAdded) { + private void logRegisteredEndpoints(Endpoints successfullyAdded) { StringBuilder endpointsAdded = new StringBuilder("Registered websocket endpoints: ") .append(System.lineSeparator()) .append(System.lineSeparator()); - if (succesfullyAdded.isEmpty()) { + if (successfullyAdded.isEmpty()) { endpointsAdded.append("\tNONE \tNo endpoints were added to the server. Check logs for errors if you registered endpoints.").append(System.lineSeparator()); } else { - for (Endpoint endpoint : succesfullyAdded) { + for (Endpoint endpoint : successfullyAdded) { String endpointLogString = String.format("\tGET\t\t%s (%s)", endpoint.getPath(), endpoint.getEndpointClass().getName()); endpointsAdded.append(endpointLogString).append(System.lineSeparator()); } diff --git a/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerInitializer.java b/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerInitializer.java index d13eba2..16b654d 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerInitializer.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerInitializer.java @@ -1,14 +1,13 @@ package be.tomcools.dropwizard.websocket.handling; - import io.dropwizard.jetty.MutableServletContextHandler; -import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; +import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; public class WebsocketContainerInitializer { public void initialize(MutableServletContextHandler contextHandler, - JakartaWebSocketServletContainerInitializer.Configurator configurator) { + WebSocketServerContainerInitializer.Configurator configurator) { try { - JakartaWebSocketServletContainerInitializer.configure(contextHandler, configurator); + WebSocketServerContainerInitializer.configure(contextHandler, configurator); } catch (Exception e) { throw new IllegalStateException("Could not initialize context handler to enable Websockets", e); } diff --git a/src/main/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistration.java b/src/main/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistration.java index aba975b..2c0560a 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistration.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistration.java @@ -2,9 +2,9 @@ import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointAnnotatedJava; import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointProgrammaticJava; -import jakarta.websocket.server.ServerEndpoint; -import jakarta.websocket.server.ServerEndpointConfig; +import javax.websocket.server.ServerEndpoint; +import javax.websocket.server.ServerEndpointConfig; import java.util.Optional; public class EndpointRegistration { diff --git a/src/main/java/be/tomcools/dropwizard/websocket/registration/endpointtypes/EndpointProgrammaticJava.java b/src/main/java/be/tomcools/dropwizard/websocket/registration/endpointtypes/EndpointProgrammaticJava.java index e0509cb..1ec83ae 100644 --- a/src/main/java/be/tomcools/dropwizard/websocket/registration/endpointtypes/EndpointProgrammaticJava.java +++ b/src/main/java/be/tomcools/dropwizard/websocket/registration/endpointtypes/EndpointProgrammaticJava.java @@ -1,7 +1,8 @@ package be.tomcools.dropwizard.websocket.registration.endpointtypes; import be.tomcools.dropwizard.websocket.registration.Endpoint; -import jakarta.websocket.server.ServerEndpointConfig; + +import javax.websocket.server.ServerEndpointConfig; public class EndpointProgrammaticJava extends Endpoint { private final ServerEndpointConfig config; diff --git a/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerFactoryTest.java b/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerFactoryTest.java index 5f96c32..68d32bf 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerFactoryTest.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerFactoryTest.java @@ -14,7 +14,7 @@ @ExtendWith(MockitoExtension.class) public class WebsocketHandlerFactoryTest { private final Environment environment = mock(Environment.class, RETURNS_DEEP_STUBS); - private WebsocketConfiguration configuration = mock(WebsocketConfiguration.class); + private final WebsocketConfiguration configuration = mock(WebsocketConfiguration.class); @InjectMocks diff --git a/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerTest.java b/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerTest.java index 9c75fb7..b860eac 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerTest.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/WebsocketHandlerTest.java @@ -5,16 +5,17 @@ import be.tomcools.dropwizard.websocket.registration.Endpoints; import io.dropwizard.core.Configuration; import io.dropwizard.core.setup.Environment; -import jakarta.websocket.*; -import jakarta.websocket.server.ServerEndpoint; -import jakarta.websocket.server.ServerEndpointConfig; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test;; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import javax.websocket.server.ServerEndpointConfig; + import static org.mockito.Mockito.*; @@ -73,7 +74,7 @@ public void whenInitializeIsCalled_InitializesWebsocketContainer() { } @ServerEndpoint("/chat") - class TestEndpoint { + static class TestEndpoint { @OnOpen public void open(Session session) { } diff --git a/src/test/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerTest.java b/src/test/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerTest.java index 04e74a7..8a19a34 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerTest.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/handling/WebsocketContainerTest.java @@ -3,8 +3,6 @@ import be.tomcools.dropwizard.websocket.WebsocketConfiguration; import be.tomcools.dropwizard.websocket.registration.Endpoints; import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointAnnotatedJava; -import jakarta.websocket.DeploymentException; -import jakarta.websocket.server.ServerContainer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -12,6 +10,9 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import javax.websocket.DeploymentException; +import javax.websocket.server.ServerContainer; + import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) diff --git a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationConfiguration.java b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationConfiguration.java index 172e3f0..d589ae7 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationConfiguration.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationConfiguration.java @@ -4,8 +4,9 @@ import be.tomcools.dropwizard.websocket.WebsocketConfiguration; import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.core.Configuration; -import jakarta.validation.Valid; -import jakarta.validation.constraints.NotNull; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; public class IntegrationConfiguration extends Configuration implements WebsocketBundleConfiguration { diff --git a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationTestApplication.java b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationTestApplication.java index de6d493..3a28ae1 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationTestApplication.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/IntegrationTestApplication.java @@ -7,10 +7,11 @@ import io.dropwizard.core.Application; import io.dropwizard.core.setup.Bootstrap; import io.dropwizard.core.setup.Environment; -import jakarta.websocket.server.ServerEndpointConfig; + +import javax.websocket.server.ServerEndpointConfig; public class IntegrationTestApplication extends Application { - private WebsocketBundle websocket = new WebsocketBundle(); + private final WebsocketBundle websocket = new WebsocketBundle(); @Override public void initialize(Bootstrap bootstrap) { diff --git a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongClientEndpoint.java b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongClientEndpoint.java index e0d7cc0..32f7960 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongClientEndpoint.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongClientEndpoint.java @@ -1,8 +1,8 @@ package be.tomcools.dropwizard.websocket.integrationtest.annotatedjavaee; -import jakarta.websocket.*; import lombok.extern.slf4j.Slf4j; +import javax.websocket.*; import java.io.IOException; import java.net.URI; @@ -33,7 +33,7 @@ public void addMessageHandler(MessageHandler msgHandler) { @OnMessage public void processMessage(String message) { - log.info("Client: Received message: " + message); + log.info("Client: Received message: {}", message); if(handler != null) { handler.handleMessage(message); } diff --git a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongServerEndpoint.java b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongServerEndpoint.java index 75cec7a..16dcbe2 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongServerEndpoint.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/annotatedjavaee/PingPongServerEndpoint.java @@ -1,9 +1,9 @@ package be.tomcools.dropwizard.websocket.integrationtest.annotatedjavaee; -import jakarta.websocket.*; -import jakarta.websocket.server.ServerEndpoint; import lombok.extern.slf4j.Slf4j; +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; import java.io.IOException; diff --git a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/programmaticjavaee/ProgrammaticServerEndpoint.java b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/programmaticjavaee/ProgrammaticServerEndpoint.java index 4a366fb..ed8ddf3 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/programmaticjavaee/ProgrammaticServerEndpoint.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/integrationtest/programmaticjavaee/ProgrammaticServerEndpoint.java @@ -1,11 +1,9 @@ package be.tomcools.dropwizard.websocket.integrationtest.programmaticjavaee; - -import jakarta.websocket.Endpoint; -import jakarta.websocket.EndpointConfig; -import jakarta.websocket.MessageHandler; -import jakarta.websocket.Session; - +import javax.websocket.Endpoint; +import javax.websocket.EndpointConfig; +import javax.websocket.MessageHandler; +import javax.websocket.Session; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/src/test/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistrationTest.java b/src/test/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistrationTest.java index 2587766..c29f0e4 100644 --- a/src/test/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistrationTest.java +++ b/src/test/java/be/tomcools/dropwizard/websocket/registration/EndpointRegistrationTest.java @@ -2,12 +2,13 @@ import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointProgrammaticJava; import be.tomcools.dropwizard.websocket.registration.endpointtypes.EndpointType; -import jakarta.websocket.Endpoint; -import jakarta.websocket.*; -import jakarta.websocket.server.ServerEndpoint; -import jakarta.websocket.server.ServerEndpointConfig; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test;; +import org.junit.jupiter.api.Test; + +import javax.websocket.Endpoint; +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import javax.websocket.server.ServerEndpointConfig; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; @@ -15,7 +16,7 @@ public class EndpointRegistrationTest { - private EndpointRegistration registration = new EndpointRegistration(); + private final EndpointRegistration registration = new EndpointRegistration(); @Test public void canAddAndReturnRegisteredEndpoints() { @@ -69,7 +70,7 @@ public void addingTwoEndpointsWithSamePathCausesException() { } @ServerEndpoint("/chat") - class AnnotatedEndpoint { + static class AnnotatedEndpoint { @OnOpen public void open(Session session) { } @@ -87,7 +88,7 @@ public void handleMessage(String message, Session session) { } } - class ProgrammaticEndpoint extends Endpoint { + static class ProgrammaticEndpoint extends Endpoint { @Override public void onOpen(Session session, EndpointConfig endpointConfig) { //To change body of implemented methods use File | Settings | File Templates.