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());
}
}
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;
}
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);
}
}
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;
}
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();
}
}
}
Aggregations