Example usage for javax.servlet UnavailableException UnavailableException

List of usage examples for javax.servlet UnavailableException UnavailableException

Introduction

In this page you can find the example usage for javax.servlet UnavailableException UnavailableException.

Prototype


public UnavailableException(String msg, int seconds) 

Source Link

Document

Constructs a new exception with a descriptive message indicating that the servlet is temporarily unavailable and giving an estimate of how long it will be unavailable.

Usage

From source file:org.eclipse.gyrex.http.jetty.internal.app.ApplicationDelegateHandler.java

@Override
public void doScope(final String target, final Request baseRequest, final HttpServletRequest request,
        final HttpServletResponse response) throws IOException, ServletException {

    /*/* w w  w .j  a va  2 s. c o m*/
     * This scope implementation is different. We delegate to
     * the application immediately. The assumption is, the context,
     *  path info and session has been properly set up by scoping
     * from previous handlers (scopes).
     *
     * The next scope would be the servlet handler which handles
     * servlets registered via IApplicationContext. However, the
     * ServletHandler already modifies the scope by setting
     * a servlet path and path info as of the found servlet.
     * This is wrong when calling Application#handleRequest.
     *
     * It's completely up to the Application if the ServletHandler
     * should be called or not. It does so via calling the
     * IApplicationContext method. This is implemented in this class
     * as well (handleApplicationRequest) and continues with the next
     * scope.
     */

    // delegated to the application
    // the application may delegate back to us via ApplicationContext
    try {
        final Application application = applicationHandler.getApplication();

        // setup MDC
        setupMdc(application, request);

        // check application status
        final IStatus status = application.getStatus();
        if (!status.isOK()) {
            // abort request processing
            final String message = StringUtils.defaultIfEmpty(status.getMessage(), "Application not ready.");
            // we convert it into UnavailableException
            if (Platform.inDebugMode()) {
                LOG.warn("Application '{}' returned a not-ok status: {}",
                        new Object[] { application.getId(), status });
                throw new UnavailableException(message, 5);
            } else
                throw new UnavailableException(message, 30); // TODO make configurable
        }

        // route to application
        if (JettyDebug.handlers) {
            LOG.debug("routing request to application {}", application);
        }
        application.handleRequest(request, response);
    } catch (final IOException e) {
        if (Platform.inDebugMode()) {
            LOG.warn("Caught IOException while processing request '{}': {}",
                    new Object[] { request, e.getMessage(), e });
        }
        throw e;
    } catch (final ApplicationException e) {
        // handle ApplicationException
        if (e.getStatus() == HttpStatus.SERVICE_UNAVAILABLE_503) {
            // we convert it into UnavailableException
            if (Platform.inDebugMode()) {
                LOG.warn("Caught ApplicationException while processing request '{}': {}",
                        new Object[] { request, e.getMessage(), e });
                throw new UnavailableException(e.getMessage(), 5);
            } else
                throw new UnavailableException(e.getMessage(), 30); // TODO make configurable
        } else {
            if (Platform.inDebugMode()) {
                LOG.warn("Caught ApplicationException while processing request '{}': {}",
                        new Object[] { request, e.getMessage(), e });
                response.sendError(e.getStatus(), e.getMessage());
            } else {
                response.sendError(e.getStatus());
            }
        }
    } catch (final IllegalStateException e) {
        // IllegalStateException are typically used in Gyrex to indicate that something isn't ready
        // we convert it into UnavailableException to allow recovering on a dynamic platform
        if (Platform.inDebugMode()) {
            LOG.warn("Caught IllegalStateException while processing request '{}': {}",
                    new Object[] { request, e.getMessage(), e });
            throw new UnavailableException(e.getMessage(), 5);
        } else
            throw new UnavailableException(e.getMessage(), 30); // TODO make configurable
    } catch (final RuntimeException e) {
        if (Platform.inDebugMode()) {
            LOG.warn("Caught RuntimeException while processing request '{}': {}",
                    new Object[] { request, e.getMessage(), e });
        }
        throw e;
    } finally {
        // clear the MDC
        clearMdc();
    }

    // mark the request handled (if this point is reached)
    baseRequest.setHandled(true);
}