Skip to content

Commit d866fd9

Browse files
Drop static method
1 parent 848f189 commit d866fd9

13 files changed

Lines changed: 40 additions & 53 deletions

File tree

problem4j-spring-web/src/main/java/io/github/problem4j/spring/web/context/AttributeSupport.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,20 @@
1414
*/
1515
package io.github.problem4j.spring.web.context;
1616

17-
import java.util.UUID;
18-
1917
/**
2018
* Utility class providing constants and helper methods for tracing support within the Problem4J.
2119
*/
2220
public final class AttributeSupport {
2321

2422
/** Request attribute key used to store a trace identifier. */
25-
public static final String TRACE_ID =
26-
"io.github.problem4j.spring.web.context.ProblemContext.traceId";
23+
public static final String TRACE_ID_ATTRIBUTE = "io.github.problem4j.spring.web.traceId";
2724

2825
/**
2926
* Request attribute key used to store the object associated with the current request. It allows
3027
* sharing contextual information (such as trace identifiers or additional diagnostic data)
3128
* between components involved in problem handling.
3229
*/
33-
public static final String PROBLEM_CONTEXT =
34-
"io.github.problem4j.spring.web.context.ProblemContext";
35-
36-
/**
37-
* Generates a random trace identifier in {@code urn:uuid:<uuid>} format.
38-
*
39-
* @return generated trace identifier
40-
*/
41-
public static String getRandomTraceId() {
42-
return "urn:uuid:" + UUID.randomUUID();
43-
}
30+
public static final String PROBLEM_CONTEXT_ATTRIBUTE = "io.github.problem4j.spring.web.traceId";
4431

4532
private AttributeSupport() {}
4633
}

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/ExceptionWebFluxAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webflux;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.web.util.ProblemSupport.resolveStatus;
1919
import static io.github.problem4j.spring.webflux.WebFluxAdviceSupport.logAdviceException;
2020

@@ -87,7 +87,7 @@ public ExceptionWebFluxAdvice(
8787
@ExceptionHandler(Exception.class)
8888
public Mono<ResponseEntity<Problem>> handleException(Exception ex, ServerWebExchange exchange) {
8989
ProblemContext context =
90-
exchange.getAttributeOrDefault(PROBLEM_CONTEXT, ProblemContext.create());
90+
exchange.getAttributeOrDefault(PROBLEM_CONTEXT_ATTRIBUTE, ProblemContext.create());
9191

9292
HttpHeaders headers = new HttpHeaders();
9393
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/ProblemEnhancedWebFluxHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webflux;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.web.util.ProblemSupport.resolveStatus;
1919
import static io.github.problem4j.spring.webflux.WebFluxAdviceSupport.logAdviceException;
2020

@@ -88,7 +88,7 @@ protected Mono<ResponseEntity<Object>> handleExceptionInternal(
8888
HttpStatusCode status,
8989
ServerWebExchange exchange) {
9090
ProblemContext context =
91-
exchange.getAttributeOrDefault(PROBLEM_CONTEXT, ProblemContext.create());
91+
exchange.getAttributeOrDefault(PROBLEM_CONTEXT_ATTRIBUTE, ProblemContext.create());
9292

9393
headers = headers != null ? HttpHeaders.writableHttpHeaders(headers) : new HttpHeaders();
9494
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/ProblemExceptionWebFluxAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webflux;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.webflux.WebFluxAdviceSupport.logAdviceException;
1919

2020
import io.github.problem4j.core.Problem;
@@ -67,7 +67,7 @@ public ProblemExceptionWebFluxAdvice(
6767
public Mono<ResponseEntity<Problem>> handleProblemException(
6868
ProblemException ex, ServerWebExchange exchange) {
6969
ProblemContext context =
70-
exchange.getAttributeOrDefault(PROBLEM_CONTEXT, ProblemContext.create());
70+
exchange.getAttributeOrDefault(PROBLEM_CONTEXT_ATTRIBUTE, ProblemContext.create());
7171

7272
HttpHeaders headers = new HttpHeaders();
7373
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/WebFluxAdviceSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webflux;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID_ATTRIBUTE;
1818

1919
import org.slf4j.Logger;
2020
import org.springframework.web.server.ServerWebExchange;
@@ -27,7 +27,7 @@ static void logAdviceException(
2727
"Unable to resolve problem response (method={}, path={}, traceId={}, message={}, originalException=[{} : {}])",
2828
exchange.getRequest().getMethod(),
2929
exchange.getRequest().getPath(),
30-
exchange.getAttribute(TRACE_ID),
30+
exchange.getAttribute(TRACE_ID_ATTRIBUTE),
3131
e.getMessage(),
3232
ex.getClass().getName(),
3333
ex.getMessage(),

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/context/ProblemContextWebFluxFilter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
*/
1515
package io.github.problem4j.spring.webflux.context;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
18-
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID;
19-
import static io.github.problem4j.spring.web.context.AttributeSupport.getRandomTraceId;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
18+
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID_ATTRIBUTE;
2019

2120
import io.github.problem4j.core.ProblemContext;
2221
import io.github.problem4j.spring.web.context.ProblemContextSettings;
2322
import java.util.Optional;
23+
import java.util.UUID;
2424
import org.springframework.util.StringUtils;
2525
import org.springframework.web.server.ServerWebExchange;
2626
import org.springframework.web.server.WebFilter;
@@ -75,7 +75,7 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
7575
* @return an existing or newly created {@link ProblemContext}
7676
*/
7777
protected ProblemContext buildProblemContext(ServerWebExchange exchange) {
78-
return exchange.getAttribute(PROBLEM_CONTEXT) instanceof ProblemContext attribute
78+
return exchange.getAttribute(PROBLEM_CONTEXT_ATTRIBUTE) instanceof ProblemContext attribute
7979
? attribute
8080
: ProblemContext.create()
8181
.put("traceId", findTraceId(exchange).orElseGet(() -> initTraceId(exchange)));
@@ -88,7 +88,7 @@ protected ProblemContext buildProblemContext(ServerWebExchange exchange) {
8888
* @return an {@link Optional} containing the trace ID if present
8989
*/
9090
protected Optional<String> findTraceId(ServerWebExchange exchange) {
91-
return Optional.ofNullable(exchange.getAttribute(TRACE_ID)).map(Object::toString);
91+
return Optional.ofNullable(exchange.getAttribute(TRACE_ID_ATTRIBUTE)).map(Object::toString);
9292
}
9393

9494
/**
@@ -119,12 +119,12 @@ protected String initTraceId(ServerWebExchange exchange) {
119119
* @return a newly generated trace ID
120120
*/
121121
protected String createNewTraceId(ServerWebExchange exchange) {
122-
return getRandomTraceId();
122+
return "urn:uuid:" + UUID.randomUUID();
123123
}
124124

125125
protected void assignContextAttributes(ServerWebExchange exchange, ProblemContext context) {
126-
exchange.getAttributes().put(PROBLEM_CONTEXT, context);
127-
exchange.getAttributes().put(TRACE_ID, context.get("traceId"));
126+
exchange.getAttributes().put(PROBLEM_CONTEXT_ATTRIBUTE, context);
127+
exchange.getAttributes().put(TRACE_ID_ATTRIBUTE, context.get("traceId"));
128128
}
129129

130130
/**
@@ -161,9 +161,9 @@ protected void assignTracingHeader(ServerWebExchange exchange, ProblemContext co
161161
* @return an updated {@link Context} containing the problem context and trace ID
162162
*/
163163
protected Context contextWrite(Context ctx, ServerWebExchange exchange, ProblemContext context) {
164-
ctx = ctx.put(PROBLEM_CONTEXT, context);
164+
ctx = ctx.put(PROBLEM_CONTEXT_ATTRIBUTE, context);
165165
if (StringUtils.hasLength(context.get("traceId"))) {
166-
ctx = ctx.put(TRACE_ID, context.get("traceId"));
166+
ctx = ctx.put(TRACE_ID_ATTRIBUTE, context.get("traceId"));
167167
}
168168
return ctx;
169169
}

problem4j-spring-webflux/src/main/java/io/github/problem4j/spring/webflux/error/ProblemErrorWebExceptionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webflux.error;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.web.util.ProblemSupport.resolveStatus;
1919
import static org.springframework.web.reactive.function.server.RequestPredicates.all;
2020
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@@ -99,7 +99,7 @@ private Mono<ServerResponse> override(ServerRequest request, ServerResponse resp
9999
Problem problem = Problem.builder().status(resolveStatus(response.statusCode())).build();
100100

101101
Optional<ProblemContext> optionalContext =
102-
request.attribute(PROBLEM_CONTEXT).map(context -> (ProblemContext) context);
102+
request.attribute(PROBLEM_CONTEXT_ATTRIBUTE).map(context -> (ProblemContext) context);
103103
if (optionalContext.isPresent()) {
104104
ProblemContext context = optionalContext.get();
105105
problem = problemPostProcessor.process(context, problem);

problem4j-spring-webmvc/src/main/java/io/github/problem4j/spring/webmvc/ExceptionMvcAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webmvc;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.web.util.ProblemSupport.resolveStatus;
1919
import static io.github.problem4j.spring.webmvc.MvcAdviceSupport.logAdviceException;
2020
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
@@ -87,7 +87,7 @@ public ExceptionMvcAdvice(
8787
*/
8888
@ExceptionHandler(Exception.class)
8989
public ResponseEntity<Object> handleException(Exception ex, WebRequest request) {
90-
ProblemContext context = (ProblemContext) request.getAttribute(PROBLEM_CONTEXT, SCOPE_REQUEST);
90+
ProblemContext context = (ProblemContext) request.getAttribute(PROBLEM_CONTEXT_ATTRIBUTE, SCOPE_REQUEST);
9191
if (context == null) {
9292
context = ProblemContext.create();
9393
}

problem4j-spring-webmvc/src/main/java/io/github/problem4j/spring/webmvc/MvcAdviceSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webmvc;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.TRACE_ID_ATTRIBUTE;
1818
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
1919

2020
import org.slf4j.Logger;
@@ -32,7 +32,7 @@ static void logAdviceException(Logger log, Exception ex, WebRequest request, Exc
3232
method = String.valueOf(req.getHttpMethod());
3333
endpoint = req.getRequest().getRequestURI();
3434

35-
Object traceIdAttr = req.getAttribute(TRACE_ID, SCOPE_REQUEST);
35+
Object traceIdAttr = req.getAttribute(TRACE_ID_ATTRIBUTE, SCOPE_REQUEST);
3636
if (traceIdAttr != null) {
3737
traceId = traceIdAttr.toString();
3838
}

problem4j-spring-webmvc/src/main/java/io/github/problem4j/spring/webmvc/ProblemEnhancedMvcHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
package io.github.problem4j.spring.webmvc;
1616

17-
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT;
17+
import static io.github.problem4j.spring.web.context.AttributeSupport.PROBLEM_CONTEXT_ATTRIBUTE;
1818
import static io.github.problem4j.spring.web.util.ProblemSupport.resolveStatus;
1919
import static io.github.problem4j.spring.webmvc.MvcAdviceSupport.logAdviceException;
2020
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
@@ -83,7 +83,7 @@ public ProblemEnhancedMvcHandler(
8383
@Override
8484
protected ResponseEntity<Object> handleExceptionInternal(
8585
Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
86-
ProblemContext context = (ProblemContext) request.getAttribute(PROBLEM_CONTEXT, SCOPE_REQUEST);
86+
ProblemContext context = (ProblemContext) request.getAttribute(PROBLEM_CONTEXT_ATTRIBUTE, SCOPE_REQUEST);
8787
if (context == null) {
8888
context = ProblemContext.create();
8989
}

0 commit comments

Comments
 (0)