Search in sources :

Example 6 with ExceptionConfig

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

the class TestActionServlet method testProcessExceptionConfigClass.

/**
     * Make sure processExceptionConfigClass() returns an instance of the
     * correct class if the base config is using a custom class.
     */
public void testProcessExceptionConfigClass() throws Exception {
    CustomExceptionConfig customBase = new CustomExceptionConfig();
    customBase.setType("java.lang.NullPointerException");
    customBase.setKey("msg.exception.npe");
    moduleConfig.addExceptionConfig(customBase);
    ExceptionConfig customSub = new ExceptionConfig();
    customSub.setType("java.lang.IllegalStateException");
    customSub.setExtends("java.lang.NullPointerException");
    moduleConfig.addExceptionConfig(customSub);
    ExceptionConfig result = actionServlet.processExceptionConfigClass(customSub, moduleConfig, null);
    assertTrue("Incorrect class of exception config", result instanceof CustomExceptionConfig);
    assertEquals("Incorrect type", customSub.getType(), result.getType());
    assertEquals("Incorrect key", customSub.getKey(), result.getKey());
    assertEquals("Incorrect extends", customSub.getExtends(), result.getExtends());
    assertSame("Result was not registered in the module config", result, moduleConfig.findExceptionConfig("java.lang.IllegalStateException"));
}
Also used : ExceptionConfig(org.apache.struts.config.ExceptionConfig)

Example 7 with ExceptionConfig

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

the class TestActionServlet method testProcessExceptionConfigClassSubConfigCustomClass.

/**
     * Make sure processExceptionConfigClass() returns the same class instance
     * if the base config isn't using a custom class.
     */
public void testProcessExceptionConfigClassSubConfigCustomClass() throws Exception {
    moduleConfig.addExceptionConfig(baseException);
    ExceptionConfig customSub = new ExceptionConfig();
    customSub.setType("java.lang.IllegalStateException");
    customSub.setExtends("java.lang.NullPointerException");
    moduleConfig.addExceptionConfig(customSub);
    ExceptionConfig result = actionServlet.processExceptionConfigClass(customSub, moduleConfig, null);
    assertSame("The instance returned should be the param given it.", customSub, result);
}
Also used : ExceptionConfig(org.apache.struts.config.ExceptionConfig)

Example 8 with ExceptionConfig

use of org.apache.struts.config.ExceptionConfig 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 9 with ExceptionConfig

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

the class ActionServlet method processExceptionConfigClass.

/**
     * <p>Checks if the current exceptionConfig is using the correct class
     * based on the class of its configuration ancestor. If actionConfig is
     * provided, then this method will process the exceptionConfig as part
     * of that actionConfig.  If actionConfig is null, the exceptionConfig
     * will be processed as a global forward.</p>
     *
     * @param exceptionConfig The config to check.
     * @param moduleConfig    The config for the current module.
     * @param actionConfig  If applicable, the config for the current action.
     * @return The exception config using the correct class as determined by
     *         the config's ancestor and its own overridden value.
     * @throws ServletException if an instance of the exception config class
     *                          cannot be created.
     */
protected ExceptionConfig processExceptionConfigClass(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
    String ancestor = exceptionConfig.getExtends();
    if (ancestor == null) {
        // Nothing to do, then
        return exceptionConfig;
    }
    // Make sure that this config is of the right class
    ExceptionConfig baseConfig = null;
    if (actionConfig != null) {
        baseConfig = actionConfig.findExceptionConfig(ancestor);
    }
    if (baseConfig == null) {
        // This means either there's no actionConfig anyway, or the
        // ancestor is not defined within the action.
        baseConfig = moduleConfig.findExceptionConfig(ancestor);
    }
    if (baseConfig == null) {
        throw new UnavailableException("Unable to find " + "exception config '" + ancestor + "' to extend.");
    }
    // Was our config's class overridden already?
    if (exceptionConfig.getClass().equals(ExceptionConfig.class)) {
        // Ensure that our config is using the correct class
        if (!baseConfig.getClass().equals(exceptionConfig.getClass())) {
            // Replace the config with an instance of the correct class
            ExceptionConfig newExceptionConfig = null;
            String baseConfigClassName = baseConfig.getClass().getName();
            try {
                newExceptionConfig = (ExceptionConfig) RequestUtils.applicationInstance(baseConfigClassName);
                // copy the values
                BeanUtils.copyProperties(newExceptionConfig, exceptionConfig);
            } catch (Exception e) {
                handleCreationException(baseConfigClassName, e);
            }
            // replace exceptionConfig with newExceptionConfig
            if (actionConfig != null) {
                actionConfig.removeExceptionConfig(exceptionConfig);
                actionConfig.addExceptionConfig(newExceptionConfig);
            } else {
                moduleConfig.removeExceptionConfig(exceptionConfig);
                moduleConfig.addExceptionConfig(newExceptionConfig);
            }
            exceptionConfig = newExceptionConfig;
        }
    }
    return exceptionConfig;
}
Also used : ExceptionConfig(org.apache.struts.config.ExceptionConfig) 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 10 with ExceptionConfig

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

the class ActionServlet method processActionConfigExtension.

/**
     * <p>Extend the action's configuration as necessary.</p>
     *
     * @param actionConfig the configuration to process.
     * @param moduleConfig the module configuration for this module.
     * @throws ServletException if initialization cannot be performed.
     */
protected void processActionConfigExtension(ActionConfig actionConfig, ModuleConfig moduleConfig) throws ServletException {
    try {
        if (!actionConfig.isExtensionProcessed()) {
            if (log.isDebugEnabled()) {
                log.debug("Processing extensions for '" + actionConfig.getPath() + "'");
            }
            actionConfig = processActionConfigClass(actionConfig, moduleConfig);
            actionConfig.processExtends(moduleConfig);
        }
        // Process forwards extensions.
        ForwardConfig[] forwards = actionConfig.findForwardConfigs();
        for (int i = 0; i < forwards.length; i++) {
            ForwardConfig forward = forwards[i];
            processForwardExtension(forward, moduleConfig, actionConfig);
        }
        // Process exception extensions.
        ExceptionConfig[] exceptions = actionConfig.findExceptionConfigs();
        for (int i = 0; i < exceptions.length; i++) {
            ExceptionConfig exception = exceptions[i];
            processExceptionExtension(exception, moduleConfig, actionConfig);
        }
    } catch (ServletException e) {
        throw e;
    } catch (Exception e) {
        handleGeneralExtensionException("Action", actionConfig.getPath(), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ExceptionConfig(org.apache.struts.config.ExceptionConfig) ForwardConfig(org.apache.struts.config.ForwardConfig) 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)

Aggregations

ExceptionConfig (org.apache.struts.config.ExceptionConfig)16 UnavailableException (javax.servlet.UnavailableException)7 ServletException (javax.servlet.ServletException)6 ForwardConfig (org.apache.struts.config.ForwardConfig)5 ActionConfig (org.apache.struts.config.ActionConfig)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 MissingResourceException (java.util.MissingResourceException)3 SAXException (org.xml.sax.SAXException)3 FormBeanConfig (org.apache.struts.config.FormBeanConfig)2 ActionConfigMatcher (org.apache.struts.config.ActionConfigMatcher)1 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)1 MessageResourcesConfig (org.apache.struts.config.MessageResourcesConfig)1 ModuleConfig (org.apache.struts.config.ModuleConfig)1 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)1 PlugInConfig (org.apache.struts.config.PlugInConfig)1