View Javadoc
1   package org.andromda.cartridges.jsf.portlet.myfaces.tomahawk;
2   
3   import java.io.IOException;
4   import javax.faces.context.ResponseWriter;
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   import org.apache.myfaces.renderkit.html.util.HtmlBufferResponseWriterWrapper;
8   import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
9   import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlResponseWriterImpl;
10  
11  /**
12   * Extends the default MyFaces Tomahawk {DefaultAddResource} class in order to
13   * allow injection of the required content into portlets.
14   *
15   * @author Chad Brandon
16   */
17  public class AddResource
18      extends org.apache.myfaces.renderkit.html.util.DefaultAddResource
19  {
20      /**
21       * Overridden to allow injection of the required content for portlets (i.e. which have no <head/>, <body/>, etc tags).
22       *
23       * @see org.apache.myfaces.renderkit.html.util.DefaultAddResource#writeWithFullHeader(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
24       */
25      @Override
26      public void writeWithFullHeader(HttpServletRequest request, HttpServletResponse response)
27          throws IOException
28      {
29          if (!parserCalled)
30          {
31              throw new IOException("Method parseResponse has to be called first");
32          }
33  
34          final ResponseWriter writer = new HtmlResponseWriterImpl(
35              response.getWriter(),
36              HtmlRendererUtils.selectContentType(request.getHeader("accept")),
37              response.getCharacterEncoding());
38  
39          // - insert anything that was supposed to go into the <head/>
40          headerInsertPosition = 0;
41          final HtmlBufferResponseWriterWrapper headContents = HtmlBufferResponseWriterWrapper
42              .getInstance(writer);
43          for (final PositionedInfo positionedInfo : (Iterable<PositionedInfo>) getHeaderBeginInfos())
44          {
45              headContents.write("\n");
46  
47              if (!(positionedInfo instanceof WritablePositionedInfo))
48              {
49                  throw new IllegalStateException("positionedInfo of type : "
50                          + positionedInfo.getClass().getName());
51              }
52              ((WritablePositionedInfo) positionedInfo).writePositionedInfo(
53                      response,
54                      headContents);
55          }
56          originalResponse.insert(headerInsertPosition, headContents.toString());
57  
58          // TODO: fix this
59          /*StringBuilder buf = new StringBuilder();
60          Set bodyInfos = getBodyOnloadInfos();
61          if (bodyInfos.size() > 0)
62          {
63              int i = 0;
64              for (Iterator it = getBodyOnloadInfos().iterator(); it.hasNext();)
65              {
66                  AttributeInfo positionedInfo = (AttributeInfo) it.next();
67                  if (i == 0)
68                  {
69                      buf.append(positionedInfo.getAttributeName());
70                      buf.append("=\"");
71                  }
72                  buf.append(positionedInfo.getAttributeValue());
73  
74                  i++;
75              }
76  
77              buf.append("\"");
78              originalResponse.insert(bodyInsertPosition - 1, " " + buf.toString());
79          }
80           */
81  
82          // insert all the items that want to go immediately after the <body> tag.
83          final HtmlBufferResponseWriterWrapper afterBodyContents = HtmlBufferResponseWriterWrapper.getInstance(writer);
84  
85          for (final PositionedInfo positionedInfo : (Iterable<PositionedInfo>) getBodyEndInfos())
86          {
87              afterBodyContents.write("\n");
88  
89              if (!(positionedInfo instanceof WritablePositionedInfo))
90              {
91                  throw new IllegalStateException("positionedInfo of type : "
92                          + positionedInfo.getClass().getName());
93              }
94              ((WritablePositionedInfo) positionedInfo).writePositionedInfo(response, afterBodyContents);
95          }
96  
97          originalResponse.insert(headContents.toString().length() + 1, afterBodyContents.toString());
98      }
99  
100     /**
101      * @see org.apache.myfaces.renderkit.html.util.DefaultAddResource#writeResponse(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
102      */
103     public void writeResponse(HttpServletRequest request,
104         HttpServletResponse response) throws IOException
105     {
106         ResponseWriter writer = new HtmlResponseWriterImpl(response.getWriter(), HtmlRendererUtils.selectContentType(null),
107         response.getCharacterEncoding());
108         writer.write(originalResponse.toString());
109     }
110 }