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  
20  package org.apache.myfaces.tobago.renderkit;
21  
22  import org.apache.myfaces.tobago.application.ProjectStage;
23  import org.apache.myfaces.tobago.config.TobagoConfig;
24  import org.apache.myfaces.tobago.internal.webapp.DebugResponseWriterWrapper;
25  import org.apache.myfaces.tobago.internal.webapp.HtmlResponseWriter;
26  import org.apache.myfaces.tobago.internal.webapp.XmlResponseWriter;
27  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import javax.faces.FactoryFinder;
32  import javax.faces.context.FacesContext;
33  import javax.faces.context.ResponseStream;
34  import javax.faces.context.ResponseWriter;
35  import javax.faces.render.ClientBehaviorRenderer;
36  import javax.faces.render.RenderKit;
37  import javax.faces.render.RenderKitFactory;
38  import javax.faces.render.Renderer;
39  import javax.faces.render.ResponseStateManager;
40  import java.io.OutputStream;
41  import java.io.Writer;
42  import java.util.HashMap;
43  import java.util.Iterator;
44  import java.util.Map;
45  
46  public class TobagoRenderKit extends RenderKit {
47  
48    private static final Logger LOG = LoggerFactory.getLogger(TobagoRenderKit.class);
49  
50    private final RenderKit htmlBasicRenderKit;
51  
52    private Map<Key, Renderer> renderers = new HashMap<Key, Renderer>();
53  
54    public TobagoRenderKit() {
55      RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
56      htmlBasicRenderKit =
57          rkFactory.getRenderKit(FacesContext.getCurrentInstance(), RenderKitFactory.HTML_BASIC_RENDER_KIT);
58      LOG.error("Creating TobagoRenderKit with base: {}", htmlBasicRenderKit);
59      if (LOG.isDebugEnabled()) {
60        LOG.debug("Creating TobagoRenderKit");
61      }
62    }
63  
64    @Override
65    public Renderer getRenderer(final String family, final String rendererType) {
66      Renderer renderer = renderers.get(new Key(family, rendererType));
67      if (renderer == null) {
68        renderer = htmlBasicRenderKit.getRenderer(family, rendererType);
69      }
70  
71      if (renderer == null) {
72        LOG.error("The class which was found by the ResourceManager cannot be "
73            + "found or instantiated: classname='" + rendererType + "'");
74      }
75  
76      return renderer;
77    }
78  
79    @Override
80    public ResponseWriter createResponseWriter(
81        final Writer writer, final String contentTypeList, final String characterEncoding) {
82      String contentType;
83      final FacesContext facesContext = FacesContext.getCurrentInstance();
84      if (facesContext.getPartialViewContext().isAjaxRequest()) {
85        contentType = "text/xml";
86      } else if (contentTypeList == null) {
87        contentType = "text/html";
88      } else {
89        contentType = "text/html";
90        LOG.warn("Content-Type '{}' not supported! Using ''{}", contentTypeList, contentType);
91      }
92  
93  // XXX enable xhtml here, by hand:
94  //    contentType = "application/xhtml+xml";
95  
96      boolean xml = false;
97      if ("application/xhtml+xml".equals(contentType)
98          || "application/xhtml".equals(contentType)
99          || "application/xml".equals(contentType)
100         || "text/xml".equals(contentType)) {
101       xml = true;
102     }
103 
104     TobagoResponseWriter responseWriter;
105     if (xml) {
106       responseWriter = new XmlResponseWriter(writer, contentType, characterEncoding);
107     } else {
108       responseWriter = new HtmlResponseWriter(writer, contentType, characterEncoding);
109     }
110     if (TobagoConfig.getInstance(facesContext).getProjectStage() == ProjectStage.Development) {
111       responseWriter = new DebugResponseWriterWrapper(responseWriter);
112     }
113     return responseWriter;
114   }
115 
116   @Override
117   public void addRenderer(final String family, final String rendererType, final Renderer renderer) {
118     renderers.put(new Key(family, rendererType), renderer);
119   }
120 
121   @Override
122   public ResponseStateManager getResponseStateManager() {
123     return htmlBasicRenderKit.getResponseStateManager();
124   }
125 
126   @Override
127   public ResponseStream createResponseStream(final OutputStream outputStream) {
128     return htmlBasicRenderKit.createResponseStream(outputStream);
129   }
130 
131   @Override
132   public void addClientBehaviorRenderer(String type, ClientBehaviorRenderer renderer) {
133     htmlBasicRenderKit.addClientBehaviorRenderer(type, renderer);
134   }
135 
136   @Override
137   public ClientBehaviorRenderer getClientBehaviorRenderer(String type) {
138     return htmlBasicRenderKit.getClientBehaviorRenderer(type);
139   }
140 
141   @Override
142   public Iterator<String> getClientBehaviorRendererTypes() {
143     return htmlBasicRenderKit.getClientBehaviorRendererTypes();
144   }
145 
146   private static final class Key {
147     private final String family;
148     private final String rendererType;
149 
150     private Key(final String family, final String rendererType) {
151       this.family = family;
152       this.rendererType = rendererType;
153     }
154 
155     public boolean equals(final Object o) {
156       if (this == o) {
157         return true;
158       }
159       if (o == null || getClass() != o.getClass()) {
160         return false;
161       }
162 
163       final Key key = (Key) o;
164 
165       if (!family.equals(key.family)) {
166         return false;
167       }
168       if (!rendererType.equals(key.rendererType)) {
169         return false;
170       }
171 
172       return true;
173     }
174 
175     public int hashCode() {
176       int result;
177       result = family.hashCode();
178       result = 31 * result + rendererType.hashCode();
179       return result;
180     }
181   }
182 }