-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration update validations to all testcontrollers
- Loading branch information
1 parent
a8b99f5
commit 138d6f4
Showing
13 changed files
with
379 additions
and
25 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
44 changes: 42 additions & 2 deletions
44
...c/main/java/com/giffing/bucket4j/spring/boot/starter/examples/gateway/TestController.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,24 +1,64 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.examples.gateway; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.utils.Bucket4JUtils; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.Validator; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.support.DefaultMessageSourceResolvable; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; | ||
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@RestController | ||
@RequestMapping("/") | ||
public class TestController { | ||
|
||
@Autowired | ||
Validator validator; | ||
|
||
private final CacheManager<String, Bucket4JConfiguration> configCacheManager; | ||
|
||
public TestController(CacheManager<String, Bucket4JConfiguration> configCacheManager){ | ||
this.configCacheManager = configCacheManager; | ||
} | ||
|
||
/** | ||
* Example of how a filter configuration can be updated during runtime | ||
* @param filterId id of the filter to update | ||
* @param newConfig the new filter configuration | ||
* @return | ||
*/ | ||
@PostMapping("filters/{filterId}") | ||
public ResponseEntity updateConfig(@PathVariable String filterId, @RequestBody Bucket4JConfiguration filter){ | ||
configCacheManager.setValue(filterId, filter); | ||
public ResponseEntity<?> updateConfig( | ||
@PathVariable String filterId, | ||
@RequestBody Bucket4JConfiguration newConfig) { | ||
|
||
//validate that there are no errors by the Jakarta validation | ||
Set<ConstraintViolation<Bucket4JConfiguration>> violations = validator.validate(newConfig); | ||
if (!violations.isEmpty()) { | ||
List<String> errors = violations.stream().map(ConstraintViolation::getMessage).toList(); | ||
return ResponseEntity.badRequest().body(new ValidationErrorResponse("Configuration validation failed", errors)); | ||
} | ||
|
||
//retrieve the old config and validate that it can be replaced by the new config | ||
Bucket4JConfiguration oldConfig = configCacheManager.getValue(filterId); | ||
ResponseEntity<String> validationResponse = Bucket4JUtils.validateConfigurationUpdate(oldConfig, newConfig); | ||
if (validationResponse != null) { | ||
return validationResponse; | ||
} | ||
|
||
//insert the new config into the cache, so it will trigger the cacheUpdateListeners | ||
configCacheManager.setValue(filterId, newConfig); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
private record ValidationErrorResponse(String message, List<String> errors) {} | ||
} |
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
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
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
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
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
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.