Search in sources :

Example 6 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.

the class ActionServlet method processFormBeanConfigClass.

/**
 * <p>Checks if the current beanConfig is using the correct class based on
 * the class of its ancestor form bean config.</p>
 *
 * @param beanConfig   The form bean to check.
 * @param moduleConfig The config for the current module.
 * @return The form bean config using the correct class as determined by
 *         the config's ancestor and its own overridden value.
 * @throws UnavailableException if an instance of the form bean config
 *                              class cannot be created.
 * @throws ServletException     on class creation error
 */
protected FormBeanConfig processFormBeanConfigClass(FormBeanConfig beanConfig, ModuleConfig moduleConfig) throws ServletException {
    String ancestor = beanConfig.getExtends();
    if (ancestor == null) {
        // Nothing to do, then
        return beanConfig;
    }
    // Make sure that this bean is of the right class
    FormBeanConfig baseConfig = moduleConfig.findFormBeanConfig(ancestor);
    if (baseConfig == null) {
        throw new UnavailableException("Unable to find " + "form bean '" + ancestor + "' to extend.");
    }
    // Was our bean's class overridden already?
    if (beanConfig.getClass().equals(FormBeanConfig.class)) {
        // Ensure that our bean is using the correct class
        if (!baseConfig.getClass().equals(beanConfig.getClass())) {
            // Replace the bean with an instance of the correct class
            FormBeanConfig newBeanConfig = null;
            String baseConfigClassName = baseConfig.getClass().getName();
            try {
                newBeanConfig = (FormBeanConfig) RequestUtils.applicationInstance(baseConfigClassName);
                // copy the values
                BeanUtils.copyProperties(newBeanConfig, beanConfig);
                FormPropertyConfig[] fpc = beanConfig.findFormPropertyConfigs();
                for (int i = 0; i < fpc.length; i++) {
                    newBeanConfig.addFormPropertyConfig(fpc[i]);
                }
            } catch (Exception e) {
                handleCreationException(baseConfigClassName, e);
            }
            // replace beanConfig with newBeanConfig
            moduleConfig.removeFormBeanConfig(beanConfig);
            moduleConfig.addFormBeanConfig(newBeanConfig);
            beanConfig = newBeanConfig;
        }
    }
    return beanConfig;
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) FormBeanConfig(org.apache.struts.config.FormBeanConfig) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) MissingResourceException(java.util.MissingResourceException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Example 7 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig in project sonar-java 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 8 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig in project sonar-java 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 9 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig in project sonar-java by SonarSource.

the class TestDynaActionFormClass method testConfigProperties.

// Check the configured FormPropertyConfig element properties
public void testConfigProperties() {
    for (int i = 0; i < dynaProperties.length; i++) {
        FormPropertyConfig dynaProperty = beanConfig.findFormPropertyConfig(dynaProperties[i].getName());
        assertNotNull("Found dynaProperty " + dynaProperties[i].getName(), dynaProperty);
        assertEquals("dynaProperty name for " + dynaProperties[i].getName(), dynaProperties[i].getName(), dynaProperty.getName());
        assertEquals("dynaProperty type for " + dynaProperties[i].getName(), dynaProperties[i].getType(), dynaProperty.getType());
        assertEquals("dynaProperty initial for " + dynaProperties[i].getName(), dynaProperties[i].getInitial(), dynaProperty.getInitial());
    }
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig)

Example 10 with FormPropertyConfig

use of org.apache.struts.config.FormPropertyConfig in project sonar-java 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