Search in sources :

Example 91 with UnavailableException

use of javax.servlet.UnavailableException in project sonar-java 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 92 with UnavailableException

use of javax.servlet.UnavailableException in project sonar-java 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 93 with UnavailableException

use of javax.servlet.UnavailableException in project sonar-java by SonarSource.

the class ComposableRequestProcessor method setActionContextClassName.

/**
 * <p>Make sure that the specified <code>className</code> identfies a
 * class which can be found and which implements the
 * <code>ActionContext</code> interface.</p>
 *
 * @param className Fully qualified name of
 * @throws ServletException     If an error occurs during initialization
 * @throws UnavailableException if class does not implement ActionContext
 *                              or is not found
 */
private void setActionContextClassName(String className) throws ServletException {
    if ((className != null) && (className.trim().length() > 0)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("setActionContextClassName: requested context class: " + className);
        }
        try {
            Class actionContextClass = RequestUtils.applicationClass(className);
            if (!ActionContext.class.isAssignableFrom(actionContextClass)) {
                throw new UnavailableException("ActionContextClass " + "[" + className + "]" + " must implement ActionContext interface.");
            }
            this.setActionContextClass(actionContextClass);
        } catch (ClassNotFoundException e) {
            throw new UnavailableException("ActionContextClass " + className + " not found.");
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("setActionContextClassName: no className specified");
        }
        this.setActionContextClass(null);
    }
}
Also used : UnavailableException(javax.servlet.UnavailableException) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ActionContext(org.apache.struts.chain.contexts.ActionContext)

Example 94 with UnavailableException

use of javax.servlet.UnavailableException in project sonar-java by SonarSource.

the class TilesPlugin method readFactoryConfig.

/**
 * Create FactoryConfig and initialize it from web.xml and struts-config.xml.
 *
 * @param servlet ActionServlet that is managing all the modules
 *  in this web application.
 * @param config ModuleConfig for the module with which
 *  this plugin is associated.
 * @exception ServletException if this <code>PlugIn</code> cannot
 *  be successfully initialized.
 */
protected DefinitionsFactoryConfig readFactoryConfig(ActionServlet servlet, ModuleConfig config) throws ServletException {
    // Create tiles definitions config object
    DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
    // Get init parameters from web.xml files
    try {
        DefinitionsUtil.populateDefinitionsFactoryConfig(factoryConfig, servlet.getServletConfig());
    } catch (Exception ex) {
        if (log.isDebugEnabled()) {
            log.debug("", ex);
        }
        ex.printStackTrace();
        throw new UnavailableException("Can't populate DefinitionsFactoryConfig class from 'web.xml': " + ex.getMessage());
    }
    // Get init parameters from struts-config.xml
    try {
        Map strutsProperties = findStrutsPlugInConfigProperties(servlet, config);
        factoryConfig.populate(strutsProperties);
    } catch (Exception ex) {
        if (log.isDebugEnabled()) {
            log.debug("", ex);
        }
        throw new UnavailableException("Can't populate DefinitionsFactoryConfig class from '" + config.getPrefix() + "/struts-config.xml':" + ex.getMessage());
    }
    return factoryConfig;
}
Also used : UnavailableException(javax.servlet.UnavailableException) Map(java.util.Map) ServletException(javax.servlet.ServletException) UnavailableException(javax.servlet.UnavailableException)

Example 95 with UnavailableException

use of javax.servlet.UnavailableException in project undertow by undertow-io.

the class ServletHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws IOException, ServletException {
    if (managedServlet.isPermanentlyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 404 for servlet %s due to permanent unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }
    if (managedServlet.isTemporarilyUnavailable()) {
        UndertowServletLogger.REQUEST_LOGGER.debugf("Returning 503 for servlet %s due to temporary unavailability", managedServlet.getServletInfo().getName());
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        return;
    }
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (!managedServlet.getServletInfo().isAsyncSupported()) {
        servletRequestContext.setAsyncSupported(false);
    }
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    InstanceHandle<? extends Servlet> servlet = null;
    try {
        servlet = managedServlet.getServlet();
        servlet.getInstance().service(request, response);
    // according to the spec we have to call AsyncContext.complete() at this point
    // straight after the service method
    // not super sure about this, surely it would make more sense to do this when the request has returned to the container, however the spec is quite clear wording wise
    // todo: should we actually enable this? Apparently other containers do not do it
    // if(!request.isAsyncStarted()) {
    // AsyncContextImpl existingAsyncContext = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
    // if (existingAsyncContext != null) {
    // existingAsyncContext.complete();
    // }
    // }
    } catch (UnavailableException e) {
        managedServlet.handleUnavailableException(e);
        if (e.isPermanent()) {
            exchange.setStatusCode(StatusCodes.NOT_FOUND);
        } else {
            exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        }
    } finally {
        if (servlet != null) {
            servlet.release();
        }
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) 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