Search in sources :

Example 1 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project atmosphere by Atmosphere.

the class AtmosphereFilterChain method doFilter.

/**
 * Invoke the next filter in this chain, passing the specified request
 * and response.  If there are no more filters in this chain, invoke
 * the <code>service()</code> method of the servlet itself.
 *
 * @param request  The servlet request we are processing
 * @param response The servlet response we are creating
 * @throws IOException      if an input/output error occurs
 * @throws ServletException if a servlet exception occurs
 */
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    // Call the next filter if there is one
    AtomicInteger pos = ((AtomicInteger) request.getAttribute("pos"));
    if (pos.get() < n) {
        FilterConfigImpl filterConfig = filters[pos.getAndIncrement()];
        Filter filter = null;
        try {
            filter = filterConfig.getFilter();
            filter.doFilter(request, response, this);
        } catch (IOException e) {
            throw e;
        } catch (ServletException e) {
            throw e;
        } catch (RuntimeException e) {
            throw e;
        } catch (Throwable e) {
            throw new ServletException("Throwable", e);
        }
        return;
    }
    try {
        if (servlet != null) {
            servlet.service(request, response);
        } else {
            RequestDispatcher rd = configImpl.getServletContext().getNamedDispatcher("default");
            if (rd == null) {
                throw new ServletException("No Servlet Found");
            }
            rd.forward(request, response);
        }
    } catch (IOException e) {
        throw e;
    } catch (ServletException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new ServletException("Throwable", e);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Filter(jakarta.servlet.Filter) IOException(java.io.IOException) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 2 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class FormAuthenticator method forwardToErrorPage.

/**
 * Called to forward to the error page
 *
 * @param request Request we are processing
 * @param response Response we are populating
 * @param config    Login configuration describing how authentication
 *              should be performed
 * @throws IOException  If the forward to the error page fails and the call
 *                      to {@link HttpServletResponse#sendError(int, String)}
 *                      throws an {@link IOException}
 */
protected void forwardToErrorPage(Request request, HttpServletResponse response, LoginConfig config) throws IOException {
    String errorPage = config.getErrorPage();
    if (errorPage == null || errorPage.length() == 0) {
        String msg = sm.getString("formAuthenticator.noErrorPage", context.getName());
        log.warn(msg);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        return;
    }
    RequestDispatcher disp = context.getServletContext().getRequestDispatcher(config.getErrorPage());
    try {
        if (context.fireRequestInitEvent(request.getRequest())) {
            disp.forward(request.getRequest(), response);
            context.fireRequestDestroyEvent(request.getRequest());
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        String msg = sm.getString("formAuthenticator.forwardErrorFail");
        log.warn(msg, t);
        request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    }
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 3 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class FormAuthenticator method forwardToLoginPage.

/**
 * Called to forward to the login page
 *
 * @param request Request we are processing
 * @param response Response we are populating
 * @param config    Login configuration describing how authentication
 *              should be performed
 * @throws IOException  If the forward to the login page fails and the call
 *                      to {@link HttpServletResponse#sendError(int, String)}
 *                      throws an {@link IOException}
 */
protected void forwardToLoginPage(Request request, HttpServletResponse response, LoginConfig config) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("formAuthenticator.forwardLogin", request.getRequestURI(), request.getMethod(), config.getLoginPage(), context.getName()));
    }
    String loginPage = config.getLoginPage();
    if (loginPage == null || loginPage.length() == 0) {
        String msg = sm.getString("formAuthenticator.noLoginPage", context.getName());
        log.warn(msg);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        return;
    }
    if (getChangeSessionIdOnAuthentication()) {
        Session session = request.getSessionInternal(false);
        if (session != null) {
            String newSessionId = changeSessionID(request, session);
            session.setNote(Constants.SESSION_ID_NOTE, newSessionId);
        }
    }
    // Always use GET for the login page, regardless of the method used
    String oldMethod = request.getMethod();
    request.getCoyoteRequest().method().setString("GET");
    RequestDispatcher disp = context.getServletContext().getRequestDispatcher(loginPage);
    try {
        if (context.fireRequestInitEvent(request.getRequest())) {
            disp.forward(request.getRequest(), response);
            context.fireRequestDestroyEvent(request.getRequest());
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        String msg = sm.getString("formAuthenticator.forwardLoginFail");
        log.warn(msg, t);
        request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } finally {
        // Restore original method so that it is written into access log
        request.getCoyoteRequest().method().setString(oldMethod);
    }
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) Session(org.apache.catalina.Session)

Example 4 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch(ServletContext servletContext, String path) {
    synchronized (asyncContextLock) {
        if (log.isDebugEnabled()) {
            logDebug("dispatch   ");
        }
        check();
        if (dispatch != null) {
            throw new IllegalStateException(sm.getString("asyncContextImpl.dispatchingStarted"));
        }
        if (request.getAttribute(ASYNC_REQUEST_URI) == null) {
            request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI());
            request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath());
            request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath());
            request.setAttribute(ASYNC_PATH_INFO, request.getPathInfo());
            request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString());
        }
        final RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher(path);
        if (!(requestDispatcher instanceof AsyncDispatcher)) {
            throw new UnsupportedOperationException(sm.getString("asyncContextImpl.noAsyncDispatcher"));
        }
        final AsyncDispatcher applicationDispatcher = (AsyncDispatcher) requestDispatcher;
        final ServletRequest servletRequest = getRequest();
        final ServletResponse servletResponse = getResponse();
        this.dispatch = new AsyncRunnable(request, applicationDispatcher, servletRequest, servletResponse);
        this.request.getCoyoteRequest().action(ActionCode.ASYNC_DISPATCH, null);
        clearServletRequestResponse();
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncDispatcher(org.apache.catalina.AsyncDispatcher) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 5 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class SSIServletExternalResolver method getFileText.

// We are making lots of unnecessary copies of the included data here. If
// someone ever complains that this is slow, we should connect the included
// stream to the print writer that SSICommand uses.
@Override
public String getFileText(String originalPath, boolean virtual) throws IOException {
    try {
        ServletContextAndPath csAndP = getServletContextAndPath(originalPath, virtual);
        ServletContext context = csAndP.getServletContext();
        String path = csAndP.getPath();
        RequestDispatcher rd = context.getRequestDispatcher(path);
        if (rd == null) {
            throw new IOException(sm.getString("ssiServletExternalResolver.requestDispatcherError", path));
        }
        ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
        ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(res, basos);
        rd.include(req, responseIncludeWrapper);
        // We can't assume the included servlet flushed its output
        responseIncludeWrapper.flushOutputStreamOrWriter();
        byte[] bytes = basos.toByteArray();
        // Assume platform default encoding unless otherwise specified
        String retVal;
        if (inputEncoding == null) {
            retVal = new String(bytes);
        } else {
            retVal = new String(bytes, B2CConverter.getCharset(inputEncoding));
        }
        // were included, but not sure how else to tell.
        if (retVal.equals("") && !req.getMethod().equalsIgnoreCase("HEAD")) {
            throw new IOException(sm.getString("ssiServletExternalResolver.noFile", path));
        }
        return retVal;
    } catch (ServletException e) {
        throw new IOException(sm.getString("ssiServletExternalResolver.noIncludeFile", originalPath), e);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (jakarta.servlet.RequestDispatcher)14 ServletContext (jakarta.servlet.ServletContext)4 ServletException (jakarta.servlet.ServletException)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 Filter (jakarta.servlet.Filter)1 ServletRequest (jakarta.servlet.ServletRequest)1 ServletResponse (jakarta.servlet.ServletResponse)1 BodyContent (jakarta.servlet.jsp.tagext.BodyContent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AsyncDispatcher (org.apache.catalina.AsyncDispatcher)1 Session (org.apache.catalina.Session)1 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1