Search in sources :

Example 11 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class TestActionServlet method setUp.

// ------------------------------------------------- setUp() and tearDown()
/**
     * Set up instance variables required by this test case.
     */
public void setUp() throws Exception {
    actionServlet = new ActionServlet();
    actionServlet.initInternal();
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig = factoryObject.createModuleConfig("");
    // Setup the base form
    baseFormBean = new FormBeanConfig();
    baseFormBean.setName("baseForm");
    baseFormBean.setType("org.apache.struts.action.DynaActionForm");
    // Set up id, name, and score
    FormPropertyConfig property = new FormPropertyConfig();
    property.setName("id");
    property.setType("java.lang.String");
    baseFormBean.addFormPropertyConfig(property);
    property = new FormPropertyConfig();
    property.setName("name");
    property.setType("java.lang.String");
    baseFormBean.addFormPropertyConfig(property);
    property = new FormPropertyConfig();
    property.setName("score");
    property.setType("java.lang.String");
    baseFormBean.addFormPropertyConfig(property);
    // Setup the exception handler
    baseException = new ExceptionConfig();
    baseException.setType("java.lang.NullPointerException");
    baseException.setKey("msg.exception.npe");
    // Setup the forward config
    baseForward = new ActionForward("success", "/succes.jsp", false);
    // Setup the action config
    baseAction = new ActionMapping();
    baseAction.setPath("/index");
    baseAction.setType("org.apache.struts.actions.DummyAction");
    baseAction.setName("someForm");
    baseAction.setInput("/input.jsp");
    baseAction.addForwardConfig(new ActionForward("next", "/next.jsp", false));
    baseAction.addForwardConfig(new ActionForward("prev", "/prev.jsp", false));
    ExceptionConfig exceptionConfig = new ExceptionConfig();
    exceptionConfig.setType("java.sql.SQLException");
    exceptionConfig.setKey("msg.exception.sql");
    baseAction.addExceptionConfig(exceptionConfig);
// Nothing is registered to our module config until they are needed
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) FormBeanConfig(org.apache.struts.config.FormBeanConfig) ExceptionConfig(org.apache.struts.config.ExceptionConfig)

Example 12 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class TestCopyFormToContext method setUp.

/*
     * @see TestCase#setUp()
     */
protected void setUp() throws Exception {
    context = new MockActionContext();
    ModuleConfigImpl moduleConfig = new ModuleConfigImpl("/");
    context.setModuleConfig(moduleConfig);
    FormBeanConfig fooFBC = new FormBeanConfig();
    fooFBC.setName("foo");
    fooFBC.setType("org.apache.struts.mock.MockFormBean");
    moduleConfig.addFormBeanConfig(fooFBC);
    FormBeanConfig barFBC = new FormBeanConfig();
    barFBC.setName("bar");
    // use a different type so we can verify lookups better
    barFBC.setType("org.apache.struts.action.DynaActionForm");
    FormPropertyConfig fpc = new FormPropertyConfig();
    fpc.setName("property");
    fpc.setType("java.lang.String");
    fpc.setInitial("test");
    barFBC.addFormPropertyConfig(fpc);
    moduleConfig.addFormBeanConfig(barFBC);
    ActionConfig testActionConfig = new ActionConfig();
    testActionConfig.setPath("/Test");
    testActionConfig.setName("foo");
    testActionConfig.setScope("request");
    moduleConfig.addActionConfig(testActionConfig);
    // otherwise, ActionConfigMatcher will be null and we'll get an NPE...
    moduleConfig.freeze();
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ActionConfig(org.apache.struts.config.ActionConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig) MockActionContext(org.apache.struts.chain.contexts.MockActionContext) ModuleConfigImpl(org.apache.struts.config.impl.ModuleConfigImpl)

Example 13 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class FormTag method lookup.

// ------------------------------------------------------ Protected Methods
/**
     * Look up values for the <code>name</code>, <code>scope</code>, and
     * <code>type</code> properties if necessary.
     *
     * @throws JspException if a required value cannot be looked up
     */
protected void lookup() throws JspException {
    // Look up the module configuration information we need
    moduleConfig = TagUtils.getInstance().getModuleConfig(pageContext);
    if (moduleConfig == null) {
        JspException e = new JspException(messages.getMessage("formTag.collections"));
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
        throw e;
    }
    String calcAction = this.action;
    // If the action is not specified, use the original request uri
    if (this.action == null) {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        postbackAction = (String) request.getAttribute(Globals.ORIGINAL_URI_KEY);
        String prefix = moduleConfig.getPrefix();
        if (postbackAction != null && prefix.length() > 0 && postbackAction.startsWith(prefix)) {
            postbackAction = postbackAction.substring(prefix.length());
        }
        calcAction = postbackAction;
    } else {
        // Translate the action if it is an actionId
        ActionConfig actionConfig = moduleConfig.findActionConfigId(this.action);
        if (actionConfig != null) {
            this.action = actionConfig.getPath();
            calcAction = this.action;
        }
    }
    servlet = (ActionServlet) pageContext.getServletContext().getAttribute(Globals.ACTION_SERVLET_KEY);
    // Look up the action mapping we will be submitting to
    String mappingName = TagUtils.getInstance().getActionMappingName(calcAction);
    mapping = (ActionMapping) moduleConfig.findActionConfig(mappingName);
    if (mapping == null) {
        JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
        throw e;
    }
    // Look up the form bean definition
    FormBeanConfig formBeanConfig = moduleConfig.findFormBeanConfig(mapping.getName());
    if (formBeanConfig == null) {
        JspException e = null;
        if (mapping.getName() == null) {
            e = new JspException(messages.getMessage("formTag.name", calcAction));
        } else {
            e = new JspException(messages.getMessage("formTag.formBean", mapping.getName(), calcAction));
        }
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
        throw e;
    }
    // Calculate the required values
    beanName = mapping.getAttribute();
    beanScope = mapping.getScope();
    beanType = formBeanConfig.getType();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) ActionConfig(org.apache.struts.config.ActionConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig)

Example 14 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class RequestUtils method createActionForm.

/**
     * <p>Create (if necessary) and return an <code>ActionForm</code> instance
     * appropriate for this request.  If no <code>ActionForm</code> instance
     * is required, return <code>null</code>.</p>
     *
     * @param request      The servlet request we are processing
     * @param mapping      The action mapping for this request
     * @param moduleConfig The configuration for this module
     * @param servlet      The action servlet
     * @return ActionForm instance associated with this request
     */
public static ActionForm createActionForm(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) {
    // 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
    String name = mapping.getName();
    FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
    if (config == null) {
        log.warn("No FormBeanConfig found under '" + name + "'");
        return (null);
    }
    ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
    // Can we recycle the existing form bean instance (if there is one)?
    if ((instance != null) && config.canReuse(instance)) {
        return (instance);
    }
    return createActionForm(config, servlet);
}
Also used : FormBeanConfig(org.apache.struts.config.FormBeanConfig) ActionForm(org.apache.struts.action.ActionForm)

Example 15 with FormBeanConfig

use of org.apache.struts.config.FormBeanConfig in project sonarqube by SonarSource.

the class DynaActionForm method reset.

/**
     * <p>Reset the properties to their <code>initial</code> value if their
     * <code>reset</code> configuration is set to true or if
     * <code>reset</code> is set to a list of HTTP request methods that
     * includes the method of given <code>request</code> object.</p>
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
public void reset(ActionMapping mapping, HttpServletRequest request) {
    String name = getDynaClass().getName();
    if (name == null) {
        return;
    }
    FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(name);
    if (config == null) {
        return;
    }
    // look for properties we should reset
    FormPropertyConfig[] props = config.findFormPropertyConfigs();
    for (int i = 0; i < props.length; i++) {
        String resetValue = props[i].getReset();
        // skip this property if there's no reset value
        if ((resetValue == null) || (resetValue.length() <= 0)) {
            continue;
        }
        boolean reset = Boolean.valueOf(resetValue).booleanValue();
        if (!reset) {
            // check for the request method
            // use a StringTokenizer with the default delimiters + a comma
            StringTokenizer st = new StringTokenizer(resetValue, ", \t\n\r\f");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (token.equalsIgnoreCase(request.getMethod())) {
                    reset = true;
                    break;
                }
            }
        }
        if (reset) {
            set(props[i].getName(), props[i].initial());
        }
    }
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) StringTokenizer(java.util.StringTokenizer) FormBeanConfig(org.apache.struts.config.FormBeanConfig)

Aggregations

FormBeanConfig (org.apache.struts.config.FormBeanConfig)18 UnavailableException (javax.servlet.UnavailableException)5 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)5 ServletException (javax.servlet.ServletException)4 ActionConfig (org.apache.struts.config.ActionConfig)4 ActionForm (org.apache.struts.action.ActionForm)3 Map (java.util.Map)2 ExceptionConfig (org.apache.struts.config.ExceptionConfig)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 MissingResourceException (java.util.MissingResourceException)1 StringTokenizer (java.util.StringTokenizer)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JspException (javax.servlet.jsp.JspException)1 MockActionContext (org.apache.struts.chain.contexts.MockActionContext)1 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)1 ActionConfigMatcher (org.apache.struts.config.ActionConfigMatcher)1 ForwardConfig (org.apache.struts.config.ForwardConfig)1 MessageResourcesConfig (org.apache.struts.config.MessageResourcesConfig)1 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)1