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 java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.log4j.Logger;
24 import org.kuali.rice.core.api.CoreApiServiceLocator;
25 import org.kuali.rice.core.api.exception.KualiException;
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 public ExceptionIncident(Exception exception,
68 Map<String,String> properties) {
69 if (LOG.isTraceEnabled()) {
70 String message=String.format("ENTRY %s%n%s",
71 (exception==null)?"null":exception.toString(),
72 (properties==null)?"":properties.toString());
73 LOG.trace(message);
74 }
75
76 initialize(exception, properties);
77
78 if (LOG.isTraceEnabled()) {
79 String message=String.format("EXIT %s", this.properties);
80 LOG.trace(message);
81 }
82
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public ExceptionIncident(Map<String, String> inputs) {
102
103 this.properties=inputs;
104
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 private void initialize(Exception thrownException, Map<String, String> inputs) {
121 if (LOG.isTraceEnabled()) {
122 String lm=String.format("ENTRY %s%n%s",
123 thrownException.getMessage(),
124 (inputs==null)?"null":inputs.toString());
125 LOG.trace(lm);
126 }
127
128 properties=new HashMap<String, String>();
129
130 if (inputs != null) {
131 properties.putAll(inputs);
132 }
133
134 properties.putAll(getExceptionInfo(thrownException));
135
136 if (LOG.isTraceEnabled()) {
137 String lm=String.format("EXIT %s", properties.toString());
138 LOG.trace(lm);
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 private Map<String, String> getExceptionInfo(Exception exception) {
156 if (LOG.isTraceEnabled()) {
157 String message=String.format("ENTRY");
158 LOG.trace(message);
159 }
160
161 Map<String, String> map=new HashMap<String, String>();
162 map.put(EXCEPTION_REPORT_SUBJECT, createReportSubject(exception));
163 map.put(EXCEPTION_MESSAGE, exception.getMessage());
164 map.put(DISPLAY_MESSAGE, getDisplayMessage(exception));
165 map.put(STACK_TRACE, getExceptionStackTrace(exception));
166 if(exception instanceof KualiException){
167 boolean hideIncidentReport = ((KualiException) exception).isHideIncidentReport();
168 map.put(EXCEPTION_HIDE_INCIDENT_REPORT, String.valueOf(hideIncidentReport));
169 }else{
170 map.put(EXCEPTION_HIDE_INCIDENT_REPORT, String.valueOf(false));
171 }
172
173 if (LOG.isTraceEnabled()) {
174 String message=String.format("ENTRY %s", map.toString());
175 LOG.trace(message);
176 }
177
178 return map;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 private String createReportSubject(Exception exception) {
197 if (LOG.isTraceEnabled()) {
198 String lm=String.format("ENTRY");
199 LOG.trace(lm);
200 }
201 String app = CoreApiServiceLocator.getKualiConfigurationService().
202 getPropertyValueAsString("application.id");
203 String env= CoreApiServiceLocator.getKualiConfigurationService().
204 getPropertyValueAsString("environment");
205 String format="%s:%s:%s:%s";
206 String componentName=properties.get(COMPONENT_NAME);
207 String subject=String.format(format,
208 app,
209 env,
210 (componentName==null)?"":componentName,
211 exception.getMessage());
212
213 if (LOG.isTraceEnabled()) {
214 String lm=String.format("EXIT %s", subject);
215 LOG.trace(lm);
216 }
217
218 return subject;
219 }
220
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 private String createReportMessage() {
246 if (LOG.isTraceEnabled()) {
247 String lm=String.format("ENTRY");
248 LOG.trace(lm);
249 }
250
251 String documentId=properties.get(DOCUMENT_ID);
252 String userEmail=properties.get(USER_EMAIL);
253 String uuid=properties.get(UUID);
254 String description=properties.get(DESCRIPTION);
255 String component=properties.get(COMPONENT_NAME);
256 String exceptionMessage=properties.get(EXCEPTION_MESSAGE);
257 String stackTrace=properties.get(STACK_TRACE);
258 String format="Document Id: %s%n"+
259 "User Email: %s%n"+
260 "Person User Identifier: %s%n"+
261 "User Input: %s%n"+
262 "component: %s%n"+
263 "errorMessage: %s%n"+
264 "%s%n";
265 String message=String.format(format,
266 (documentId==null)?"":documentId,
267 (userEmail==null)?"":userEmail,
268 (uuid==null)?"":uuid,
269 (description==null)?"":description,
270 (component==null)?"":component,
271 (exceptionMessage==null)?"":exceptionMessage,
272 (stackTrace==null)?"":stackTrace);
273
274 if (LOG.isTraceEnabled()) {
275 String lm=String.format("EXIT %s", message);
276 LOG.trace(lm);
277 }
278
279 return message;
280 }
281
282
283
284
285
286
287
288 public String getExceptionStackTrace(Exception thrownException) {
289 if (LOG.isTraceEnabled()) {
290 String lm=String.format("ENTRY");
291 LOG.trace(lm);
292 }
293
294 StringWriter wrt=new StringWriter();
295 PrintWriter pw=new PrintWriter(wrt);
296 thrownException.printStackTrace(pw);
297 pw.flush();
298 String stackTrace=wrt.toString();
299 try {
300 wrt.close();
301 pw.close();
302 } catch (Exception e) {
303 LOG.trace(e.getMessage(), e);
304 }
305
306 if (LOG.isTraceEnabled()) {
307 String lm=String.format("EXIT %s", stackTrace);
308 LOG.trace(lm);
309 }
310
311 return stackTrace;
312 }
313
314
315
316
317
318
319
320 public String getDisplayMessage(Exception exception) {
321 if (LOG.isTraceEnabled()) {
322 String message=String.format("ENTRY %s", exception.getMessage());
323 LOG.trace(message);
324 }
325
326
327 String displayMessage;
328 if (exception instanceof KualiException) {
329 displayMessage=exception.getMessage();
330 } else {
331 displayMessage=GENERIC_SYSTEM_ERROR_MESSAGE;
332 }
333
334 if (LOG.isTraceEnabled()) {
335 String message=String.format("EXIT %s", displayMessage);
336 LOG.trace(message);
337 }
338
339 return displayMessage;
340 }
341
342
343
344
345
346
347
348 public String getProperty(String key) {
349 if (LOG.isTraceEnabled()) {
350 String message=String.format("ENTRY %s", key);
351 LOG.trace(message);
352 }
353
354 String value;
355 if (key.equals(EXCEPTION_REPORT_MESSAGE) && !properties.containsKey(key)) {
356 value=createReportMessage();
357 properties.put(EXCEPTION_REPORT_MESSAGE, value);
358 } else {
359 value=properties.get(key);
360 }
361
362 if (LOG.isTraceEnabled()) {
363 String message=String.format("EXIT %s", value);
364 LOG.trace(message);
365 }
366
367 return value;
368 }
369
370
371
372
373
374
375 public Map<String, String> toProperties() {
376 if (LOG.isTraceEnabled()) {
377 String message=String.format("ENTRY");
378 LOG.trace(message);
379 }
380
381 if (LOG.isTraceEnabled()) {
382 String message=String.format("EXIT %s", properties.toString());
383 LOG.trace(message);
384 }
385
386 return properties;
387 }
388 }