KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > SuffixViewResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.view;
25
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.springframework.web.servlet.View;
30 import org.springframework.web.servlet.ViewResolver;
31 import org.springframework.web.servlet.view.AbstractCachingViewResolver;
32 import org.springframework.web.servlet.view.InternalResourceView;
33 import org.springframework.web.servlet.view.RedirectView;
34
35 /**
36  * View resolver that supports view names with a suffix. For example
37  * <tt>someView.jsp</tt> would be delegated to the ViewResolver
38  * registered for the <tt>jsp</tt> suffix.
39  */

40 public class SuffixViewResolver extends AbstractCachingViewResolver {
41
42     /**
43      * Prefix for special view names that specify a redirect URL (usually
44      * to a controller after a form has been submitted and processed).
45      * Such view names will not be resolved in the configured default
46      * way but rather be treated as special shortcut.
47      */

48     public static final String JavaDoc REDIRECT_URL_PREFIX = "redirect:";
49
50     /**
51      * Prefix for special view names that specify a forward URL (usually
52      * to a controller after a form has been submitted and processed).
53      * Such view names will not be resolved in the configured default
54      * way but rather be treated as special shortcut.
55      */

56     public static final String JavaDoc FORWARD_URL_PREFIX = "forward:";
57     
58     private Map JavaDoc resolvers;
59
60     private String JavaDoc defaultSuffix;
61
62     private ViewResolver defaultResolver;
63
64     private boolean redirectContextRelative = true;
65
66     private boolean redirectHttp10Compatible = true;
67     
68     /**
69      * @param resolvers ViewResolvers keyed by suffix (without dots).
70      */

71     public void setResolvers(Map JavaDoc resolvers) {
72         this.resolvers = resolvers;
73     }
74     
75     /**
76      * Sets a default suffix that will be appended to view names that don't
77      * contain a dot. If set to <code>null</code> (default), view names without
78      * an extension won't be resolved which allows resolver chaining.
79      */

80     public void setDefaultSuffix(String JavaDoc defaultSuffix) {
81         this.defaultSuffix = defaultSuffix;
82     }
83
84     /**
85      * Set whether to interpret a given redirect URL that starts with a
86      * slash ("/") as relative to the current ServletContext, i.e. as
87      * relative to the web application root.
88      * <p>Default is "true": A redirect URL that starts with a slash will be
89      * interpreted as relative to the web application root, i.e. the context
90      * path will be prepended to the URL.
91      * <p><b>Redirect URLs can be specified via the "redirect:" prefix.</b>
92      * E.g.: "redirect:myAction.do"
93      * @see RedirectView#setContextRelative
94      * @see #REDIRECT_URL_PREFIX
95      */

96     public void setRedirectContextRelative(boolean redirectContextRelative) {
97         this.redirectContextRelative = redirectContextRelative;
98     }
99
100     /**
101      * Set whether redirects should stay compatible with HTTP 1.0 clients.
102      * <p>In the default implementation, this will enforce HTTP status code 302
103      * in any case, i.e. delegate to <code>HttpServletResponse.sendRedirect</code>.
104      * Turning this off will send HTTP status code 303, which is the correct
105      * code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.
106      * <p>Many HTTP 1.1 clients treat 302 just like 303, not making any
107      * difference. However, some clients depend on 303 when redirecting
108      * after a POST request; turn this flag off in such a scenario.
109      * <p><b>Redirect URLs can be specified via the "redirect:" prefix.</b>
110      * E.g.: "redirect:myAction.do"
111      * @see RedirectView#setHttp10Compatible
112      * @see #REDIRECT_URL_PREFIX
113      */

114     public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) {
115         this.redirectHttp10Compatible = redirectHttp10Compatible;
116     }
117     
118     protected View loadView(String JavaDoc viewName, Locale JavaDoc locale) throws Exception JavaDoc {
119         
120         // Check for special "redirect:" prefix.
121
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
122             String JavaDoc redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
123             return new RedirectView(redirectUrl, redirectContextRelative,
124                     redirectHttp10Compatible);
125         }
126
127         // Check for special "forward:" prefix.
128
if (viewName.startsWith(FORWARD_URL_PREFIX)) {
129             String JavaDoc forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
130             return new InternalResourceView(forwardUrl);
131         }
132                 
133         String JavaDoc suffix = null;
134         ViewResolver resolver = null;
135         int i = viewName.lastIndexOf('.');
136         if (i > 0) {
137             suffix = viewName.substring(i + 1);
138             resolver = (ViewResolver) resolvers.get(suffix);
139             if (logger.isDebugEnabled()) {
140                 if (resolver != null) {
141                     logger.debug("Using " + resolver + " for suffix ["
142                             + suffix + ']');
143                 }
144             }
145         }
146         
147         if (resolver == null) {
148             if (suffix != null) {
149                 throw new IllegalArgumentException JavaDoc("No resolver defined for " +
150                         "suffix [" + suffix + ']');
151             }
152             if (defaultSuffix != null) {
153                 viewName += "." + defaultSuffix;
154                 resolver = defaultResolver;
155             }
156             else {
157                 return null;
158             }
159         }
160         return resolver.resolveViewName(viewName, locale);
161     }
162
163 }
164
Popular Tags