Skip to content

Commit

Permalink
♻️ refactor: refactor codebase #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Aug 24, 2024
1 parent e9fefa0 commit eeaf3fc
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 8 deletions.
53 changes: 53 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/common/Rmq4j.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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.
* <p>
* 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;
}
}
8 changes: 0 additions & 8 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jCallback.java

This file was deleted.

10 changes: 10 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ public interface Rmq4jService {

Optional<ConnectionFactory> createFactory(Rmq4jProperties.Connection connection);

Optional<ConnectionFactory> createFactory(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback);

Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Connection connection);

Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback);

Optional<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection);

Optional<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback);

Optional<RabbitTemplate> dispatch(CachingConnectionFactory factory);

Optional<RabbitTemplate> dispatch(CachingConnectionFactory factory, Rmq4jWrapCallback callback);

Optional<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection);

Optional<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback);

String getURLConnSchema(Rmq4jProperties.Connection connection);
}
20 changes: 20 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jWrapCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.rmq4j.service;

import org.unify4j.model.response.WrapResponse;

/**
* This interface defines a callback mechanism for handling RabbitMQ-related operations.
* Implementations of this interface should provide logic for handling exceptions
* that may occur during these operations and return a wrapped response.
*/
public interface Rmq4jWrapCallback {

/**
* This method is called when an exception occurs during a RabbitMQ operation.
* Implementations should handle the exception and return an appropriate
* WrapResponse object.
*
* @param response a WrapResponse object containing the result or error information, class {@link WrapResponse}
*/
void onCallback(WrapResponse<?> response);
}
178 changes: 178 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/impl/Rmq4jServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.rmq4j.service.impl;

import com.rabbitmq.client.ConnectionFactory;
import org.rmq4j.common.Rmq4j;
import org.rmq4j.config.props.Rmq4jProperties;
import org.rmq4j.service.Rmq4jService;
import org.rmq4j.service.Rmq4jWrapCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
Expand All @@ -12,7 +14,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unify4j.common.Collection4j;
import org.unify4j.common.Json4j;
import org.unify4j.common.String4j;
import org.unify4j.model.builder.HttpStatusBuilder;
import org.unify4j.model.builder.HttpWrapBuilder;
import org.unify4j.model.enums.IconType;

import java.util.Map;
Expand Down Expand Up @@ -136,6 +141,40 @@ public Optional<ConnectionFactory> createFactory(Rmq4jProperties.Connection conn
}
}

/**
* Creates a {@link ConnectionFactory} for RabbitMQ based on the provided cluster configuration.
* <p>
* 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.
* <p>
* 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<ConnectionFactory> createFactory(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback) {
HttpWrapBuilder<?> response = new HttpWrapBuilder<>().ok(null).requestId(Rmq4j.getCurrentSessionId());
Optional<ConnectionFactory> factory = Optional.empty();
try {
factory = this.createFactory(connection);
} catch (Exception e) {
response
.statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR)
.message("creating connection factory failed")
.debug("cause", e.getMessage())
.errors(e)
.customFields("conn_string", Json4j.toJson(connection));
}
if (callback != null) {
callback.onCallback(response.build());
}
return factory;
}

/**
* Creates a {@link CachingConnectionFactory} for RabbitMQ based on the provided cluster configuration.
* <p>
Expand All @@ -160,6 +199,40 @@ public Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties
return Optional.of(new CachingConnectionFactory(factory.get()));
}

/**
* Creates a {@link CachingConnectionFactory} for RabbitMQ based on the provided cluster configuration.
* <p>
* 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}.
* <p>
* 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<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback) {
HttpWrapBuilder<?> response = new HttpWrapBuilder<>().ok(null).requestId(Rmq4j.getCurrentSessionId());
Optional<CachingConnectionFactory> factory = Optional.empty();
try {
factory = this.createCacheConnFactory(connection);
} catch (Exception e) {
response
.statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR)
.message("creating cache connection factory failed")
.debug("cause", e.getMessage())
.errors(e)
.customFields("conn_string", Json4j.toJson(connection));
}
if (callback != null) {
callback.onCallback(response.build());
}
return factory;
}

/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
Expand All @@ -185,6 +258,41 @@ public Optional<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection)
return this.dispatch(factory.get());
}

/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
* 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}.
* <p>
* 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<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback) {
HttpWrapBuilder<?> response = new HttpWrapBuilder<>().ok(null).requestId(Rmq4j.getCurrentSessionId());
Optional<RabbitTemplate> template = Optional.empty();
try {
template = this.dispatch(connection);
} catch (Exception e) {
response
.statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR)
.message("creating RabbitMQ Template failed")
.debug("cause", e.getMessage())
.errors(e)
.customFields("conn_string", Json4j.toJson(connection));
}
if (callback != null) {
callback.onCallback(response.build());
}
return template;
}

/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
Expand All @@ -210,6 +318,41 @@ public Optional<RabbitTemplate> dispatch(CachingConnectionFactory factory) {
return Optional.of(template);
}

/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
* 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}.
* <p>
* 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<RabbitTemplate> dispatch(CachingConnectionFactory factory, Rmq4jWrapCallback callback) {
HttpWrapBuilder<?> response = new HttpWrapBuilder<>().ok(null).requestId(Rmq4j.getCurrentSessionId());
Optional<RabbitTemplate> template = Optional.empty();
try {
template = this.dispatch(factory);
} catch (Exception e) {
response
.statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR)
.message("creating RabbitMQ Template failed")
.debug("cause", e.getMessage())
.errors(e)
.customFields("cache_conn_string", factory != null ? factory.getCacheProperties().toString() : "undefined cache connection factory");
}
if (callback != null) {
callback.onCallback(response.build());
}
return template;
}

/**
* Creates a {@link RabbitAdmin} for RabbitMQ based on the provided cluster configuration.
* <p>
Expand All @@ -235,6 +378,41 @@ public Optional<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection) {
return Optional.of(new RabbitAdmin(factory.get()));
}

/**
* Creates a {@link RabbitAdmin} for RabbitMQ based on the provided cluster configuration.
* <p>
* 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}.
* <p>
* 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<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection, Rmq4jWrapCallback callback) {
HttpWrapBuilder<?> response = new HttpWrapBuilder<>().ok(null).requestId(Rmq4j.getCurrentSessionId());
Optional<RabbitAdmin> adm = Optional.empty();
try {
adm = this.createAdm(connection);
} catch (Exception e) {
response
.statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR)
.message("creating RabbitMQ Admin failed")
.debug("cause", e.getMessage())
.errors(e)
.customFields("conn_string", Json4j.toJson(connection));
}
if (callback != null) {
callback.onCallback(response.build());
}
return adm;
}

/**
* Generates the connection URL schema for RabbitMQ based on the provided cluster configuration.
* <p>
Expand Down

0 comments on commit eeaf3fc

Please sign in to comment.