Search in sources :

Example 1 with ExtAsyncContext

use of com.helger.servlet.async.ExtAsyncContext in project ph-web by phax.

the class XServletAsyncHandler method _handleAsync.

private void _handleAsync(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, @Nonnull final IRequestWebScope aRequestScope) {
    final ExtAsyncContext aExtAsyncCtx = ExtAsyncContext.create(aHttpRequest, aHttpResponse, eHttpVersion, eHttpMethod, m_aAsyncSpec);
    // Remember outside before it is too late :)
    final IAttributeContainerAny<String> aAttrs = aRequestScope.attrs().getClone();
    final IAttributeContainerAny<String> aParams = aRequestScope.params().getClone();
    // Put into async processing queue
    s_aAsyncServletRunner.runAsync(aHttpRequest, aHttpResponse, aExtAsyncCtx, () -> {
        try (final WebScoped aWebScoped = new WebScoped(aHttpRequest, aHttpResponse)) {
            // Restore all attributes (display locale etc.) that are missing
            aWebScoped.getRequestScope().attrs().putAllIn(aAttrs);
            aWebScoped.getRequestScope().params().putAllIn(aParams);
            m_aNestedHandler.onRequest(aExtAsyncCtx.getRequest(), aExtAsyncCtx.getResponse(), aExtAsyncCtx.getHTTPVersion(), aExtAsyncCtx.getHTTPMethod(), aWebScoped.getRequestScope());
        } catch (final Exception ex) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error("Error processing async request " + aExtAsyncCtx.getRequest(), ex);
            try {
                final String sErrorMsg = "Internal error processing your request. Please try again later. Technical details: " + ex.getClass().getName() + ":" + ex.getMessage();
                aExtAsyncCtx.getResponse().getWriter().write(sErrorMsg);
            } catch (final Exception ex2) {
                LOGGER.error("Error writing first exception to response", ex2);
            }
        } finally {
            try {
                aExtAsyncCtx.complete();
            } catch (final Exception ex) {
                LOGGER.error("Error completing async context", ex);
            }
        }
    });
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) ExtAsyncContext(com.helger.servlet.async.ExtAsyncContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

ExtAsyncContext (com.helger.servlet.async.ExtAsyncContext)1 WebScoped (com.helger.web.scope.mgr.WebScoped)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1