diff --git a/modules/cpr/src/main/java/org/atmosphere/annotation/MeteorServiceProcessor.java b/modules/cpr/src/main/java/org/atmosphere/annotation/MeteorServiceProcessor.java index af73c708d4..daf01c1bd7 100644 --- a/modules/cpr/src/main/java/org/atmosphere/annotation/MeteorServiceProcessor.java +++ b/modules/cpr/src/main/java/org/atmosphere/annotation/MeteorServiceProcessor.java @@ -41,7 +41,9 @@ public class MeteorServiceProcessor implements Processor { @Override public void handle(AtmosphereFramework framework, Class annotatedClass) { try { - ReflectorServletProcessor r = framework.newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class); + + Class processorClass = ReflectorServletProcessor.class; + ReflectorServletProcessor r = framework.newClassInstance(processorClass, processorClass); r.setServletClassName(annotatedClass.getName()); LinkedList l = new LinkedList<>(); diff --git a/modules/cpr/src/main/java/org/atmosphere/config/WebSocketSessionConfiguration.java b/modules/cpr/src/main/java/org/atmosphere/config/WebSocketSessionConfiguration.java new file mode 100644 index 0000000000..f1427da1a2 --- /dev/null +++ b/modules/cpr/src/main/java/org/atmosphere/config/WebSocketSessionConfiguration.java @@ -0,0 +1,29 @@ +package org.atmosphere.config; + +import jakarta.websocket.Session; + +public class WebSocketSessionConfiguration { + private final Session session; + private final int maxBinaryBufferSize; + private final int webSocketIdleTimeoutMs; + private final int maxTextBufferSize; + + public WebSocketSessionConfiguration(Session session, int maxBinaryBufferSize, int webSocketIdleTimeoutMs, int maxTextBufferSize) { + this.session = session; + this.maxBinaryBufferSize = maxBinaryBufferSize; + this.webSocketIdleTimeoutMs = webSocketIdleTimeoutMs; + this.maxTextBufferSize = maxTextBufferSize; + } + + public void configure() { + if (maxBinaryBufferSize != -1) { + session.setMaxBinaryMessageBufferSize(maxBinaryBufferSize); + } + if (webSocketIdleTimeoutMs != -1) { + session.setMaxIdleTimeout(webSocketIdleTimeoutMs); + } + if (maxTextBufferSize != -1) { + session.setMaxTextMessageBufferSize(maxTextBufferSize); + } + } +} \ No newline at end of file diff --git a/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java b/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java index 914e869387..046a007ec3 100644 --- a/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java +++ b/modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java @@ -23,6 +23,7 @@ import jakarta.websocket.MessageHandler; import jakarta.websocket.Session; import jakarta.websocket.server.HandshakeRequest; +import org.atmosphere.config.WebSocketSessionConfiguration; import org.atmosphere.container.version.JSR356WebSocket; import org.atmosphere.cpr.ApplicationConfig; import org.atmosphere.cpr.AtmosphereFramework; @@ -117,9 +118,11 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) { return; } - if (maxBinaryBufferSize != -1) session.setMaxBinaryMessageBufferSize(maxBinaryBufferSize); - if (webSocketIdleTimeoutMs != -1) session.setMaxIdleTimeout(webSocketIdleTimeoutMs); - if (maxTextBufferSize != -1) session.setMaxTextMessageBufferSize(maxTextBufferSize); + + WebSocketSessionConfiguration sessionConfigurator = new WebSocketSessionConfiguration( + session, maxBinaryBufferSize, webSocketIdleTimeoutMs, maxTextBufferSize + ); + sessionConfigurator.configure(); webSocket = new JSR356WebSocket(session, framework.getAtmosphereConfig()); diff --git a/modules/cpr/src/main/java/org/atmosphere/cpr/ContainerInitializer.java b/modules/cpr/src/main/java/org/atmosphere/cpr/ContainerInitializer.java index 8d2468d423..7029c24f37 100644 --- a/modules/cpr/src/main/java/org/atmosphere/cpr/ContainerInitializer.java +++ b/modules/cpr/src/main/java/org/atmosphere/cpr/ContainerInitializer.java @@ -71,7 +71,7 @@ public void onStartup(Set> classes, final ServletContext c) { force = false; } - if (force || l.size() == size && resolver.testClassExists(DefaultAsyncSupportResolver.JSR356_WEBSOCKET)) { + if (force || l.size() == size && resolver.testClassExists("jakarta.websocket.Endpoint")) { try { framework.setAsyncSupport(new JSR356AsyncSupport(framework.getAtmosphereConfig(), c)); } catch (IllegalStateException ex) { diff --git a/modules/cpr/src/main/java/org/atmosphere/cpr/DefaultAsyncSupportResolver.java b/modules/cpr/src/main/java/org/atmosphere/cpr/DefaultAsyncSupportResolver.java index 19f51cba91..5e1ca87c91 100644 --- a/modules/cpr/src/main/java/org/atmosphere/cpr/DefaultAsyncSupportResolver.java +++ b/modules/cpr/src/main/java/org/atmosphere/cpr/DefaultAsyncSupportResolver.java @@ -34,20 +34,68 @@ */ public class DefaultAsyncSupportResolver implements AsyncSupportResolver { - private static final Logger logger = LoggerFactory.getLogger(DefaultAsyncSupportResolver.class); + private static Logger logger = LoggerFactory.getLogger(DefaultAsyncSupportResolver.class); - public final static String SERVLET_30 = "jakarta.servlet.AsyncListener"; - public final static String NETTY = "org.jboss.netty.channel.Channel"; - public final static String JSR356_WEBSOCKET = "jakarta.websocket.Endpoint"; + private static String SERVLET_30 = "jakarta.servlet.AsyncListener"; + private static String NETTY = "org.jboss.netty.channel.Channel"; + private static String JSR356_WEBSOCKET = "jakarta.websocket.Endpoint"; - private final AtmosphereConfig config; - - private final boolean suppress356; + private AtmosphereConfig config; + private boolean suppress356; public DefaultAsyncSupportResolver(final AtmosphereConfig config) { this.config = config; - this.suppress356 = - Boolean.parseBoolean(config.getInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356)); + this.suppress356 = Boolean.parseBoolean(config.getInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356)); + } + + // Getter and Setter Methods for the variables + public static String getServlet30() { + return SERVLET_30; + } + + public static void setServlet30(String servlet30) { + SERVLET_30 = servlet30; + } + + public static String getNetty() { + return NETTY; + } + + public static void setNetty(String netty) { + NETTY = netty; + } + + public static String getJsr356WebSocket() { + return JSR356_WEBSOCKET; + } + + public static void setJsr356WebSocket(String jsr356WebSocket) { + JSR356_WEBSOCKET = jsr356WebSocket; + } + + public AtmosphereConfig getConfig() { + return config; + } + + public void setConfig(AtmosphereConfig config) { + this.config = config; + } + + public boolean isSuppress356() { + return suppress356; + } + + public void setSuppress356(boolean suppress356) { + this.suppress356 = suppress356; + } + + // Getter and Setter for logger + public static Logger getLogger() { + return logger; + } + + public static void setLogger(Logger logger) { + DefaultAsyncSupportResolver.logger = logger; } /** @@ -59,7 +107,7 @@ public DefaultAsyncSupportResolver(final AtmosphereConfig config) { protected boolean testClassExists(final String testClass) { try { final boolean exists = testClass != null && testClass.length() > 0 && IOUtils.loadClass(null, testClass) != null; - logger.debug(exists ? "Found {}" : "Not found {}", testClass); + getLogger().debug(exists ? "Found {}" : "Not found {}", testClass); return exists; } catch (Exception ex) { return false; @@ -74,23 +122,22 @@ protected boolean testClassExists(final String testClass) { public List> detectContainersPresent() { return new LinkedList>() { { - if (testClassExists(NETTY)) + if (testClassExists(getNetty())) { add(NettyCometSupport.class); + } } }; } public List> detectWebSocketPresent(final boolean useNativeIfPossible, final boolean useServlet30Async) { - return new LinkedList>() { { if (useServlet30Async && !useNativeIfPossible) { - - if (!suppress356 && testClassExists(JSR356_WEBSOCKET)) { + if (!isSuppress356() && testClassExists(getJsr356WebSocket())) { add(JSR356AsyncSupport.class); } } else { - if (!suppress356 && testClassExists(JSR356_WEBSOCKET)) { + if (!isSuppress356() && testClassExists(getJsr356WebSocket())) { add(JSR356AsyncSupport.class); } } @@ -105,10 +152,10 @@ public List> detectWebSocketPresent(final boolean * @return */ public AsyncSupport defaultCometSupport(final boolean preferBlocking) { - if (!preferBlocking && testClassExists(SERVLET_30)) { - return new Servlet30CometSupport(config); + if (!preferBlocking && testClassExists(getServlet30())) { + return new Servlet30CometSupport(getConfig()); } else { - return new BlockingIOCometSupport(config); + return new BlockingIOCometSupport(getConfig()); } } @@ -123,13 +170,13 @@ public AsyncSupport defaultCometSupport(final boolean preferBlocking) { public AsyncSupport newCometSupport(final Class targetClass) { try { return targetClass.getDeclaredConstructor(new Class[]{AtmosphereConfig.class}) - .newInstance(config); + .newInstance(getConfig()); } catch (final Exception e) { - logger.warn("Failed to create AsyncSupport class: {}, error: {}", targetClass, e); + getLogger().warn("Failed to create AsyncSupport class: {}, error: {}", targetClass, e); Throwable cause = e.getCause(); if (cause != null) { - logger.error("Real error: {}", cause.getMessage(), cause); + getLogger().error("Real error: {}", cause.getMessage(), cause); } return null; } @@ -139,12 +186,12 @@ public AsyncSupport newCometSupport(final String targetClassFQN) { try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); return (AsyncSupport) cl.loadClass(targetClassFQN) - .getDeclaredConstructor(new Class[]{AtmosphereConfig.class}).newInstance(config); + .getDeclaredConstructor(new Class[]{AtmosphereConfig.class}).newInstance(getConfig()); } catch (final Exception e) { - logger.error("Failed to create AsyncSupport class: {}, error: {}", targetClassFQN, e); + getLogger().error("Failed to create AsyncSupport class: {}, error: {}", targetClassFQN, e); Throwable cause = e.getCause(); if (cause != null) { - logger.error("Real error: {}", cause.getMessage(), cause); + getLogger().error("Real error: {}", cause.getMessage(), cause); } throw new IllegalArgumentException("Unable to create " + targetClassFQN, e); } @@ -174,7 +221,7 @@ public AsyncSupport resolve(boolean useNativeIfPossible, boolean defaultToBlocki AsyncSupport cs = null; // Validate the value for old Servlet Container. - useServlet30Async = testClassExists(SERVLET_30); + useServlet30Async = testClassExists(getServlet30()); if (!defaultToBlocking) { List> l = detectWebSocketPresent(useNativeIfPossible, useServlet30Async); @@ -201,7 +248,7 @@ public AsyncSupport resolveWebSocket(final java.util.List> available) { if (available == null || available.isEmpty()) return null; @@ -221,7 +268,8 @@ protected AsyncSupport resolveMultipleNativeSupportConflict(final List