Repository navigation
Expand file tree
/
Copy pathMockPushEndpoint.java
More file actions
188 lines (176 loc) · 8.47 KB
/
Copy pathMockPushEndpoint.java
File metadata and controls
188 lines (176 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dev.jinhyeon.notificationmock;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MockPushEndpoint {
private static final int MAX_BODY_BYTES = 8192;
private final HttpMockProperties settings;
private final ObjectMapper mapper;
private final Validator validator;
private final MockAcceptanceStore acceptances;
private final FixedWindowQuota quota;
private final Semaphore slots;
private final LongAdder requests = new LongAdder();
private final LongAdder accepted = new LongAdder();
private final LongAdder rateLimited = new LongAdder();
private final LongAdder serverErrors = new LongAdder();
private final LongAdder permanentFailures = new LongAdder();
private final LongAdder busy = new LongAdder();
private final AtomicInteger active = new AtomicInteger();
private final AtomicInteger maxActive = new AtomicInteger();
private final LongAdder idempotencyReplays = new LongAdder();
private boolean storageFailed;
private record Reply(ResponseEntity<?> response, boolean acquiredSlot) { }
public MockPushEndpoint(HttpMockProperties settings, ObjectMapper mapper, Validator validator, MockAcceptanceStore acceptances) {
this.settings = settings;
this.mapper = mapper;
this.validator = validator;
this.acceptances = acceptances;
this.quota = new FixedWindowQuota(settings.requestsPerWindow(), settings.windowMs(), System::nanoTime);
this.slots = new Semaphore(settings.maxInFlight());
}
@PostMapping(path = "/pushes", consumes = "application/json", produces = "application/json")
public ResponseEntity<?> push(HttpServletRequest request) throws IOException {
if (request.getContentLengthLong() > MAX_BODY_BYTES) return error(413, "MOCK_PAYLOAD_TOO_LARGE");
byte[] bytes = request.getInputStream().readNBytes(MAX_BODY_BYTES + 1);
if (bytes.length > MAX_BODY_BYTES) return error(413, "MOCK_PAYLOAD_TOO_LARGE");
Payload payload;
try {
payload = mapper.readerFor(Payload.class).with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readValue(bytes);
} catch (IOException invalid) {
return error(400, "MOCK_INVALID_REQUEST");
}
if (payload == null || !validator.validate(payload).isEmpty()) return error(400, "MOCK_INVALID_REQUEST");
var keys = request.getHeaders("Idempotency-Key");
UUID key = null;
byte[] fingerprint = null;
if (keys.hasMoreElements()) {
if (!payload.notificationId().toString().equals(keys.nextElement()) || keys.hasMoreElements()) {
return error(400, "MOCK_INVALID_IDEMPOTENCY_KEY");
}
key = payload.notificationId();
fingerprint = fingerprint(payload);
}
requests.increment();
Reply reply = begin(key, fingerprint);
if (!reply.acquiredSlot()) return reply.response();
try {
Thread.sleep(settings.responseDelayMs());
return reply.response();
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
return error(503, "MOCK_RESPONSE_INTERRUPTED");
} finally {
active.decrementAndGet();
slots.release();
}
}
private synchronized Reply begin(UUID key, byte[] fingerprint) {
if (key != null) {
if (storageFailed) return storageUnavailable();
MockAcceptanceStore.Acceptance first;
try {
first = acceptances.find(key);
} catch (SQLException failure) {
return storageUnavailable();
}
if (first != null) {
if (!MessageDigest.isEqual(first.fingerprint(), fingerprint)) {
return new Reply(error(409, "MOCK_IDEMPOTENCY_CONFLICT"), false);
}
idempotencyReplays.increment();
return new Reply(accepted(first.providerId()), false);
}
if (settings.mode() == HttpMockProperties.Mode.SUCCESS && acceptances.count() >= settings.idempotencyCapacity()) {
return new Reply(error(503, "MOCK_IDEMPOTENCY_CAPACITY"), false);
}
}
long retryAfter = quota.acquire();
if (retryAfter > 0) {
rateLimited.increment();
return new Reply(ResponseEntity.status(429).header("Retry-After", Long.toString(retryAfter))
.body(Map.of("errorCode", "MOCK_RATE_LIMITED")), false);
}
if (!slots.tryAcquire()) {
busy.increment();
return new Reply(error(503, "MOCK_BUSY"), false);
}
ResponseEntity<?> response;
try {
response = switch (settings.mode()) {
case SUCCESS -> {
UUID providerId = UUID.randomUUID();
if (key != null) acceptances.save(key, fingerprint, providerId);
accepted.increment();
yield accepted(providerId);
}
case TOO_MANY_REQUESTS -> {
rateLimited.increment();
yield ResponseEntity.status(429).header("Retry-After", "1").body(Map.of("errorCode", "MOCK_RATE_LIMITED"));
}
case SERVER_ERROR -> {
serverErrors.increment();
yield error(500, "MOCK_UNAVAILABLE");
}
case INVALID_RECIPIENT -> {
permanentFailures.increment();
yield error(400, "MOCK_INVALID_RECIPIENT");
}
};
} catch (SQLException failure) {
slots.release();
return storageUnavailable();
}
maxActive.accumulateAndGet(active.incrementAndGet(), Math::max);
return new Reply(response, true);
}
private Reply storageUnavailable() {
// A failed commit or sync can leave the receipt's durability uncertain until the store is reopened.
storageFailed = true;
return new Reply(error(503, "MOCK_IDEMPOTENCY_STORAGE_UNAVAILABLE"), false);
}
private byte[] fingerprint(Payload payload) throws IOException {
try {
// Hash the parsed fields so formatting does not affect equality; retain no message body.
return MessageDigest.getInstance("SHA-256").digest(mapper.writeValueAsBytes(payload));
} catch (NoSuchAlgorithmException missing) {
throw new IllegalStateException("SHA-256 is unavailable", missing);
}
}
private ResponseEntity<?> accepted(UUID providerId) {
return ResponseEntity.accepted().body(Map.of("providerMessageId", "http-mock-" + providerId));
}
@GetMapping("/mock/stats")
public Stats stats() {
return new Stats(requests.sum(), accepted.sum(), rateLimited.sum(), serverErrors.sum(),
permanentFailures.sum(), busy.sum(), active.get(), maxActive.get(), idempotencyReplays.sum(), acceptances.count());
}
@GetMapping("/mock/config")
public HttpMockProperties config() { return settings; }
private ResponseEntity<?> error(int status, String code) {
return ResponseEntity.status(status).body(Map.of("errorCode", code));
}
public record Payload(@NotNull UUID notificationId, @NotBlank @Size(max = 100) String recipientId,
@NotBlank @Size(max = 100) String title, @NotBlank @Size(max = 500) String body) { }
public record Stats(long requests, long accepted, long rateLimited, long serverErrors,
long permanentFailures, long busy, int active, int maxActive, long idempotencyReplays,
int storedAcceptances) { }
}