Provide access to interceptors in AbstractHandlerMapping

Closes gh-28985
This commit is contained in:
rstoyanchev 2023-01-30 17:20:52 +00:00
parent 17f1eadb6f
commit e564a0de46
1 changed files with 29 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -277,6 +277,34 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
this.interceptors.addAll(Arrays.asList(interceptors));
}
/**
* Return all configured interceptors adapted to {@link HandlerInterceptor}.
* @return the array of configured interceptors, or {@code null} if none
* are configured; this method also returns {@code null} also if called too
* early, or more specifically before.
* {@link org.springframework.context.ApplicationContextAware#setApplicationContext}.
*/
@Nullable
public final HandlerInterceptor[] getAdaptedInterceptors() {
return (!this.adaptedInterceptors.isEmpty() ?
this.adaptedInterceptors.toArray(new HandlerInterceptor[0]) : null);
}
/**
* Return all configured {@link MappedInterceptor}s as an array.
* @return the array of {@link MappedInterceptor}s, or {@code null} if none
*/
@Nullable
protected final MappedInterceptor[] getMappedInterceptors() {
List<MappedInterceptor> mappedInterceptors = new ArrayList<>(this.adaptedInterceptors.size());
for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
if (interceptor instanceof MappedInterceptor mappedInterceptor) {
mappedInterceptors.add(mappedInterceptor);
}
}
return (!mappedInterceptors.isEmpty() ? mappedInterceptors.toArray(new MappedInterceptor[0]) : null);
}
/**
* Set "global" CORS configuration mappings. The first matching URL pattern
* determines the {@code CorsConfiguration} to use which is then further
@ -455,33 +483,6 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
}
}
/**
* Return the adapted interceptors as {@link HandlerInterceptor} array.
* @return the array of {@link HandlerInterceptor HandlerInterceptor}s,
* or {@code null} if none
*/
@Nullable
protected final HandlerInterceptor[] getAdaptedInterceptors() {
return (!this.adaptedInterceptors.isEmpty() ?
this.adaptedInterceptors.toArray(new HandlerInterceptor[0]) : null);
}
/**
* Return all configured {@link MappedInterceptor}s as an array.
* @return the array of {@link MappedInterceptor}s, or {@code null} if none
*/
@Nullable
protected final MappedInterceptor[] getMappedInterceptors() {
List<MappedInterceptor> mappedInterceptors = new ArrayList<>(this.adaptedInterceptors.size());
for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
if (interceptor instanceof MappedInterceptor mappedInterceptor) {
mappedInterceptors.add(mappedInterceptor);
}
}
return (!mappedInterceptors.isEmpty() ? mappedInterceptors.toArray(new MappedInterceptor[0]) : null);
}
/**
* Return "true" if this {@code HandlerMapping} has been
* {@link #setPatternParser enabled} to use parsed {@code PathPattern}s.