save some attributes for request log and error dispatch
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
8550493c31
commit
57a863088b
|
@ -97,6 +97,9 @@ import org.eclipse.jetty.util.security.SecurityUtils;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.eclipse.jetty.ee9.nested.HttpChannel.SERVLET_CONTEXT_ATTRIBUTE;
|
||||
import static org.eclipse.jetty.ee9.nested.HttpChannel.SERVLET_PATH_IN_CONTEXT_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
* ContextHandler.
|
||||
*
|
||||
|
@ -902,6 +905,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Supplie
|
|||
(DispatcherType.INCLUDE.equals(dispatch) || !target.startsWith("/")) ? oldPathInContext : pathInContext);
|
||||
|
||||
org.eclipse.jetty.server.handler.ContextHandler.ScopedContext context = getCoreContextHandler().getContext();
|
||||
|
||||
baseRequest.setAttribute(SERVLET_CONTEXT_ATTRIBUTE, _apiContext);
|
||||
baseRequest.setAttribute(SERVLET_PATH_IN_CONTEXT_ATTRIBUTE, baseRequest.getPathInContext());
|
||||
|
||||
if (context == org.eclipse.jetty.server.handler.ContextHandler.getCurrentContext())
|
||||
{
|
||||
nextScope(target, baseRequest, request, response);
|
||||
|
|
|
@ -28,6 +28,7 @@ import java.util.Map;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.servlet.RequestDispatcher;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
@ -44,6 +45,8 @@ import org.eclipse.jetty.util.StringUtil;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.eclipse.jetty.ee9.nested.HttpChannel.SERVLET_CONTEXT_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
* Handler for Error pages
|
||||
*/
|
||||
|
@ -88,7 +91,7 @@ public class ErrorHandler extends AbstractHandler
|
|||
// This logic really should be in ErrorPageErrorHandler, but some implementations extend ErrorHandler
|
||||
// and implement ErrorPageMapper directly, so we do this here in the base class.
|
||||
String errorPage = (this instanceof ErrorPageMapper) ? ((ErrorPageMapper)this).getErrorPage(request) : null;
|
||||
ContextHandler.APIContext context = baseRequest.getErrorContext();
|
||||
ServletContext context = (ServletContext)request.getAttribute(SERVLET_CONTEXT_ATTRIBUTE);
|
||||
Dispatcher errorDispatcher = (errorPage != null && context != null)
|
||||
? (Dispatcher)context.getRequestDispatcher(errorPage) : null;
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ import java.util.stream.Stream;
|
|||
|
||||
import jakarta.servlet.DispatcherType;
|
||||
import jakarta.servlet.RequestDispatcher;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletException;
|
||||
import org.eclipse.jetty.http.BadMessageException;
|
||||
import org.eclipse.jetty.http.HttpException;
|
||||
|
@ -72,6 +73,9 @@ import static org.eclipse.jetty.util.thread.Invocable.InvocationType.NON_BLOCKIN
|
|||
public class HttpChannel implements Runnable, HttpOutput.Interceptor
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HttpChannel.class);
|
||||
public static final String SERVLET_CONTEXT_ATTRIBUTE = "jetty.servletContext.attribute";
|
||||
public static final String SERVLET_NAME_ATTRIBUTE = "jetty.servletName.attribute";
|
||||
public static final String SERVLET_PATH_IN_CONTEXT_ATTRIBUTE = "jetty.servletPathInContext.attribute";
|
||||
|
||||
private final ContextHandler _contextHandler;
|
||||
private final ConnectionMetaData _connectionMetaData;
|
||||
|
@ -1010,11 +1014,15 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
|
|||
if (idleTO >= 0 && getIdleTimeout() != _oldIdleTimeout)
|
||||
setIdleTimeout(_oldIdleTimeout);
|
||||
|
||||
// TODO: We're not in scope for the servlet context so this won't always work.
|
||||
if (getServer().getRequestLog() instanceof CustomRequestLog)
|
||||
{
|
||||
String servletName = (String)_request.getAttribute(SERVLET_NAME_ATTRIBUTE);
|
||||
String pathInContext = (String)_request.getAttribute(SERVLET_PATH_IN_CONTEXT_ATTRIBUTE);
|
||||
ServletContext servletContext = (ServletContext)_request.getAttribute(SERVLET_CONTEXT_ATTRIBUTE);
|
||||
CustomRequestLog.LogDetail logDetail = new CustomRequestLog.LogDetail(
|
||||
_request.getServletName(),
|
||||
_request.getServletContext().getRealPath(_request.getPathInContext())
|
||||
servletName,
|
||||
servletContext.getRealPath(pathInContext)
|
||||
);
|
||||
_request.setAttribute(CustomRequestLog.LOG_DETAIL, logDetail);
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import static jakarta.servlet.RequestDispatcher.ERROR_MESSAGE;
|
|||
import static jakarta.servlet.RequestDispatcher.ERROR_REQUEST_URI;
|
||||
import static jakarta.servlet.RequestDispatcher.ERROR_SERVLET_NAME;
|
||||
import static jakarta.servlet.RequestDispatcher.ERROR_STATUS_CODE;
|
||||
import static org.eclipse.jetty.ee9.nested.HttpChannel.SERVLET_CONTEXT_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
* Implementation of AsyncContext interface that holds the state of request-response cycle.
|
||||
|
@ -922,7 +923,8 @@ public class HttpChannelState
|
|||
response.setStatus(code);
|
||||
response.errorClose();
|
||||
|
||||
request.setAttribute(ErrorHandler.ERROR_CONTEXT, request.getErrorContext());
|
||||
ServletContext errorContext = (ServletContext)request.getAttribute(SERVLET_CONTEXT_ATTRIBUTE);
|
||||
request.setAttribute(ErrorHandler.ERROR_CONTEXT, errorContext);
|
||||
request.setAttribute(ERROR_REQUEST_URI, request.getRequestURI());
|
||||
request.setAttribute(ERROR_SERVLET_NAME, request.getServletName());
|
||||
request.setAttribute(ERROR_STATUS_CODE, code);
|
||||
|
|
|
@ -702,15 +702,6 @@ public class Request implements HttpServletRequest
|
|||
_pathInContext = pathInContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The current {@link ContextHandler.APIContext context} used for this error handling for this request. If the request is asynchronous,
|
||||
* then it is the context that called async. Otherwise it is the last non-null context passed to #setContext
|
||||
*/
|
||||
public ContextHandler.APIContext getErrorContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContextPath()
|
||||
{
|
||||
|
|
|
@ -70,6 +70,8 @@ import org.eclipse.jetty.util.thread.AutoLock;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.eclipse.jetty.ee9.nested.HttpChannel.SERVLET_NAME_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
* Servlet HttpHandler.
|
||||
* <p>
|
||||
|
@ -470,6 +472,7 @@ public class ServletHandler extends ScopedHandler
|
|||
if (servletPathMapping != null)
|
||||
{
|
||||
baseRequest.setServletPathMapping(servletPathMapping);
|
||||
baseRequest.setAttribute(SERVLET_NAME_ATTRIBUTE, servletPathMapping.getServletName());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
package org.eclipse.jetty.ee9.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import jakarta.servlet.AsyncContext;
|
||||
import jakarta.servlet.AsyncEvent;
|
||||
|
@ -30,6 +29,7 @@ import org.eclipse.jetty.client.HttpClient;
|
|||
import org.eclipse.jetty.ee9.nested.ContextHandler;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.util.BlockingArrayQueue;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -44,7 +44,7 @@ public class ContextHandlerClassLoaderTest
|
|||
private ServerConnector _connector;
|
||||
private Server _server;
|
||||
private HttpClient _client;
|
||||
private final Queue<String> _asyncListenerLog = new ArrayDeque<>();
|
||||
private final BlockingArrayQueue<String> _asyncListenerLog = new BlockingArrayQueue<>();
|
||||
|
||||
public static class MyCustomClassLoader extends ClassLoader
|
||||
{
|
||||
|
@ -139,7 +139,7 @@ public class ContextHandlerClassLoaderTest
|
|||
assertThat(responseContent, containsString("MyCustomClassLoader"));
|
||||
|
||||
// AsyncListener should also have the correct ClassLoader set.
|
||||
assertThat(_asyncListenerLog.poll(), equalTo("onComplete(): MyCustomClassLoader"));
|
||||
assertThat(_asyncListenerLog.poll(5, TimeUnit.SECONDS), equalTo("onComplete(): MyCustomClassLoader"));
|
||||
assertThat(_asyncListenerLog.size(), equalTo(0));
|
||||
}
|
||||
|
||||
|
@ -159,7 +159,7 @@ public class ContextHandlerClassLoaderTest
|
|||
assertThat(responseContent, containsString("MyCustomClassLoader"));
|
||||
|
||||
// AsyncListener should also have the correct ClassLoader set.
|
||||
assertThat(_asyncListenerLog.poll(), equalTo("onComplete(): MyCustomClassLoader"));
|
||||
assertThat(_asyncListenerLog.poll(5, TimeUnit.SECONDS), equalTo("onComplete(): MyCustomClassLoader"));
|
||||
assertThat(_asyncListenerLog.size(), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue