1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.stylesheet;
20
21 import java.io.IOException;
22
23 import javax.faces.FacesException;
24 import javax.faces.application.ProjectStage;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.myfaces.custom.fileupload.HtmlFileUploadRenderer;
32 import org.apache.myfaces.custom.stylesheet.TextResourceFilter.ResourceInfo;
33 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
34 import org.apache.myfaces.shared_tomahawk.util.WebConfigParamUtils;
35 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
36 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
37 import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
38 import org.apache.myfaces.webapp.filter.ExtensionsFilter;
39 import org.apache.myfaces.webapp.filter.ServeResourcePhaseListener;
40 import org.apache.myfaces.webapp.filter.TomahawkFacesContextFactory;
41
42
43
44
45
46
47
48
49
50
51 public class StylesheetRenderer extends HtmlRenderer
52 {
53 private static final Log log = LogFactory.getLog(StylesheetRenderer.class);
54
55 private static boolean getBooleanInitParameterValue(String initParameter, boolean defaultVal)
56 {
57 if(initParameter == null || initParameter.trim().length()==0)
58 return defaultVal;
59
60 return (initParameter.equalsIgnoreCase("on") || initParameter.equals("1") || initParameter.equalsIgnoreCase("true"));
61 }
62
63 public void encodeEnd(FacesContext context, UIComponent component)
64 throws IOException, FacesException
65 {
66
67 if ((context == null) || (component == null))
68 {
69 throw new NullPointerException();
70 }
71 Stylesheet stylesheet = (Stylesheet) component;
72 ResponseWriter writer = context.getResponseWriter();
73
74 String path = stylesheet.getPath();
75
76 if (stylesheet.isInline())
77 {
78
79
80 if (!path.startsWith("/"))
81 {
82 throw new FacesException("Inline stylesheets require absolute resource path");
83 }
84
85 writer.startElement("style", component);
86 writer.writeAttribute("type", "text/css", null);
87 if (stylesheet.getMedia() != null)
88 {
89 writer.writeAttribute("media", stylesheet.getMedia(), null);
90 }
91
92 Object text;
93 if (stylesheet.isFiltered())
94 {
95
96 ResourceInfo info = TextResourceFilter.getInstance(context).getOrCreateFilteredResource(context, path);
97 text = info.getText();
98 }
99 else
100 {
101
102 text = RendererUtils.loadResourceFile(context, path);
103 }
104 if (text != null)
105 {
106 writer.writeText(text, null);
107 }
108 writer.endElement("style");
109 }
110 else
111 {
112
113 writer.startElement("link", component);
114 writer.writeAttribute("rel", "stylesheet", null);
115 writer.writeAttribute("type", "text/css", null);
116 if (stylesheet.getMedia() != null)
117 {
118 writer.writeAttribute("media", stylesheet.getMedia(), null);
119 }
120
121 String stylesheetPath;
122 if (stylesheet.isFiltered())
123 {
124 if (!path.startsWith("/"))
125 {
126 throw new FacesException("Filtered stylesheets require absolute resource path");
127 }
128
129
130 TextResourceFilter.getInstance(context).getOrCreateFilteredResource(context, path);
131
132
133
134 boolean isDisabledTomahawkFacesContextWrapper = getBooleanInitParameterValue(
135 WebConfigParamUtils.getStringInitParameter(context.getExternalContext(),
136 TomahawkFacesContextFactory.DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER),
137 TomahawkFacesContextFactory.DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER_DEFAULT);
138
139 if (!context.isProjectStage(ProjectStage.Production))
140 {
141 boolean isPortlet = ExternalContextUtils.getRequestType(context.getExternalContext()).isPortlet();
142
143
144
145 boolean extensionsFilterInitialized = context.getExternalContext().getApplicationMap().containsKey(ExtensionsFilter.EXTENSIONS_FILTER_INITIALIZED);
146 if ( (isPortlet && isDisabledTomahawkFacesContextWrapper) ||
147 (isDisabledTomahawkFacesContextWrapper && !extensionsFilterInitialized))
148 {
149 if (log.isWarnEnabled())
150 {
151 log.warn("t:stylesheet with filtered=\"true\" requires ExtensionsFilter or TomahawkFacesContextFactory to handle the css resource, and any of " +
152 "them has been detected. Please read the instructions on http://myfaces.apache.org/tomahawk/extensionsFilter.html " +
153 "for more information about how to setup your environment correctly. ServeResourcePhaseListener could handle the resource request " +
154 "too automatically if you use prefix mapping and org.apache.myfaces.RESOURCE_VIRTUAL_PATH match (Default is /faces/myFacesExtensionResource), " +
155 "so please ignore this warning if you are using it.");
156 }
157 }
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172 String nastyPathHack = path.substring(1);
173 stylesheetPath = AddResourceFactory.getInstance(context).getResourceUri(
174 context,
175 TextResourceFilterProvider.class,
176 nastyPathHack,
177 true);
178 }
179 else
180 {
181 stylesheetPath = context.getApplication().getViewHandler().getResourceURL(context, path);
182 }
183
184 writer.writeURIAttribute("href", stylesheetPath, "path");
185 writer.endElement("link");
186 }
187 }
188 }