Search in sources :

Example 11 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig 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)

Example 12 with FormPropertyConfig

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

the class DynaActionFormClass method introspect.

/**
     * <p>Introspect our form bean configuration to identify the supported
     * properties.</p>
     *
     * @param config The FormBeanConfig instance describing the properties of
     *               the bean to be created
     * @throws IllegalArgumentException if the bean implementation class
     *                                  specified in the configuration is not
     *                                  DynaActionForm (or a subclass of
     *                                  DynaActionForm)
     */
protected void introspect(FormBeanConfig config) {
    this.config = config;
    // Validate the ActionFormBean implementation class
    try {
        beanClass = RequestUtils.applicationClass(config.getType());
    } catch (Throwable t) {
        throw new IllegalArgumentException("Cannot instantiate ActionFormBean class '" + config.getType() + "': " + t);
    }
    if (!DynaActionForm.class.isAssignableFrom(beanClass)) {
        throw new IllegalArgumentException("Class '" + config.getType() + "' is not a subclass of " + "'org.apache.struts.action.DynaActionForm'");
    }
    // Set the name we will know ourselves by from the form bean name
    this.name = config.getName();
    // Look up the property descriptors for this bean class
    FormPropertyConfig[] descriptors = config.findFormPropertyConfigs();
    if (descriptors == null) {
        descriptors = new FormPropertyConfig[0];
    }
    // Create corresponding dynamic property definitions
    properties = new DynaProperty[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        properties[i] = new DynaProperty(descriptors[i].getName(), descriptors[i].getTypeClass());
        propertiesMap.put(properties[i].getName(), properties[i]);
    }
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) FormPropertyConfig(org.apache.struts.config.FormPropertyConfig)

Example 13 with FormPropertyConfig

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

the class TestMockBase method setUpSecondApp.

protected void setUpSecondApp() {
    ActionFormBean formBean = null;
    ActionMapping mapping = null;
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig2 = factoryObject.createModuleConfig("/2");
    context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2);
    // Forward "external" to "http://jakarta.apache.org/"
    moduleConfig2.addForwardConfig(new ActionForward("external", "http://jakarta.apache.org/", false));
    // Forward "foo" to "/baz.jsp" (different from default)
    moduleConfig2.addForwardConfig(new ActionForward("foo", "/baz.jsp", false));
    // Forward "relative1" to "relative.jsp" non-context-relative
    moduleConfig2.addForwardConfig(new ActionForward("relative1", "relative.jsp", false));
    // Forward "relative2" to "relative.jsp" context-relative
    moduleConfig2.addForwardConfig(new ActionForward("relative2", "relative.jsp", false));
    // Form Bean "static" is a standard ActionForm subclass (same as default)
    formBean = new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
    moduleConfig2.addFormBeanConfig(formBean);
    // Action "/static" uses the "static" form bean in request scope (same as default)
    mapping = new ActionMapping();
    mapping.setInput("/static.jsp");
    mapping.setName("static");
    mapping.setPath("/static");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Form Bean "dynamic2" is a DynaActionForm with the same properties
    formBean = new ActionFormBean("dynamic2", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "false"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", null));
    moduleConfig2.addFormBeanConfig(formBean);
    // Action "/dynamic2" uses the "dynamic2" form bean in session scope
    mapping = new ActionMapping();
    mapping.setInput("/dynamic2.jsp");
    mapping.setName("dynamic2");
    mapping.setPath("/dynamic2");
    mapping.setScope("session");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Action "/noform" has no form bean associated with it (same as default)
    mapping = new ActionMapping();
    mapping.setPath("/noform");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Configure global forward declarations
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleForward", "/module/forward", // No redirect, same module
    false));
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleRedirect", "/module/redirect", // Redirect, same module
    true));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextForward", // No redirect
    "/forward", // No redirect
    false, // Specify module
    "/context"));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextRedirect", // Redirect
    "/redirect", // Redirect
    true, // Specify module
    "/context"));
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleNoslash", "module/noslash", // No redirect, same module
    false));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextNoslash", // No redirect
    "noslash", // No redirect
    false, // Specify module
    "/context"));
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ActionMapping(org.apache.struts.action.ActionMapping) ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionForward(org.apache.struts.action.ActionForward) ActionFormBean(org.apache.struts.action.ActionFormBean)

Example 14 with FormPropertyConfig

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

the class TestMockBase method setUpDefaultApp.

protected void setUpDefaultApp() {
    ActionFormBean formBean = null;
    ActionMapping mapping = null;
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig = factoryObject.createModuleConfig("");
    context.setAttribute(Globals.MODULE_KEY, moduleConfig);
    // Forward "external" to "http://jakarta.apache.org/"
    moduleConfig.addForwardConfig(new ActionForward("external", "http://jakarta.apache.org/", false));
    // Forward "foo" to "/bar.jsp"
    moduleConfig.addForwardConfig(new ActionForward("foo", "/bar.jsp", false));
    // Forward "relative1" to "relative.jsp" non-context-relative
    moduleConfig.addForwardConfig(new ActionForward("relative1", "relative.jsp", false));
    // Forward "relative2" to "relative.jsp" context-relative
    moduleConfig.addForwardConfig(new ActionForward("relative2", "relative.jsp", false));
    // Form Bean "static" is a standard ActionForm subclass
    formBean = new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/static" uses the "static" form bean in request scope
    mapping = new ActionMapping();
    mapping.setInput("/static.jsp");
    mapping.setName("static");
    mapping.setPath("/static");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Form Bean "dynamic" is a DynaActionForm with the same properties
    formBean = new ActionFormBean("dynamic", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "false"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", null));
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/dynamic" uses the "dynamic" form bean in session scope
    mapping = new ActionMapping();
    mapping.setInput("/dynamic.jsp");
    mapping.setName("dynamic");
    mapping.setPath("/dynamic");
    mapping.setScope("session");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Form Bean "/dynamic0" is a DynaActionForm with initializers
    formBean = new ActionFormBean("dynamic0", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "true"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", "String Property"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("intArray1", "int[]", "{1,2,3}", // 4 should be ignored
    4));
    formBean.addFormPropertyConfig(new FormPropertyConfig("intArray2", "int[]", null, // 5 should be respected
    5));
    formBean.addFormPropertyConfig(new FormPropertyConfig("principal", "org.apache.struts.mock.MockPrincipal", null));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray1", "java.lang.String[]", "{aaa,bbb,ccc}", // 2 should be ignored
    2));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray2", "java.lang.String[]", null, // 3 should be respected
    3));
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/dynamic0" uses the "dynamic0" form bean in request scope
    mapping = new ActionMapping();
    mapping.setName("dynamic0");
    mapping.setPath("/dynamic0");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Action "/noform" has no form bean associated with it
    mapping = new ActionMapping();
    mapping.setPath("/noform");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Configure global forward declarations
    moduleConfig.addForwardConfig(new ForwardConfig("moduleForward", "/module/forward", // No redirect, same module
    false));
    moduleConfig.addForwardConfig(new ForwardConfig("moduleRedirect", "/module/redirect", // Redirect, same module
    true));
    moduleConfig.addForwardConfig(new ForwardConfig("contextForward", // No redirect
    "/forward", // No redirect
    false, // Specify module
    "/context"));
    moduleConfig.addForwardConfig(new ForwardConfig("contextRedirect", // Redirect
    "/redirect", // Redirect
    true, // Specify module
    "/context"));
    moduleConfig.addForwardConfig(new ForwardConfig("moduleNoslash", "module/noslash", // No redirect, same module
    false));
    moduleConfig.addForwardConfig(new ForwardConfig("contextNoslash", // No redirect
    "noslash", // No redirect
    false, // Specify module
    "/context"));
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ActionMapping(org.apache.struts.action.ActionMapping) ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionForward(org.apache.struts.action.ActionForward) ActionFormBean(org.apache.struts.action.ActionFormBean)

Example 15 with FormPropertyConfig

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

the class TestDynaActionFormClass method testConfigDuplicate.

// Check attempts to add a duplicate property name
public void testConfigDuplicate() {
    FormPropertyConfig prop = null;
    assertNull("booleanProperty is found", prop);
    try {
        beanConfig.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "java.lang.String", ""));
        fail("Adding duplicate property not prevented");
    } catch (IllegalArgumentException e) {
        // Expected result
        ;
    }
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig)

Aggregations

FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)24 FormBeanConfig (org.apache.struts.config.FormBeanConfig)10 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)6 ActionFormBean (org.apache.struts.action.ActionFormBean)4 ActionForward (org.apache.struts.action.ActionForward)4 ActionMapping (org.apache.struts.action.ActionMapping)4 ForwardConfig (org.apache.struts.config.ForwardConfig)4 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 MissingResourceException (java.util.MissingResourceException)2 StringTokenizer (java.util.StringTokenizer)2 ServletException (javax.servlet.ServletException)2 UnavailableException (javax.servlet.UnavailableException)2 DynaProperty (org.apache.commons.beanutils.DynaProperty)2 MockActionContext (org.apache.struts.chain.contexts.MockActionContext)2 ActionConfig (org.apache.struts.config.ActionConfig)2 ExceptionConfig (org.apache.struts.config.ExceptionConfig)2 ModuleConfigImpl (org.apache.struts.config.impl.ModuleConfigImpl)2 SAXException (org.xml.sax.SAXException)2