init commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package ru.mtusi.messenger.validation;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.mtusi.messenger.config.properties.IdempotencyProperties;
|
||||
import ru.mtusi.messenger.exception.InvalidRequestException;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class IdempotencyValidator {
|
||||
|
||||
private final IdempotencyProperties properties;
|
||||
|
||||
public void validateKey(String key) {
|
||||
requireNotBlank(key);
|
||||
requireMaxLength(key);
|
||||
requirePattern(key);
|
||||
}
|
||||
|
||||
private void requireNotBlank(String key) {
|
||||
if (key == null || key.isEmpty()) {
|
||||
throw new InvalidRequestException("Idempotency-Key header is required");
|
||||
}
|
||||
}
|
||||
|
||||
private void requireMaxLength(String key) {
|
||||
if (key.length() > properties.keyMaxLength()) {
|
||||
throw new InvalidRequestException("Idempotency-Key is too long");
|
||||
}
|
||||
}
|
||||
|
||||
private void requirePattern(String key) {
|
||||
if (!Pattern.matches(properties.keyPattern(), key)) {
|
||||
throw new InvalidRequestException("Idempotency-Key contains invalid characters");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user