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