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;
}
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;
}
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));
}
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();
}
}
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;
}
Aggregations