001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.web;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.struts.Globals;
027: import org.apache.struts.action.ActionForm;
028: import org.apache.struts.action.ActionForward;
029: import org.apache.struts.action.ActionMapping;
030: import org.apache.struts.action.ActionMessage;
031: import org.apache.struts.action.ActionMessages;
032: import org.apache.struts.action.ExceptionHandler;
033: import org.apache.struts.config.ExceptionConfig;
034:
035: import edu.iu.uis.eden.WorkflowServiceError;
036: import edu.iu.uis.eden.WorkflowServiceErrorException;
037: import edu.iu.uis.eden.routetemplate.WorkflowAttributeValidationError;
038: import edu.iu.uis.eden.validation.ValidationResult;
039: import edu.iu.uis.eden.validation.ValidationResults;
040:
041: /**
042: * Catches exceptions throw from Workflow Actions If exception is of type
043: * WorkflowServiceErrorException any WorkflowServiceErrors saved on the exception
044: * are stripped off of the exception put into ActionMessages in the Error que and
045: * the request is directed back to the original ActionMapping input page. Other
046: * exceptions are logged and directed to the mapped jsp according to the
047: * exception handler mapping.
048: *
049: * @author rkirkend
050: * @author temay
051: * @author ewestfal
052: */
053: public class StrutsExceptionHandler extends ExceptionHandler {
054:
055: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
056: .getLogger(StrutsExceptionHandler.class);
057:
058: public ActionForward execute(Exception e,
059: ExceptionConfig exceptionConfig, ActionMapping mapping,
060: ActionForm form, HttpServletRequest request,
061: HttpServletResponse response) throws ServletException {
062:
063: if (e instanceof WorkflowServiceErrorException
064: || e.getCause() instanceof WorkflowServiceErrorException) {
065: WorkflowServiceErrorException serviceException = null;
066: if (!(e instanceof WorkflowServiceErrorException)) {
067: serviceException = (WorkflowServiceErrorException) e
068: .getCause();
069: } else {
070: serviceException = (WorkflowServiceErrorException) e;
071: }
072:
073: Collection serviceErrors = serviceException
074: .getServiceErrors();
075: saveServiceErrors(serviceErrors, request);
076: } else {
077: LOG.error(
078: "Mapping " + mapping.getPath() + " threw error: ",
079: e);
080: saveServiceErrors("general.workflow.error", e, request);
081: }
082:
083: if (mapping.getInputForward() != null
084: && mapping.getInputForward().getPath() != null) {
085: return mapping.getInputForward();
086: } else if (request.getParameter("inputPage") != null
087: && request.getParameter("inputPage").length() > 0) {
088: mapping.setInput(request.getParameter("inputPage"));
089: return mapping.getInputForward();
090: } else if (mapping.getPath() != null) {
091: return new ActionForward(mapping.getPath());
092: } else {
093: request.setAttribute("WORKFLOW_ERROR", e.getMessage());
094: return mapping.findForward("WorkflowError");
095: }
096: }
097:
098: protected void saveServiceErrors(Collection srvErrors,
099: HttpServletRequest request) {
100: ActionMessages messages = new ActionMessages();
101: Iterator errors = srvErrors.iterator();
102: while (errors.hasNext()) {
103: Object errorObject = errors.next();
104: if (errorObject instanceof WorkflowServiceError) {
105: WorkflowServiceError err = (WorkflowServiceError) errorObject;
106: if (err.getArg1() == null && err.getArg2() == null) {
107: messages.add(Globals.ERROR_KEY, new ActionMessage(
108: err.getKey()));
109: } else if (err.getArg1() != null
110: && err.getArg2() == null) {
111: messages.add(Globals.ERROR_KEY, new ActionMessage(
112: err.getKey(), err.getArg1()));
113: } else {
114: messages
115: .add(Globals.ERROR_KEY, new ActionMessage(
116: err.getKey(), err.getArg1(), err
117: .getArg2()));
118: }
119: } else if (errorObject instanceof ValidationResults) {
120: ValidationResults results = (ValidationResults) errorObject;
121: for (Iterator iterator = results.getValidationResults()
122: .iterator(); iterator.hasNext();) {
123: ValidationResult result = (ValidationResult) iterator
124: .next();
125: messages.add(Globals.ERROR_KEY,
126: new ActionMessage("general.message", result
127: .getErrorMessage()));
128: }
129: } else if (errorObject instanceof WorkflowAttributeValidationError) {
130: WorkflowAttributeValidationError error = (WorkflowAttributeValidationError) errorObject;
131: messages.add(Globals.ERROR_KEY, new ActionMessage(error
132: .getKey(), error.getMessage()));
133: }
134: }
135: request.setAttribute("workflowServiceError", messages);
136: }
137:
138: protected void saveServiceErrors(String key, Exception e,
139: HttpServletRequest request) {
140: ActionMessages messages = new ActionMessages();
141: messages.add(Globals.ERROR_KEY, new ActionMessage(key, e
142: .getMessage()));
143: request.setAttribute("exceptionError", messages);
144: }
145:
146: }
|