|
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 |
390
| public ServletWebResponse(HttpServletResponse response)
|
|
51 |
| { |
|
52 |
390
| this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch"));
|
|
53 |
| } |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
396
| ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch)
|
|
59 |
| { |
|
60 |
396
| Defense.notNull(response, "response");
|
|
61 |
396
| Defense.notNull(log, "log");
|
|
62 |
| |
|
63 |
396
| _servletResponse = response;
|
|
64 |
396
| _log = log;
|
|
65 |
396
| _tomcatPatch = tomcatPatch;
|
|
66 |
| } |
|
67 |
| |
|
68 |
9
| public OutputStream getOutputStream(ContentType contentType)
|
|
69 |
| { |
|
70 |
9
| Defense.notNull(contentType, "contentType");
|
|
71 |
| |
|
72 |
9
| _servletResponse.setContentType(contentType.getMimeType());
|
|
73 |
| |
|
74 |
9
| try
|
|
75 |
| { |
|
76 |
9
| return _servletResponse.getOutputStream();
|
|
77 |
| } |
|
78 |
| catch (IOException ex) |
|
79 |
| { |
|
80 |
3
| throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
|
|
81 |
| null, ex); |
|
82 |
| } |
|
83 |
| } |
|
84 |
| |
|
85 |
414
| public PrintWriter getPrintWriter(ContentType contentType) throws IOException
|
|
86 |
| { |
|
87 |
414
| Defense.notNull(contentType, "contentType");
|
|
88 |
| |
|
89 |
414
| if (_needsReset)
|
|
90 |
33
| reset();
|
|
91 |
| |
|
92 |
414
| _needsReset = true;
|
|
93 |
| |
|
94 |
414
| if (_printWriterContentType == null || ! _tomcatPatch)
|
|
95 |
| { |
|
96 |
408
| _servletResponse.setContentType(contentType.toString());
|
|
97 |
408
| _printWriterContentType = contentType;
|
|
98 |
| } |
|
99 |
| else |
|
100 |
| { |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
6
| if (!_printWriterContentType.equals(contentType))
|
|
105 |
3
| _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType));
|
|
106 |
| } |
|
107 |
| |
|
108 |
414
| try
|
|
109 |
| { |
|
110 |
414
| return _servletResponse.getWriter();
|
|
111 |
| } |
|
112 |
| catch (IOException ex) |
|
113 |
| { |
|
114 |
3
| throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
|
|
115 |
| null, ex); |
|
116 |
| } |
|
117 |
| } |
|
118 |
| |
|
119 |
558
| public String encodeURL(String url)
|
|
120 |
| { |
|
121 |
558
| return _servletResponse.encodeURL(url);
|
|
122 |
| } |
|
123 |
| |
|
124 |
36
| public void reset()
|
|
125 |
| { |
|
126 |
36
| try
|
|
127 |
| { |
|
128 |
36
| _servletResponse.reset();
|
|
129 |
| } |
|
130 |
| catch (IllegalStateException ex) |
|
131 |
| { |
|
132 |
0
| _log.error(WebMessages.resetFailed(ex), ex);
|
|
133 |
| } |
|
134 |
| } |
|
135 |
| |
|
136 |
3
| public void setContentLength(int length)
|
|
137 |
| { |
|
138 |
3
| _servletResponse.setContentLength(length);
|
|
139 |
| } |
|
140 |
| |
|
141 |
348
| public String getNamespace()
|
|
142 |
| { |
|
143 |
348
| return "";
|
|
144 |
| } |
|
145 |
| |
|
146 |
9
| public void setDateHeader(String name, long date)
|
|
147 |
| { |
|
148 |
9
| _servletResponse.setDateHeader(name, date);
|
|
149 |
| } |
|
150 |
| |
|
151 |
0
| public void setStatus(int status)
|
|
152 |
| { |
|
153 |
0
| _servletResponse.setStatus(status);
|
|
154 |
| } |
|
155 |
| |
|
156 |
3
| public void setHeader(String name, String value)
|
|
157 |
| { |
|
158 |
3
| _servletResponse.setHeader(name, value);
|
|
159 |
| } |
|
160 |
| |
|
161 |
3
| public void setIntHeader(String name, int value)
|
|
162 |
| { |
|
163 |
3
| _servletResponse.setIntHeader(name, value);
|
|
164 |
| } |
|
165 |
| |
|
166 |
3
| public void sendError(int statusCode, String message) throws IOException
|
|
167 |
| { |
|
168 |
3
| _servletResponse.sendError(statusCode, message);
|
|
169 |
| } |
|
170 |
| |
|
171 |
| } |