View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @JSFRenderer
44   *   renderKitId = "HTML_BASIC" 
45   *   family = "javax.faces.Output"
46   *   type = "org.apache.myfaces.Stylesheet"
47   * 
48   * @author mwessendorf (latest modification by $Author: skitching $)
49   * @version $Revision: 671131 $ $Date: 2008-06-24 06:19:43 -0500 (mar, 24 jun 2008) $
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              //include as inline css
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                  // Load, filter and cache the resource. Then return the cached data.
96                  ResourceInfo info = TextResourceFilter.getInstance(context).getOrCreateFilteredResource(context, path); 
97                  text = info.getText();
98              }
99              else
100             {
101                 // Just load the data (not cached)
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             //refere as link-element
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                 // Load, filter and cache the resource
130                 TextResourceFilter.getInstance(context).getOrCreateFilteredResource(context, path);
131                 
132                 //Check the current setup has ExtensionsFilter or TomahawkFacesContextWrapper
133                 //Check the current setup has ExtensionsFilter or TomahawkFacesContextWrapper
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                     //if it is a portlet request and no TomahawkFacesContextWrapper set or
143                     //is servlet and no ExtensionsFilter/TomahawkFacesContextWrapper set
144                     //log a warning, because something is not right
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                 // Compute a URL that goes via the tomahawk ExtensionsFilter and the
161                 // TextResourceFilterProvider to fetch the resource from the cache.
162                 //
163                 // Unfortunately the getResourceUri(context, Class, String, bool) api below is
164                 // really meant for serving resources out of the Tomahawk jarfile, relative to
165                 // some class that the resource belongs to. So it only expects to receive
166                 // relative paths. We are abusing it here to serve resources out of the
167                 // webapp, specified by an absolute path. So here, the leading slash is
168                 // stripped off and in the TextResourceFilterProvider a matching hack
169                 // puts it back on again. A better solution would be to write a custom
170                 // ResourceHandler class and pass that to the getResourceUri method...
171                 // TODO: fixme
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 }