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