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.internal.webapp.DebugResponseWriterWrapper;
23  import org.apache.myfaces.tobago.internal.webapp.HtmlResponseWriter;
24  import org.apache.myfaces.tobago.internal.webapp.XmlResponseWriter;
25  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.faces.FactoryFinder;
30  import javax.faces.application.ProjectStage;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseStream;
33  import javax.faces.context.ResponseWriter;
34  import javax.faces.render.ClientBehaviorRenderer;
35  import javax.faces.render.RenderKit;
36  import javax.faces.render.RenderKitFactory;
37  import javax.faces.render.Renderer;
38  import javax.faces.render.ResponseStateManager;
39  import java.io.OutputStream;
40  import java.io.Writer;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.Map;
44  
45  public class TobagoRenderKit extends RenderKit {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(TobagoRenderKit.class);
48    private static final String CONTENT_TYPE_TEXT_HTML = "text/html";
49    private static final String CONTENT_TYPE_TEXT_XML = "text/xml";
50  
51    private final RenderKit htmlBasicRenderKit;
52  
53    private Map<Key, Renderer> renderers = new HashMap<Key, Renderer>();
54  
55    public TobagoRenderKit() {
56      RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
57      htmlBasicRenderKit =
58          rkFactory.getRenderKit(FacesContext.getCurrentInstance(), RenderKitFactory.HTML_BASIC_RENDER_KIT);
59      if (LOG.isDebugEnabled()) {
60        LOG.debug("Creating TobagoRenderKit with base: {}", htmlBasicRenderKit);
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 contentType, final String characterEncoding) {
82      final FacesContext facesContext = FacesContext.getCurrentInstance();
83      TobagoResponseWriter responseWriter;
84      if (facesContext.getPartialViewContext().isAjaxRequest()) {
85        responseWriter = new XmlResponseWriter(writer, CONTENT_TYPE_TEXT_XML, characterEncoding);
86      } else {
87        if (contentType != null && !contentType.contains(CONTENT_TYPE_TEXT_HTML)) {
88          LOG.warn("Content-Type '{}' not supported! Using '{}'", contentType, CONTENT_TYPE_TEXT_HTML);
89        }
90        responseWriter = new HtmlResponseWriter(writer, CONTENT_TYPE_TEXT_HTML, characterEncoding);
91        // XXX enable xhtml here, by hand:
92        //      responseWriter = new XmlResponseWriter(writer, "application/xhtml+xml", characterEncoding);
93      }
94      if (facesContext.isProjectStage(ProjectStage.Development)) {
95        responseWriter = new DebugResponseWriterWrapper(responseWriter);
96      }
97      return responseWriter;
98    }
99  
100   @Override
101   public void addRenderer(final String family, final String rendererType, final Renderer renderer) {
102     renderers.put(new Key(family, rendererType), renderer);
103   }
104 
105   @Override
106   public ResponseStateManager getResponseStateManager() {
107     return htmlBasicRenderKit.getResponseStateManager();
108   }
109 
110   @Override
111   public ResponseStream createResponseStream(final OutputStream outputStream) {
112     return htmlBasicRenderKit.createResponseStream(outputStream);
113   }
114 
115   @Override
116   public void addClientBehaviorRenderer(String type, ClientBehaviorRenderer renderer) {
117     htmlBasicRenderKit.addClientBehaviorRenderer(type, renderer);
118   }
119 
120   @Override
121   public ClientBehaviorRenderer getClientBehaviorRenderer(String type) {
122     return htmlBasicRenderKit.getClientBehaviorRenderer(type);
123   }
124 
125   @Override
126   public Iterator<String> getClientBehaviorRendererTypes() {
127     return htmlBasicRenderKit.getClientBehaviorRendererTypes();
128   }
129 
130   private static final class Key {
131     private final String family;
132     private final String rendererType;
133 
134     private Key(final String family, final String rendererType) {
135       this.family = family;
136       this.rendererType = rendererType;
137     }
138 
139     public boolean equals(final Object o) {
140       if (this == o) {
141         return true;
142       }
143       if (o == null || getClass() != o.getClass()) {
144         return false;
145       }
146 
147       final Key key = (Key) o;
148 
149       if (!family.equals(key.family)) {
150         return false;
151       }
152       if (!rendererType.equals(key.rendererType)) {
153         return false;
154       }
155 
156       return true;
157     }
158 
159     public int hashCode() {
160       int result;
161       result = family.hashCode();
162       result = 31 * result + rendererType.hashCode();
163       return result;
164     }
165   }
166 }