1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.jsp; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Locale; |
23 | |
import java.util.logging.Level; |
24 | |
import java.util.logging.Logger; |
25 | |
|
26 | |
import javax.faces.FacesException; |
27 | |
import javax.faces.component.UIViewRoot; |
28 | |
import javax.faces.context.ExternalContext; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
import javax.faces.event.PostAddToViewEvent; |
31 | |
import javax.faces.render.ResponseStateManager; |
32 | |
import javax.servlet.ServletRequest; |
33 | |
import javax.servlet.ServletResponse; |
34 | |
import javax.servlet.http.HttpServletResponse; |
35 | |
import javax.servlet.jsp.jstl.core.Config; |
36 | |
|
37 | |
import org.apache.myfaces.application.jsp.ServletViewResponseWrapper; |
38 | |
import org.apache.myfaces.application.viewstate.StateCacheUtils; |
39 | |
import org.apache.myfaces.shared.view.JspViewDeclarationLanguageBase; |
40 | |
import org.apache.myfaces.view.facelets.tag.composite.CompositeLibrary; |
41 | |
import org.apache.myfaces.view.facelets.tag.jsf.core.CoreLibrary; |
42 | |
import org.apache.myfaces.view.facelets.tag.jsf.html.HtmlLibrary; |
43 | |
import org.apache.myfaces.view.facelets.tag.ui.UILibrary; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class JspViewDeclarationLanguage extends JspViewDeclarationLanguageBase |
52 | |
{ |
53 | |
|
54 | 0 | public static final Logger log = Logger.getLogger(JspViewDeclarationLanguage.class.getName()); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | public static final String[] FACELETS_ONLY_F_TAGS = {"ajax", "event", "metadata"}; |
62 | 0 | public static final String[] FACELETS_ONLY_H_TAGS = {"outputScript", "outputStylesheet", |
63 | |
"head", "body", "button", "link"}; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public JspViewDeclarationLanguage() |
69 | 0 | { |
70 | 0 | if (log.isLoggable(Level.FINEST)) |
71 | |
{ |
72 | 0 | log.finest("New JspViewDeclarationLanguage instance created"); |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
@Override |
80 | |
public void buildView(FacesContext context, UIViewRoot view) throws IOException |
81 | |
{ |
82 | |
|
83 | 0 | super.buildView(context, view); |
84 | |
|
85 | 0 | ExternalContext externalContext = context.getExternalContext(); |
86 | 0 | ServletResponse response = (ServletResponse) externalContext.getResponse(); |
87 | 0 | ServletRequest request = (ServletRequest) externalContext.getRequest(); |
88 | |
|
89 | 0 | Locale locale = view.getLocale(); |
90 | 0 | response.setLocale(locale); |
91 | 0 | Config.set(request, Config.FMT_LOCALE, context.getViewRoot().getLocale()); |
92 | |
|
93 | 0 | String viewId = view.getViewId(); |
94 | 0 | ServletViewResponseWrapper wrappedResponse = new ServletViewResponseWrapper((HttpServletResponse) response); |
95 | |
|
96 | 0 | externalContext.setResponse(wrappedResponse); |
97 | |
try |
98 | |
{ |
99 | 0 | externalContext.dispatch(viewId); |
100 | |
} |
101 | 0 | catch (FacesException e) |
102 | |
{ |
103 | |
|
104 | |
|
105 | |
|
106 | 0 | String message = e.getMessage(); |
107 | |
|
108 | |
|
109 | 0 | if (message != null) |
110 | |
{ |
111 | |
|
112 | |
|
113 | 0 | if (message.contains("\"f\"") |
114 | |
|| message.contains("\"" + CoreLibrary.NAMESPACE + "\"")) |
115 | |
{ |
116 | |
|
117 | 0 | for (String tag : FACELETS_ONLY_F_TAGS) |
118 | |
{ |
119 | 0 | if (message.contains("\"" + tag + "\"")) |
120 | |
{ |
121 | 0 | String exceptionMessage = "The tag f:" + tag + |
122 | |
" is only available on facelets."; |
123 | 0 | throw new FacesException(exceptionMessage, |
124 | |
new FaceletsOnlyException(exceptionMessage, e.getCause())); |
125 | |
} |
126 | |
} |
127 | |
} |
128 | 0 | else if (message.contains("\"h\"") |
129 | |
|| message.contains("\"" + HtmlLibrary.NAMESPACE + "\"")) |
130 | |
{ |
131 | |
|
132 | 0 | for (String tag : FACELETS_ONLY_H_TAGS) |
133 | |
{ |
134 | 0 | if (message.contains("\"" + tag + "\"")) |
135 | |
{ |
136 | 0 | String exceptionMessage = "The tag h:" + tag + |
137 | |
" is only available on facelets."; |
138 | 0 | throw new FacesException(exceptionMessage, |
139 | |
new FaceletsOnlyException(exceptionMessage, e.getCause())); |
140 | |
} |
141 | |
} |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | |
|
146 | 0 | String namespace = null; |
147 | 0 | if (message.contains(UILibrary.NAMESPACE)) |
148 | |
{ |
149 | 0 | namespace = UILibrary.NAMESPACE; |
150 | |
} |
151 | 0 | else if (message.contains(CompositeLibrary.NAMESPACE)) |
152 | |
{ |
153 | 0 | namespace = CompositeLibrary.NAMESPACE; |
154 | |
} |
155 | |
|
156 | 0 | if (namespace != null) |
157 | |
{ |
158 | |
|
159 | 0 | String exceptionMessage = "All tags with namespace " + |
160 | |
namespace + " are only available on facelets."; |
161 | 0 | throw new FacesException(exceptionMessage, |
162 | |
new FaceletsOnlyException(exceptionMessage, e.getCause())); |
163 | |
} |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | 0 | throw e; |
169 | |
} |
170 | |
finally |
171 | |
{ |
172 | 0 | externalContext.setResponse(response); |
173 | 0 | } |
174 | |
|
175 | 0 | boolean errorResponse = wrappedResponse.getStatus() < 200 || wrappedResponse.getStatus() > 299; |
176 | 0 | if (errorResponse) |
177 | |
{ |
178 | 0 | wrappedResponse.flushToWrappedResponse(); |
179 | 0 | return; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | 0 | if (!context.getPartialViewContext().isPartialRequest()) |
187 | |
{ |
188 | |
|
189 | 0 | setAfterViewTagResponseWrapper(externalContext, wrappedResponse); |
190 | |
} |
191 | |
|
192 | |
|
193 | 0 | context.getApplication().publishEvent(context, PostAddToViewEvent.class, UIViewRoot.class, view); |
194 | 0 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
@Override |
200 | |
protected boolean isViewStateAlreadyEncoded(FacesContext context) |
201 | |
{ |
202 | 0 | ResponseStateManager responseStateManager = context.getRenderKit().getResponseStateManager(); |
203 | 0 | if (StateCacheUtils.isMyFacesResponseStateManager(responseStateManager)) |
204 | |
{ |
205 | 0 | if (StateCacheUtils.getMyFacesResponseStateManager(responseStateManager). |
206 | |
isWriteStateAfterRenderViewRequired(context)) |
207 | |
{ |
208 | 0 | return false; |
209 | |
} |
210 | |
else |
211 | |
{ |
212 | 0 | return true; |
213 | |
} |
214 | |
} |
215 | |
else |
216 | |
{ |
217 | 0 | return false; |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
@Override |
222 | |
protected void sendSourceNotFound(FacesContext context, String message) |
223 | |
{ |
224 | 0 | HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); |
225 | |
try |
226 | |
{ |
227 | 0 | context.responseComplete(); |
228 | 0 | response.sendError(HttpServletResponse.SC_NOT_FOUND, message); |
229 | |
} |
230 | 0 | catch (IOException ioe) |
231 | |
{ |
232 | 0 | throw new FacesException(ioe); |
233 | 0 | } |
234 | 0 | } |
235 | |
|
236 | |
} |