Search in sources :

Example 56 with UnavailableException

use of javax.servlet.UnavailableException in project sonarqube by SonarSource.

the class ActionServlet method processForwardConfigClass.

/**
     * <p>Checks if the current forwardConfig is using the correct class based
     * on the class of its configuration ancestor.  If actionConfig is
     * provided, then this method will process the forwardConfig as part
     * of that actionConfig.  If actionConfig is null, the forwardConfig
     * will be processed as a global forward.</p>
     *
     * @param forwardConfig The forward to check.
     * @param moduleConfig  The config for the current module.
     * @param actionConfig  If applicable, the config for the current action.
     * @return The forward config using the correct class as determined by the
     *         config's ancestor and its own overridden value.
     * @throws UnavailableException if an instance of the forward config class
     *                              cannot be created.
     * @throws ServletException     on class creation error
     */
protected ForwardConfig processForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
    String ancestor = forwardConfig.getExtends();
    if (ancestor == null) {
        // Nothing to do, then
        return forwardConfig;
    }
    // Make sure that this config is of the right class
    ForwardConfig baseConfig = null;
    if (actionConfig != null) {
        // Look for this in the actionConfig
        baseConfig = actionConfig.findForwardConfig(ancestor);
    }
    if (baseConfig == null) {
        // Either this is a forwardConfig that inherits a global config,
        //  or actionConfig is null
        baseConfig = moduleConfig.findForwardConfig(ancestor);
    }
    if (baseConfig == null) {
        throw new UnavailableException("Unable to find " + "forward '" + ancestor + "' to extend.");
    }
    // Was our forwards's class overridden already?
    if (forwardConfig.getClass().equals(ActionForward.class)) {
        // Ensure that our forward is using the correct class
        if (!baseConfig.getClass().equals(forwardConfig.getClass())) {
            // Replace the config with an instance of the correct class
            ForwardConfig newForwardConfig = null;
            String baseConfigClassName = baseConfig.getClass().getName();
            try {
                newForwardConfig = (ForwardConfig) RequestUtils.applicationInstance(baseConfigClassName);
                // copy the values
                BeanUtils.copyProperties(newForwardConfig, forwardConfig);
            } catch (Exception e) {
                handleCreationException(baseConfigClassName, e);
            }
            // replace forwardConfig with newForwardConfig
            if (actionConfig != null) {
                actionConfig.removeForwardConfig(forwardConfig);
                actionConfig.addForwardConfig(newForwardConfig);
            } else {
                // this is a global forward
                moduleConfig.removeForwardConfig(forwardConfig);
                moduleConfig.addForwardConfig(newForwardConfig);
            }
            forwardConfig = newForwardConfig;
        }
    }
    return forwardConfig;
}
Also used : UnavailableException(javax.servlet.UnavailableException) 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)

Example 57 with UnavailableException

use of javax.servlet.UnavailableException 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 58 with UnavailableException

use of javax.servlet.UnavailableException in project sonarqube by SonarSource.

the class ActionServlet method init.

/**
     * <p>Initialize this servlet.  Most of the processing has been factored
     * into support methods so that you can override particular functionality
     * at a fairly granular level.</p>
     *
     * @throws ServletException if we cannot configure ourselves correctly
     */
public void init() throws ServletException {
    final String configPrefix = "config/";
    final int configPrefixLength = configPrefix.length() - 1;
    // to the developer
    try {
        initInternal();
        initOther();
        initServlet();
        initChain();
        getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
        initModuleConfigFactory();
        // Initialize modules as needed
        ModuleConfig moduleConfig = initModuleConfig("", config);
        initModuleMessageResources(moduleConfig);
        initModulePlugIns(moduleConfig);
        initModuleFormBeans(moduleConfig);
        initModuleForwards(moduleConfig);
        initModuleExceptionConfigs(moduleConfig);
        initModuleActions(moduleConfig);
        moduleConfig.freeze();
        Enumeration names = getServletConfig().getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            if (!name.startsWith(configPrefix)) {
                continue;
            }
            String prefix = name.substring(configPrefixLength);
            moduleConfig = initModuleConfig(prefix, getServletConfig().getInitParameter(name));
            initModuleMessageResources(moduleConfig);
            initModulePlugIns(moduleConfig);
            initModuleFormBeans(moduleConfig);
            initModuleForwards(moduleConfig);
            initModuleExceptionConfigs(moduleConfig);
            initModuleActions(moduleConfig);
            moduleConfig.freeze();
        }
        this.initModulePrefixes(this.getServletContext());
        this.destroyConfigDigester();
    } catch (UnavailableException ex) {
        throw ex;
    } catch (Throwable t) {
        // The follow error message is not retrieved from internal message
        // resources as they may not have been able to have been
        // initialized
        log.error("Unable to initialize Struts ActionServlet due to an " + "unexpected exception or error thrown, so marking the " + "servlet as unavailable.  Most likely, this is due to an " + "incorrect or missing library dependency.", t);
        throw new UnavailableException(t.getMessage());
    }
}
Also used : Enumeration(java.util.Enumeration) UnavailableException(javax.servlet.UnavailableException) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 59 with UnavailableException

use of javax.servlet.UnavailableException in project sonarqube by SonarSource.

the class TestActionServlet method notestProcessForwardConfigClassError.

/**
     * Make sure the code throws the correct exception when it can't create an
     * instance of the base config's custom class.
     */
public void notestProcessForwardConfigClassError() throws Exception {
    ForwardConfig customBase = new CustomForwardConfigArg("success", "/success.jsp");
    moduleConfig.addForwardConfig(customBase);
    ForwardConfig customSub = new ActionForward();
    customSub.setName("failure");
    customSub.setExtends("success");
    moduleConfig.addForwardConfig(customSub);
    try {
        actionServlet.processForwardConfigClass(customSub, moduleConfig, null);
        fail("Exception should be thrown");
    } catch (UnavailableException e) {
    // success
    } catch (Exception e) {
        fail("Unexpected exception thrown.");
    }
}
Also used : UnavailableException(javax.servlet.UnavailableException) ForwardConfig(org.apache.struts.config.ForwardConfig) ServletException(javax.servlet.ServletException) UnavailableException(javax.servlet.UnavailableException)

Example 60 with UnavailableException

use of javax.servlet.UnavailableException in project sonarqube by SonarSource.

the class TestActionServlet method testInitModuleExceptionConfigsNullFormType.

/**
     * Test that initModuleExceptionConfigs throws an exception when a handler
     * with a null key is present.
     */
public void testInitModuleExceptionConfigsNullFormType() throws ServletException {
    ExceptionConfig handler = new ExceptionConfig();
    handler.setType("java.lang.NullPointerException");
    moduleConfig.addExceptionConfig(handler);
    try {
        actionServlet.initModuleExceptionConfigs(moduleConfig);
        fail("An exception should've been thrown here.");
    } catch (UnavailableException e) {
    // success
    } catch (Exception e) {
        fail("Unrecognized exception thrown: " + e);
    }
}
Also used : ExceptionConfig(org.apache.struts.config.ExceptionConfig) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) UnavailableException(javax.servlet.UnavailableException)

Aggregations

UnavailableException (javax.servlet.UnavailableException)95 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)33 MalformedURLException (java.net.MalformedURLException)15 SAXException (org.xml.sax.SAXException)14 MissingResourceException (java.util.MissingResourceException)12 ExceptionConfig (org.apache.struts.config.ExceptionConfig)10 URL (java.net.URL)8 Servlet (javax.servlet.Servlet)8 FormBeanConfig (org.apache.struts.config.FormBeanConfig)8 ForwardConfig (org.apache.struts.config.ForwardConfig)8 ServletContext (javax.servlet.ServletContext)6 ActionConfig (org.apache.struts.config.ActionConfig)6 ArrayList (java.util.ArrayList)5 ServletConfig (javax.servlet.ServletConfig)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ClientAbortException (org.apache.catalina.connector.ClientAbortException)4 InvalidConfigException (com.revolsys.ui.web.config.InvalidConfigException)3 XmlConfigLoader (com.revolsys.ui.web.config.XmlConfigLoader)3 BufferedImage (java.awt.image.BufferedImage)3