-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Write "regex:" in front of value to indicate, that this is a…
… pattern. (#9) * fix: throw a more speaking exception when regex in query was wrong * feat: write "regex:" in query-param to indicate that the query contains a regExp * feat: implemented change requests * fix: move test-package
- Loading branch information
1 parent
5456fbc
commit 24b4720
Showing
11 changed files
with
486 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 14 additions & 7 deletions
21
...java/rocks/inspectit/gepard/agentmanager/connection/model/dto/QueryConnectionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agentmanager.connection.model.dto; | ||
|
||
import jakarta.validation.Valid; | ||
import java.util.Map; | ||
import rocks.inspectit.gepard.agentmanager.connection.validation.ValidRegexPattern; | ||
|
||
/** | ||
* Represents a request against the {@code ConnectionController} Query-Endpoint. | ||
* | ||
* <p>All fields are optional. If a field is not set, it is not considered in the query. | ||
*/ | ||
public record QueryConnectionRequest(String id, String registrationTime, QueryAgentRequest agent) { | ||
public record QueryConnectionRequest( | ||
@ValidRegexPattern(message = "Invalid connection ID pattern") String id, | ||
@ValidRegexPattern(message = "Invalid registration time pattern") String registrationTime, | ||
@Valid QueryAgentRequest agent) { | ||
|
||
public record QueryAgentRequest( | ||
String serviceName, | ||
Long pid, | ||
String gepardVersion, | ||
String otelVersion, | ||
Long startTime, | ||
String javaVersion, | ||
@ValidRegexPattern(message = "Invalid service name pattern") String serviceName, | ||
@ValidRegexPattern(message = "Invalid process ID pattern") | ||
String pid, // pid just has to be a number | ||
@ValidRegexPattern(message = "Invalid Gepard version pattern") String gepardVersion, | ||
@ValidRegexPattern(message = "Invalid OpenTelemetry version pattern") String otelVersion, | ||
@ValidRegexPattern(message = "Invalid start time pattern") | ||
String startTime, // startTime just has to be a number | ||
@ValidRegexPattern(message = "Invalid Java version pattern") String javaVersion, | ||
Map<String, String> attributes) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...java/rocks/inspectit/gepard/agentmanager/connection/validation/RegexPatternValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agentmanager.connection.validation; | ||
|
||
import static rocks.inspectit.gepard.agentmanager.connection.validation.RegexQueryService.REGEX_INDICATOR; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
/** | ||
* Validates that a given string is a valid regex pattern. | ||
* | ||
* <p>Used in conjunction with the {@link ValidRegexPattern} annotation. | ||
*/ | ||
public class RegexPatternValidator implements ConstraintValidator<ValidRegexPattern, String> { | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
if (value.startsWith(REGEX_INDICATOR)) { | ||
try { | ||
Pattern.compile(value.substring(REGEX_INDICATOR.length())); | ||
return true; | ||
} catch (PatternSyntaxException e) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...ain/java/rocks/inspectit/gepard/agentmanager/connection/validation/RegexQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agentmanager.connection.validation; | ||
|
||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.regex.Pattern; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
/** | ||
* Service for querying values against regex patterns. | ||
* | ||
* <p>Used for filtering entities based on regex patterns. | ||
*/ | ||
@Service | ||
@Validated | ||
public class RegexQueryService { | ||
|
||
public static final String REGEX_INDICATOR = "regex:"; | ||
|
||
/** | ||
* Checks if the given value matches the given pattern. If the pattern starts with "regex:", the | ||
* pattern is treated as a regex pattern. Otherwise, the pattern is treated as an exact match. If | ||
* the pattern or value is null, the method returns true, because this means: No filtering. | ||
* | ||
* @param value the value to match | ||
* @param pattern the pattern to match against | ||
* @return true if the value matches the pattern or is an exact match, false otherwise | ||
*/ | ||
public boolean matches(String value, String pattern) { | ||
|
||
if (pattern == null || value == null) { | ||
return true; | ||
} | ||
|
||
if (pattern.startsWith(REGEX_INDICATOR)) { | ||
String regexPattern = pattern.substring(REGEX_INDICATOR.length()); | ||
return Pattern.compile(regexPattern).matcher(value).matches(); | ||
} else { | ||
return value.equals(pattern); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given Long matches the given pattern. If the pattern starts with "regex:", the | ||
* pattern is treated as a regex pattern. Otherwise, the pattern is treated as an exact match. If | ||
* the pattern or value is null, the method returns true, because this means: No filtering. | ||
* | ||
* @param value the value to match | ||
* @param pattern the pattern to match against | ||
* @return true if the value matches the pattern or is an exact match, false otherwise | ||
*/ | ||
public boolean matchesLong(Long value, String pattern) { | ||
if (pattern == null || value == null) { | ||
return true; | ||
} | ||
|
||
if (pattern.startsWith(REGEX_INDICATOR)) { | ||
String regexPattern = pattern.substring(REGEX_INDICATOR.length()); | ||
return Pattern.compile(regexPattern).matcher(value.toString()).matches(); | ||
} else { | ||
return value.toString().equals(pattern); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given Instant matches the given pattern. If the pattern starts with "regex:", the | ||
* pattern is treated as a regex pattern. Otherwise, the pattern is treated as an exact match. If | ||
* the pattern or value is null, the method returns true, because this means: No filtering. | ||
* | ||
* @param value The value to match | ||
* @param pattern The pattern to match against | ||
* @return true if the value matches the pattern or is an exact match, false otherwise | ||
*/ | ||
public boolean matchesInstant(Instant value, String pattern) { | ||
if (pattern == null || value == null) { | ||
return true; | ||
} | ||
|
||
if (pattern.startsWith(REGEX_INDICATOR)) { | ||
String regexPattern = pattern.substring(REGEX_INDICATOR.length()); | ||
// Convert Instant to ISO-8601 string for regex matching | ||
String instantString = value.truncatedTo(ChronoUnit.MILLIS).toString(); | ||
return Pattern.compile(regexPattern).matcher(instantString).matches(); | ||
} else { | ||
try { | ||
// For exact matching, parse the pattern as Instant and compare | ||
Instant patternInstant = Instant.parse(pattern); | ||
return value | ||
.truncatedTo(ChronoUnit.MILLIS) | ||
.equals(patternInstant.truncatedTo(ChronoUnit.MILLIS)); | ||
} catch (DateTimeParseException e) { | ||
throw new IllegalArgumentException( | ||
"Invalid timestamp format. Expected ISO-8601 format (e.g., 2024-10-29T10:15:30Z)", e); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ain/java/rocks/inspectit/gepard/agentmanager/connection/validation/ValidRegexPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agentmanager.connection.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = RegexPatternValidator.class) | ||
public @interface ValidRegexPattern { | ||
String message() default "Invalid regex pattern"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.