/*
* 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.forms.control;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.util.ParseUtils;
import de.odysseus.calyxo.control.Command;
import de.odysseus.calyxo.control.Filter;
import de.odysseus.calyxo.control.MessageSupport;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.conf.FilterConfig;
import de.odysseus.calyxo.control.conf.ParamConfig;
import de.odysseus.calyxo.forms.Form;
import de.odysseus.calyxo.forms.FormInputValues;
import de.odysseus.calyxo.forms.FormResult;
import de.odysseus.calyxo.forms.FormProperties;
import de.odysseus.calyxo.forms.misc.RequestInputValues;
/**
* The filter providing form handling capabilities.
*
* @author Christoph Beck
*/
public class FormsFilter implements Filter {
private static Log log = LogFactory.getLog(FormsPlugin.class);
private FilterConfig config;
private FormsSupport formsSupport;
private MessageSupport messageSupport;
private DispatchConfig dispatch;
private boolean commit;
public void init(FilterConfig config, ModuleContext context) throws ConfigException {
formsSupport = (FormsSupport)FormsSupport.getInstance(context);
if (formsSupport == null) {
throw new ConfigException("No forms support! Forms plugin loaded?");
}
dispatch = config.getDispatchConfig(null);
ParamConfig param = config.getParamConfig("dispatch");
if ((param == null) == (dispatch == null)) {
throw new ConfigException("One of 'dispatch' parameter or anonymous <dispatch> child must be given for filter '" + config.toInlineString() + "'");
}
if (dispatch == null) {
dispatch = config.getActionConfig().findDispatchConfig(param.getValue());
if (dispatch == null) {
throw new ConfigException("Cannot find dispatch '" + param.getValue() + "' for filter '" + config.toInlineString() + "'");
}
}
messageSupport = MessageSupport.getInstance(context);
param = config.getParamConfig("commit");
if (param == null) {
commit = false;
} else {
try {
commit = ((Boolean)ParseUtils.parse(Boolean.class, param.getValue())).booleanValue();
} catch (Exception e) {
throw new ConfigException("Bad commit value '" + param.getValue() + "' in " + config.toInlineString());
}
}
formsSupport.add(config);
this.config = config;
}
/**
* Validate request parameters.
* <p/>
* The validation is done according to the forms filter settings
* of the action configuration. If the input turns out to be valid, the
* form data (if any) is populated and the filter chain execution continues
* normally. If input validation fails, action messages are set to
* validation error messages and the action's source dispatch is
* returned (that is, filter chain execution is interrupted.).
*
*/
public DispatchConfig filter(
HttpServletRequest request,
HttpServletResponse response,
Command command) throws Exception {
String path = config.getActionConfig().getPath();
// validate form
log.debug("validating form for action '" + path + "'");
Form form = formsSupport.getForm(request, path);
if (form == null) {
throw new ServletException("Cannot find form for action '" + path);
}
FormInputValues formParams = new RequestInputValues(request);
FormResult result = form.validate(request, formParams);
request.setAttribute(FormsSupport.FORM_RESULT_KEY, result);
if (result.isValid()) {
log.debug("valid: yes");
FormProperties properties = formsSupport.getFormProperties(request, path);
if (properties != null && commit) {
properties.commit();
}
return command.execute(request, response);
} else {
log.debug("valid: no");
messageSupport.setErrors(request, result.getMessages());
return dispatch;
}
}
}