1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.portlet; |
16 | |
|
17 | |
import java.io.CharArrayWriter; |
18 | |
import java.io.IOException; |
19 | |
import java.io.PrintWriter; |
20 | |
|
21 | |
import javax.portlet.ActionResponse; |
22 | |
|
23 | |
import org.apache.hivemind.ApplicationRuntimeException; |
24 | |
import org.apache.tapestry.IMarkupWriter; |
25 | |
import org.apache.tapestry.IRequestCycle; |
26 | |
import org.apache.tapestry.describe.RenderStrategy; |
27 | |
import org.apache.tapestry.error.ErrorMessages; |
28 | |
import org.apache.tapestry.error.ExceptionPresenter; |
29 | |
import org.apache.tapestry.error.RequestExceptionReporter; |
30 | |
import org.apache.tapestry.markup.MarkupWriterSource; |
31 | |
import org.apache.tapestry.services.ServiceConstants; |
32 | |
import org.apache.tapestry.util.ContentType; |
33 | |
import org.apache.tapestry.util.exception.ExceptionAnalyzer; |
34 | |
import org.apache.tapestry.util.exception.ExceptionDescription; |
35 | |
import org.apache.tapestry.util.exception.ExceptionProperty; |
36 | |
import org.apache.tapestry.web.WebRequest; |
37 | |
import org.apache.tapestry.web.WebResponse; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class PortletExceptionPresenter implements ExceptionPresenter |
53 | |
{ |
54 | |
|
55 | |
private PortletRequestGlobals _globals; |
56 | |
|
57 | |
private RenderStrategy _renderStrategy; |
58 | |
|
59 | |
private WebRequest _request; |
60 | |
|
61 | |
private RequestExceptionReporter _requestExceptionReporter; |
62 | |
|
63 | |
private WebResponse _response; |
64 | |
|
65 | |
private MarkupWriterSource _markupWriterSource; |
66 | |
|
67 | |
public void presentException(IRequestCycle cycle, Throwable cause) |
68 | |
{ |
69 | |
try |
70 | |
{ |
71 | 0 | if (_globals.isRenderRequest()) |
72 | 0 | reportRenderRequestException(cycle, cause); |
73 | 0 | else reportActionRequestException(cycle, cause); |
74 | |
} |
75 | 0 | catch (Exception ex) |
76 | |
{ |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | _requestExceptionReporter.reportRequestException(PortletMessages |
84 | |
.errorReportingException(ex), ex); |
85 | |
|
86 | |
|
87 | |
|
88 | 0 | throw new ApplicationRuntimeException(ex.getMessage(), ex); |
89 | 0 | } |
90 | |
|
91 | 0 | _requestExceptionReporter.reportRequestException(ErrorMessages |
92 | |
.unableToProcessClientRequest(cause), cause); |
93 | 0 | } |
94 | |
|
95 | |
private void reportActionRequestException(IRequestCycle cycle, |
96 | |
Throwable cause) |
97 | |
{ |
98 | 0 | CharArrayWriter caw = new CharArrayWriter(); |
99 | 0 | PrintWriter pw = new PrintWriter(caw); |
100 | |
|
101 | 0 | IMarkupWriter writer = _markupWriterSource.newMarkupWriter(pw, |
102 | |
new ContentType("text/html")); |
103 | |
|
104 | 0 | writeException(writer, cycle, cause); |
105 | |
|
106 | 0 | writer.close(); |
107 | |
|
108 | 0 | String markup = caw.toString(); |
109 | |
|
110 | 0 | _request.getSession(true).setAttribute( |
111 | |
PortletConstants.PORTLET_EXCEPTION_MARKUP_ATTRIBUTE, markup); |
112 | |
|
113 | 0 | ActionResponse response = _globals.getActionResponse(); |
114 | |
|
115 | 0 | response.setRenderParameter(ServiceConstants.SERVICE, |
116 | |
PortletConstants.EXCEPTION_SERVICE); |
117 | 0 | } |
118 | |
|
119 | |
private void reportRenderRequestException(IRequestCycle cycle, |
120 | |
Throwable cause) |
121 | |
throws IOException |
122 | |
{ |
123 | 0 | PrintWriter pw = _response.getPrintWriter(new ContentType("text/html")); |
124 | |
|
125 | 0 | IMarkupWriter writer = _markupWriterSource.newMarkupWriter(pw, |
126 | |
new ContentType("text/html")); |
127 | |
|
128 | 0 | writeException(writer, cycle, cause); |
129 | 0 | } |
130 | |
|
131 | |
public void setGlobals(PortletRequestGlobals globals) |
132 | |
{ |
133 | 0 | _globals = globals; |
134 | 0 | } |
135 | |
|
136 | |
public void setRenderStrategy(RenderStrategy renderStrategy) |
137 | |
{ |
138 | 0 | _renderStrategy = renderStrategy; |
139 | 0 | } |
140 | |
|
141 | |
public void setRequest(WebRequest request) |
142 | |
{ |
143 | 0 | _request = request; |
144 | 0 | } |
145 | |
|
146 | |
public void setRequestExceptionReporter( |
147 | |
RequestExceptionReporter requestExceptionReporter) |
148 | |
{ |
149 | 0 | _requestExceptionReporter = requestExceptionReporter; |
150 | 0 | } |
151 | |
|
152 | |
public void setResponse(WebResponse response) |
153 | |
{ |
154 | 0 | _response = response; |
155 | 0 | } |
156 | |
|
157 | |
public void setMarkupWriterSource(MarkupWriterSource markupWriterSource) |
158 | |
{ |
159 | 0 | _markupWriterSource = markupWriterSource; |
160 | 0 | } |
161 | |
|
162 | |
private void writeException(IMarkupWriter writer, IRequestCycle cycle, |
163 | |
ExceptionDescription exception, boolean showStackTrace) |
164 | |
{ |
165 | 0 | writer.begin("div"); |
166 | 0 | writer.attribute("class", "portlet-section-header"); |
167 | 0 | writer.print(exception.getExceptionClassName()); |
168 | 0 | writer.end(); |
169 | 0 | writer.println(); |
170 | |
|
171 | 0 | writer.begin("div"); |
172 | 0 | writer.attribute("class", "portlet-msg-error"); |
173 | 0 | writer.print(exception.getMessage()); |
174 | 0 | writer.end(); |
175 | 0 | writer.println(); |
176 | |
|
177 | 0 | ExceptionProperty[] properties = exception.getProperties(); |
178 | |
|
179 | 0 | if (properties.length > 0) |
180 | |
{ |
181 | |
|
182 | 0 | writer.begin("table"); |
183 | 0 | writer.attribute("class", "portlet-section-subheader"); |
184 | |
|
185 | 0 | for(int i = 0; i < properties.length; i++) |
186 | |
{ |
187 | 0 | writer.begin("tr"); |
188 | |
|
189 | 0 | writer.attribute("class", i % 2 == 0 ? "portlet-section-body" |
190 | |
: "portlet-section-alternate"); |
191 | |
|
192 | 0 | writer.begin("th"); |
193 | 0 | writer.print(properties[i].getName()); |
194 | 0 | writer.end(); |
195 | 0 | writer.println(); |
196 | |
|
197 | 0 | writer.begin("td"); |
198 | |
|
199 | 0 | _renderStrategy.renderObject(properties[i].getValue(), writer, |
200 | |
cycle); |
201 | 0 | writer.end("tr"); |
202 | 0 | writer.println(); |
203 | |
} |
204 | |
|
205 | 0 | writer.end(); |
206 | 0 | writer.println(); |
207 | |
} |
208 | |
|
209 | 0 | if (!showStackTrace) return; |
210 | |
|
211 | 0 | writer.begin("ul"); |
212 | |
|
213 | 0 | String[] trace = exception.getStackTrace(); |
214 | |
|
215 | 0 | for(int i = 0; i < trace.length; i++) |
216 | |
{ |
217 | 0 | writer.begin("li"); |
218 | 0 | writer.print(trace[i]); |
219 | 0 | writer.end(); |
220 | 0 | writer.println(); |
221 | |
} |
222 | |
|
223 | 0 | writer.end(); |
224 | 0 | writer.println(); |
225 | |
|
226 | 0 | } |
227 | |
|
228 | |
private void writeException(IMarkupWriter writer, IRequestCycle cycle, |
229 | |
Throwable cause) |
230 | |
{ |
231 | 0 | ExceptionDescription[] exceptions = new ExceptionAnalyzer() |
232 | |
.analyze(cause); |
233 | |
|
234 | 0 | for(int i = 0; i < exceptions.length; i++) |
235 | 0 | writeException(writer, cycle, exceptions[i], |
236 | |
i + 1 == exceptions.length); |
237 | 0 | } |
238 | |
|
239 | |
} |