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 public static final Logger log = Logger.getLogger(JspViewDeclarationLanguage.class.getName());
55
56
57
58
59
60
61 public static final String[] FACELETS_ONLY_F_TAGS = {"ajax", "event", "metadata"};
62 public static final String[] FACELETS_ONLY_H_TAGS = {"outputScript", "outputStylesheet",
63 "head", "body", "button", "link"};
64
65
66
67
68 public JspViewDeclarationLanguage()
69 {
70 if (log.isLoggable(Level.FINEST))
71 {
72 log.finest("New JspViewDeclarationLanguage instance created");
73 }
74 }
75
76
77
78
79 @Override
80 public void buildView(FacesContext context, UIViewRoot view) throws IOException
81 {
82
83 super.buildView(context, view);
84
85 ExternalContext externalContext = context.getExternalContext();
86 ServletResponse response = (ServletResponse) externalContext.getResponse();
87 ServletRequest request = (ServletRequest) externalContext.getRequest();
88
89 Locale locale = view.getLocale();
90 response.setLocale(locale);
91 Config.set(request, Config.FMT_LOCALE, context.getViewRoot().getLocale());
92
93 String viewId = view.getViewId();
94 ServletViewResponseWrapper wrappedResponse = new ServletViewResponseWrapper((HttpServletResponse) response);
95
96 externalContext.setResponse(wrappedResponse);
97 try
98 {
99 externalContext.dispatch(viewId);
100 }
101 catch (FacesException e)
102 {
103
104
105
106 String message = e.getMessage();
107
108
109 if (message != null)
110 {
111
112
113 if (message.contains("\"f\"")
114 || message.contains("\"" + CoreLibrary.NAMESPACE + "\""))
115 {
116
117 for (String tag : FACELETS_ONLY_F_TAGS)
118 {
119 if (message.contains("\"" + tag + "\""))
120 {
121 String exceptionMessage = "The tag f:" + tag +
122 " is only available on facelets.";
123 throw new FacesException(exceptionMessage,
124 new FaceletsOnlyException(exceptionMessage, e.getCause()));
125 }
126 }
127 }
128 else if (message.contains("\"h\"")
129 || message.contains("\"" + HtmlLibrary.NAMESPACE + "\""))
130 {
131
132 for (String tag : FACELETS_ONLY_H_TAGS)
133 {
134 if (message.contains("\"" + tag + "\""))
135 {
136 String exceptionMessage = "The tag h:" + tag +
137 " is only available on facelets.";
138 throw new FacesException(exceptionMessage,
139 new FaceletsOnlyException(exceptionMessage, e.getCause()));
140 }
141 }
142 }
143 else
144 {
145
146 String namespace = null;
147 if (message.contains(UILibrary.NAMESPACE))
148 {
149 namespace = UILibrary.NAMESPACE;
150 }
151 else if (message.contains(CompositeLibrary.NAMESPACE))
152 {
153 namespace = CompositeLibrary.NAMESPACE;
154 }
155
156 if (namespace != null)
157 {
158
159 String exceptionMessage = "All tags with namespace " +
160 namespace + " are only available on facelets.";
161 throw new FacesException(exceptionMessage,
162 new FaceletsOnlyException(exceptionMessage, e.getCause()));
163 }
164 }
165 }
166
167
168 throw e;
169 }
170 finally
171 {
172 externalContext.setResponse(response);
173 }
174
175 boolean errorResponse = wrappedResponse.getStatus() < 200 || wrappedResponse.getStatus() > 299;
176 if (errorResponse)
177 {
178 wrappedResponse.flushToWrappedResponse();
179 return;
180 }
181
182
183
184
185
186 if (!context.getPartialViewContext().isPartialRequest())
187 {
188
189 setAfterViewTagResponseWrapper(externalContext, wrappedResponse);
190 }
191
192
193 context.getApplication().publishEvent(context, PostAddToViewEvent.class, UIViewRoot.class, view);
194 }
195
196
197
198
199 @Override
200 protected boolean isViewStateAlreadyEncoded(FacesContext context)
201 {
202 ResponseStateManager responseStateManager = context.getRenderKit().getResponseStateManager();
203 if (StateCacheUtils.isMyFacesResponseStateManager(responseStateManager))
204 {
205 if (StateCacheUtils.getMyFacesResponseStateManager(responseStateManager).
206 isWriteStateAfterRenderViewRequired(context))
207 {
208 return false;
209 }
210 else
211 {
212 return true;
213 }
214 }
215 else
216 {
217 return false;
218 }
219 }
220
221 @Override
222 protected void sendSourceNotFound(FacesContext context, String message)
223 {
224 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
225 try
226 {
227 context.responseComplete();
228 response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
229 }
230 catch (IOException ioe)
231 {
232 throw new FacesException(ioe);
233 }
234 }
235
236 }