Skip to content

Commit

Permalink
Merge pull request #14 from Dh3356/feature/remove-build-method-#13
Browse files Browse the repository at this point in the history
Remove unnecessary build() method of InterceptorRegistry
  • Loading branch information
Dh3356 authored Dec 22, 2024
2 parents 41d46ff + d88a198 commit 51d280c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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}.
Expand All @@ -13,43 +15,41 @@
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...)}.
* <p> Call InterceptorRegistration's addPathPatterns method with the paths of the RestfulPatterns.
*/
public RestInterceptorRegistration addRestfulPatterns(Collection<RestfulPattern> restfulPatterns) {
restInterceptor.restfulPatterns = new ArrayList<>(restfulPatterns);
registration.addPathPatterns(restfulPatterns.stream()
.map(RestfulPattern::getPath)
.toList()
);
return this;
}

/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -14,7 +11,6 @@
public class RestInterceptorRegistry {

private final InterceptorRegistry registry;
private final List<RestInterceptorRegistration> registrations = new ArrayList<>();

public RestInterceptorRegistry(InterceptorRegistry registry) {
this.registry = registry;
Expand All @@ -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.
* <p>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);
}
}

0 comments on commit 51d280c

Please sign in to comment.