1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.web; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.io.OutputStream; |
19 |
| import java.io.PrintWriter; |
20 |
| |
21 |
| import javax.servlet.http.HttpServletResponse; |
22 |
| |
23 |
| import org.apache.commons.logging.Log; |
24 |
| import org.apache.commons.logging.LogFactory; |
25 |
| import org.apache.hivemind.ApplicationRuntimeException; |
26 |
| import org.apache.hivemind.util.Defense; |
27 |
| import org.apache.tapestry.util.ContentType; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class ServletWebResponse implements WebResponse |
37 |
| { |
38 |
| private static final Log DEFAULT_LOG = LogFactory.getLog(ServletWebResponse.class); |
39 |
| |
40 |
| private final Log _log; |
41 |
| |
42 |
| private final boolean _tomcatPatch; |
43 |
| |
44 |
| private final HttpServletResponse _servletResponse; |
45 |
| |
46 |
| private boolean _needsReset; |
47 |
| |
48 |
| private ContentType _printWriterContentType; |
49 |
| |
50 |
260
| public ServletWebResponse(HttpServletResponse response)
|
51 |
| { |
52 |
260
| this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch"));
|
53 |
| } |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
264
| ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch)
|
59 |
| { |
60 |
264
| Defense.notNull(response, "response");
|
61 |
264
| Defense.notNull(log, "log");
|
62 |
| |
63 |
264
| _servletResponse = response;
|
64 |
264
| _log = log;
|
65 |
264
| _tomcatPatch = tomcatPatch;
|
66 |
| } |
67 |
| |
68 |
6
| public OutputStream getOutputStream(ContentType contentType)
|
69 |
| { |
70 |
6
| Defense.notNull(contentType, "contentType");
|
71 |
| |
72 |
6
| _servletResponse.setContentType(contentType.getMimeType());
|
73 |
| |
74 |
6
| try
|
75 |
| { |
76 |
6
| return _servletResponse.getOutputStream();
|
77 |
| } |
78 |
| catch (IOException ex) |
79 |
| { |
80 |
2
| throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
|
81 |
| null, ex); |
82 |
| } |
83 |
| } |
84 |
| |
85 |
276
| public PrintWriter getPrintWriter(ContentType contentType) throws IOException
|
86 |
| { |
87 |
276
| Defense.notNull(contentType, "contentType");
|
88 |
| |
89 |
276
| if (_needsReset)
|
90 |
22
| reset();
|
91 |
| |
92 |
276
| _needsReset = true;
|
93 |
| |
94 |
276
| if (_printWriterContentType == null || ! _tomcatPatch)
|
95 |
| { |
96 |
272
| _servletResponse.setContentType(contentType.toString());
|
97 |
272
| _printWriterContentType = contentType;
|
98 |
| } |
99 |
| else |
100 |
| { |
101 |
| |
102 |
| |
103 |
| |
104 |
4
| if (!_printWriterContentType.equals(contentType))
|
105 |
2
| _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType));
|
106 |
| } |
107 |
| |
108 |
276
| try
|
109 |
| { |
110 |
276
| return _servletResponse.getWriter();
|
111 |
| } |
112 |
| catch (IOException ex) |
113 |
| { |
114 |
2
| throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
|
115 |
| null, ex); |
116 |
| } |
117 |
| } |
118 |
| |
119 |
372
| public String encodeURL(String url)
|
120 |
| { |
121 |
372
| return _servletResponse.encodeURL(url);
|
122 |
| } |
123 |
| |
124 |
24
| public void reset()
|
125 |
| { |
126 |
24
| try
|
127 |
| { |
128 |
24
| _servletResponse.reset();
|
129 |
| } |
130 |
| catch (IllegalStateException ex) |
131 |
| { |
132 |
0
| _log.error(WebMessages.resetFailed(ex), ex);
|
133 |
| } |
134 |
| } |
135 |
| |
136 |
2
| public void setContentLength(int length)
|
137 |
| { |
138 |
2
| _servletResponse.setContentLength(length);
|
139 |
| } |
140 |
| |
141 |
232
| public String getNamespace()
|
142 |
| { |
143 |
232
| return "";
|
144 |
| } |
145 |
| |
146 |
6
| public void setDateHeader(String name, long date)
|
147 |
| { |
148 |
6
| _servletResponse.setDateHeader(name, date);
|
149 |
| } |
150 |
| |
151 |
0
| public void setStatus(int status)
|
152 |
| { |
153 |
0
| _servletResponse.setStatus(status);
|
154 |
| } |
155 |
| |
156 |
2
| public void setHeader(String name, String value)
|
157 |
| { |
158 |
2
| _servletResponse.setHeader(name, value);
|
159 |
| } |
160 |
| |
161 |
2
| public void setIntHeader(String name, int value)
|
162 |
| { |
163 |
2
| _servletResponse.setIntHeader(name, value);
|
164 |
| } |
165 |
| |
166 |
2
| public void sendError(int statusCode, String message) throws IOException
|
167 |
| { |
168 |
2
| _servletResponse.sendError(statusCode, message);
|
169 |
| } |
170 |
| |
171 |
| } |