/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.control.misc;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import de.odysseus.calyxo.base.Message;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.control.ExceptionHandler;
import de.odysseus.calyxo.control.MessageSupport;
import de.odysseus.calyxo.control.conf.ActionConfig;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.conf.ExceptionHandlerConfig;
import de.odysseus.calyxo.control.conf.ParamConfig;
/**
* This is a simple exception handler implementation, which saves an
* error message and then dispatches to a specified target.
* The dispatch configuration may be specified by an anonymous <code><dispatch></code>
* child (without name attribute) or via the <code>target</code> parameter (containing the
* name of a dispatch configuration visible in the action configuration, that caused the
* exception to be thrown).
* <p/>
* It requires the following parameters:
* <ul>
* <li>bundle - bundle name used to get the error message</li>
* <li>key - resource key used to lookup the error message</li>
* </ul>
*
* @author Christoph Beck
*/
public class SimpleExceptionHandler implements ExceptionHandler {
private DispatchConfig dispatch;
private String target;
private String bundle;
private String key;
private MessageSupport messages;
/**
* Initialize the <code>bundle</code>, <code>key</code> and <code>target</code>
* parameters.
* @see de.odysseus.calyxo.control.ExceptionHandler#init(de.odysseus.calyxo.control.conf.ExceptionHandlerConfig, de.odysseus.calyxo.base.ModuleContext)
*/
public void init(ExceptionHandlerConfig config, ModuleContext module) throws ConfigException {
ParamConfig param = null;
param = config.getParamConfig("bundle");
if (param == null) {
throw new ConfigException("Missing parameter 'bundle'!");
}
bundle = param.getValue();
param = config.getParamConfig("key");
if (param == null) {
throw new ConfigException("Missing parameter 'key'!");
}
key = param.getValue();
dispatch = config.getDispatchConfig(null);
param = config.getParamConfig("target");
if ((param == null) == (dispatch == null)) {
throw new ConfigException("One of 'target' parameter or anonymous <dispatch> child must be given for filter '" + config.toInlineString() + "'");
}
if (param != null) {
target = param.getValue();
}
messages = MessageSupport.getInstance(module);
}
/**
* Save error message according to <code>bundle</code> and
* <code>key</code> parameters. Pass the exception message as a
* message argument.
* @return nested dispatch configuration or according to specified
* <code>target</code> parameter.
* @see de.odysseus.calyxo.control.ExceptionHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, de.odysseus.calyxo.control.conf.ActionConfig, java.lang.Exception)
*/
public DispatchConfig handle(
HttpServletRequest request,
HttpServletResponse response,
ActionConfig actionConfig,
Exception exception)
throws IOException, ServletException {
Message.Arg arg = new Message.ValueArg(exception.getMessage());
messages.addError(request, new Message(bundle, key, arg));
DispatchConfig result = dispatch;
if (result == null) {
result = actionConfig.findDispatchConfig(target);
if (result == null) {
throw new ServletException("Cannot find dispatch '" + target + "' in '" + actionConfig.toInlineString() + "'");
}
}
return result;
}
}