1 /* 2 * Copyright 2003,2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.struts.chain.servlet; 18 19 20 import javax.servlet.http.HttpServletRequest; 21 import javax.servlet.http.HttpServletResponse; 22 import org.apache.commons.chain.Context; 23 import org.apache.commons.chain.web.servlet.ServletWebContext; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.struts.chain.AbstractExceptionHandler; 27 import org.apache.struts.chain.Constants; 28 import org.apache.struts.chain.util.ClassUtils; 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.config.ActionConfig; 32 import org.apache.struts.config.ExceptionConfig; 33 import org.apache.struts.config.ForwardConfig; 34 import org.apache.struts.config.ModuleConfig; 35 36 37 /** 38 * <p>Handle the specified exception.</p> 39 * 40 * @version $Rev: 54933 $ $Date: 2004-10-16 18:04:52 +0100 (Sat, 16 Oct 2004) $ 41 */ 42 43 public class ExceptionHandler extends AbstractExceptionHandler { 44 45 46 // ------------------------------------------------------ Instance Variables 47 48 49 private String actionFormKey = Constants.ACTION_FORM_KEY; 50 51 private static final Log log = LogFactory.getLog(ExceptionHandler.class); 52 53 54 // -------------------------------------------------------------- Properties 55 56 57 /** 58 * <p>Return the context attribute key under which the 59 * <code>ActionForm</code> for the currently selected application 60 * action is stored.</p> 61 */ 62 public String getActionFormKey() { 63 64 return (this.actionFormKey); 65 66 } 67 68 69 /** 70 * <p>Set the context attribute key under which the 71 * <code>ActionForm</code> for the currently selected application 72 * action is stored.</p> 73 * 74 * @param actionFormKey The new context attribute key 75 */ 76 public void setActionFormKey(String actionFormKey) { 77 78 this.actionFormKey = actionFormKey; 79 80 } 81 82 83 // ------------------------------------------------------- Protected Methods 84 85 86 protected ForwardConfig handle(Context context, 87 Exception exception, 88 ExceptionConfig exceptionConfig, 89 ActionConfig actionConfig, 90 ModuleConfig moduleConfig) 91 throws Exception { 92 93 // Look up the remaining properties needed for this handler 94 ServletWebContext swcontext = (ServletWebContext) context; 95 ActionForm actionForm = (ActionForm) 96 swcontext.get(getActionFormKey()); 97 HttpServletRequest request = swcontext.getRequest(); 98 HttpServletResponse response = swcontext.getResponse(); 99 100 // Handle this exception 101 org.apache.struts.action.ExceptionHandler handler = 102 (org.apache.struts.action.ExceptionHandler) 103 ClassUtils.getApplicationInstance(exceptionConfig.getHandler()); 104 return (handler.execute(exception, 105 exceptionConfig, 106 (ActionMapping) actionConfig, 107 actionForm, 108 request, 109 response)); 110 111 } 112 113 114 }