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