From 4b183329f48b70e4b6d0ac7ab001a6a5729703b6 Mon Sep 17 00:00:00 2001 From: KimDaehyeon Date: Sun, 22 Dec 2024 06:14:35 +0900 Subject: [PATCH 1/2] refactor: Change RestInterceptorRegistration to manage InterceptorRegistration - #13 - Directly manages InterceptorRegistration and calls methods: - When calling addRestfulPatterns(), addPathPatterns() of InterceptorRegistration is called. - When calling order(), call order() of InterceptorRegistration. - By directly managing InterceptorRegistration, Interceptor settings can be reflected immediately. --- .../RestInterceptorRegistration.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistration.java b/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistration.java index 56be0bd..b312f13 100644 --- a/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistration.java +++ b/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistration.java @@ -1,8 +1,10 @@ package com.restful_spring.rest_interceptor; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; -import java.util.List; +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; /** * Assists with the creation of a {@link RestInterceptor}. @@ -13,27 +15,33 @@ public class RestInterceptorRegistration { private final RestInterceptor restInterceptor; - private int order = 0; + private final InterceptorRegistration registration; /** * Creates a new instance of {@link RestInterceptorRegistration}. */ - public RestInterceptorRegistration(RestInterceptor restInterceptor) { + RestInterceptorRegistration(RestInterceptor restInterceptor, InterceptorRegistry registry) { this.restInterceptor = restInterceptor; + this.registration = registry.addInterceptor(restInterceptor); } /** * Add RestfulPatterns the interceptor should be included in. */ public RestInterceptorRegistration addRestfulPatterns(RestfulPattern... restfulPatterns) { - return addRestfulPatterns(List.of(restfulPatterns)); + return addRestfulPatterns(Arrays.asList(restfulPatterns)); } /** - * List-based variant of {@link #addRestfulPatterns(RestfulPattern...)}. + * Collection-based variant of {@link #addRestfulPatterns(RestfulPattern...)}. + *

Call InterceptorRegistration's addPathPatterns method with the paths of the RestfulPatterns. */ public RestInterceptorRegistration addRestfulPatterns(Collection restfulPatterns) { restInterceptor.restfulPatterns = new ArrayList<>(restfulPatterns); + registration.addPathPatterns(restfulPatterns.stream() + .map(RestfulPattern::getPath) + .toList() + ); return this; } @@ -41,15 +49,7 @@ public RestInterceptorRegistration addRestfulPatterns(Collection * Specify an order position to be used. Default is 0. */ public RestInterceptorRegistration order(int order) { - this.order = order; + registration.order(order); return this; } - - protected RestInterceptor getRestInterceptor() { - return restInterceptor; - } - - protected int getOrder() { - return order; - } } From d88a19849972a2d0d5a151ceb7ffed5a7234f9f4 Mon Sep 17 00:00:00 2001 From: KimDaehyeon Date: Sun, 22 Dec 2024 06:19:27 +0900 Subject: [PATCH 2/2] refactor: Remove build method - #13 - build() method can cause inconvenience to users. - And it cannot guarantee that the build() method was called at compile time. --- .../RestInterceptorRegistry.java | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistry.java b/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistry.java index 4de5b23..364ab18 100644 --- a/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistry.java +++ b/src/main/java/com/restful_spring/rest_interceptor/RestInterceptorRegistry.java @@ -1,8 +1,5 @@ package com.restful_spring.rest_interceptor; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; /** @@ -14,7 +11,6 @@ public class RestInterceptorRegistry { private final InterceptorRegistry registry; - private final List registrations = new ArrayList<>(); public RestInterceptorRegistry(InterceptorRegistry registry) { this.registry = registry; @@ -28,24 +24,6 @@ public RestInterceptorRegistry(InterceptorRegistry registry) { * restInterceptor further for example adding RestfulPatterns it should apply to. */ public RestInterceptorRegistration addInterceptor(RestInterceptor restInterceptor) { - RestInterceptorRegistration registration = new RestInterceptorRegistration(restInterceptor); - registrations.add(registration); - return registration; - } - - /** - * Reflects the registrations in the registry. This method should be called after all the registrations are done. - *

Will be deprecated in the future. - */ - public void build() { - this.registrations.forEach(registration -> { - RestInterceptor restInterceptor = registration.getRestInterceptor(); - - registry.addInterceptor(restInterceptor) - .addPathPatterns(restInterceptor.restfulPatterns.stream() - .map(RestfulPattern::getPath) - .collect(Collectors.toList())) - .order(registration.getOrder()); - }); + return new RestInterceptorRegistration(restInterceptor, registry); } }