Skip to content

Commit bb9bdad

Browse files
committed
refactor: Address Sonar findings on transport-neutral Request
- OpenApiServer: bundle handlers/interceptors/decorators/exceptionHandler/ extras into a private HandlerConfig record, dropping the constructor from 9 params to 5 (under Sonar's brain-overload threshold of 7). - OpenApiServer.jsonMapper: use the existing JSON constant instead of duplicating the "application/json" literal. - Request: switch headerLookup from Function<String,String> to the more specialised UnaryOperator<String>. - DispatchHandlerTest.withRequest: drop the unused HttpExchange parameter (no longer needed now that Request is built from primitives).
1 parent 791203c commit bb9bdad

4 files changed

Lines changed: 28 additions & 29 deletions

File tree

src/main/java/com/retailsvc/http/OpenApiServer.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,26 @@ public class OpenApiServer implements AutoCloseable {
4343
private final HttpServer httpServer;
4444
private final int shutdownTimeoutSeconds;
4545

46-
OpenApiServer(
47-
Spec spec,
48-
Map<String, TypeMapper> bodyMappers,
46+
/** Internal grouping of handler-related configuration to keep the constructor signature small. */
47+
record HandlerConfig(
4948
Map<String, RequestHandler> handlers,
50-
List<ResponseDecorator> decorators,
5149
List<RequestInterceptor> interceptors,
50+
List<ResponseDecorator> decorators,
5251
ExceptionHandler exceptionHandler,
52+
Map<String, HttpHandler> extras) {}
53+
54+
OpenApiServer(
55+
Spec spec,
56+
Map<String, TypeMapper> bodyMappers,
57+
HandlerConfig handlerConfig,
5358
int port,
54-
Map<String, HttpHandler> extras,
5559
int shutdownTimeoutSeconds)
5660
throws IOException {
5761

5862
requireNonNull(spec, "Spec must not be null");
5963
requireNonNull(bodyMappers, "bodyMappers must not be null");
60-
requireNonNull(handlers, "handlers must not be null");
64+
requireNonNull(handlerConfig.handlers(), "handlers must not be null");
65+
ExceptionHandler exceptionHandler = handlerConfig.exceptionHandler();
6166
if (exceptionHandler == null) {
6267
LOG.warn("No ExceptionHandler set, using default");
6368
exceptionHandler = Handlers.defaultExceptionHandler();
@@ -74,9 +79,13 @@ public class OpenApiServer implements AutoCloseable {
7479
ctx.getFilters().add(new ExceptionFilter(exceptionHandler));
7580
ctx.getFilters().add(new RequestPreparationFilter(spec, router, validator, bodyMappers));
7681
ctx.setHandler(
77-
new DispatchHandler(handlers, interceptors, decorators, new ResponseRenderer(bodyMappers)));
82+
new DispatchHandler(
83+
handlerConfig.handlers(),
84+
handlerConfig.interceptors(),
85+
handlerConfig.decorators(),
86+
new ResponseRenderer(bodyMappers)));
7887

79-
for (Map.Entry<String, HttpHandler> e : extras.entrySet()) {
88+
for (Map.Entry<String, HttpHandler> e : handlerConfig.extras().entrySet()) {
8089
HttpContext extraCtx = httpServer.createContext(e.getKey());
8190
extraCtx.getFilters().add(new ExceptionFilter(exceptionHandler));
8291
extraCtx.setHandler(e.getValue());
@@ -146,7 +155,7 @@ public Builder bodyMapper(String mediaType, TypeMapper mapper) {
146155
}
147156

148157
public Builder jsonMapper(TypeMapper mapper) {
149-
return bodyMapper("application/json", mapper);
158+
return bodyMapper(JSON, mapper);
150159
}
151160

152161
public Builder handlers(Map<String, RequestHandler> handlers) {
@@ -224,16 +233,9 @@ public OpenApiServer build() throws IOException {
224233
}
225234
}
226235
Map<String, TypeMapper> resolved = resolveBodyMappers(bodyMappers);
227-
return new OpenApiServer(
228-
spec,
229-
resolved,
230-
handlers,
231-
decorators,
232-
interceptors,
233-
exceptionHandler,
234-
port,
235-
extras,
236-
shutdownTimeoutSeconds);
236+
HandlerConfig handlerConfig =
237+
new HandlerConfig(handlers, interceptors, decorators, exceptionHandler, extras);
238+
return new OpenApiServer(spec, resolved, handlerConfig, port, shutdownTimeoutSeconds);
237239
}
238240

239241
private static Map<String, TypeMapper> resolveBodyMappers(

src/main/java/com/retailsvc/http/Request.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Map;
77
import java.util.Objects;
88
import java.util.Optional;
9-
import java.util.function.Function;
9+
import java.util.function.UnaryOperator;
1010

1111
/**
1212
* Read-only per-request handle passed to {@link RequestHandler}. Carries the parsed body, path
@@ -28,7 +28,7 @@ public final class Request {
2828
private final String operationId;
2929
private final Map<String, String> pathParameters;
3030
private final String rawQuery;
31-
private final Function<String, String> headerLookup;
31+
private final UnaryOperator<String> headerLookup;
3232
private Map<String, String> queryParamCache;
3333

3434
/**
@@ -51,7 +51,7 @@ public Request(
5151
String operationId,
5252
Map<String, String> pathParameters,
5353
String rawQuery,
54-
Function<String, String> headerLookup) {
54+
UnaryOperator<String> headerLookup) {
5555
this.body = body;
5656
this.parsed = parsed;
5757
this.bodyMapper = bodyMapper;

src/test/java/com/retailsvc/http/RequestTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import java.nio.charset.StandardCharsets;
99
import java.util.Map;
1010
import java.util.concurrent.atomic.AtomicReference;
11-
import java.util.function.Function;
11+
import java.util.function.UnaryOperator;
1212
import org.junit.jupiter.api.Test;
1313

1414
class RequestTest {
1515

16-
private static final Function<String, String> NO_HEADERS = name -> null;
16+
private static final UnaryOperator<String> NO_HEADERS = name -> null;
1717

18-
private static Function<String, String> headers(String... pairs) {
18+
private static UnaryOperator<String> headers(String... pairs) {
1919
Map<String, String> map = new java.util.HashMap<>();
2020
for (int i = 0; i < pairs.length; i += 2) {
2121
map.put(pairs[i].toLowerCase(), pairs[i + 1]);

src/test/java/com/retailsvc/http/internal/DispatchHandlerTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ private static DispatchHandler dispatcher(Map<String, RequestHandler> handlers)
2929
return new DispatchHandler(handlers, List.of(), List.of(), new ResponseRenderer(Map.of()));
3030
}
3131

32-
private static void withRequest(
33-
HttpExchange exchange, String operationId, ScopedValue.CallableOp<Void, Exception> body)
32+
private static void withRequest(String operationId, ScopedValue.CallableOp<Void, Exception> body)
3433
throws Exception {
3534
Request req = new Request(new byte[0], null, null, operationId, Map.of(), null, n -> null);
3635
ScopedValue.where(DispatchHandler.CURRENT, req).call(body);
@@ -47,7 +46,6 @@ void invokesRegisteredHandler() throws Exception {
4746
HttpExchange ex = stubExchange();
4847

4948
withRequest(
50-
ex,
5149
"get-x",
5250
() -> {
5351
dispatcher(Map.of("get-x", handler)).handle(ex);
@@ -65,7 +63,6 @@ void throwsWhenHandlerMissing() {
6563
assertThatThrownBy(
6664
() ->
6765
withRequest(
68-
ex,
6966
"ghost",
7067
() -> {
7168
d.handle(ex);

0 commit comments

Comments
 (0)