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