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 LOG = LogFactory.getLog(ServletWebResponse.class); |
39 |
| |
40 |
| private final HttpServletResponse _servletResponse; |
41 |
| |
42 |
| private boolean _needsReset; |
43 |
| |
44 |
134
| public ServletWebResponse(HttpServletResponse response)
|
45 |
| { |
46 |
134
| Defense.notNull(response, "response");
|
47 |
| |
48 |
134
| _servletResponse = response;
|
49 |
| } |
50 |
| |
51 |
3
| public OutputStream getOutputStream(ContentType contentType)
|
52 |
| { |
53 |
3
| Defense.notNull(contentType, "contentType");
|
54 |
| |
55 |
3
| _servletResponse.setContentType(contentType.getMimeType());
|
56 |
| |
57 |
3
| try
|
58 |
| { |
59 |
3
| return _servletResponse.getOutputStream();
|
60 |
| } |
61 |
| catch (IOException ex) |
62 |
| { |
63 |
1
| throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
|
64 |
| null, ex); |
65 |
| } |
66 |
| } |
67 |
| |
68 |
140
| public PrintWriter getPrintWriter(ContentType contentType) throws IOException
|
69 |
| { |
70 |
140
| Defense.notNull(contentType, "contentType");
|
71 |
| |
72 |
140
| if (_needsReset)
|
73 |
11
| reset();
|
74 |
| |
75 |
140
| _needsReset = true;
|
76 |
| |
77 |
140
| _servletResponse.setContentType(contentType.toString());
|
78 |
| |
79 |
140
| try
|
80 |
| { |
81 |
140
| return _servletResponse.getWriter();
|
82 |
| } |
83 |
| catch (IOException ex) |
84 |
| { |
85 |
1
| throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
|
86 |
| null, ex); |
87 |
| } |
88 |
| } |
89 |
| |
90 |
120
| public String encodeURL(String url)
|
91 |
| { |
92 |
120
| return _servletResponse.encodeURL(url);
|
93 |
| } |
94 |
| |
95 |
12
| public void reset()
|
96 |
| { |
97 |
12
| try
|
98 |
| { |
99 |
12
| _servletResponse.reset();
|
100 |
| } |
101 |
| catch (IllegalStateException ex) |
102 |
| { |
103 |
0
| LOG.error(WebMessages.resetFailed(ex), ex);
|
104 |
| } |
105 |
| } |
106 |
| |
107 |
1
| public void setContentLength(int length)
|
108 |
| { |
109 |
1
| _servletResponse.setContentLength(length);
|
110 |
| } |
111 |
| |
112 |
119
| public String getNamespace()
|
113 |
| { |
114 |
119
| return "";
|
115 |
| } |
116 |
| |
117 |
3
| public void setDateHeader(String name, long date)
|
118 |
| { |
119 |
3
| _servletResponse.setDateHeader(name, date);
|
120 |
| } |
121 |
| |
122 |
0
| public void setStatus(int status)
|
123 |
| { |
124 |
0
| _servletResponse.setStatus(status);
|
125 |
| } |
126 |
| |
127 |
1
| public void setHeader(String name, String value)
|
128 |
| { |
129 |
1
| _servletResponse.setHeader(name, value);
|
130 |
| } |
131 |
| |
132 |
1
| public void setIntHeader(String name, int value)
|
133 |
| { |
134 |
1
| _servletResponse.setIntHeader(name, value);
|
135 |
| } |
136 |
| |
137 |
1
| public void sendError(int statusCode, String message) throws IOException
|
138 |
| { |
139 |
1
| _servletResponse.sendError(statusCode, message);
|
140 |
| } |
141 |
| |
142 |
| } |