Search in sources :

Example 1 with ServletRequestWrapper

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

the class ApplicationDispatcher method unwrapRequest.

/**
 * Unwrap the request if we have wrapped it.
 */
private void unwrapRequest(State state) {
    if (state.wrapRequest == null) {
        return;
    }
    if (state.outerRequest.isAsyncStarted()) {
        if (!state.outerRequest.getAsyncContext().hasOriginalRequestAndResponse()) {
            return;
        }
    }
    ServletRequest previous = null;
    ServletRequest current = state.outerRequest;
    while (current != null) {
        // If we run into the container request we are done
        if ((current instanceof Request) || (current instanceof RequestFacade)) {
            break;
        }
        // Remove the current request if it is our wrapper
        if (current == state.wrapRequest) {
            ServletRequest next = ((ServletRequestWrapper) current).getRequest();
            if (previous == null) {
                state.outerRequest = next;
            } else {
                ((ServletRequestWrapper) previous).setRequest(next);
            }
            break;
        }
        // Advance to the next request in the chain
        previous = current;
        current = ((ServletRequestWrapper) current).getRequest();
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request) ServletRequest(jakarta.servlet.ServletRequest) RequestFacade(org.apache.catalina.connector.RequestFacade)

Example 2 with ServletRequestWrapper

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

the class ApplicationDispatcher method wrapRequest.

/**
 * Create and return a request wrapper that has been inserted in the
 * appropriate spot in the request chain.
 */
private ServletRequest wrapRequest(State state) {
    // Locate the request we should insert in front of
    ServletRequest previous = null;
    ServletRequest current = state.outerRequest;
    while (current != null) {
        if (state.hrequest == null && (current instanceof HttpServletRequest)) {
            state.hrequest = (HttpServletRequest) current;
        }
        if (!(current instanceof ServletRequestWrapper)) {
            break;
        }
        if (current instanceof ApplicationHttpRequest) {
            break;
        }
        if (current instanceof ApplicationRequest) {
            break;
        }
        previous = current;
        current = ((ServletRequestWrapper) current).getRequest();
    }
    // Instantiate a new wrapper at this point and insert it in the chain
    ServletRequest wrapper = null;
    if ((current instanceof ApplicationHttpRequest) || (current instanceof Request) || (current instanceof HttpServletRequest)) {
        // Compute a crossContext flag
        HttpServletRequest hcurrent = (HttpServletRequest) current;
        boolean crossContext = false;
        if ((state.outerRequest instanceof ApplicationHttpRequest) || (state.outerRequest instanceof Request) || (state.outerRequest instanceof HttpServletRequest)) {
            HttpServletRequest houterRequest = (HttpServletRequest) state.outerRequest;
            Object contextPath = houterRequest.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH);
            if (contextPath == null) {
                // Forward
                contextPath = houterRequest.getContextPath();
            }
            crossContext = !(context.getPath().equals(contextPath));
        }
        wrapper = new ApplicationHttpRequest(hcurrent, context, crossContext);
    } else {
        wrapper = new ApplicationRequest(current);
    }
    if (previous == null) {
        state.outerRequest = wrapper;
    } else {
        ((ServletRequestWrapper) previous).setRequest(wrapper);
    }
    state.wrapRequest = wrapper;
    return wrapper;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request) ServletRequest(jakarta.servlet.ServletRequest)

Example 3 with ServletRequestWrapper

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

the class ApplicationDispatcher method checkSameObjects.

private void checkSameObjects(ServletRequest appRequest, ServletResponse appResponse) throws ServletException {
    ServletRequest originalRequest = ApplicationFilterChain.getLastServicedRequest();
    ServletResponse originalResponse = ApplicationFilterChain.getLastServicedResponse();
    // Some forwards, eg from valves will not set original values
    if (originalRequest == null || originalResponse == null) {
        return;
    }
    boolean same = false;
    ServletRequest dispatchedRequest = appRequest;
    // find the request that was passed into the service method
    while (originalRequest instanceof ServletRequestWrapper && ((ServletRequestWrapper) originalRequest).getRequest() != null) {
        originalRequest = ((ServletRequestWrapper) originalRequest).getRequest();
    }
    // compare with the dispatched request
    while (!same) {
        if (originalRequest.equals(dispatchedRequest)) {
            same = true;
        }
        if (!same && dispatchedRequest instanceof ServletRequestWrapper) {
            dispatchedRequest = ((ServletRequestWrapper) dispatchedRequest).getRequest();
        } else {
            break;
        }
    }
    if (!same) {
        throw new ServletException(sm.getString("applicationDispatcher.specViolation.request"));
    }
    same = false;
    ServletResponse dispatchedResponse = appResponse;
    // find the response that was passed into the service method
    while (originalResponse instanceof ServletResponseWrapper && ((ServletResponseWrapper) originalResponse).getResponse() != null) {
        originalResponse = ((ServletResponseWrapper) originalResponse).getResponse();
    }
    // compare with the dispatched response
    while (!same) {
        if (originalResponse.equals(dispatchedResponse)) {
            same = true;
        }
        if (!same && dispatchedResponse instanceof ServletResponseWrapper) {
            dispatchedResponse = ((ServletResponseWrapper) dispatchedResponse).getResponse();
        } else {
            break;
        }
    }
    if (!same) {
        throw new ServletException(sm.getString("applicationDispatcher.specViolation.response"));
    }
}
Also used : ServletException(jakarta.servlet.ServletException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) ServletResponseWrapper(jakarta.servlet.ServletResponseWrapper)

Aggregations

ServletRequest (jakarta.servlet.ServletRequest)3 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)3 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 Request (org.apache.catalina.connector.Request)2 ServletException (jakarta.servlet.ServletException)1 ServletResponse (jakarta.servlet.ServletResponse)1 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 RequestFacade (org.apache.catalina.connector.RequestFacade)1