001/*
002 * Copyright 2012-2017 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.autoconfigure.web;
018
019import java.util.LinkedHashMap;
020import java.util.Locale;
021import java.util.Map;
022
023import org.springframework.boot.context.properties.ConfigurationProperties;
024import org.springframework.http.MediaType;
025import org.springframework.validation.DefaultMessageCodesResolver;
026
027/**
028 * {@link ConfigurationProperties properties} for Spring MVC.
029 *
030 * @author Phillip Webb
031 * @author Sébastien Deleuze
032 * @author Stephane Nicoll
033 * @author Eddú Meléndez
034 * @since 1.1
035 */
036@ConfigurationProperties(prefix = "spring.mvc")
037public class WebMvcProperties {
038
039        /**
040         * Formatting strategy for message codes (PREFIX_ERROR_CODE, POSTFIX_ERROR_CODE).
041         */
042        private DefaultMessageCodesResolver.Format messageCodesResolverFormat;
043
044        /**
045         * Locale to use. By default, this locale is overridden by the "Accept-Language"
046         * header.
047         */
048        private Locale locale;
049
050        /**
051         * Define how the locale should be resolved.
052         */
053        private LocaleResolver localeResolver = LocaleResolver.ACCEPT_HEADER;
054
055        /**
056         * Date format to use (e.g. dd/MM/yyyy).
057         */
058        private String dateFormat;
059
060        /**
061         * Dispatch TRACE requests to the FrameworkServlet doService method.
062         */
063        private boolean dispatchTraceRequest = false;
064
065        /**
066         * Dispatch OPTIONS requests to the FrameworkServlet doService method.
067         */
068        private boolean dispatchOptionsRequest = true;
069
070        /**
071         * If the content of the "default" model should be ignored during redirect scenarios.
072         */
073        private boolean ignoreDefaultModelOnRedirect = true;
074
075        /**
076         * If a "NoHandlerFoundException" should be thrown if no Handler was found to process
077         * a request.
078         */
079        private boolean throwExceptionIfNoHandlerFound = false;
080
081        /**
082         * Enable warn logging of exceptions resolved by a "HandlerExceptionResolver".
083         */
084        private boolean logResolvedException = false;
085
086        /**
087         * Maps file extensions to media types for content negotiation, e.g. yml->text/yaml.
088         */
089        private Map<String, MediaType> mediaTypes = new LinkedHashMap<String, MediaType>();
090
091        /**
092         * Path pattern used for static resources.
093         */
094        private String staticPathPattern = "/**";
095
096        private final Async async = new Async();
097
098        private final Servlet servlet = new Servlet();
099
100        private final View view = new View();
101
102        public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
103                return this.messageCodesResolverFormat;
104        }
105
106        public void setMessageCodesResolverFormat(
107                        DefaultMessageCodesResolver.Format messageCodesResolverFormat) {
108                this.messageCodesResolverFormat = messageCodesResolverFormat;
109        }
110
111        public Locale getLocale() {
112                return this.locale;
113        }
114
115        public void setLocale(Locale locale) {
116                this.locale = locale;
117        }
118
119        public LocaleResolver getLocaleResolver() {
120                return this.localeResolver;
121        }
122
123        public void setLocaleResolver(LocaleResolver localeResolver) {
124                this.localeResolver = localeResolver;
125        }
126
127        public String getDateFormat() {
128                return this.dateFormat;
129        }
130
131        public void setDateFormat(String dateFormat) {
132                this.dateFormat = dateFormat;
133        }
134
135        public boolean isIgnoreDefaultModelOnRedirect() {
136                return this.ignoreDefaultModelOnRedirect;
137        }
138
139        public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect) {
140                this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
141        }
142
143        public boolean isThrowExceptionIfNoHandlerFound() {
144                return this.throwExceptionIfNoHandlerFound;
145        }
146
147        public void setThrowExceptionIfNoHandlerFound(
148                        boolean throwExceptionIfNoHandlerFound) {
149                this.throwExceptionIfNoHandlerFound = throwExceptionIfNoHandlerFound;
150        }
151
152        public boolean isLogResolvedException() {
153                return this.logResolvedException;
154        }
155
156        public void setLogResolvedException(boolean logResolvedException) {
157                this.logResolvedException = logResolvedException;
158        }
159
160        public Map<String, MediaType> getMediaTypes() {
161                return this.mediaTypes;
162        }
163
164        public void setMediaTypes(Map<String, MediaType> mediaTypes) {
165                this.mediaTypes = mediaTypes;
166        }
167
168        public boolean isDispatchOptionsRequest() {
169                return this.dispatchOptionsRequest;
170        }
171
172        public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
173                this.dispatchOptionsRequest = dispatchOptionsRequest;
174        }
175
176        public boolean isDispatchTraceRequest() {
177                return this.dispatchTraceRequest;
178        }
179
180        public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
181                this.dispatchTraceRequest = dispatchTraceRequest;
182        }
183
184        public String getStaticPathPattern() {
185                return this.staticPathPattern;
186        }
187
188        public void setStaticPathPattern(String staticPathPattern) {
189                this.staticPathPattern = staticPathPattern;
190        }
191
192        public Async getAsync() {
193                return this.async;
194        }
195
196        public Servlet getServlet() {
197                return this.servlet;
198        }
199
200        public View getView() {
201                return this.view;
202        }
203
204        public static class Async {
205
206                /**
207                 * Amount of time (in milliseconds) before asynchronous request handling times
208                 * out. If this value is not set, the default timeout of the underlying
209                 * implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
210                 */
211                private Long requestTimeout;
212
213                public Long getRequestTimeout() {
214                        return this.requestTimeout;
215                }
216
217                public void setRequestTimeout(Long requestTimeout) {
218                        this.requestTimeout = requestTimeout;
219                }
220
221        }
222
223        public static class Servlet {
224
225                /**
226                 * Load on startup priority of the dispatcher servlet.
227                 */
228                private int loadOnStartup = -1;
229
230                public int getLoadOnStartup() {
231                        return this.loadOnStartup;
232                }
233
234                public void setLoadOnStartup(int loadOnStartup) {
235                        this.loadOnStartup = loadOnStartup;
236                }
237
238        }
239
240        public static class View {
241
242                /**
243                 * Spring MVC view prefix.
244                 */
245                private String prefix;
246
247                /**
248                 * Spring MVC view suffix.
249                 */
250                private String suffix;
251
252                public String getPrefix() {
253                        return this.prefix;
254                }
255
256                public void setPrefix(String prefix) {
257                        this.prefix = prefix;
258                }
259
260                public String getSuffix() {
261                        return this.suffix;
262                }
263
264                public void setSuffix(String suffix) {
265                        this.suffix = suffix;
266                }
267
268        }
269
270        public enum LocaleResolver {
271
272                /**
273                 * Always use the configured locale.
274                 */
275                FIXED,
276
277                /**
278                 * Use the "Accept-Language" header or the configured locale if the header is not
279                 * set.
280                 */
281                ACCEPT_HEADER
282
283        }
284
285}