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
13
14
15
16
17 public class AddResource
18 extends org.apache.myfaces.renderkit.html.util.DefaultAddResource
19 {
20
21
22
23
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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
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 }