diff --git a/plugin/src/main/groovy/org/rmq4j/common/Rmq4j.java b/plugin/src/main/groovy/org/rmq4j/common/Rmq4j.java index 1a11e34..c7876b5 100644 --- a/plugin/src/main/groovy/org/rmq4j/common/Rmq4j.java +++ b/plugin/src/main/groovy/org/rmq4j/common/Rmq4j.java @@ -1,4 +1,57 @@ package org.rmq4j.common; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.unify4j.common.UniqueId4j; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + public class Rmq4j { + + /** + * @return the HTTP servlet request, class {@link HttpServletRequest} + */ + public static HttpServletRequest getRequest() { + ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); + return s.getRequest(); + } + + /** + * Retrieves the current session ID from the request context. + *
+ * This method accesses the current request attributes from the RequestContextHolder + * and extracts the session ID associated with the current request. This is useful + * for tracking the session of the user making the request, especially in web + * applications where session management is crucial for user authentication and + * maintaining user state across multiple requests. + * + * @return the session ID of the current request, or null if no session is associated with the current request context + */ + public static String getCurrentSessionId() { + try { + ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); + return s.getSessionId(); + } catch (IllegalStateException e) { + return String.valueOf(UniqueId4j.getUniqueId19()); + } + } + + /** + * Retrieves the session ID from the given HttpServletRequest. + *
+ * This method gets the current HttpSession associated with the request,
+ * and then extracts the session ID from it. If there is no current session
+ * and create is false, it returns null.
+ *
+ * @param request the HttpServletRequest from which to retrieve the session ID
+ * @return the session ID, or null if there is no current session
+ */
+ public static String getSessionId(HttpServletRequest request) {
+ if (request == null) {
+ return String.valueOf(UniqueId4j.getUniqueId19());
+ }
+ HttpSession session = request.getSession(false); // Pass false to prevent creating a new session if one does not exist
+ return (session != null) ? session.getId() : null;
+ }
}
diff --git a/plugin/src/main/groovy/org/rmq4j/service/Rmq4jCallback.java b/plugin/src/main/groovy/org/rmq4j/service/Rmq4jCallback.java
deleted file mode 100644
index 4b1a234..0000000
--- a/plugin/src/main/groovy/org/rmq4j/service/Rmq4jCallback.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.rmq4j.service;
-
-public interface Rmq4jCallback {
-
- void onSuccess();
-
- void onError();
-}
diff --git a/plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java b/plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
index 282dfc2..6b5ecb8 100644
--- a/plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
+++ b/plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
@@ -24,13 +24,23 @@ public interface Rmq4jService {
Optional
+ * This method initializes a {@link ConnectionFactory} using the details from the given {@link Rmq4jProperties.Connection}.
+ * It sets the host, port, virtual host, username, and password on the factory. If SSL is enabled in the configuration,
+ * it applies SSL settings to the factory.
+ *
+ * If the configuration is null or the cluster is not enabled, it returns an empty {@link Optional}.
+ * If an exception occurs while applying SSL settings, it logs the error and throws a {@link RuntimeException}.
+ *
+ * @param connection The cluster configuration used to create the {@link ConnectionFactory}.
+ * @return An {@link Optional} containing the configured {@link ConnectionFactory} if the configuration is valid and SSL settings are applied successfully;
+ * otherwise, an empty {@link Optional}.
+ */
+ @Override
+ public Optional
@@ -160,6 +199,40 @@ public Optional
+ * This method first uses {@link #createFactory(Rmq4jProperties.Connection)} to create a {@link ConnectionFactory}
+ * from the given cluster configuration. If the factory creation is successful, it wraps the factory in a
+ * {@link CachingConnectionFactory} and returns it as an {@link Optional}.
+ *
+ * If the {@link ConnectionFactory} could not be created (e.g., due to invalid configuration), it returns
+ * an empty {@link Optional}.
+ *
+ * @param connection The cluster configuration used to create the {@link ConnectionFactory}.
+ * @return An {@link Optional} containing the {@link CachingConnectionFactory} if the {@link ConnectionFactory}
+ * was created successfully; otherwise, an empty {@link Optional}.
+ */
+ @Override
+ public Optional
@@ -185,6 +258,41 @@ public Optional
+ * This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
+ * {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
+ * {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitTemplate}
+ * and returns it as an {@link Optional}.
+ *
+ * If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
+ * it returns an empty {@link Optional}.
+ *
+ * @param connection The cluster configuration used to create the {@link CachingConnectionFactory}.
+ * @return An {@link Optional} containing the {@link RabbitTemplate} if the {@link CachingConnectionFactory}
+ * was created successfully; otherwise, an empty {@link Optional}.
+ */
+ @Override
+ public Optional
@@ -210,6 +318,41 @@ public Optional
+ * This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
+ * {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
+ * {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitTemplate}
+ * and returns it as an {@link Optional}.
+ *
+ * If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
+ * it returns an empty {@link Optional}.
+ *
+ * @param factory The cluster configuration used to create the {@link CachingConnectionFactory}.
+ * @return An {@link Optional} containing the {@link RabbitTemplate} if the {@link CachingConnectionFactory}
+ * was created successfully; otherwise, an empty {@link Optional}.
+ */
+ @Override
+ public Optional
@@ -235,6 +378,41 @@ public Optional
+ * This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
+ * {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
+ * {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitAdmin}
+ * and returns it as an {@link Optional}.
+ *
+ * If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
+ * it returns an empty {@link Optional}.
+ *
+ * @param connection The cluster configuration used to create the {@link CachingConnectionFactory}.
+ * @return An {@link Optional} containing the {@link RabbitAdmin} if the {@link CachingConnectionFactory}
+ * was created successfully; otherwise, an empty {@link Optional}.
+ */
+ @Override
+ public Optional