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.renderkit.html;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import javax.faces.FacesException;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.context.ResponseWriter;
27  import org.apache.myfaces.shared.renderkit.html.HTML;
28  import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
29  
30  /**
31   * This implementation is just the default html response writer with the early flush logic. The
32   * idea is detect when the end "head" element is rendered and in that moment, when the flush call
33   * is done, force the flush of the current underlying writer.
34   *
35   * @author Leonardo Uribe
36   */
37  public class EarlyFlushHtmlResponseWriterImpl extends HtmlResponseWriterImpl
38  {
39      /**
40       * Check if a end head tag was recently done. The idea is 
41       */
42      private boolean _endHeadTag;
43  
44      public EarlyFlushHtmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding)
45      {
46          super(writer, contentType, characterEncoding);
47      }
48  
49      public EarlyFlushHtmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding, 
50              boolean wrapScriptContentWithXmlCommentTag)
51      {
52          super(writer, contentType, characterEncoding, wrapScriptContentWithXmlCommentTag);
53      }
54  
55      public EarlyFlushHtmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding,
56              boolean wrapScriptContentWithXmlCommentTag, String writerContentTypeMode) throws FacesException
57      {
58          super(writer, contentType, characterEncoding, wrapScriptContentWithXmlCommentTag, writerContentTypeMode);
59      }
60  
61      @Override
62      public ResponseWriter cloneWithWriter(Writer writer)
63      {
64          EarlyFlushHtmlResponseWriterImpl newWriter
65                  = new EarlyFlushHtmlResponseWriterImpl(writer, getContentType(), getCharacterEncoding(), 
66                          getWrapScriptContentWithXmlCommentTag(), getWriterContentTypeMode());
67          return newWriter;        
68      }
69  
70      @Override
71      public void startElement(String name, UIComponent uiComponent) throws IOException
72      {
73          _endHeadTag = false;
74          super.startElement(name, uiComponent);
75      }
76  
77      @Override
78      public void endElement(String name) throws IOException
79      {
80          super.endElement(name);
81          if (HTML.HEAD_ELEM.equalsIgnoreCase(name))
82          {
83              _endHeadTag = true;
84          }
85      }
86      
87      @Override
88      public void flush() throws IOException
89      {
90          super.flush();
91          
92          if (_endHeadTag)
93          {
94              FacesContext facesContext = getFacesContext();
95              if (!facesContext.getPartialViewContext().isAjaxRequest() &&
96                  !facesContext.getPartialViewContext().isPartialRequest())
97              {
98                  forceFlush();
99              }
100         }
101     }
102 }