1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.lifecycle; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.List; |
23 | |
import java.util.logging.Level; |
24 | |
import java.util.logging.Logger; |
25 | |
|
26 | |
import javax.faces.FacesException; |
27 | |
import javax.faces.application.Application; |
28 | |
import javax.faces.application.FacesMessage; |
29 | |
import javax.faces.application.ProjectStage; |
30 | |
import javax.faces.application.ViewHandler; |
31 | |
import javax.faces.component.UIViewRoot; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.event.PhaseId; |
34 | |
import javax.faces.event.PreRenderViewEvent; |
35 | |
import javax.faces.view.ViewDeclarationLanguage; |
36 | |
|
37 | |
import org.apache.myfaces.shared.config.MyfacesConfig; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | class RenderResponseExecutor extends PhaseExecutor |
46 | |
{ |
47 | |
|
48 | 0 | private static final Logger log = Logger.getLogger(RenderResponseExecutor.class.getName()); |
49 | |
private MyfacesConfig _myfacesConfig; |
50 | |
|
51 | |
public boolean execute(FacesContext facesContext) |
52 | |
{ |
53 | 0 | Application application = facesContext.getApplication(); |
54 | 0 | ViewHandler viewHandler = application.getViewHandler(); |
55 | |
UIViewRoot root; |
56 | |
UIViewRoot previousRoot; |
57 | |
String viewId; |
58 | |
String newViewId; |
59 | |
boolean isNotSameRoot; |
60 | 0 | int loops = 0; |
61 | 0 | int maxLoops = 15; |
62 | |
|
63 | 0 | if (facesContext.getViewRoot() == null) |
64 | |
{ |
65 | 0 | throw new ViewNotFoundException("A view is required to execute "+facesContext.getCurrentPhaseId()); |
66 | |
} |
67 | |
|
68 | 0 | forceSessionCreation(facesContext); |
69 | |
|
70 | |
try |
71 | |
{ |
72 | |
|
73 | |
do |
74 | |
{ |
75 | 0 | root = facesContext.getViewRoot(); |
76 | 0 | previousRoot = root; |
77 | 0 | viewId = root.getViewId(); |
78 | |
|
79 | 0 | ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage( |
80 | |
facesContext, viewId); |
81 | 0 | if (vdl != null) |
82 | |
{ |
83 | 0 | vdl.buildView(facesContext, root); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | application.publishEvent(facesContext, PreRenderViewEvent.class, root); |
90 | |
|
91 | |
|
92 | 0 | if (facesContext.getResponseComplete()) |
93 | |
{ |
94 | 0 | return false; |
95 | |
} |
96 | |
|
97 | 0 | root = facesContext.getViewRoot(); |
98 | |
|
99 | 0 | newViewId = root.getViewId(); |
100 | |
|
101 | 0 | isNotSameRoot = !( (newViewId == null ? newViewId == viewId : newViewId.equals(viewId) ) && |
102 | |
previousRoot.equals(root) ); |
103 | |
|
104 | 0 | loops++; |
105 | |
} |
106 | 0 | while ((newViewId == null && viewId != null) |
107 | |
|| (newViewId != null && (!newViewId.equals(viewId) || isNotSameRoot ) ) && loops < maxLoops); |
108 | |
|
109 | 0 | if (loops == maxLoops) |
110 | |
{ |
111 | |
|
112 | 0 | boolean production = facesContext.isProjectStage(ProjectStage.Production); |
113 | 0 | Level level = production ? Level.FINE : Level.WARNING; |
114 | 0 | if (log.isLoggable(level)) |
115 | |
{ |
116 | 0 | log.log(level, "Cicle over buildView-PreRenderViewEvent on RENDER_RESPONSE phase " |
117 | |
+ "reaches maximal limit, please check listeners for infinite recursion."); |
118 | |
} |
119 | |
} |
120 | |
|
121 | 0 | viewHandler.renderView(facesContext, root); |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | 0 | List<FacesMessage> messageList = facesContext.getMessageList(); |
128 | 0 | if (!messageList.isEmpty()) |
129 | |
{ |
130 | 0 | StringBuilder builder = new StringBuilder(); |
131 | 0 | boolean shouldLog = false; |
132 | 0 | for (int i = 0, size = messageList.size(); i < size; i++) |
133 | |
{ |
134 | 0 | FacesMessage message = messageList.get(i); |
135 | 0 | if (!message.isRendered()) |
136 | |
{ |
137 | 0 | builder.append("\n- "); |
138 | 0 | builder.append(message.getDetail()); |
139 | |
|
140 | 0 | shouldLog = true; |
141 | |
} |
142 | |
} |
143 | 0 | if (shouldLog) |
144 | |
{ |
145 | 0 | log.log(Level.WARNING, "There are some unhandled FacesMessages, " + |
146 | |
"this means not every FacesMessage had a chance to be rendered.\n" + |
147 | |
"These unhandled FacesMessages are: " + builder.toString()); |
148 | |
} |
149 | |
} |
150 | |
} |
151 | 0 | catch (IOException e) |
152 | |
{ |
153 | 0 | throw new FacesException(e.getMessage(), e); |
154 | 0 | } |
155 | 0 | return false; |
156 | |
} |
157 | |
|
158 | |
public PhaseId getPhase() |
159 | |
{ |
160 | 0 | return PhaseId.RENDER_RESPONSE; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private void forceSessionCreation(FacesContext context) |
178 | |
{ |
179 | 0 | if (context.getExternalContext().getSession(false) == null) |
180 | |
{ |
181 | 0 | if (_myfacesConfig == null) |
182 | |
{ |
183 | 0 | _myfacesConfig = MyfacesConfig.getCurrentInstance(context.getExternalContext()); |
184 | |
} |
185 | 0 | if (_myfacesConfig.isAlwaysForceSessionCreation() |
186 | |
|| (!context.getViewRoot().isTransient() |
187 | |
&& !context.getApplication().getStateManager().isSavingStateInClient(context))) |
188 | |
{ |
189 | 0 | context.getExternalContext().getSession(true); |
190 | |
} |
191 | |
} |
192 | 0 | } |
193 | |
} |