1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.renderkit.html.util;
20
21 import java.io.IOException;
22 import javax.faces.FacesWrapper;
23
24 import javax.faces.application.Resource;
25 import javax.faces.application.ResourceHandler;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29
30 import org.apache.myfaces.shared.config.MyfacesConfig;
31 import org.apache.myfaces.shared.renderkit.JSFAttr;
32 import org.apache.myfaces.shared.renderkit.html.HTML;
33 import org.apache.myfaces.shared.resource.ContractResource;
34
35
36
37
38 public class ResourceUtils
39 {
40 @Deprecated
41 public final static String JAVAX_FACES_LIBRARY_NAME = "javax.faces";
42 @Deprecated
43 public final static String JSF_JS_RESOURCE_NAME = "jsf.js";
44
45 public final static String MYFACES_JS_RESOURCE_NAME = "oamSubmit.js";
46 public final static String MYFACES_JS_RESOURCE_NAME_UNCOMPRESSED = "oamSubmit-uncompressed.js";
47 public final static String MYFACES_LIBRARY_NAME = "org.apache.myfaces";
48 private final static String RENDERED_MYFACES_JS = "org.apache.myfaces.RENDERED_MYFACES_JS";
49
50 public final static String JSF_MYFACES_JSFJS_MINIMAL = "minimal";
51 public final static String JSF_MYFACES_JSFJS_MINIMAL_MODERN = "minimal-modern";
52 public final static String JSF_MYFACES_JSFJS_NORMAL = "normal";
53
54 public final static String JSF_UNCOMPRESSED_JS_RESOURCE_NAME = "jsf-uncompressed.js";
55 public final static String JSF_MINIMAL_JS_RESOURCE_NAME = "jsf-minimal.js";
56 public final static String JSF_MINIMAL_MODERN_JS_RESOURCE_NAME = "jsf-minimal-modern.js";
57
58 public final static String JSF_MYFACES_JSFJS_I18N = "jsf-i18n.js";
59 public final static String JSF_MYFACES_JSFJS_EXPERIMENTAL = "jsf-experimental.js";
60 public final static String JSF_MYFACES_JSFJS_LEGACY = "jsf-legacy.js";
61
62 private final static String RENDERED_JSF_JS = "org.apache.myfaces.RENDERED_JSF_JS";
63 public final static String HEAD_TARGET = "head";
64 public final static String BODY_TARGET = "body";
65 public final static String FORM_TARGET = "form";
66
67 public static final String JAVAX_FACES_OUTPUT_COMPONENT_TYPE = "javax.faces.Output";
68 public static final String JAVAX_FACES_TEXT_RENDERER_TYPE = "javax.faces.Text";
69 public static final String DEFAULT_SCRIPT_RENDERER_TYPE = "javax.faces.resource.Script";
70 public static final String DEFAULT_STYLESHEET_RENDERER_TYPE = "javax.faces.resource.Stylesheet";
71
72 public static void markScriptAsRendered(FacesContext facesContext, String libraryName, String resourceName)
73 {
74 facesContext.getApplication().getResourceHandler().markResourceRendered(
75 facesContext, resourceName, libraryName);
76 }
77
78 public static void markStylesheetAsRendered(FacesContext facesContext, String libraryName, String resourceName)
79 {
80 facesContext.getApplication().getResourceHandler().markResourceRendered(
81 facesContext, resourceName, libraryName);
82 }
83
84 public static boolean isRenderedScript(FacesContext facesContext, String libraryName, String resourceName)
85 {
86 return facesContext.getApplication().getResourceHandler().isResourceRendered(
87 facesContext, resourceName, libraryName);
88 }
89
90 public static boolean isRenderedStylesheet(FacesContext facesContext, String libraryName, String resourceName)
91 {
92 return facesContext.getApplication().getResourceHandler().isResourceRendered(
93 facesContext, resourceName, libraryName);
94 }
95
96 public static void writeScriptInline(FacesContext facesContext, ResponseWriter writer, String libraryName,
97 String resourceName) throws IOException
98 {
99 if (!ResourceUtils.isRenderedScript(facesContext, libraryName, resourceName))
100 {
101 if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isRiImplAvailable())
102 {
103
104 UIComponent outputScript = facesContext.getApplication().
105 createComponent(facesContext, JAVAX_FACES_OUTPUT_COMPONENT_TYPE, DEFAULT_SCRIPT_RENDERER_TYPE);
106 outputScript.getAttributes().put(JSFAttr.NAME_ATTR, resourceName);
107 outputScript.getAttributes().put(JSFAttr.LIBRARY_ATTR, libraryName);
108 outputScript.encodeAll(facesContext);
109 }
110 else
111 {
112
113 Resource resource = facesContext.getApplication().getResourceHandler().createResource(
114 resourceName, libraryName);
115 markScriptAsRendered(facesContext, libraryName, resourceName);
116 writer.startElement(HTML.SCRIPT_ELEM, null);
117 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT , null);
118 writer.writeURIAttribute(HTML.SRC_ATTR, resource.getRequestPath(), null);
119 writer.endElement(HTML.SCRIPT_ELEM);
120 }
121 }
122 }
123
124 public static void renderDefaultJsfJsInlineIfNecessary(FacesContext facesContext, ResponseWriter writer)
125 throws IOException
126 {
127 if (facesContext.getAttributes().containsKey(RENDERED_JSF_JS))
128 {
129 return;
130 }
131
132
133
134 if (isRenderedScript(facesContext, ResourceHandler.JSF_SCRIPT_LIBRARY_NAME,
135 ResourceHandler.JSF_SCRIPT_RESOURCE_NAME))
136 {
137 facesContext.getAttributes().put(RENDERED_JSF_JS, Boolean.TRUE);
138 return;
139 }
140
141
142
143 if (facesContext.getPartialViewContext() != null &&
144 (facesContext.getPartialViewContext().isPartialRequest() ||
145 facesContext.getPartialViewContext().isAjaxRequest() )
146 )
147 {
148 return;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isRiImplAvailable())
166 {
167
168 UIComponent outputScript = facesContext.getApplication().
169 createComponent(facesContext, JAVAX_FACES_OUTPUT_COMPONENT_TYPE, DEFAULT_SCRIPT_RENDERER_TYPE);
170 outputScript.getAttributes().put(JSFAttr.NAME_ATTR, ResourceHandler.JSF_SCRIPT_RESOURCE_NAME);
171 outputScript.getAttributes().put(JSFAttr.LIBRARY_ATTR, ResourceHandler.JSF_SCRIPT_LIBRARY_NAME);
172 outputScript.encodeAll(facesContext);
173 }
174 else
175 {
176
177 Resource resource = facesContext.getApplication().getResourceHandler().createResource(
178 ResourceHandler.JSF_SCRIPT_RESOURCE_NAME, ResourceHandler.JSF_SCRIPT_LIBRARY_NAME);
179 markScriptAsRendered(facesContext, ResourceHandler.JSF_SCRIPT_LIBRARY_NAME,
180 ResourceHandler.JSF_SCRIPT_RESOURCE_NAME);
181 writer.startElement(HTML.SCRIPT_ELEM, null);
182 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
183 writer.writeURIAttribute(HTML.SRC_ATTR, resource.getRequestPath(), null);
184 writer.endElement(HTML.SCRIPT_ELEM);
185 }
186
187
188 facesContext.getAttributes().put(RENDERED_JSF_JS, Boolean.TRUE);
189 return;
190 }
191
192 public static void renderMyfacesJSInlineIfNecessary(FacesContext facesContext, ResponseWriter writer)
193 throws IOException
194 {
195 if (facesContext.getAttributes().containsKey(RENDERED_MYFACES_JS))
196 {
197 return;
198 }
199
200
201
202
203
204 if (facesContext.getPartialViewContext() != null &&
205 (facesContext.getPartialViewContext().isPartialRequest() ||
206 facesContext.getPartialViewContext().isAjaxRequest() )
207 )
208 {
209 return;
210 }
211
212
213 if (isRenderedScript(facesContext, MYFACES_LIBRARY_NAME, MYFACES_JS_RESOURCE_NAME))
214 {
215 facesContext.getAttributes().put(RENDERED_MYFACES_JS, Boolean.TRUE);
216 return;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isRiImplAvailable())
233 {
234
235 UIComponent outputScript = facesContext.getApplication().
236 createComponent(facesContext, JAVAX_FACES_OUTPUT_COMPONENT_TYPE, DEFAULT_SCRIPT_RENDERER_TYPE);
237 outputScript.getAttributes().put(JSFAttr.NAME_ATTR, MYFACES_JS_RESOURCE_NAME);
238 outputScript.getAttributes().put(JSFAttr.LIBRARY_ATTR, MYFACES_LIBRARY_NAME);
239 outputScript.encodeAll(facesContext);
240 }
241 else
242 {
243
244 Resource resource = facesContext.getApplication().getResourceHandler().createResource(
245 MYFACES_JS_RESOURCE_NAME, MYFACES_LIBRARY_NAME);
246 markScriptAsRendered(facesContext, MYFACES_LIBRARY_NAME, MYFACES_JS_RESOURCE_NAME);
247 writer.startElement(HTML.SCRIPT_ELEM, null);
248 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
249 writer.writeURIAttribute(HTML.SRC_ATTR, resource.getRequestPath(), null);
250 writer.endElement(HTML.SCRIPT_ELEM);
251 }
252
253
254 facesContext.getAttributes().put(RENDERED_MYFACES_JS, Boolean.TRUE);
255 return;
256 }
257
258 public static String getContractName(Resource resource)
259 {
260 while (resource != null)
261 {
262 if (resource instanceof ContractResource)
263 {
264 return ((ContractResource)resource).getContractName();
265 }
266 else if (resource instanceof FacesWrapper)
267 {
268 resource = (Resource)((FacesWrapper)resource).getWrapped();
269 }
270 else
271 {
272 resource = null;
273 }
274 }
275 return null;
276 }
277 }