1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.exception;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.core.api.CoreApiServiceLocator;
20 import org.kuali.rice.core.api.exception.KualiException;
21
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28
29
30
31
32
33
34 public class ExceptionIncident implements KualiExceptionIncident {
35 private static final Logger LOG = Logger.getLogger(ExceptionIncident.class);
36 public static final String GENERIC_SYSTEM_ERROR_MESSAGE = "The system has" +
37 " encountered an error and is unable to complete your request at this time."+
38 " Please provide more information regarding this error by completing"+
39 " this Incident Report.";
40
41
42
43
44
45
46
47
48
49
50
51 protected Map<String, String> properties=new HashMap<String, String>();
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public ExceptionIncident(Exception exception,
70 Map<String,String> properties) {
71 if (LOG.isTraceEnabled()) {
72 String message=String.format("ENTRY %s%n%s",
73 (exception==null)?"null":exception.toString(),
74 (properties==null)?"":properties.toString());
75 LOG.trace(message);
76 }
77
78 initialize(exception, properties);
79
80 if (LOG.isTraceEnabled()) {
81 String message=String.format("EXIT %s", this.properties);
82 LOG.trace(message);
83 }
84
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public ExceptionIncident(Map<String, String> inputs) {
104
105 this.properties=inputs;
106
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 private void initialize(Exception thrownException, Map<String, String> inputs) {
123 if (LOG.isTraceEnabled()) {
124 String lm=String.format("ENTRY %s%n%s",
125 thrownException.getMessage(),
126 (inputs==null)?"null":inputs.toString());
127 LOG.trace(lm);
128 }
129
130 properties=new HashMap<String, String>();
131
132 if (inputs != null) {
133 properties.putAll(inputs);
134 }
135
136 properties.putAll(getExceptionInfo(thrownException));
137
138 if (LOG.isTraceEnabled()) {
139 String lm=String.format("EXIT %s", properties.toString());
140 LOG.trace(lm);
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 private Map<String, String> getExceptionInfo(Exception exception) {
158 if (LOG.isTraceEnabled()) {
159 String message=String.format("ENTRY");
160 LOG.trace(message);
161 }
162
163 Map<String, String> map=new HashMap<String, String>();
164 map.put(EXCEPTION_REPORT_SUBJECT, createReportSubject(exception));
165 map.put(EXCEPTION_MESSAGE, exception.getMessage());
166 map.put(DISPLAY_MESSAGE, getDisplayMessage(exception));
167 map.put(STACK_TRACE, getExceptionStackTrace(exception));
168 if(exception instanceof KualiException){
169 boolean hideIncidentReport = ((KualiException) exception).isHideIncidentReport();
170 map.put(EXCEPTION_HIDE_INCIDENT_REPORT, String.valueOf(hideIncidentReport));
171 }else{
172 map.put(EXCEPTION_HIDE_INCIDENT_REPORT, String.valueOf(false));
173 }
174
175 if (LOG.isTraceEnabled()) {
176 String message=String.format("ENTRY %s", map.toString());
177 LOG.trace(message);
178 }
179
180 return map;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 private String createReportSubject(Exception exception) {
199 if (LOG.isTraceEnabled()) {
200 String lm=String.format("ENTRY");
201 LOG.trace(lm);
202 }
203 String app = CoreApiServiceLocator.getKualiConfigurationService().
204 getPropertyValueAsString("application.id");
205 String env= CoreApiServiceLocator.getKualiConfigurationService().
206 getPropertyValueAsString("environment");
207 String format="%s:%s:%s:%s";
208 String componentName=properties.get(COMPONENT_NAME);
209 String subject=String.format(format,
210 app,
211 env,
212 (componentName==null)?"":componentName,
213 exception.getMessage());
214
215 if (LOG.isTraceEnabled()) {
216 String lm=String.format("EXIT %s", subject);
217 LOG.trace(lm);
218 }
219
220 return subject;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 private String createReportMessage() {
248 if (LOG.isTraceEnabled()) {
249 String lm=String.format("ENTRY");
250 LOG.trace(lm);
251 }
252
253 String documentId=properties.get(DOCUMENT_ID);
254 String userEmail=properties.get(USER_EMAIL);
255 String uuid=properties.get(UUID);
256 String description=properties.get(DESCRIPTION);
257 String component=properties.get(COMPONENT_NAME);
258 String exceptionMessage=properties.get(EXCEPTION_MESSAGE);
259 String stackTrace=properties.get(STACK_TRACE);
260 String format="Document Id: %s%n"+
261 "User Email: %s%n"+
262 "Person User Identifier: %s%n"+
263 "User Input: %s%n"+
264 "component: %s%n"+
265 "errorMessage: %s%n"+
266 "%s%n";
267 String message=String.format(format,
268 (documentId==null)?"":documentId,
269 (userEmail==null)?"":userEmail,
270 (uuid==null)?"":uuid,
271 (description==null)?"":description,
272 (component==null)?"":component,
273 (exceptionMessage==null)?"":exceptionMessage,
274 (stackTrace==null)?"":stackTrace);
275
276 if (LOG.isTraceEnabled()) {
277 String lm=String.format("EXIT %s", message);
278 LOG.trace(lm);
279 }
280
281 return message;
282 }
283
284
285
286
287
288
289
290 public String getExceptionStackTrace(Exception thrownException) {
291 if (LOG.isTraceEnabled()) {
292 String lm=String.format("ENTRY");
293 LOG.trace(lm);
294 }
295
296 StringWriter wrt=new StringWriter();
297 PrintWriter pw=new PrintWriter(wrt);
298 thrownException.printStackTrace(pw);
299 pw.flush();
300 String stackTrace=wrt.toString();
301 try {
302 wrt.close();
303 pw.close();
304 } catch (Exception e) {
305 LOG.trace(e.getMessage(), e);
306 }
307
308 if (LOG.isTraceEnabled()) {
309 String lm=String.format("EXIT %s", stackTrace);
310 LOG.trace(lm);
311 }
312
313 return stackTrace;
314 }
315
316
317
318
319
320
321
322 public String getDisplayMessage(Exception exception) {
323 if (LOG.isTraceEnabled()) {
324 String message=String.format("ENTRY %s", exception.getMessage());
325 LOG.trace(message);
326 }
327
328
329 String displayMessage;
330 if (exception instanceof KualiException) {
331 displayMessage=exception.getMessage();
332 } else {
333 displayMessage=GENERIC_SYSTEM_ERROR_MESSAGE;
334 }
335
336 if (LOG.isTraceEnabled()) {
337 String message=String.format("EXIT %s", displayMessage);
338 LOG.trace(message);
339 }
340
341 return displayMessage;
342 }
343
344
345
346
347
348
349
350 public String getProperty(String key) {
351 if (LOG.isTraceEnabled()) {
352 String message=String.format("ENTRY %s", key);
353 LOG.trace(message);
354 }
355
356 String value;
357 if (key.equals(EXCEPTION_REPORT_MESSAGE) && !properties.containsKey(key)) {
358 value=createReportMessage();
359 properties.put(EXCEPTION_REPORT_MESSAGE, value);
360 } else {
361 value=properties.get(key);
362 }
363
364 if (LOG.isTraceEnabled()) {
365 String message=String.format("EXIT %s", value);
366 LOG.trace(message);
367 }
368
369 return value;
370 }
371
372
373
374
375
376
377 public Map<String, String> toProperties() {
378 if (LOG.isTraceEnabled()) {
379 String message=String.format("ENTRY");
380 LOG.trace(message);
381 }
382
383 if (LOG.isTraceEnabled()) {
384 String message=String.format("EXIT %s", properties.toString());
385 LOG.trace(message);
386 }
387
388 return properties;
389 }
390 }