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.core.Ordered;
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<String>();
042
043        private final List<String> excludePatterns = new ArrayList<String>();
044
045        private PathMatcher pathMatcher;
046
047        private int order = 0;
048
049
050        /**
051         * Create an {@link InterceptorRegistration} instance.
052         */
053        public InterceptorRegistration(HandlerInterceptor interceptor) {
054                Assert.notNull(interceptor, "Interceptor is required");
055                this.interceptor = interceptor;
056        }
057
058
059        /**
060         * Add URL patterns to which the registered interceptor should apply to.
061         */
062        public InterceptorRegistration addPathPatterns(String... patterns) {
063                this.includePatterns.addAll(Arrays.asList(patterns));
064                return this;
065        }
066
067        /**
068         * Add URL patterns to which the registered interceptor should not apply to.
069         */
070        public InterceptorRegistration excludePathPatterns(String... patterns) {
071                this.excludePatterns.addAll(Arrays.asList(patterns));
072                return this;
073        }
074
075        /**
076         * A PathMatcher implementation to use with this interceptor. This is an optional,
077         * advanced property required only if using custom PathMatcher implementations
078         * that support mapping metadata other than the Ant path patterns supported
079         * by default.
080         */
081        public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
082                this.pathMatcher = pathMatcher;
083                return this;
084        }
085
086        /**
087         * Specify an order position to be used. Default is 0.
088         * @since 4.3.23
089         */
090        public InterceptorRegistration order(int order){
091                this.order = order;
092                return this;
093        }
094
095        /**
096         * Build the underlying interceptor. If URL patterns are provided, the returned
097         * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
098         */
099        protected Object getInterceptor() {
100                if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
101                        return this.interceptor;
102                }
103
104                String[] include = StringUtils.toStringArray(this.includePatterns);
105                String[] exclude = StringUtils.toStringArray(this.excludePatterns);
106                MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
107                if (this.pathMatcher != null) {
108                        mappedInterceptor.setPathMatcher(this.pathMatcher);
109                }
110                return mappedInterceptor;
111        }
112
113        Ordered toOrdered() {
114                return new Ordered() {
115                        @Override
116                        public int getOrder() {
117                                return order;
118                        }
119                };
120        }
121
122}