Package de.odysseus.calyxo.forms.control

Source Code of de.odysseus.calyxo.forms.control.FormsSupport

/*
* 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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

import de.odysseus.calyxo.control.conf.FilterConfig;
import de.odysseus.calyxo.control.conf.ParamConfig;
import de.odysseus.calyxo.forms.FormData;
import de.odysseus.calyxo.forms.misc.FormDataBean;
import de.odysseus.calyxo.forms.misc.FormDataMap;


/**
* Forms support implementation used with calyxo plugin.
*
* @author Christoph Beck
*/
public class FormsSupport extends de.odysseus.calyxo.forms.FormsSupport {
  private static Log log = LogFactory.getLog(FormsSupport.class);

  private final HashMap filters = new HashMap();
  private final Class defaultDataClass;
  private final ClassLoader loader;

  /**
   * Constructor.
   */
  public FormsSupport(ClassLoader loader, Class defaultDataClass) {
    this.loader = loader;
    this.defaultDataClass = defaultDataClass;
  }

  /**
   * Add filter configuration.
   * This method is called from <code>FormsFilter.init()</code>.
   * @param config
   * @throws ConfigException
   */
  void add(FilterConfig config) throws ConfigException {
    check(config);
    filters.put(config.getActionConfig().getPath(), config);
  }
 
  /**
   * Check filter configuration.
   *
   * @param config
   * @throws ConfigException
   */
  private void check(FilterConfig config) throws ConfigException {
    if (config.getParamConfig("form") == null) {
      throw new ConfigException("Missing parameter 'form' in " + config.toInlineString());
    }
    Iterator params = config.getParamConfigs();
    while (params.hasNext()) {
      ParamConfig param = (ParamConfig)params.next();
      if ("scope".equals(param.getName())) {
        String scope = param.getValue();
        if (!"request".equals(scope) && !"session".equals(scope)) {
          throw new ConfigException("Bad scope '" + scope + "' in " + config.toInlineString());
        }
        if (config.getParamConfig("attribute") == null) {
          throw new ConfigException("Parameter 'scope' without 'attribute' in " + config.toInlineString());
        }     
      } else if ("class".equals(param.getName())) {
        String clazz = param.getValue();
        try {
          loader.loadClass(clazz);
        } catch (Exception e) {
          throw new ConfigException("Bad class '" + clazz + "' in " + config.toInlineString(), e);
        }
      }
    }
  }
 
  /**
   * Lookup filter element for specified action path.
   * The specified action path may omit the leading slash and may
   * have a query string appended.
   */
  private FilterConfig getFilterConfig(String action) {
    String path = action;

    if (!path.startsWith("/")) {
      path = "/" + path;
    }

    // cut off query string
    int question = action.indexOf("?");
    if (question > 0) {
      path = path.substring(0, question);
    }

    // lookup action configuration
    return (FilterConfig)filters.get(path);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#getFormData(javax.servlet.http.HttpServletRequest, java.lang.String)
   */
  public FormData getFormData(HttpServletRequest request, String action, boolean create) throws Exception {
    return getFormData(request, getFilterConfig(action), create);
  }

 
  /* (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#removeFormData(javax.servlet.http.HttpServletRequest, java.lang.String)
   */
  public void removeFormData(HttpServletRequest request, String action) throws Exception {
    removeFormData(request, getFilterConfig(action));
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#lookupFormName(java.lang.String)
   */
  protected String lookupFormName(String action) {
    FilterConfig filter = getFilterConfig(action);
    return filter == null ? null : filter.getParamConfig("form").getValue();
  }

  /**
   * Get the form data for specified filter configuration.
   * This method uses various filter parameters to do its work.
   * <ul>
   <li>the <code>attribute</code> parameter is used to save/restore
   *      form data in request or session scope, depending on the
   *      <code>scope</code> parameter. If no scope is given, session
   *      scope is the default.
   *  </li>
   <li>The <code>class</code> parameter specifies a data class.
   *      It is used to create a new object instance if no existing
   *      instance could be found using <code>attribute</code> and
   *      <code>scope</code> parameters.
   *  </li>
   * </ul>
   * If the resulting object is instance of {@link FormData}, answer it.
   * Otherwise, wrap it into {@link FormDataMap} or {@link FormDataBean}.
   *
   */
  FormData getFormData(HttpServletRequest request, FilterConfig filterConfig, boolean create) throws Exception {
    if (filterConfig == null) {
      return null;
    }
    Object data = null;
    ParamConfig attribute = filterConfig.getParamConfig("attribute");
    if (attribute != null) {
      ParamConfig scope = filterConfig.getParamConfig("scope");
      if (scope != null && "request".equals(scope.getValue())) {
        data = request.getAttribute(attribute.getValue());
      } else {
        data = request.getSession().getAttribute(attribute.getValue());
      }
    }
    if (data == null && create) {
      ParamConfig clazz = filterConfig.getParamConfig("class");
      if (clazz != null) {
        data = loader.loadClass(clazz.getValue()).newInstance();
      } else if (defaultDataClass != null) { // use default class
        data = defaultDataClass.newInstance();
      }
      if (data != null) {
        if (attribute != null) {
          ParamConfig scope = filterConfig.getParamConfig("scope");
          if (scope != null && "request".equals(scope.getValue())) {
            request.setAttribute(attribute.getValue(), data);
          } else {
            request.getSession().setAttribute(attribute.getValue(), data);
          }
        }
      }
    }
    if (data instanceof FormData) {
      return (FormData)data;
    } else if (data instanceof Map) {
      return new FormDataMap((Map)data);
    } else if (data != null) {
      return new FormDataBean(data);
    } else {
      return null;
    }
  }

  /**
   * Remove existing form data for specified filter configuration.
   * This method uses filter various parameters to do its work:
   * the <code>attribute</code> parameter is used to save/restore
   * form data in request or session scope, depending on the
   * <code>scope</code> parameter. If no scope is given, session
   * scope is the default.
   */
  void removeFormData(HttpServletRequest request, FilterConfig filterConfig) throws Exception {
    if (filterConfig == null) {
      return;
    }
    ParamConfig attribute = filterConfig.getParamConfig("attribute");
    if (attribute != null) {
      ParamConfig scope = filterConfig.getParamConfig("scope");
      if (scope == null || "session".equals(scope.getValue())) {
        request.getSession().removeAttribute(attribute.getValue());
      } else if ("request".equals(scope.getValue())) {
        request.removeAttribute(attribute.getValue());
      } else {
        throw new ConfigException("Bad scope '" +  scope.getValue() + "' in " + filterConfig.toInlineString());
      }
    }
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.control.FormsSupport

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.