Skip to content

Commit 9a9b583

Browse files
committed
♻️ refactor: refactor codebase #2
1 parent dd1311a commit 9a9b583

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11
package org.clarify4j.common;
22

3+
import org.springframework.web.context.request.RequestContextHolder;
4+
import org.springframework.web.context.request.ServletRequestAttributes;
5+
import org.unify4j.common.UniqueId4j;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpSession;
9+
310
public class Clarify4j {
11+
12+
/**
13+
* @return the HTTP servlet request, class {@link HttpServletRequest}
14+
*/
15+
public static HttpServletRequest getRequest() {
16+
ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
17+
return s.getRequest();
18+
}
19+
20+
/**
21+
* Retrieves the current session ID from the request context.
22+
* <p>
23+
* This method accesses the current request attributes from the RequestContextHolder
24+
* and extracts the session ID associated with the current request. This is useful
25+
* for tracking the session of the user making the request, especially in web
26+
* applications where session management is crucial for user authentication and
27+
* maintaining user state across multiple requests.
28+
*
29+
* @return the session ID of the current request, or null if no session is associated with the current request context
30+
*/
31+
public static String getCurrentSessionId() {
32+
try {
33+
ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
34+
return s.getSessionId();
35+
} catch (IllegalStateException e) {
36+
return String.valueOf(UniqueId4j.getUniqueId19());
37+
}
38+
}
39+
40+
/**
41+
* Retrieves the session ID from the given HttpServletRequest.
42+
* <p>
43+
* This method gets the current HttpSession associated with the request,
44+
* and then extracts the session ID from it. If there is no current session
45+
* and create is false, it returns null.
46+
*
47+
* @param request the HttpServletRequest from which to retrieve the session ID
48+
* @return the session ID, or null if there is no current session
49+
*/
50+
public static String getSessionId(HttpServletRequest request) {
51+
if (request == null) {
52+
return String.valueOf(UniqueId4j.getUniqueId19());
53+
}
54+
HttpSession session = request.getSession(false); // Pass false to prevent creating a new session if one does not exist
55+
return (session != null) ? session.getId() : null;
56+
}
457
}

plugin/src/main/groovy/org/clarify4j/common/annotation/Saga.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
public @interface Saga {
99
String expression();
1010

11-
boolean disable() default false;
11+
boolean disabled() default false;
1212
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.clarify4j.common.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Documented
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Target({ElementType.METHOD, ElementType.TYPE})
8+
public @interface SagaHeader {
9+
boolean disabled() default false;
10+
}

plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void execute(JoinPoint joinPoint) throws Throwable {
3333
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
3434
Method method = signature.getMethod();
3535
Saga saga = method.getAnnotation(Saga.class);
36-
if (saga.disable()) {
36+
if (saga.disabled()) {
3737
return;
3838
}
3939
EvaluationContext context = new StandardEvaluationContext();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.clarify4j.config.handler;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.reflect.MethodSignature;
7+
import org.clarify4j.common.Clarify4j;
8+
import org.clarify4j.common.annotation.SagaHeader;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.stereotype.Component;
12+
import org.unify4j.common.Json4j;
13+
import org.unify4j.common.Request4j;
14+
15+
import java.lang.reflect.Method;
16+
import java.util.Map;
17+
18+
@Aspect
19+
@Component
20+
public class SagaHeaderHandler {
21+
protected static Logger logger = LoggerFactory.getLogger(SagaHeaderHandler.class);
22+
23+
@Around(value = "@annotation(org.clarify4j.common.annotation.SagaHeader)")
24+
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
25+
Object proceed = joinPoint.proceed();
26+
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
27+
Method method = signature.getMethod();
28+
SagaHeader saga = method.getAnnotation(SagaHeader.class);
29+
if (saga.disabled()) {
30+
return proceed;
31+
}
32+
Map<String, Object> headers = Request4j.getHeaders(Clarify4j.getRequest());
33+
logger.info("Clarify4j, header(s) requesting: {}", Json4j.toJson(headers));
34+
return proceed;
35+
}
36+
}

0 commit comments

Comments
 (0)