001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import org.apache.struts.action.ExceptionHandler;
022: import org.apache.struts.action.ActionForward;
023: import org.apache.struts.action.ActionMapping;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.action.ActionMessage;
026: import org.apache.struts.action.ActionMessages;
027: import org.apache.struts.config.ExceptionConfig;
028: import org.apache.struts.util.ModuleException;
029: import org.apache.struts.Globals;
030: import org.apache.beehive.netui.pageflow.ExpressionMessage;
031: import org.apache.beehive.netui.pageflow.config.PageFlowExceptionConfig;
032: import org.apache.beehive.netui.util.logging.Logger;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import javax.servlet.ServletException;
037:
038: public class PageFlowExceptionHandler extends ExceptionHandler {
039: private static final Logger _log = Logger
040: .getInstance(PageFlowExceptionHandler.class);
041:
042: public ActionForward execute(Exception ex, ExceptionConfig ec,
043: ActionMapping mapping, ActionForm formInstance,
044: HttpServletRequest request, HttpServletResponse response)
045: throws ServletException {
046: ActionForward forward = null;
047: ActionMessage error = null;
048: String property = null;
049: String path = ec.getPath();
050:
051: // Build the forward from the exception mapping if it exists or from the form input
052: forward = path != null ? new ActionForward(path) : mapping
053: .getInputForward();
054:
055: PageFlowExceptionConfig pfec = ec instanceof PageFlowExceptionConfig ? (PageFlowExceptionConfig) ec
056: : null;
057: if (pfec != null && pfec.isPathContextRelative()) {
058: forward.setContextRelative(true);
059: }
060:
061: // Figure out the error
062: if (ex instanceof ModuleException) {
063: error = ((ModuleException) ex).getError();
064: property = ((ModuleException) ex).getProperty();
065: } else {
066: if (pfec != null) {
067: String expressionMessage = pfec.getDefaultMessage();
068: if (expressionMessage != null) {
069: error = new ExpressionMessage(expressionMessage,
070: new Object[] { ex.getMessage() });
071: }
072: }
073:
074: if (error == null)
075: error = new ActionMessage(ec.getKey(), ex.getMessage());
076: property = ec.getKey();
077: }
078:
079: if (_log.isDebugEnabled())
080: _log.debug("Handling exception", ex);
081:
082: // Store the exception
083: request.setAttribute(Globals.EXCEPTION_KEY, ex);
084: storeException(request, property, error, forward, ec.getScope());
085:
086: return forward;
087: }
088:
089: // TODO: this does nothing different than the Struts 1.2 version of the same method. Remove this when we don't
090: // support Struts 1.1 anymore (or when Struts 1.1 support goes into a legacy version).
091: protected void storeException(HttpServletRequest request,
092: String property, ActionMessage error,
093: ActionForward forward, String scope) {
094: ActionMessages errors = new ActionMessages();
095: errors.add(property, error);
096:
097: if ("request".equals(scope)) {
098: request.setAttribute(Globals.ERROR_KEY, errors);
099: } else {
100: request.getSession()
101: .setAttribute(Globals.ERROR_KEY, errors);
102: }
103: }
104: }
|