Skip to content

Commit 03c1c2c

Browse files
authored
Refactor ExtensionFilter to use simplified operation methods
Simplified the example by using the `getExtension()` method added in 4.1, along with the preexisting `addExtension()` method, rather than handling the map of extensions directly.
1 parent 9009141 commit 03c1c2c

1 file changed

Lines changed: 14 additions & 34 deletions

File tree

modules/ROOT/pages/chapter04/chapter04.adoc

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,6 @@ package io.microprofile.tutorial.store.product.config;
855855
856856
import org.eclipse.microprofile.openapi.models.Operation;
857857
import org.eclipse.microprofile.openapi.OASFilter;
858-
import java.util.LinkedHashMap;
859-
import java.util.Map;
860858
861859
public class ExtensionFilter implements OASFilter {
862860
@@ -867,49 +865,31 @@ public class ExtensionFilter implements OASFilter {
867865
}
868866
869867
try {
870-
// Get existing extensions (or create new map if null)
871-
Map<String, Object> existingExtensions = operation.getExtensions();
872-
if (existingExtensions == null) {
873-
return operation; // No extensions to process
874-
}
875-
876-
// Create a new map with existing extensions
877-
Map<String, Object> newExtensions = new LinkedHashMap<>(existingExtensions);
878-
879868
// Check if a custom timeout extension exists
880-
if (newExtensions.containsKey("x-custom-timeout")) {
881-
Object timeout = newExtensions.get("x-custom-timeout");
882-
if (timeout != null) {
883-
int timeoutValue = Integer.parseInt(timeout.toString());
884-
if (timeoutValue > 30) {
885-
newExtensions.put("x-requires-approval", "true");
886-
}
869+
Object timeout = operation.getExtension("x-custom-timeout");
870+
if (timeout != null) {
871+
int timeoutValue = Integer.parseInt(timeout.toString());
872+
if (timeoutValue > 30) {
873+
operation.addExtension("x-requires-approval", "true");
887874
}
888875
}
889876
890877
// Check for rate limiting configuration
891-
if (newExtensions.containsKey("x-rate-limit")) {
892-
Object rateLimit = newExtensions.get("x-rate-limit");
893-
if (rateLimit != null) {
894-
int rateLimitValue = Integer.parseInt(rateLimit.toString());
895-
if (rateLimitValue > 500) {
896-
newExtensions.put("x-high-volume", "true");
897-
}
878+
Object rateLimit = operation.getExtension("x-rate-limit");
879+
if (rateLimit != null) {
880+
int rateLimitValue = Integer.parseInt(rateLimit.toString());
881+
if (rateLimitValue > 500) {
882+
operation.addExtension("x-high-volume", "true");
898883
}
899884
}
900885
901886
// Check for authentication requirements
902-
if (newExtensions.containsKey("x-requires-auth")) {
903-
Object authLevel = newExtensions.get("x-requires-auth");
904-
if (authLevel != null && "admin".equals(authLevel.toString())) {
905-
newExtensions.put("x-security-notice",
906-
"This operation requires administrator privileges");
907-
}
887+
Object authLevel = operation.getExtension("x-requires-auth");
888+
if (authLevel != null && "admin".equals(authLevel.toString())) {
889+
operation.addExtension("x-security-notice",
890+
"This operation requires administrator privileges");
908891
}
909892
910-
// Set the modified extensions map back
911-
operation.setExtensions(newExtensions);
912-
913893
} catch (Exception e) {
914894
System.err.println("Error in ExtensionFilter: " + e.getMessage());
915895
e.printStackTrace();

0 commit comments

Comments
 (0)