|
| 1 | +/* Copyright 2025 predic8 GmbH, www.predic8.com |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +package com.predic8.membrane.core.interceptor.idempotency; |
| 16 | + |
| 17 | +import com.predic8.membrane.annot.MCAttribute; |
| 18 | +import com.predic8.membrane.annot.MCElement; |
| 19 | +import com.predic8.membrane.annot.Required; |
| 20 | +import com.predic8.membrane.core.exchange.Exchange; |
| 21 | +import com.predic8.membrane.core.interceptor.AbstractInterceptor; |
| 22 | +import com.predic8.membrane.core.interceptor.Outcome; |
| 23 | +import com.predic8.membrane.core.lang.ExchangeExpression; |
| 24 | +import com.predic8.membrane.core.lang.ExchangeExpression.Language; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | +import java.util.concurrent.ConcurrentHashMap; |
| 28 | + |
| 29 | +import static com.predic8.membrane.core.exceptions.ProblemDetails.user; |
| 30 | +import static com.predic8.membrane.core.interceptor.Interceptor.Flow.REQUEST; |
| 31 | +import static com.predic8.membrane.core.interceptor.Outcome.ABORT; |
| 32 | +import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE; |
| 33 | +import static com.predic8.membrane.core.lang.ExchangeExpression.Language.SPEL; |
| 34 | + |
| 35 | +/** |
| 36 | + * @description <p>Prevents duplicate request processing based on a dynamic idempotency key.</p> |
| 37 | + * |
| 38 | + * <p>This interceptor evaluates an expression (e.g., from headers or body) to extract an idempotency key. |
| 39 | + * If the key has already been processed, it aborts the request with a 400 response.</p> |
| 40 | + * |
| 41 | + * <p>Useful for handling retries from clients to avoid duplicate side effects like double payment submissions.</p> |
| 42 | + * @topic 3. Security and Validation |
| 43 | + */ |
| 44 | +@MCElement(name = "idempotency") |
| 45 | +public class IdempotencyInterceptor extends AbstractInterceptor { |
| 46 | + |
| 47 | + private String key; |
| 48 | + private ExchangeExpression exchangeExpression; |
| 49 | + private Language language = SPEL; |
| 50 | + private final Map<String, Boolean> processedKeys = new ConcurrentHashMap<>(); |
| 51 | + |
| 52 | + @Override |
| 53 | + public void init() { |
| 54 | + super.init(); |
| 55 | + exchangeExpression = ExchangeExpression.newInstance(router, language, key); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Outcome handleRequest(Exchange exc) { |
| 60 | + String key = normalizeKey(exchangeExpression.evaluate(exc, REQUEST, String.class)); |
| 61 | + if (key.isEmpty()) { |
| 62 | + return CONTINUE; |
| 63 | + } |
| 64 | + |
| 65 | + if (processedKeys.containsKey(key)) { |
| 66 | + return handleDuplicateKey(exc, key); |
| 67 | + } |
| 68 | + |
| 69 | + if (processedKeys.putIfAbsent(key, Boolean.TRUE) != null) { |
| 70 | + return handleDuplicateKey(exc, key); |
| 71 | + } |
| 72 | + return CONTINUE; |
| 73 | + } |
| 74 | + |
| 75 | + private String normalizeKey(String key) { |
| 76 | + return key == null ? "" : key; |
| 77 | + } |
| 78 | + |
| 79 | + private Outcome handleDuplicateKey(Exchange exc, String key) { |
| 80 | + user(false, "idempotency") |
| 81 | + .statusCode(409) |
| 82 | + .detail("key %s has already been processed".formatted(key)) |
| 83 | + .buildAndSetResponse(exc); |
| 84 | + return ABORT; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getDisplayName() { |
| 89 | + return "Idempotency"; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @description Expression used to extract the idempotency key from the exchange. |
| 94 | + * Can be an XPath, JSONPath, header, or other supported syntax depending on the language. |
| 95 | + * @example ${req.header.idempotency-key} |
| 96 | + */ |
| 97 | + @MCAttribute |
| 98 | + @Required |
| 99 | + public void setKey(String key) { |
| 100 | + this.key = key; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @description Language used to interpret the expression (e.g., spel, jsonpath, xpath). |
| 105 | + * Determines how the key string will be evaluated. |
| 106 | + * @default SpEL |
| 107 | + * @example jsonpath |
| 108 | + */ |
| 109 | + @MCAttribute |
| 110 | + public void setLanguage(Language language) { |
| 111 | + this.language = language; |
| 112 | + } |
| 113 | + |
| 114 | + public String getKey() { |
| 115 | + return key; |
| 116 | + } |
| 117 | + |
| 118 | + public Language getLanguage() { |
| 119 | + return language; |
| 120 | + } |
| 121 | +} |
0 commit comments