001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.config.annotation;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025import org.springframework.util.PathMatcher;
026import org.springframework.util.StringUtils;
027import org.springframework.web.servlet.HandlerInterceptor;
028import org.springframework.web.servlet.handler.MappedInterceptor;
029
030/**
031 * Assists with the creation of a {@link MappedInterceptor}.
032 *
033 * @author Rossen Stoyanchev
034 * @author Keith Donald
035 * @since 3.1
036 */
037public class InterceptorRegistration {
038
039        private final HandlerInterceptor interceptor;
040
041        private final List<String> includePatterns = new ArrayList<>();
042
043        private final List<String> excludePatterns = new ArrayList<>();
044
045        @Nullable
046        private PathMatcher pathMatcher;
047
048        private int order = 0;
049
050
051        /**
052         * Create an {@link InterceptorRegistration} instance.
053         */
054        public InterceptorRegistration(HandlerInterceptor interceptor) {
055                Assert.notNull(interceptor, "Interceptor is required");
056                this.interceptor = interceptor;
057        }
058
059
060        /**
061         * Add URL patterns to which the registered interceptor should apply to.
062         */
063        public InterceptorRegistration addPathPatterns(String... patterns) {
064                return addPathPatterns(Arrays.asList(patterns));
065        }
066
067        /**
068         * List-based variant of {@link #addPathPatterns(String...)}.
069         * @since 5.0.3
070         */
071        public InterceptorRegistration addPathPatterns(List<String> patterns) {
072                this.includePatterns.addAll(patterns);
073                return this;
074        }
075
076        /**
077         * Add URL patterns to which the registered interceptor should not apply to.
078         */
079        public InterceptorRegistration excludePathPatterns(String... patterns) {
080                return excludePathPatterns(Arrays.asList(patterns));
081        }
082
083        /**
084         * List-based variant of {@link #excludePathPatterns(String...)}.
085         * @since 5.0.3
086         */
087        public InterceptorRegistration excludePathPatterns(List<String> patterns) {
088                this.excludePatterns.addAll(patterns);
089                return this;
090        }
091
092        /**
093         * A PathMatcher implementation to use with this interceptor. This is an optional,
094         * advanced property required only if using custom PathMatcher implementations
095         * that support mapping metadata other than the Ant path patterns supported
096         * by default.
097         */
098        public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
099                this.pathMatcher = pathMatcher;
100                return this;
101        }
102
103        /**
104         * Specify an order position to be used. Default is 0.
105         * @since 4.3.23
106         */
107        public InterceptorRegistration order(int order){
108                this.order = order;
109                return this;
110        }
111
112        /**
113         * Return the order position to be used.
114         */
115        protected int getOrder() {
116                return this.order;
117        }
118
119        /**
120         * Build the underlying interceptor. If URL patterns are provided, the returned
121         * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
122         */
123        protected Object getInterceptor() {
124                if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
125                        return this.interceptor;
126                }
127
128                String[] include = StringUtils.toStringArray(this.includePatterns);
129                String[] exclude = StringUtils.toStringArray(this.excludePatterns);
130                MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
131                if (this.pathMatcher != null) {
132                        mappedInterceptor.setPathMatcher(this.pathMatcher);
133                }
134                return mappedInterceptor;
135        }
136
137}