|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static com.retailsvc.http.Request.BODY; |
| 4 | +import static com.retailsvc.http.Request.OPERATION_ID; |
| 5 | +import static com.retailsvc.http.Request.PARSED_BODY; |
| 6 | +import static com.retailsvc.http.Request.PATH_PARAMETERS; |
| 7 | + |
| 8 | +import com.retailsvc.http.JsonMapper; |
| 9 | +import com.retailsvc.http.MethodNotAllowedException; |
| 10 | +import com.retailsvc.http.NotFoundException; |
| 11 | +import com.retailsvc.http.ValidationException; |
| 12 | +import com.retailsvc.http.spec.HttpMethod; |
| 13 | +import com.retailsvc.http.spec.MediaType; |
| 14 | +import com.retailsvc.http.spec.Operation; |
| 15 | +import com.retailsvc.http.spec.Parameter; |
| 16 | +import com.retailsvc.http.spec.RequestBody; |
| 17 | +import com.retailsvc.http.spec.Spec; |
| 18 | +import com.retailsvc.http.validate.ValidationError; |
| 19 | +import com.retailsvc.http.validate.Validator; |
| 20 | +import com.sun.net.httpserver.Filter; |
| 21 | +import com.sun.net.httpserver.HttpExchange; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Locale; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +public final class RequestPreparationFilter extends Filter { |
| 29 | + |
| 30 | + private final Spec spec; |
| 31 | + private final Router router; |
| 32 | + private final Validator validator; |
| 33 | + private final JsonMapper jsonMapper; |
| 34 | + |
| 35 | + public RequestPreparationFilter( |
| 36 | + Spec spec, Router router, Validator validator, JsonMapper jsonMapper) { |
| 37 | + this.spec = spec; |
| 38 | + this.router = router; |
| 39 | + this.validator = validator; |
| 40 | + this.jsonMapper = jsonMapper; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String description() { |
| 45 | + return "Request preparation"; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void doFilter(HttpExchange exchange, Chain chain) throws IOException { |
| 50 | + byte[] body = exchange.getRequestBody().readAllBytes(); |
| 51 | + exchange.setAttribute(BODY, body); |
| 52 | + |
| 53 | + HttpMethod method = HttpMethod.parse(exchange.getRequestMethod()); |
| 54 | + String path = stripBasePath(exchange.getRequestURI().getPath()); |
| 55 | + |
| 56 | + var matchOpt = router.match(method, path); |
| 57 | + if (matchOpt.isEmpty()) { |
| 58 | + var allowed = router.allowedMethods(path); |
| 59 | + if (allowed.isEmpty()) { |
| 60 | + throw new NotFoundException(method + " " + path); |
| 61 | + } |
| 62 | + throw new MethodNotAllowedException(allowed); |
| 63 | + } |
| 64 | + Router.Match match = matchOpt.get(); |
| 65 | + |
| 66 | + Operation op = match.operation(); |
| 67 | + exchange.setAttribute(OPERATION_ID, op.operationId()); |
| 68 | + exchange.setAttribute(PATH_PARAMETERS, match.pathParameters()); |
| 69 | + |
| 70 | + validateParameters(exchange, op, match.pathParameters()); |
| 71 | + validateBody(exchange, op, body); |
| 72 | + |
| 73 | + chain.doFilter(exchange); |
| 74 | + } |
| 75 | + |
| 76 | + private String stripBasePath(String path) { |
| 77 | + String base = spec.basePath(); |
| 78 | + if (base == null || base.isEmpty() || base.equals("/")) { |
| 79 | + return path; |
| 80 | + } |
| 81 | + return path.startsWith(base) ? path.substring(base.length()) : path; |
| 82 | + } |
| 83 | + |
| 84 | + private void validateParameters( |
| 85 | + HttpExchange exchange, Operation op, Map<String, String> pathParams) { |
| 86 | + Map<String, String> query = parseQuery(exchange.getRequestURI().getQuery()); |
| 87 | + for (Parameter p : op.parameters()) { |
| 88 | + String pointer = "/" + p.in().name().toLowerCase(Locale.ROOT) + "/" + p.name(); |
| 89 | + String value = |
| 90 | + switch (p.in()) { |
| 91 | + case PATH -> pathParams.get(p.name()); |
| 92 | + case QUERY -> query.get(p.name()); |
| 93 | + case HEADER -> exchange.getRequestHeaders().getFirst(p.name()); |
| 94 | + case COOKIE -> null; // handled by future spec |
| 95 | + }; |
| 96 | + if (value == null) { |
| 97 | + if (p.required()) { |
| 98 | + throw new ValidationException( |
| 99 | + new ValidationError( |
| 100 | + pointer, |
| 101 | + "required", |
| 102 | + "required " + p.in().name().toLowerCase(Locale.ROOT) + " parameter is missing", |
| 103 | + null)); |
| 104 | + } |
| 105 | + continue; |
| 106 | + } |
| 107 | + validator.validate(value, p.schema(), pointer); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void validateBody(HttpExchange exchange, Operation op, byte[] body) { |
| 112 | + Optional<RequestBody> rb = op.requestBody(); |
| 113 | + if (rb.isEmpty()) { |
| 114 | + return; |
| 115 | + } |
| 116 | + if (body.length == 0) { |
| 117 | + if (rb.get().required()) { |
| 118 | + throw new ValidationException( |
| 119 | + new ValidationError("/body", "required", "request body is required", null)); |
| 120 | + } |
| 121 | + return; |
| 122 | + } |
| 123 | + String contentType = exchange.getRequestHeaders().getFirst("Content-Type"); |
| 124 | + if (contentType == null) { |
| 125 | + contentType = "application/json"; |
| 126 | + } |
| 127 | + contentType = contentType.split(";", 2)[0].trim(); |
| 128 | + MediaType mt = rb.get().content().get(contentType); |
| 129 | + if (mt == null) { |
| 130 | + throw new ValidationException( |
| 131 | + new ValidationError( |
| 132 | + "/body", "content-type", "unsupported content type: " + contentType, null)); |
| 133 | + } |
| 134 | + Object parsed = jsonMapper.mapFrom(body); |
| 135 | + exchange.setAttribute(PARSED_BODY, parsed); |
| 136 | + validator.validate(parsed, mt.schema(), ""); |
| 137 | + } |
| 138 | + |
| 139 | + private static Map<String, String> parseQuery(String query) { |
| 140 | + if (query == null || query.isBlank()) { |
| 141 | + return Map.of(); |
| 142 | + } |
| 143 | + Map<String, String> out = new HashMap<>(); |
| 144 | + for (String pair : query.split("&")) { |
| 145 | + int eq = pair.indexOf('='); |
| 146 | + if (eq <= 0) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + out.putIfAbsent(pair.substring(0, eq), pair.substring(eq + 1)); |
| 150 | + } |
| 151 | + return out; |
| 152 | + } |
| 153 | +} |
0 commit comments