001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.control.misc;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.ServletException;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import de.odysseus.calyxo.base.Message;
025: import de.odysseus.calyxo.base.ModuleContext;
026: import de.odysseus.calyxo.base.conf.ConfigException;
027: import de.odysseus.calyxo.control.ExceptionHandler;
028: import de.odysseus.calyxo.control.MessageSupport;
029: import de.odysseus.calyxo.control.conf.ActionConfig;
030: import de.odysseus.calyxo.control.conf.DispatchConfig;
031: import de.odysseus.calyxo.control.conf.ExceptionHandlerConfig;
032: import de.odysseus.calyxo.control.conf.ParamConfig;
033:
034: /**
035: * This is a simple exception handler implementation, which saves an
036: * error message and then dispatches to a specified target.
037: * The dispatch configuration may be specified by an anonymous <code><dispatch></code>
038: * child (without name attribute) or via the <code>target</code> parameter (containing the
039: * name of a dispatch configuration visible in the action configuration, that caused the
040: * exception to be thrown).
041: * <p/>
042: * It requires the following parameters:
043: * <ul>
044: * <li>bundle - bundle name used to get the error message</li>
045: * <li>key - resource key used to lookup the error message</li>
046: * </ul>
047: *
048: * @author Christoph Beck
049: */
050: public class SimpleExceptionHandler implements ExceptionHandler {
051: private DispatchConfig dispatch;
052: private String target;
053: private String bundle;
054: private String key;
055:
056: private MessageSupport messages;
057:
058: /**
059: * Initialize the <code>bundle</code>, <code>key</code> and <code>target</code>
060: * parameters.
061: * @see de.odysseus.calyxo.control.ExceptionHandler#init(de.odysseus.calyxo.control.conf.ExceptionHandlerConfig, de.odysseus.calyxo.base.ModuleContext)
062: */
063: public void init(ExceptionHandlerConfig config, ModuleContext module)
064: throws ConfigException {
065: ParamConfig param = null;
066:
067: param = config.getParamConfig("bundle");
068: if (param == null) {
069: throw new ConfigException("Missing parameter 'bundle'!");
070: }
071: bundle = param.getValue();
072:
073: param = config.getParamConfig("key");
074: if (param == null) {
075: throw new ConfigException("Missing parameter 'key'!");
076: }
077: key = param.getValue();
078:
079: dispatch = config.getDispatchConfig(null);
080: param = config.getParamConfig("target");
081: if ((param == null) == (dispatch == null)) {
082: throw new ConfigException(
083: "One of 'target' parameter or anonymous <dispatch> child must be given for filter '"
084: + config.toInlineString() + "'");
085: }
086: if (param != null) {
087: target = param.getValue();
088: }
089:
090: messages = MessageSupport.getInstance(module);
091: }
092:
093: /**
094: * Save error message according to <code>bundle</code> and
095: * <code>key</code> parameters. Pass the exception message as a
096: * message argument.
097: * @return nested dispatch configuration or according to specified
098: * <code>target</code> parameter.
099: * @see de.odysseus.calyxo.control.ExceptionHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, de.odysseus.calyxo.control.conf.ActionConfig, java.lang.Exception)
100: */
101: public DispatchConfig handle(HttpServletRequest request,
102: HttpServletResponse response, ActionConfig actionConfig,
103: Exception exception) throws IOException, ServletException {
104:
105: Message.Arg arg = new Message.ValueArg(exception.getMessage());
106: messages.addError(request, new Message(bundle, key, arg));
107:
108: DispatchConfig result = dispatch;
109: if (result == null) {
110: result = actionConfig.findDispatchConfig(target);
111: if (result == null) {
112: throw new ServletException("Cannot find dispatch '"
113: + target + "' in '"
114: + actionConfig.toInlineString() + "'");
115: }
116: }
117: return result;
118: }
119: }
|