Search in sources :

Example 56 with Request

use of org.apache.catalina.connector.Request in project Payara by payara.

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 ("org.apache.catalina.servlets.InvokerHttpRequest".equals(current.getClass().getName())) {
            // KLUDGE - Make nested RD.forward() using invoker work
            break;
        }
        if (!(current instanceof ServletRequestWrapper)) {
            break;
        }
        // If we find container-generated wrapper, break out
        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));
        }
        // START OF 6364900
        crossContextFlag = crossContext;
        if (this.name != null) {
            this.mappingForDispatch = computeNamedDispatchHttpServletMapping(context, hcurrent);
        }
        if (DispatcherType.ASYNC.equals(state.dispatcherType)) {
            this.mappingForDispatch = hcurrent.getHttpServletMapping();
        }
        wrapper = new ApplicationHttpRequest(hcurrent, context, crossContext, mappingForDispatch, state.dispatcherType);
    } else {
        wrapper = new ApplicationRequest(current);
    }
    if (previous == null) {
        state.outerRequest = wrapper;
    } else {
        ((ServletRequestWrapper) previous).setRequest(wrapper);
    }
    state.wrapRequest = wrapper;
    return wrapper;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request)

Example 57 with Request

use of org.apache.catalina.connector.Request in project keycloak by keycloak.

the class KeycloakAuthenticatorValve method forwardToErrorPageInternal.

@Override
protected boolean forwardToErrorPageInternal(Request request, HttpServletResponse response, Object loginConfig) throws IOException {
    if (loginConfig == null)
        return false;
    LoginConfig config = (LoginConfig) loginConfig;
    if (config.getErrorPage() == null)
        return false;
    // had to do this to get around compiler/IDE issues :(
    try {
        Method method = null;
        /*
            for (Method m : getClass().getDeclaredMethods()) {
                if (m.getName().equals("forwardToErrorPage")) {
                    method = m;
                    break;
                }
            }
            */
        method = FormAuthenticator.class.getDeclaredMethod("forwardToErrorPage", Request.class, HttpServletResponse.class, LoginConfig.class);
        method.setAccessible(true);
        method.invoke(this, request, response, config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return true;
}
Also used : LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) Request(org.apache.catalina.connector.Request) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) FormAuthenticator(org.apache.catalina.authenticator.FormAuthenticator) IOException(java.io.IOException)

Example 58 with Request

use of org.apache.catalina.connector.Request in project nzbhydra2 by theotherp.

the class HydraEmbeddedServletContainer method customize.

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    if (!(container instanceof TomcatEmbeddedServletContainerFactory)) {
        // Is the case in tests
        return;
    }
    TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
    containerFactory.addContextValves(new ValveBase() {

        @Override
        public void invoke(Request request, Response response) throws IOException, ServletException {
            int originalPort = -1;
            final String forwardedPort = request.getHeader("X-Forwarded-Port");
            if (forwardedPort != null) {
                try {
                    originalPort = request.getServerPort();
                    request.setServerPort(Integer.valueOf(forwardedPort));
                } catch (final NumberFormatException e) {
                    logger.debug("ignoring forwarded port {}", forwardedPort);
                }
            }
            final MessageBytes serverNameMB = request.getCoyoteRequest().serverName();
            String originalServerName = null;
            String forwardedHost = request.getHeader("X-Forwarded-Host");
            if (forwardedHost == null) {
                forwardedHost = request.getHeader("host");
            }
            if (forwardedHost != null) {
                int colonIndex = forwardedHost.indexOf(":");
                if (colonIndex > -1) {
                    if (originalPort == -1) {
                        originalPort = request.getServerPort();
                    }
                    request.setServerPort(Integer.valueOf(forwardedHost.substring(colonIndex + 1)));
                    forwardedHost = forwardedHost.substring(0, colonIndex);
                }
                originalServerName = serverNameMB.getString();
                serverNameMB.setString(forwardedHost);
            }
            Boolean originallySecure = null;
            final String forwardedProto = request.getHeader("X-Forwarded-Proto");
            if (forwardedProto != null) {
                originallySecure = request.isSecure();
                request.setSecure(forwardedProto.equalsIgnoreCase("https"));
            }
            try {
                getNext().invoke(request, response);
            } finally {
                if (originallySecure != null) {
                    request.setSecure(originallySecure);
                }
                if (forwardedHost != null) {
                    serverNameMB.setString(originalServerName);
                }
                if (forwardedPort != null) {
                    request.setServerPort(originalPort);
                }
            }
        }
    });
    ((TomcatEmbeddedServletContainerFactory) container).addContextCustomizers(context -> context.setMapperContextRootRedirectEnabled(true));
}
Also used : Response(org.apache.catalina.connector.Response) ServletException(javax.servlet.ServletException) TomcatEmbeddedServletContainerFactory(org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory) Request(org.apache.catalina.connector.Request) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) ValveBase(org.apache.catalina.valves.ValveBase) IOException(java.io.IOException)

Example 59 with Request

use of org.apache.catalina.connector.Request 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 60 with Request

use of org.apache.catalina.connector.Request 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)

Aggregations

Request (org.apache.catalina.connector.Request)80 Test (org.junit.Test)44 Response (org.apache.catalina.connector.Response)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 IOException (java.io.IOException)9 HttpSession (javax.servlet.http.HttpSession)9 Context (org.apache.catalina.Context)9 ServletRequest (javax.servlet.ServletRequest)8 Valve (org.apache.catalina.Valve)7 RequestFacade (org.apache.catalina.connector.RequestFacade)7 TesterContext (org.apache.tomcat.unittest.TesterContext)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)5 HttpSession (jakarta.servlet.http.HttpSession)5 ServletException (javax.servlet.ServletException)5 Connector (org.apache.catalina.connector.Connector)5 ServletRequest (jakarta.servlet.ServletRequest)4 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)4 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)3 LifecycleException (org.apache.catalina.LifecycleException)3 ServletException (jakarta.servlet.ServletException)2