use of javax.servlet.UnavailableException in project Payara by payara.
the class InvokerServlet method serveRequest.
// -------------------------------------------------------- Private Methods
/**
* Serve the specified request, creating the corresponding response.
* After the first time a particular servlet class is requested, it will
* be served directly (like any registered servlet) because it will have
* been registered and mapped in our associated Context.
*
* <p>Synchronize to avoid race conditions when multiple requests
* try to initialize the same servlet at the same time
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet-specified error occurs
*/
public synchronized void serveRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Disallow calling this servlet via a named dispatcher
if (request.getAttribute(Globals.NAMED_DISPATCHER_ATTR) != null)
throw new ServletException(rb.getString(LogFacade.CANNOT_CALL_INVOKER_SERVLET));
// Identify the input parameters and our "included" state
String inRequestURI = null;
String inServletPath = null;
String inPathInfo = null;
boolean included = (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null);
if (included) {
inRequestURI = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
inServletPath = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
inPathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
} else {
inRequestURI = request.getRequestURI();
inServletPath = request.getServletPath();
inPathInfo = request.getPathInfo();
}
if (debug >= 1) {
log("included='" + included + "', requestURI='" + inRequestURI + "'");
log(" servletPath='" + inServletPath + "', pathInfo='" + inPathInfo + "'");
}
// Make sure a servlet name or class name was specified
if (inPathInfo == null) {
if (debug >= 1)
log("Invalid pathInfo 'null'");
String msg = MessageFormat.format(rb.getString(LogFacade.INVALID_PATH_EXCEPTION), inRequestURI);
if (included) {
throw new ServletException(msg);
} else {
/* IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND,
inRequestURI);
*/
// BEGIN IASRI 4878272
log(msg);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
// END IASRI 4878272
return;
}
}
// Identify the outgoing servlet name or class, and outgoing path info
String pathInfo = inPathInfo;
String servletClass = pathInfo.substring(1);
int slash = servletClass.indexOf('/');
// "', pathInfo='" + pathInfo + "', slash=" + slash);
if (slash >= 0) {
pathInfo = servletClass.substring(slash);
servletClass = servletClass.substring(0, slash);
} else {
pathInfo = "";
}
if (servletClass.startsWith("org.apache.catalina")) {
/* IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND,
inRequestURI);
*/
// BEGIN IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND);
// END IASRI 4878272
return;
}
if (debug >= 1)
log("Processing servlet '" + servletClass + "' with path info '" + pathInfo + "'");
String name = "org.apache.catalina.INVOKER." + servletClass;
String pattern = inServletPath + "/" + servletClass + "/*";
Wrapper wrapper = null;
// Are we referencing an existing servlet class or name?
wrapper = (Wrapper) context.findChild(servletClass);
if (wrapper == null)
wrapper = (Wrapper) context.findChild(name);
if (wrapper != null) {
String actualServletClass = wrapper.getServletClassName();
if ((actualServletClass != null) && (actualServletClass.startsWith("org.apache.catalina"))) {
/* IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND,
inRequestURI);
*/
// BEGIN IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND);
// END IASRI 4878272
return;
}
if (debug >= 1)
log("Using wrapper for servlet '" + wrapper.getName() + "' with mapping '" + pattern + "'");
context.addServletMapping(pattern, wrapper.getName());
} else // No, create a new wrapper for the specified servlet class
{
if (debug >= 1)
log("Creating wrapper for '" + servletClass + "' with mapping '" + pattern + "'");
try {
wrapper = context.createWrapper();
wrapper.setName(name);
wrapper.setLoadOnStartup(1);
wrapper.setServletClassName(servletClass);
context.addChild(wrapper);
context.addServletMapping(pattern, name);
} catch (Throwable t) {
String msg = MessageFormat.format(rb.getString(LogFacade.CANNOT_CREATE_SERVLET_WRAPPER_EXCEPTION), inRequestURI);
log(msg, t);
context.removeServletMapping(pattern);
context.removeChild(wrapper);
if (included)
throw new ServletException(msg, t);
else {
/* IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND,
inRequestURI);
*/
// BEGIN IASRI 4878272
String invalidPathMsg = MessageFormat.format(rb.getString(LogFacade.INVALID_PATH_EXCEPTION), inRequestURI);
log(invalidPathMsg);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
// END IASRI 4878272
return;
}
}
}
// Create a request wrapper to pass on to the invoked servlet
InvokerHttpRequest wrequest = new InvokerHttpRequest(request);
wrequest.setRequestURI(inRequestURI);
StringBuilder sb = new StringBuilder(inServletPath);
sb.append("/");
sb.append(servletClass);
wrequest.setServletPath(sb.toString());
if ((pathInfo == null) || (pathInfo.length() < 1)) {
wrequest.setPathInfo(null);
wrequest.setPathTranslated(null);
} else {
wrequest.setPathInfo(pathInfo);
wrequest.setPathTranslated(getServletContext().getRealPath(pathInfo));
}
// Allocate a servlet instance to perform this request
Servlet instance = null;
String cannotAllocateMsg = MessageFormat.format(rb.getString(LogFacade.CANNOT_ALLOCATE_SERVLET_INSTANCE_EXCEPTION), inRequestURI);
String invalidPathMsg = MessageFormat.format(rb.getString(LogFacade.INVALID_PATH_EXCEPTION), inRequestURI);
try {
// if (debug >= 2)
// log(" Allocating servlet instance");
instance = wrapper.allocate();
} catch (ServletException e) {
log(cannotAllocateMsg, e);
context.removeServletMapping(pattern);
context.removeChild(wrapper);
Throwable rootCause = e.getRootCause();
if (rootCause == null)
rootCause = e;
if (rootCause instanceof ClassNotFoundException) {
/* IASRI 4878272
response.sendError(HttpServletResponse.SC_NOT_FOUND,
inRequestURI);
*/
// BEGIN IASRI 4878272
log(invalidPathMsg);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
// END IASRI 4878272
return;
} else if (rootCause instanceof IOException) {
throw (IOException) rootCause;
} else if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
} else if (rootCause instanceof ServletException) {
throw (ServletException) rootCause;
} else {
throw new ServletException(cannotAllocateMsg, rootCause);
}
} catch (Throwable e) {
log(cannotAllocateMsg, e);
context.removeServletMapping(pattern);
context.removeChild(wrapper);
throw new ServletException(cannotAllocateMsg, e);
}
// After loading the wrapper, restore some of the fields when including
if (included) {
wrequest.setRequestURI(request.getRequestURI());
wrequest.setPathInfo(request.getPathInfo());
wrequest.setServletPath(request.getServletPath());
}
// Invoke the service() method of the allocated servlet
try {
String jspFile = wrapper.getJspFile();
if (jspFile != null)
request.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
else
request.removeAttribute(Globals.JSP_FILE_ATTR);
request.setAttribute(Globals.INVOKED_ATTR, request.getServletPath());
// if (debug >= 2)
// log(" Calling service() method, jspFile=" +
// jspFile);
instance.service(wrequest, response);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
} catch (IOException e) {
// if (debug >= 2)
// log(" service() method IOException", e);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
try {
wrapper.deallocate(instance);
} catch (Throwable f) {
;
}
throw e;
} catch (UnavailableException e) {
// if (debug >= 2)
// log(" service() method UnavailableException", e);
context.removeServletMapping(pattern);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
try {
wrapper.deallocate(instance);
} catch (Throwable f) {
;
}
throw e;
} catch (ServletException e) {
// if (debug >= 2)
// log(" service() method ServletException", e);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
try {
wrapper.deallocate(instance);
} catch (Throwable f) {
;
}
throw e;
} catch (RuntimeException e) {
// if (debug >= 2)
// log(" service() method RuntimeException", e);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
try {
wrapper.deallocate(instance);
} catch (Throwable f) {
;
}
throw e;
} catch (Throwable e) {
// if (debug >= 2)
// log(" service() method Throwable", e);
request.removeAttribute(Globals.INVOKED_ATTR);
request.removeAttribute(Globals.JSP_FILE_ATTR);
try {
wrapper.deallocate(instance);
} catch (Throwable f) {
;
}
throw new ServletException("Invoker service() exception", e);
}
// Deallocate the allocated servlet instance
String cannotDeallocateMsg = MessageFormat.format(rb.getString(LogFacade.CANNOT_DEALLOCATE_SERVLET_INSTANCE_EXCEPTION), inRequestURI);
try {
// if (debug >= 2)
// log(" deallocate servlet instance");
wrapper.deallocate(instance);
} catch (ServletException e) {
log(cannotDeallocateMsg, e);
throw e;
} catch (Throwable e) {
log(cannotDeallocateMsg, e);
throw new ServletException(cannotDeallocateMsg, e);
}
}
use of javax.servlet.UnavailableException in project Payara by payara.
the class StandardWrapperValve method invoke.
// --------------------------------------------------------- Public Methods
/**
* Invoke the servlet we are managing, respecting the rules regarding
* servlet lifecycle and SingleThreadModel support.
*
* @param request Request to be processed
* @param response Response to be produced
*
* @exception IOException if an input/output error occurred
* @exception ServletException if a servlet error occurred
*/
@Override
public int invoke(Request request, Response response) throws IOException, ServletException {
boolean unavailable = false;
Throwable throwable = null;
Servlet servlet = null;
StandardWrapper wrapper = (StandardWrapper) getContainer();
Context context = (Context) wrapper.getParent();
HttpRequest hrequest = (HttpRequest) request;
/*
* Create a request facade such that if the request was received
* at the root context, and the root context is mapped to a
* default-web-module, the default-web-module mapping is masked from
* the application code to which the request facade is being passed.
* For example, the request.facade's getContextPath() method will
* return "/", rather than the context root of the default-web-module,
* in this case.
*/
RequestFacade hreq = (RequestFacade) request.getRequest(true);
HttpServletResponse hres = (HttpServletResponse) response.getResponse();
// Check for the application being marked unavailable
if (!context.getAvailable()) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setDetailMessage(rb.getString(LogFacade.APP_UNAVAILABLE));
// END S1AS 4878272
unavailable = true;
}
// Check for the servlet being marked unavailable
if (!unavailable && wrapper.isUnavailable()) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
log(msg);
if (hres == null) {
// NOTE - Not much we can do generically
;
} else {
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE)) {
hres.setDateHeader("Retry-After", available);
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setDetailMessage(msg);
// END S1AS 4878272
} else if (available == Long.MAX_VALUE) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
}
}
unavailable = true;
}
// Allocate a servlet instance to process this request
try {
if (!unavailable) {
servlet = wrapper.allocate();
}
} catch (UnavailableException e) {
if (e.isPermanent()) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
} else {
hres.setDateHeader("Retry-After", e.getUnavailableSeconds());
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
}
} catch (ServletException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_ALLOCATE_EXCEPTION), wrapper.getName());
log(msg, StandardWrapper.getRootCause(e));
throwable = e;
exception(request, response, e);
servlet = null;
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_ALLOCATE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
servlet = null;
}
// Acknowlege the request
try {
response.sendAcknowledgement();
} catch (IOException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SEND_ACKNOWLEDGEMENT_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SEND_ACKNOWLEDGEMENT_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
servlet = null;
}
DataChunk requestPathMB = hrequest.getRequestPathMB();
hreq.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, requestPathMB);
// Create the filter chain for this request
ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
ApplicationFilterChain filterChain = factory.createFilterChain((ServletRequest) request, wrapper, servlet);
// NOTE: This also calls the servlet's service() method
try {
String jspFile = wrapper.getJspFile();
if (jspFile != null) {
hreq.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
}
// START IASRI 4665318
if (servlet != null) {
if (filterChain != null) {
filterChain.setWrapper(wrapper);
filterChain.doFilter(hreq, hres);
} else {
wrapper.service(hreq, hres, servlet);
}
}
// END IASRI 4665318
} catch (ClientAbortException e) {
throwable = e;
exception(request, response, e);
} catch (IOException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
} catch (UnavailableException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
// throwable = e;
// exception(request, response, e);
wrapper.unavailable(e);
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE)) {
hres.setDateHeader("Retry-After", available);
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
String msgServletUnavailable = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
response.setDetailMessage(msgServletUnavailable);
// END S1AS 4878272
} else if (available == Long.MAX_VALUE) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
String msgServletNotFound = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msgServletNotFound);
// END S1AS 4878272
}
// Do not save exception in 'throwable', because we
// do not want to do exception(request, response, e) processing
} catch (ServletException e) {
Throwable rootCause = StandardWrapper.getRootCause(e);
if (!(rootCause instanceof ClientAbortException)) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, rootCause);
}
throwable = e;
exception(request, response, e);
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
}
// Release the filter chain (if any) for this request
try {
if (filterChain != null)
filterChain.release();
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.RELEASE_FILTERS_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
throwable = e;
exception(request, response, e);
}
}
// Deallocate the allocated servlet instance
try {
if (servlet != null) {
wrapper.deallocate(servlet);
}
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.DEALLOCATE_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
throwable = e;
exception(request, response, e);
}
}
// unload it and release this instance
try {
if ((servlet != null) && (wrapper.getAvailable() == Long.MAX_VALUE)) {
wrapper.unload();
}
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNLOAD_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
exception(request, response, e);
}
}
return END_PIPELINE;
}
use of javax.servlet.UnavailableException in project jaffa-framework by jaffa-projects.
the class ActionServlet method initModuleConfig.
protected ModuleConfig initModuleConfig(String prefix, String paths) throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + prefix + "' configuration from '" + paths + "'");
}
// to support default behaviour.
if (!"classpath*:/META-INF/struts-config.xml".equals(paths)) {
return super.initModuleConfig(prefix, paths);
}
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
ModuleConfig config = factoryObject.createModuleConfig(prefix);
Digester digester = initConfigDigester();
PathMatchingResourcePatternResolver resolver = OrderedPathMatchingResourcePatternResolver.getInstance();
try {
Resource[] resources = resolver.getResources(paths);
if (resources != null && resources.length > 0) {
for (Resource resource : resources) {
digester.push(config);
if (resource == null) {
continue;
}
parseModuleConfigFile(digester, resource);
}
} else {
String msg = internal.getMessage("configMissing", paths);
log.error(msg);
throw new UnavailableException(msg);
}
getServletContext().setAttribute("org.apache.struts.action.MODULE" + config.getPrefix(), config);
FormBeanConfig[] fbs = config.findFormBeanConfigs();
for (int i = 0; i < fbs.length; i++) {
if (fbs[i].getDynamic()) {
fbs[i].getDynaActionFormClass();
}
}
} catch (IOException ie) {
throw new ServletException(ie);
}
return config;
}
use of javax.servlet.UnavailableException in project winstone by jenkinsci.
the class UnavailableServlet method init.
public void init() throws ServletException {
String errorTime = getServletConfig().getInitParameter("errorTime");
this.errorAtInit = ((errorTime == null) || errorTime.equals("init"));
if (this.errorAtInit)
throw new UnavailableException("Error thrown deliberately during init");
}
use of javax.servlet.UnavailableException in project sonar-java by SonarSource.
the class ActionServlet method getRequestProcessor.
/**
* <p>Look up and return the {@link RequestProcessor} responsible for the
* specified module, creating a new one if necessary.</p>
*
* @param config The module configuration for which to acquire and return
* a RequestProcessor.
* @return The {@link RequestProcessor} responsible for the specified
* module,
* @throws ServletException If we cannot instantiate a RequestProcessor
* instance a {@link UnavailableException} is
* thrown, meaning your application is not loaded
* and will not be available.
* @since Struts 1.1
*/
protected synchronized RequestProcessor getRequestProcessor(ModuleConfig config) throws ServletException {
RequestProcessor processor = this.getProcessorForModule(config);
if (processor == null) {
try {
processor = (RequestProcessor) RequestUtils.applicationInstance(config.getControllerConfig().getProcessorClass());
} catch (Exception e) {
throw new UnavailableException("Cannot initialize RequestProcessor of class " + config.getControllerConfig().getProcessorClass() + ": " + e);
}
processor.init(this, config);
String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
getServletContext().setAttribute(key, processor);
}
return (processor);
}
Aggregations