Package de.odysseus.calyxo.struts.forms

Source Code of de.odysseus.calyxo.struts.forms.StrutsFormsSupport

/*
* 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.struts.forms;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.RequestUtils;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.forms.FormData;
import de.odysseus.calyxo.forms.FormFactory;
import de.odysseus.calyxo.forms.conf.FormsRootConfig;

/**
* Struts Forms support.
*
* @author Christoph Beck
*/
public class StrutsFormsSupport extends de.odysseus.calyxo.forms.FormsSupport {
  private ModuleConfig moduleConfig;
  private ActionServlet servlet;

  StrutsFormsSupport(ModuleConfig config) {
    this.moduleConfig = config;
  }
 
  /* (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#init(de.odysseus.calyxo.base.ModuleContext, de.odysseus.calyxo.forms.conf.FormsRootConfig)
   */
  protected void init(ModuleContext context, FormsRootConfig config, FormFactory factory) throws ConfigException {   
    super.init(context, config, factory);
    ServletContext servletContext = context.getServletContext();
    servlet = (ActionServlet)servletContext.getAttribute(Globals.ACTION_SERVLET_KEY);
  }

  /**
   * Look up an action mapping.
   * The <code>action</code> parameter is manipulated as follows in
   * computing the name of the requested mapping:
   * <ul>
   * <li>Any query string (starting with a querstion mark) is removed.</li>
   * <li>If the resulting value does not start with a slash, then a slash is prepended.</li>
   * </ul>
   *
   * @param action (module-relative) action path
   * @return action mapping
   */
  protected ActionMapping getActionMapping(String action) {
    int question = action.indexOf("?");
    if (question > 0) {
      action = action.substring(0, question);
    }
    if (!action.startsWith("/")) {
      action = "/" + action;
    }
    return (ActionMapping)moduleConfig.findActionConfig(action);
  }
 
  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#getFormData(javax.servlet.http.HttpServletRequest, java.lang.String, boolean)
   */
  public FormData getFormData(HttpServletRequest request, String action, boolean create) {
    ActionForm form = null;

    // Look up the action mapping
    ActionMapping mapping = getActionMapping(action);
    if (mapping == null)
      return null;

    // Is there a form bean associated with this mapping?
    String attribute = mapping.getAttribute();
    if (attribute == null) {
      return null;
    }

    // Look up the form bean configuration information to use
    FormBeanConfig config =
      moduleConfig.findFormBeanConfig(mapping.getName());
    if (config == null) {
      return null;
    }

    // Look up any existing form bean instance
    if ("request".equals(mapping.getScope())) {
      form = (ActionForm) request.getAttribute(attribute);
    } else {
      form = (ActionForm) request.getSession().getAttribute(attribute);
    }

    if (form == null && create) {
      // get the action form instance
      form = RequestUtils.createActionForm(config, servlet);
      if (form == null) {
        return null;
      }
      if ("request".equals(mapping.getScope())) {
        request.setAttribute(attribute, form);
      } else {
        request.getSession().setAttribute(attribute, form);
      }
    }

    if (form instanceof FormData) {
      return (FormData)form;
    }
    return null;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#removeFormData(javax.servlet.http.HttpServletRequest, java.lang.String)
   */
  public void removeFormData(HttpServletRequest request, String action) {
    // Look up the action mapping
    ActionMapping mapping = getActionMapping(action);
    if (mapping == null)
      return;

    // Is there a form bean associated with this mapping?
    String attribute = mapping.getAttribute();
    if (attribute == null) {
      return;
    }

    // Look up the form bean configuration information to use
    FormBeanConfig config =
      moduleConfig.findFormBeanConfig(mapping.getName());
    if (config == null) {
      return;
    }

    // Remove existing form bean instance
    if ("request".equals(mapping.getScope())) {
      request.removeAttribute(attribute);
    } else {
      request.getSession().removeAttribute(attribute);
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormsSupport#lookupFormName(java.lang.String)
   */
  protected String lookupFormName(String action) {
    // Look up the action mapping
    ActionMapping mapping = getActionMapping(action);
    // answer the form name
    return mapping == null ? null : mapping.getName();
  }
}
TOP

Related Classes of de.odysseus.calyxo.struts.forms.StrutsFormsSupport

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.