Coverage Report - org.kuali.rice.kew.web.StrutsExceptionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
StrutsExceptionHandler
0%
0/49
0%
0/36
8.333
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.web;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import javax.servlet.ServletException;
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import org.apache.struts.Globals;
 27  
 import org.apache.struts.action.ActionForm;
 28  
 import org.apache.struts.action.ActionForward;
 29  
 import org.apache.struts.action.ActionMapping;
 30  
 import org.apache.struts.action.ActionMessage;
 31  
 import org.apache.struts.action.ActionMessages;
 32  
 import org.apache.struts.action.ExceptionHandler;
 33  
 import org.apache.struts.config.ExceptionConfig;
 34  
 import org.kuali.rice.kew.exception.WorkflowServiceError;
 35  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 36  
 import org.kuali.rice.kew.rule.WorkflowAttributeValidationError;
 37  
 import org.kuali.rice.kew.validation.ValidationResult;
 38  
 import org.kuali.rice.kew.validation.ValidationResults;
 39  
 import org.kuali.rice.kns.exception.AuthorizationException;
 40  
 
 41  
 
 42  
 /**
 43  
  * Catches exceptions throw from Workflow Actions If exception is of type 
 44  
  * WorkflowServiceErrorException any WorkflowServiceErrors saved on the exception 
 45  
  * are stripped off of the exception put into ActionMessages in the Error que and 
 46  
  * the request is directed back to the original ActionMapping input page. Other 
 47  
  * exceptions are logged and directed to the mapped jsp according to the
 48  
  * exception handler mapping.
 49  
  * 
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  0
 public class StrutsExceptionHandler extends ExceptionHandler {
 53  
 
 54  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StrutsExceptionHandler.class);
 55  
 
 56  
     public ActionForward execute(Exception e, ExceptionConfig exceptionConfig, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
 57  
 
 58  0
         if (e instanceof WorkflowServiceErrorException || e.getCause() instanceof WorkflowServiceErrorException) {
 59  0
                 WorkflowServiceErrorException serviceException = null;
 60  0
                 if (! (e instanceof WorkflowServiceErrorException)) {
 61  0
                         serviceException = (WorkflowServiceErrorException) e.getCause();
 62  
                 } else {
 63  0
                         serviceException = (WorkflowServiceErrorException) e;        
 64  
                 }
 65  
                 
 66  0
             Collection serviceErrors = serviceException.getServiceErrors();
 67  0
             saveServiceErrors(serviceErrors, request);
 68  0
         } else if (e instanceof AuthorizationException) {
 69  0
                 return mapping.findForward("NotAuthorized");        
 70  
         } else {
 71  0
             LOG.error("Mapping " + mapping.getPath() + " threw error: ", e);
 72  0
             saveServiceErrors("general.workflow.error", e, request);
 73  
         }
 74  
 
 75  0
         if (mapping.getInputForward() != null && mapping.getInputForward().getPath() != null) {
 76  0
             return mapping.getInputForward();
 77  0
         } else if (request.getParameter("inputPage") != null && request.getParameter("inputPage").length() > 0) {
 78  0
             mapping.setInput(request.getParameter("inputPage"));
 79  0
             return mapping.getInputForward();
 80  0
         } else if (mapping.getPath() != null) {
 81  0
             return new ActionForward(mapping.getPath());
 82  
         } else {
 83  0
             request.setAttribute("WORKFLOW_ERROR", e.getMessage());
 84  0
             return mapping.findForward("WorkflowError");
 85  
         }
 86  
     }
 87  
 
 88  
     protected void saveServiceErrors(Collection srvErrors, HttpServletRequest request) {
 89  0
         ActionMessages messages = new ActionMessages();
 90  0
         Iterator errors = srvErrors.iterator();
 91  0
         while (errors.hasNext()) {
 92  0
                 Object errorObject = errors.next();
 93  0
                 if (errorObject instanceof WorkflowServiceError) {
 94  0
                         WorkflowServiceError err = (WorkflowServiceError)errorObject;
 95  0
                         if (err.getArg1() == null && err.getArg2() == null) {
 96  0
                                 messages.add(Globals.ERROR_KEY, new ActionMessage(err.getKey()));
 97  0
                         } else if (err.getArg1() != null && err.getArg2() == null) {
 98  0
                                 messages.add(Globals.ERROR_KEY, new ActionMessage(err.getKey(), err.getArg1())); 
 99  
                         } else {
 100  0
                                 messages.add(Globals.ERROR_KEY, new ActionMessage(err.getKey(), err.getArg1(), err.getArg2()));
 101  
                         }
 102  0
                 } else if (errorObject instanceof ValidationResults) {
 103  0
                         ValidationResults results = (ValidationResults)errorObject;
 104  0
                         for (Iterator iterator = results.getValidationResults().iterator(); iterator.hasNext(); ) {
 105  0
                                         ValidationResult result = (ValidationResult) iterator.next();
 106  0
                                         messages.add(Globals.ERROR_KEY, new ActionMessage("general.message", result.getErrorMessage()));
 107  0
                                 }
 108  0
                 } else if (errorObject instanceof WorkflowAttributeValidationError) {
 109  0
                         WorkflowAttributeValidationError error = (WorkflowAttributeValidationError)errorObject;
 110  0
                         messages.add(Globals.ERROR_KEY, new ActionMessage(error.getKey(), error.getMessage()));
 111  
                 }
 112  0
         }
 113  0
         request.setAttribute("workflowServiceError", messages);
 114  0
     }
 115  
 
 116  
     protected void saveServiceErrors(String key, Exception e, HttpServletRequest request) {
 117  0
         ActionMessages messages = new ActionMessages();
 118  0
         messages.add(Globals.ERROR_KEY, new ActionMessage(key, e.getMessage()));
 119  0
         request.setAttribute("exceptionError", messages);
 120  0
     }
 121  
 
 122  
 }