KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > ApplicationViewSelector


1 /*
2  * Copyright 2002-2006 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 package org.springframework.webflow.engine.support;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.binding.expression.Expression;
21 import org.springframework.core.style.ToStringCreator;
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24 import org.springframework.webflow.engine.ViewSelector;
25 import org.springframework.webflow.engine.ViewState;
26 import org.springframework.webflow.execution.RequestContext;
27 import org.springframework.webflow.execution.ViewSelection;
28 import org.springframework.webflow.execution.support.ApplicationView;
29 import org.springframework.webflow.execution.support.FlowExecutionRedirect;
30
31 /**
32  * Simple view selector that makes an {@link ApplicationView} selection using a
33  * view name expression.
34  * <p>
35  * This factory will treat all attributes returned from calling
36  * {@link RequestContext#getModel()} as the application model exposed to the
37  * view during rendering. This is typically the union of attributes in request,
38  * flow, and conversation scope.
39  * <p>
40  * This selector also supports setting a <i>redirect</i> flag that can be used
41  * to trigger a redirect to the {@link ApplicationView} at a bookmarkable URL
42  * using an {@link FlowExecutionRedirect}}.
43  *
44  * @see org.springframework.webflow.execution.support.ApplicationView
45  * @see org.springframework.webflow.execution.support.FlowExecutionRedirect
46  *
47  * @author Keith Donald
48  * @author Erwin Vervaet
49  */

50 public class ApplicationViewSelector implements ViewSelector, Serializable JavaDoc {
51
52     /**
53      * Flow execution attribute name that indicates that we should always render
54      * an application view via a redirect.
55      */

56     public static final String JavaDoc ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE = "alwaysRedirectOnPause";
57
58     /**
59      * The view name to render.
60      */

61     private Expression viewName;
62
63     /**
64      * A flag indicating if a redirect to the selected application view should
65      * be requested.
66      * <p>
67      * Setting this allows you to redirect while the flow is in progress to a
68      * stable URL that can be safely refreshed.
69      */

70     private boolean redirect;
71
72     /**
73      * Creates a application view selector that will make application view
74      * selections requesting that the specified view be rendered.
75      * @param viewName the view name expression
76      */

77     public ApplicationViewSelector(Expression viewName) {
78         this(viewName, false);
79     }
80
81     /**
82      * Creates a application view selector that will make application view
83      * selections requesting that the specified view be rendered. No redirects
84      * will be done.
85      * @param viewName the view name expression
86      * @param redirect indicates if a redirect to the view should be initiated
87      */

88     public ApplicationViewSelector(Expression viewName, boolean redirect) {
89         Assert.notNull(viewName, "The view name expression is required");
90         this.viewName = viewName;
91         this.redirect = redirect;
92     }
93
94     /**
95      * Returns the name of the view that should be rendered.
96      */

97     public Expression getViewName() {
98         return viewName;
99     }
100
101     /**
102      * Returns if a redirect to the view should be done.
103      */

104     public boolean isRedirect() {
105         return redirect;
106     }
107
108     public boolean isEntrySelectionRenderable(RequestContext context) {
109         return !shouldRedirect(context);
110     }
111
112     public ViewSelection makeEntrySelection(RequestContext context) {
113         if (shouldRedirect(context)) {
114             return FlowExecutionRedirect.INSTANCE;
115         }
116         else {
117             return makeRefreshSelection(context);
118         }
119     }
120
121     public ViewSelection makeRefreshSelection(RequestContext context) {
122         String JavaDoc viewName = resolveViewName(context);
123         if (!StringUtils.hasText(viewName)) {
124             throw new IllegalStateException JavaDoc("Resolved application view name was empty; programmer error! -- "
125                     + "The expression that was evaluated against the request context was '" + getViewName() + "'");
126         }
127         return createApplicationView(viewName, context);
128     }
129
130     // internal helpers
131

132     /**
133      * Resolves the application view name from the request context.
134      * @param context the context
135      * @return the view name
136      */

137     protected String JavaDoc resolveViewName(RequestContext context) {
138         return (String JavaDoc) getViewName().evaluate(context, null);
139     }
140
141     /**
142      * Creates the application view selection.
143      * @param viewName the resolved view name
144      * @param context the context
145      * @return the application view
146      */

147     protected ApplicationView createApplicationView(String JavaDoc viewName, RequestContext context) {
148         return new ApplicationView(viewName, context.getModel().asMap());
149     }
150
151     /**
152      * Determine whether or not a redirect should be used to render the
153      * application view.
154      * @param context the context
155      * @return true or false
156      */

157     protected boolean shouldRedirect(RequestContext context) {
158         return context.getCurrentState() instanceof ViewState && (redirect || alwaysRedirectOnPause(context));
159     }
160
161     /**
162      * Checks the {@link #ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE} to see if every
163      * application view of the flow execution should be rendered via a redirect.
164      * @param context the flow execution request context
165      * @return true or false
166      */

167     protected boolean alwaysRedirectOnPause(RequestContext context) {
168         return context.getFlowExecutionContext().getAttributes().getBoolean(
169                 ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE, Boolean.FALSE).booleanValue();
170     }
171
172     public String JavaDoc toString() {
173         return new ToStringCreator(this).append("viewName", viewName).append("redirect", redirect).toString();
174     }
175 }
Popular Tags