Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class WebConfig implements WebMvcConfigurer {
RestInterceptorRegistry restInterceptorRegistry = new RestInterceptorRegistry(registry);

restInterceptorRegistry.addInterceptor(testInterceptor)
.addRestfulPatterns(RestfulPattern.of("/memos", HttpMethod.GET)); // GET /memos
.addRestPatterns(RestPattern.of("/memos", HttpMethod.GET)); // GET /memos

restInterceptorRegistry.build(); // Deprecated Since v1.0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
public abstract class RestInterceptor implements HandlerInterceptor {

RestfulPatterns restfulPatterns = RestfulPatterns.empty();
RestfulPatterns excludePatterns = RestfulPatterns.empty();
RestPatterns restPatterns = RestPatterns.empty();
RestPatterns excludePatterns = RestPatterns.empty();

@Override
public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
Expand All @@ -40,7 +40,7 @@ private boolean isPreFlightRequest(final HttpServletRequest request) {
* <p> If request path is matched with any of the excludePatterns, it should be skipped.
*/
private boolean shouldSkip(final HttpServletRequest request) {
return excludePatterns.anyMatches(request) || restfulPatterns.noneMatches(request);
return excludePatterns.anyMatches(request) || restPatterns.noneMatches(request);
}

/**
Expand All @@ -52,26 +52,26 @@ protected boolean doInternal(HttpServletRequest request, HttpServletResponse res
}

/**
* Adds all RestfulPatterns from the given RestfulPatterns instance.
* Adds all RestPatterns from the given RestPatterns instance.
* <p>
* This method merges the provided RestfulPatterns into the existing collection.
* This method merges the provided RestPatterns into the existing collection.
*
* @param restfulPatterns the RestfulPatterns to be added
* @param restPatterns the RestPatterns to be added
* @since 1.0.2
*/
void addRestfulPatterns(final RestfulPatterns restfulPatterns) {
this.restfulPatterns.addAll(restfulPatterns);
void addRestPatterns(final RestPatterns restPatterns) {
this.restPatterns.addAll(restPatterns);
}

/**
* Adds all RestfulPatterns from the given RestfulPatterns instance.
* Adds all RestPatterns from the given RestPatterns instance.
* <p>
* This method merges the provided RestfulPatterns into the existing collection.
* This method merges the provided RestPatterns into the existing collection.
*
* @param excludePatterns the RestfulPatterns to be added
* @param excludePatterns the RestPatterns to be added
* @since 1.0.2
*/
void addExcludePatterns(final RestfulPatterns excludePatterns) {
void addExcludePatterns(final RestPatterns excludePatterns) {
this.excludePatterns.addAll(excludePatterns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,60 @@ public final class RestInterceptorRegistration {
}

/**
* Add RestfulPatterns the interceptor should be included in.
* Add RestPatterns the interceptor should be included in.
*/
public RestInterceptorRegistration addRestfulPatterns(RestfulPattern... restfulPatterns) {
return addRestfulPatterns(Arrays.asList(restfulPatterns));
public RestInterceptorRegistration addRestPatterns(RestPattern... restPatterns) {
return addRestPatterns(Arrays.asList(restPatterns));
}

/**
* Collection-based variant of {@link #addRestfulPatterns(RestfulPattern...)}.
* Collection-based variant of {@link #addRestPatterns(RestPattern...)}.
*/
public RestInterceptorRegistration addRestfulPatterns(Collection<RestfulPattern> restfulPatterns) {
return addRestfulPatterns(RestfulPatterns.from(restfulPatterns));
public RestInterceptorRegistration addRestPatterns(Collection<RestPattern> restPatterns) {
return addRestPatterns(RestPatterns.from(restPatterns));
}

/**
* Adds the given RestfulPatterns to the RestInterceptor and updates path patterns.
* Adds the given RestPatterns to the RestInterceptor and updates path patterns.
* <p>
* This method registers the provided RestfulPatterns with the RestInterceptor and ensures that the corresponding
* paths are added to the interceptor registration.
* This method registers the provided RestPatterns with the RestInterceptor and ensures that the corresponding paths
* are added to the interceptor registration.
*
* @param restfulPatterns the RestfulPatterns to be registered
* @param restPatterns the RestPatterns to be registered
* @return this RestInterceptorRegistration instance for method chaining
* @since 1.0.2
*/
RestInterceptorRegistration addRestfulPatterns(RestfulPatterns restfulPatterns) {
restInterceptor.addRestfulPatterns(restfulPatterns);
registration.addPathPatterns(restfulPatterns.getPaths());
RestInterceptorRegistration addRestPatterns(RestPatterns restPatterns) {
restInterceptor.addRestPatterns(restPatterns);
registration.addPathPatterns(restPatterns.getPaths());
return this;
}

/**
* Add RestfulPatterns the interceptor should be excluded from.
* Add RestPatterns the interceptor should be excluded from.
*/
public RestInterceptorRegistration excludeRestfulPatterns(RestfulPattern... restfulPatterns) {
return excludeRestfulPatterns(Arrays.asList(restfulPatterns));
public RestInterceptorRegistration excludeRestPatterns(RestPattern... restPatterns) {
return excludeRestPatterns(Arrays.asList(restPatterns));
}

/**
* Collection-based variant of {@link #excludeRestfulPatterns(RestfulPattern...)}.
* Collection-based variant of {@link #excludeRestPatterns(RestPattern...)}.
*/
public RestInterceptorRegistration excludeRestfulPatterns(Collection<RestfulPattern> restfulPatterns) {
return excludeRestfulPatterns(RestfulPatterns.from(restfulPatterns));
public RestInterceptorRegistration excludeRestPatterns(Collection<RestPattern> restPatterns) {
return excludeRestPatterns(RestPatterns.from(restPatterns));
}

/**
* Adds the given RestfulPatterns to the RestInterceptor and updates exclude path patterns.
* Adds the given RestPatterns to the RestInterceptor and updates exclude path patterns.
* <p>
* This method registers the provided RestfulPatterns with the RestInterceptor and ensures that the corresponding
* paths are added to the exclude path patterns of the interceptor registration.
* This method registers the provided RestPatterns with the RestInterceptor and ensures that the corresponding paths
* are added to the exclude path patterns of the interceptor registration.
*
* @param excludePatterns the RestfulPatterns to be registered
* @param excludePatterns the RestPatterns to be registered
* @return this RestInterceptorRegistration instance for method chaining
* @since 1.0.2
*/
RestInterceptorRegistration excludeRestfulPatterns(RestfulPatterns excludePatterns) {
RestInterceptorRegistration excludeRestPatterns(RestPatterns excludePatterns) {
restInterceptor.addExcludePatterns(excludePatterns);
registration.excludePathPatterns(excludePatterns.getPaths());
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RestInterceptorRegistry(InterceptorRegistry registry) {
*
* @param restInterceptor the restInterceptor to add
* @return an {@link RestInterceptorRegistration} that allows you optionally configure the registered
* restInterceptor further for example adding RestfulPatterns it should apply to.
* restInterceptor further for example adding RestPatterns it should apply to.
*/
public RestInterceptorRegistration addInterceptor(RestInterceptor restInterceptor) {
return new RestInterceptorRegistration(restInterceptor, registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,54 @@
* @author cookie-meringue
* @since 0.1
*/
public final class RestfulPattern {
public final class RestPattern {

private final String path;
private final Set<HttpMethod> methods;
private final AntPathMatcher pathMatcher = new AntPathMatcher();

private RestfulPattern(final String path, final Set<HttpMethod> methods) {
private RestPattern(final String path, final Set<HttpMethod> methods) {
this.path = path;
this.methods = methods;
}

/**
* Create a new instance of {@link RestfulPattern} with the given path.
* Create a new instance of {@link RestPattern} with the given path.
* <p> Default HTTP methods are GET, POST, PUT, DELETE, PATCH, TRACE, OPTIONS, HEAD.
*
* @author cookie-meringue
* @since 1.0
*/
public static RestfulPattern fromPath(final String path) {
return new RestfulPattern(path, Set.of(HttpMethod.values()));
public static RestPattern fromPath(final String path) {
return new RestPattern(path, Set.of(HttpMethod.values()));
}

/**
* Create a new instance of {@link RestfulPattern} with the given path and HTTP methods.
* Create a new instance of {@link RestPattern} with the given path and HTTP methods.
*
* @author cookie-meringue
* @since 1.0
*/
public static RestfulPattern of(final String path, final HttpMethod... methods) {
return new RestfulPattern(path, Set.of(methods));
public static RestPattern of(final String path, final HttpMethod... methods) {
return new RestPattern(path, Set.of(methods));
}

/**
* Create a new instance of {@link RestfulPattern} with the given path and HTTP method Collections.
* Create a new instance of {@link RestPattern} with the given path and HTTP method Collections.
*/
public static RestfulPattern of(final String path, final Collection<HttpMethod> methods) {
return new RestfulPattern(path, new HashSet<>(methods));
public static RestPattern of(final String path, final Collection<HttpMethod> methods) {
return new RestPattern(path, new HashSet<>(methods));
}

/**
* Create a new instance of {@link RestfulPattern} with the given path and HTTP method.
* Create a new instance of {@link RestPattern} with the given path and HTTP method.
*/
public static RestfulPattern of(final String path, final HttpMethod method) {
return new RestfulPattern(path, Set.of(method));
public static RestPattern of(final String path, final HttpMethod method) {
return new RestPattern(path, Set.of(method));
}

public static RestfulPatternBuilder builder() {
return new RestfulPatternBuilder();
public static RestPatternBuilder builder() {
return new RestPatternBuilder();
}

/**
Expand All @@ -75,7 +75,7 @@ public static RestfulPatternBuilder builder() {
*/
boolean matches(final HttpServletRequest request) {
return methods.contains(HttpMethod.valueOf(request.getMethod())) &&
pathMatcher.match(path, request.getRequestURI());
pathMatcher.match(path, request.getRequestURI());
}

String getPath() {
Expand All @@ -87,7 +87,7 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RestfulPattern that)) {
if (!(o instanceof RestPattern that)) {
return false;
}

Expand All @@ -106,79 +106,79 @@ public int hashCode() {

@Override
public String toString() {
return "RestfulPattern{" +
"methods=" + methods +
", path=" + path +
'}';
return "RestPattern{" +
"methods=" + methods +
", path=" + path +
'}';
}

/**
* Builder for {@link RestfulPattern}.
* <p> This class is used to create a new instance of {@link RestfulPattern}.
* Builder for {@link RestPattern}.
* <p> This class is used to create a new instance of {@link RestPattern}.
* <p> Can set HTTP methods easily.
*/
public static class RestfulPatternBuilder {
public static class RestPatternBuilder {

private final Set<HttpMethod> methods = new HashSet<>();
private String path = "/**";

public RestfulPatternBuilder() {
public RestPatternBuilder() {
}

public RestfulPatternBuilder path(String path) {
public RestPatternBuilder path(String path) {
this.path = path;
return this;
}

public RestfulPatternBuilder get() {
public RestPatternBuilder get() {
this.methods.add(GET);
return this;
}

public RestfulPatternBuilder post() {
public RestPatternBuilder post() {
this.methods.add(HttpMethod.POST);
return this;
}

public RestfulPatternBuilder put() {
public RestPatternBuilder put() {
this.methods.add(HttpMethod.PUT);
return this;
}

public RestfulPatternBuilder delete() {
public RestPatternBuilder delete() {
this.methods.add(HttpMethod.DELETE);
return this;
}

public RestfulPatternBuilder patch() {
public RestPatternBuilder patch() {
this.methods.add(HttpMethod.PATCH);
return this;
}

public RestfulPatternBuilder trace() {
public RestPatternBuilder trace() {
this.methods.add(HttpMethod.TRACE);
return this;
}

public RestfulPatternBuilder options() {
public RestPatternBuilder options() {
this.methods.add(HttpMethod.OPTIONS);
return this;
}

public RestfulPatternBuilder head() {
public RestPatternBuilder head() {
this.methods.add(HttpMethod.HEAD);
return this;
}

public RestfulPatternBuilder all() {
public RestPatternBuilder all() {
return get().post().put().delete().patch().trace().options().head();
}

public RestfulPattern build() {
public RestPattern build() {
if (methods.isEmpty()) {
return new RestfulPattern(path, Set.of(HttpMethod.values()));
return new RestPattern(path, Set.of(HttpMethod.values()));
}
return new RestfulPattern(path, methods);
return new RestPattern(path, methods);
}
}
}
Loading
Loading