001: /*
002: * Copyright 2002-2006 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:
017: package org.springframework.web.servlet.view;
018:
019: import javax.servlet.http.HttpServletRequest;
020:
021: import org.springframework.util.StringUtils;
022: import org.springframework.util.Assert;
023: import org.springframework.web.servlet.RequestToViewNameTranslator;
024: import org.springframework.web.util.UrlPathHelper;
025:
026: /**
027: * Simply transforms the URI of the incoming request into the view name.
028: *
029: * <p>Can be explicitly defined as the "viewNameTranslator" bean in a
030: * {@link org.springframework.web.servlet.DispatcherServlet} context; else,
031: * a plain default instance will be used.
032: *
033: * <p>The default transformation simply strips the leading slash and file
034: * extension of the URI and returns the result as the view name with the
035: * configured {@link #setPrefix(String) "prefix"} and and
036: * {@link #setSuffix(String) "suffix"} added as appropriate.
037: *
038: * <p>The stripping of the leading slash and file extension can be disabled
039: * using the {@link #setStripLeadingSlash(boolean) "stripLeadingSlash"} and
040: * {@link #setStripExtension(boolean) "stripExtension"} properties,
041: * respectively.
042: *
043: * <p>Find below some examples of request to view name translation.
044: *
045: * <pre class="code"> http://localhost:8080/gamecast/display.html -> display
046: * http://localhost:8080/gamecast/displayShoppingCart.html -> displayShoppingCart
047: * http://localhost:8080/gamecast/admin/index.html -> admin/index
048: * </pre>
049: *
050: * @author Rob Harrop
051: * @since 2.0
052: * @see org.springframework.web.servlet.RequestToViewNameTranslator
053: * @see org.springframework.web.servlet.ViewResolver
054: */
055: public class DefaultRequestToViewNameTranslator implements
056: RequestToViewNameTranslator {
057:
058: private static final String SLASH = "/";
059:
060: private String prefix = "";
061:
062: private String suffix = "";
063:
064: private String separator = SLASH;
065:
066: private boolean stripLeadingSlash = true;
067:
068: private boolean stripExtension = true;
069:
070: private UrlPathHelper urlPathHelper = new UrlPathHelper();
071:
072: /**
073: * Set the prefix to prepend to generated view names.
074: * @param prefix the prefix to prepend to generated view names
075: */
076: public void setPrefix(String prefix) {
077: this .prefix = (prefix == null ? "" : prefix);
078: }
079:
080: /**
081: * Set the suffix to append to generated view names.
082: * @param suffix the suffix to append to generated view names
083: */
084: public void setSuffix(String suffix) {
085: this .suffix = (suffix == null ? "" : suffix);
086: }
087:
088: /**
089: * Set the value that will replace '<code>/</code>' as the separator
090: * in the view name. The default behavior simply leaves '<code>/</code>'
091: * as the separator.
092: * @param separator the desired separator value
093: */
094: public void setSeparator(String separator) {
095: this .separator = separator;
096: }
097:
098: /**
099: * Set whether or not leading slashes should be stripped from the URI when
100: * generating the view name. Default is "true".
101: * @param stripLeadingSlash <code>true</code> if leading slashes are to be stripped
102: */
103: public void setStripLeadingSlash(boolean stripLeadingSlash) {
104: this .stripLeadingSlash = stripLeadingSlash;
105: }
106:
107: /**
108: * Set whether or not file extensions should be stripped from the URI when
109: * generating the view name. Default is "true".
110: * @param stripExtension <code>true</code> if file extensions should be stripped
111: */
112: public void setStripExtension(boolean stripExtension) {
113: this .stripExtension = stripExtension;
114: }
115:
116: /**
117: * Set if URL lookup should always use the full path within the current servlet
118: * context. Else, the path within the current servlet mapping is used
119: * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
120: * Default is "false".
121: * @param alwaysUseFullPath <code>true</code> if URL lookup should always use the full path
122: * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
123: */
124: public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
125: this .urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
126: }
127:
128: /**
129: * Set if the context path and request URI should be URL-decoded.
130: * Both are returned <i>undecoded</i> by the Servlet API,
131: * in contrast to the servlet path.
132: * <p>Uses either the request encoding or the default encoding according
133: * to the Servlet spec (ISO-8859-1).
134: * <p>Note: Setting this to "true" requires JDK 1.4 if the encoding differs
135: * from the VM's platform default encoding, as JDK 1.3's URLDecoder class
136: * does not offer a way to specify the encoding.
137: * @param urlDecode <code>true</code> if the context path and request URI should be URL-decoded
138: * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
139: */
140: public void setUrlDecode(boolean urlDecode) {
141: this .urlPathHelper.setUrlDecode(urlDecode);
142: }
143:
144: /**
145: * Set the {@link org.springframework.web.util.UrlPathHelper} to use for
146: * the resolution of lookup paths.
147: * <p>Use this to override the default UrlPathHelper with a custom subclass,
148: * or to share common UrlPathHelper settings across multiple web components.
149: * @param urlPathHelper the desired helper
150: * @throws IllegalArgumentException if the supplied UrlPathHelper is <code>null</code>
151: */
152: public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
153: Assert.notNull(urlPathHelper);
154: this .urlPathHelper = urlPathHelper;
155: }
156:
157: /**
158: * Translates the request URI of the incoming {@link HttpServletRequest} to the
159: * view name based on the configured parameters.
160: * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest
161: * @see #transformPath
162: */
163: public final String getViewName(HttpServletRequest request) {
164: String lookupPath = this .urlPathHelper
165: .getLookupPathForRequest(request);
166: return this .prefix + transformPath(lookupPath) + this .suffix;
167: }
168:
169: /**
170: * Transform the request URI (in the context of the webapp) stripping
171: * slashes and extensions, and replacing the separator as required.
172: * @param lookupPath the lookup path for the current request,
173: * as determined by the UrlPathHelper
174: * @return the transformed path, with slashes and extensions stripped
175: * if desired
176: */
177: protected String transformPath(String lookupPath) {
178: String path = lookupPath;
179: if (this .stripLeadingSlash && path.startsWith(SLASH)) {
180: path = path.substring(1);
181: }
182: if (this.stripExtension) {
183: path = StringUtils.stripFilenameExtension(path);
184: }
185: if (!SLASH.equals(this.separator)) {
186: path = StringUtils.replace(path, SLASH, this.separator);
187: }
188: return path;
189: }
190:
191: }
|