001/*
002 * Copyright 2002-2012 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.mvc;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.util.Assert;
023import org.springframework.web.servlet.ModelAndView;
024import org.springframework.web.servlet.support.RequestContextUtils;
025import org.springframework.web.util.UrlPathHelper;
026
027/**
028 * Abstract base class for {@code Controllers} that return a view name
029 * based on the request URL.
030 *
031 * <p>Provides infrastructure for determining view names from URLs and configurable
032 * URL lookup. For information on the latter, see {@code alwaysUseFullPath}
033 * and {@code urlDecode} properties.
034 *
035 * @author Juergen Hoeller
036 * @since 1.2.6
037 * @see #setAlwaysUseFullPath
038 * @see #setUrlDecode
039 */
040public abstract class AbstractUrlViewController extends AbstractController {
041
042        private UrlPathHelper urlPathHelper = new UrlPathHelper();
043
044
045        /**
046         * Set if URL lookup should always use full path within current servlet
047         * context. Else, the path within the current servlet mapping is used
048         * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
049         * Default is "false".
050         * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
051         */
052        public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
053                this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
054        }
055
056        /**
057         * Set if context path and request URI should be URL-decoded.
058         * Both are returned <i>undecoded</i> by the Servlet API,
059         * in contrast to the servlet path.
060         * <p>Uses either the request encoding or the default encoding according
061         * to the Servlet spec (ISO-8859-1).
062         * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
063         */
064        public void setUrlDecode(boolean urlDecode) {
065                this.urlPathHelper.setUrlDecode(urlDecode);
066        }
067
068        /**
069         * Set if ";" (semicolon) content should be stripped from the request URI.
070         * @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
071         */
072        public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
073                this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
074        }
075
076        /**
077         * Set the UrlPathHelper to use for the resolution of lookup paths.
078         * <p>Use this to override the default UrlPathHelper with a custom subclass,
079         * or to share common UrlPathHelper settings across multiple MethodNameResolvers
080         * and HandlerMappings.
081         * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
082         */
083        public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
084                Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
085                this.urlPathHelper = urlPathHelper;
086        }
087
088        /**
089         * Return the UrlPathHelper to use for the resolution of lookup paths.
090         */
091        protected UrlPathHelper getUrlPathHelper() {
092                return this.urlPathHelper;
093        }
094
095
096        /**
097         * Retrieves the URL path to use for lookup and delegates to
098         * {@link #getViewNameForRequest}. Also adds the content of
099         * {@link RequestContextUtils#getInputFlashMap} to the model.
100         */
101        @Override
102        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
103                String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
104                String viewName = getViewNameForRequest(request);
105                if (logger.isDebugEnabled()) {
106                        logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
107                }
108                return new ModelAndView(viewName, RequestContextUtils.getInputFlashMap(request));
109        }
110
111        /**
112         * Return the name of the view to render for this request, based on the
113         * given lookup path. Called by {@link #handleRequestInternal}.
114         * @param request current HTTP request
115         * @return a view name for this request (never {@code null})
116         * @see #handleRequestInternal
117         * @see #setAlwaysUseFullPath
118         * @see #setUrlDecode
119         */
120        protected abstract String getViewNameForRequest(HttpServletRequest request);
121
122}