001/*
002 * Copyright 2012-2018 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 *      http://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.boot.actuate.web.mappings.servlet;
018
019import java.util.List;
020import java.util.Set;
021import java.util.stream.Collectors;
022
023import org.springframework.web.bind.annotation.RequestMethod;
024import org.springframework.web.servlet.mvc.condition.MediaTypeExpression;
025import org.springframework.web.servlet.mvc.condition.NameValueExpression;
026import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
027
028/**
029 * Description of the conditions of a {@link RequestMappingInfo}.
030 *
031 * @author Andy Wilkinson
032 * @since 2.0.0
033 */
034public class RequestMappingConditionsDescription {
035
036        private final List<MediaTypeExpressionDescription> consumes;
037
038        private final List<NameValueExpressionDescription> headers;
039
040        private final Set<RequestMethod> methods;
041
042        private final List<NameValueExpressionDescription> params;
043
044        private final Set<String> patterns;
045
046        private final List<MediaTypeExpressionDescription> produces;
047
048        RequestMappingConditionsDescription(RequestMappingInfo requestMapping) {
049                this.consumes = requestMapping.getConsumesCondition().getExpressions().stream()
050                                .map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
051                this.headers = requestMapping.getHeadersCondition().getExpressions().stream()
052                                .map(NameValueExpressionDescription::new).collect(Collectors.toList());
053                this.methods = requestMapping.getMethodsCondition().getMethods();
054                this.params = requestMapping.getParamsCondition().getExpressions().stream()
055                                .map(NameValueExpressionDescription::new).collect(Collectors.toList());
056                this.patterns = requestMapping.getPatternsCondition().getPatterns();
057                this.produces = requestMapping.getProducesCondition().getExpressions().stream()
058                                .map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
059        }
060
061        public List<MediaTypeExpressionDescription> getConsumes() {
062                return this.consumes;
063        }
064
065        public List<NameValueExpressionDescription> getHeaders() {
066                return this.headers;
067        }
068
069        public Set<RequestMethod> getMethods() {
070                return this.methods;
071        }
072
073        public List<NameValueExpressionDescription> getParams() {
074                return this.params;
075        }
076
077        public Set<String> getPatterns() {
078                return this.patterns;
079        }
080
081        public List<MediaTypeExpressionDescription> getProduces() {
082                return this.produces;
083        }
084
085        /**
086         * A description of a {@link MediaTypeExpression} in a request mapping condition.
087         */
088        public static class MediaTypeExpressionDescription {
089
090                private final String mediaType;
091
092                private final boolean negated;
093
094                MediaTypeExpressionDescription(MediaTypeExpression expression) {
095                        this.mediaType = expression.getMediaType().toString();
096                        this.negated = expression.isNegated();
097                }
098
099                public String getMediaType() {
100                        return this.mediaType;
101                }
102
103                public boolean isNegated() {
104                        return this.negated;
105                }
106
107        }
108
109        /**
110         * A description of a {@link NameValueExpression} in a request mapping condition.
111         */
112        public static class NameValueExpressionDescription {
113
114                private final String name;
115
116                private final Object value;
117
118                private final boolean negated;
119
120                NameValueExpressionDescription(NameValueExpression<?> expression) {
121                        this.name = expression.getName();
122                        this.value = expression.getValue();
123                        this.negated = expression.isNegated();
124                }
125
126                public String getName() {
127                        return this.name;
128                }
129
130                public Object getValue() {
131                        return this.value;
132                }
133
134                public boolean isNegated() {
135                        return this.negated;
136                }
137
138        }
139
140}