Package de.odysseus.calyxo.control.misc

Source Code of de.odysseus.calyxo.control.misc.CancelFilter

/*
* 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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;

import de.odysseus.calyxo.control.Command;
import de.odysseus.calyxo.control.Filter;
import de.odysseus.calyxo.control.Plugin;
import de.odysseus.calyxo.control.PluginContext;
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.control.conf.PluginConfig;

/**
* Dispatch as specified if a "cancel" parameter is present in the request.
* When submitting an image button with name <code>p</code>, HTTP
* parameters <code>p.x</code> and <code>p.y</code> will be sent.
* Therefore, this filter checks for presence of <code>p</code> and <code>p.x</code>.
* <p/>
* The dispatch configuration may be specified by an anonymous <code>&lt;dispatch&gt;</code>
* child (without name attribute) or via the <code>target</code> parameter (containing the
* name of a dispatch configuration visible in the enclosing action configuration).
* If neither is given, target <code>"cancel"</code> is assumed.
* <p/>
* The parameter name may be specified by filter parameter <code>parameter</code>.
* Again, the default value is <code>"cancel"</code>
* <p/>
* This class also implements the plugin interface. The default
* filter name is <code>cancel</code> and may be overridden by plugin
* parameter <code>name</code>.
*
* @author Christoph Beck
*/
public class CancelFilter implements Filter, Plugin {
  private String name1, name2;
  private DispatchConfig dispatch;
 
  /**
   * The plugin registers itself as a filter factory.
   * The default filter name is <code>cancel</code>, which may be overridden
   * by specifying the <code>name</code> plugin parameter.
   * @see de.odysseus.calyxo.control.Plugin#init(de.odysseus.calyxo.control.conf.PluginConfig, de.odysseus.calyxo.control.PluginContext)
   */
  public void init(PluginConfig config, PluginContext context) throws Exception {
    ParamConfig param = config.getParamConfig("name");
    String name = param == null ? "cancel" : param.getValue();
    context.setFilterClass(name, getClass());
  }

  /**
   * Do nothing.
   * @see de.odysseus.calyxo.control.Plugin#destroy()
   */
  public void destroy() {
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.Filter#init(de.odysseus.calyxo.control.conf.FilterConfig, de.odysseus.calyxo.base.ModuleContext)
   */
  public void init(FilterConfig config, ModuleContext module) throws ConfigException {
    ParamConfig param;
   
    name1 = "cancel";
    param = config.getParamConfig("parameter");
    if (param != null) {
      name1 = param.getValue();
    }
    name2 = name1.endsWith(".x") || name1.endsWith(".y") ? null : name1 + ".x";

    dispatch = config.getDispatchConfig(null);
    param = config.getParamConfig("target");
    if (param != null && dispatch != null) {
      throw new ConfigException("At most one of 'target' parameter or anonymous <dispatch> child must be given for filter '" + config.toInlineString() + "'");
    }
    if (dispatch == null) {
      String target = param == null ? "cancel" : param.getValue();
      dispatch = config.getActionConfig().findDispatchConfig(target);
      if (dispatch == null) {
        throw new ConfigException("Cannot find dispatch '" + target + "' for filter '" + config.toInlineString() + "'");
      }
    }
  }
 
  /**
   * If the "cancel" parameter is present in the request, dispatch
   * according to our configuration. Otherwise, execute the chain.
   */
  public DispatchConfig filter(
    HttpServletRequest request,
    HttpServletResponse response,
    Command command) throws Exception {

    if (request.getParameter(name1) != null) {
      return dispatch;
    }
    if (name2 != null && request.getParameter(name2) != null) {
      return dispatch;
    }
    return command.execute(request, response);
  }
}
TOP

Related Classes of de.odysseus.calyxo.control.misc.CancelFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.