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 | 0 | 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 | 0 | this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch")); |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch) |
65 | 0 | { |
66 | 0 | Defense.notNull(response, "response"); |
67 | 0 | Defense.notNull(log, "log"); |
68 | |
|
69 | 0 | _servletResponse = response; |
70 | 0 | _log = log; |
71 | 0 | _tomcatPatch = tomcatPatch; |
72 | 0 | } |
73 | |
|
74 | |
public OutputStream getOutputStream(ContentType contentType) |
75 | |
{ |
76 | 0 | Defense.notNull(contentType, "contentType"); |
77 | |
|
78 | 0 | _servletResponse.setContentType(contentType.getMimeType()); |
79 | |
|
80 | |
try |
81 | |
{ |
82 | 0 | return _servletResponse.getOutputStream(); |
83 | |
} |
84 | 0 | catch (IOException ex) |
85 | |
{ |
86 | 0 | throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex), |
87 | |
null, ex); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
public PrintWriter getPrintWriter(ContentType contentType) throws IOException |
92 | |
{ |
93 | 0 | Defense.notNull(contentType, "contentType"); |
94 | |
|
95 | 0 | if (_needsReset) |
96 | 0 | reset(); |
97 | |
|
98 | 0 | _needsReset = true; |
99 | |
|
100 | 0 | if (_printWriterContentType == null || ! _tomcatPatch) |
101 | |
{ |
102 | 0 | _servletResponse.setContentType(contentType.toString()); |
103 | 0 | _printWriterContentType = contentType; |
104 | |
} |
105 | |
else |
106 | |
{ |
107 | |
|
108 | |
|
109 | |
|
110 | 0 | if (!_printWriterContentType.equals(contentType)) |
111 | 0 | _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType)); |
112 | |
} |
113 | |
|
114 | |
try |
115 | |
{ |
116 | 0 | return _servletResponse.getWriter(); |
117 | |
} |
118 | 0 | catch (IOException ex) |
119 | |
{ |
120 | 0 | 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 | 0 | _servletResponse.reset(); |
135 | |
} |
136 | 0 | catch (IllegalStateException ex) |
137 | |
{ |
138 | 0 | _log.error(WebMessages.resetFailed(ex), ex); |
139 | 0 | } |
140 | 0 | } |
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 | 0 | _servletResponse.setDateHeader(name, date); |
155 | 0 | } |
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 | 0 | _servletResponse.setHeader(name, value); |
165 | 0 | } |
166 | |
|
167 | |
public void setIntHeader(String name, int value) |
168 | |
{ |
169 | 0 | _servletResponse.setIntHeader(name, value); |
170 | 0 | } |
171 | |
|
172 | |
public void sendError(int statusCode, String message) throws IOException |
173 | |
{ |
174 | 0 | _servletResponse.sendError(statusCode, message); |
175 | 0 | } |
176 | |
|
177 | |
} |