KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > UrlFilenameViewController


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.mvc;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.springframework.util.StringUtils;
26 import org.springframework.web.servlet.HandlerMapping;
27
28 /**
29  * Simple <code>Controller</code> implementation that transforms the virtual
30  * path of a URL into a view name and returns that view.
31  *
32  * <p>Can optionally prepend a {@link #setPrefix prefix} and/or append a
33  * {@link #setSuffix suffix} to build the viewname from the URL filename.
34  *
35  * <p>Find below some examples:
36  *
37  * <ol>
38  * <li><code>"/index" -> "index"</code></li>
39  * <li><code>"/index.html" -> "index"</code></li>
40  * <li><code>"/index.html"</code> + prefix <code>"pre_"</code> and suffix <code>"_suf" -> "pre_index_suf"</code></li>
41  * <li><code>"/products/view.html" -> "products/view"</code></li>
42  * </ol>
43  *
44  * <p>Thanks to David Barri for suggesting prefix/suffix support!
45  *
46  * @author Alef Arendsen
47  * @author Juergen Hoeller
48  * @author Rob Harrop
49  * @see #setPrefix
50  * @see #setSuffix
51  */

52 public class UrlFilenameViewController extends AbstractUrlViewController {
53
54     private String JavaDoc prefix = "";
55
56     private String JavaDoc suffix = "";
57
58     /** Request URL path String --> view name String */
59     private final Map JavaDoc viewNameCache = Collections.synchronizedMap(new HashMap JavaDoc());
60
61
62     /**
63      * Set the prefix to prepend to the request URL filename
64      * to build a view name.
65      */

66     public void setPrefix(String JavaDoc prefix) {
67         this.prefix = (prefix != null ? prefix : "");
68     }
69
70     /**
71      * Return the prefix to prepend to the request URL filename.
72      */

73     protected String JavaDoc getPrefix() {
74         return this.prefix;
75     }
76
77     /**
78      * Set the suffix to append to the request URL filename
79      * to build a view name.
80      */

81     public void setSuffix(String JavaDoc suffix) {
82         this.suffix = (suffix != null ? suffix : "");
83     }
84
85     /**
86      * Return the suffix to append to the request URL filename.
87      */

88     protected String JavaDoc getSuffix() {
89         return this.suffix;
90     }
91
92
93     /**
94      * Returns view name based on the URL filename,
95      * with prefix/suffix applied when appropriate.
96      * @see #extractViewNameFromUrlPath
97      * @see #setPrefix
98      * @see #setSuffix
99      */

100     protected String JavaDoc getViewNameForRequest(HttpServletRequest JavaDoc request) {
101         String JavaDoc uri = extractOperableUrl(request);
102         return getViewNameForUrlPath(uri);
103     }
104
105     /**
106      * Extract a URL path from the given request,
107      * suitable for view name extraction.
108      * @param request current HTTP request
109      * @return the URL to use for view name extraction
110      */

111     protected String JavaDoc extractOperableUrl(HttpServletRequest JavaDoc request) {
112         String JavaDoc urlPath = (String JavaDoc) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
113         if (!StringUtils.hasText(urlPath)) {
114             urlPath = getUrlPathHelper().getLookupPathForRequest(request);
115         }
116         return urlPath;
117     }
118
119     /**
120      * Returns view name based on the URL filename,
121      * with prefix/suffix applied when appropriate.
122      * @param uri the request URI; for example <code>"/index.html"</code>
123      * @return the extracted URI filename; for example <code>"index"</code>
124      * @see #extractViewNameFromUrlPath
125      * @see #postProcessViewName
126      */

127     protected String JavaDoc getViewNameForUrlPath(String JavaDoc uri) {
128         String JavaDoc viewName = (String JavaDoc) this.viewNameCache.get(uri);
129         if (viewName == null) {
130             viewName = extractViewNameFromUrlPath(uri);
131             viewName = postProcessViewName(viewName);
132             this.viewNameCache.put(uri, viewName);
133         }
134         return viewName;
135     }
136
137     /**
138      * Extract the URL filename from the given request URI.
139      * @param uri the request URI; for example <code>"/index.html"</code>
140      * @return the extracted URI filename; for example <code>"index"</code>
141      */

142     protected String JavaDoc extractViewNameFromUrlPath(String JavaDoc uri) {
143         int start = (uri.charAt(0) == '/' ? 1 : 0);
144         int lastIndex = uri.lastIndexOf(".");
145         int end = (lastIndex < 0 ? uri.length() : lastIndex);
146         return uri.substring(start, end);
147     }
148
149     /**
150      * Build the full view name based on the given view name
151      * as indicated by the URL path.
152      * <p>The default implementation simply applies prefix and suffix.
153      * This can be overridden, for example, to manipulate upper case
154      * / lower case, etc.
155      * @param viewName the original view name, as indicated by the URL path
156      * @return the full view name to use
157      * @see #getPrefix()
158      * @see #getSuffix()
159      */

160     protected String JavaDoc postProcessViewName(String JavaDoc viewName) {
161         return getPrefix() + viewName + getSuffix();
162     }
163
164 }
165
Popular Tags