Mark,
On 1/14/22 11:27, Mark Thomas wrote:
On 14/01/2022 14:50, Christopher Schultz wrote:
<snip/>
But I'm not sure how to get the "[Service]" name. I can cast
HTtpServletRequest to o.a.c.connector.Request and from there I can get
the o.a.coyote.Request but neither of those seem to have access to the
engine, service, etc. I could make it a configurable setting of the
Filter, with "Catalina" being the default. Is there any other way to
get the service name from within a running Tomcat?
Via the servletcontext? You should be able to work up the contect ->
Host -> Engine -> Service tree.
Looks like something similar to the following would work:
if(response instanceof HttpServletResponse) {
HttpServletResponse httpResponse =
(HttpServletResponse)response;
ServletContext context = request.getServletContext();
String jvmRoute = null;
if(context instanceof
org.apache.catalina.core.StandardContext) {
org.apache.catalina.Container parent =
((org.apache.catalina.core.StandardContext)context).getParent();
while(!(parent instanceof org.apache.catalina.Engine)) {
parent = parent.getParent();
}
if(null == parent) {
// :/
} else {
jvmRoute =
((org.apache.catalina.Engine)parent).getJvmRoute();
}
}
if(null != jvmRoute && 0 != jvmRoute.length()) {
httpResponse.setHeader("X-App-Route", jvmRoute);
}
httpResponse.setHeader("X-App-IP", request.getLocalAddr() +
":" + request.getLocalPort());
}
Does this look like something potentially useful to add to Tomcat? I'd
add all kinds of configuration options like setting the header names,
conditionally including certain items (i.e. maybe you don't want to
reveal internal IP addresses), etc.
Actually... I think you'd want to get this information even if a Valve
(e.g. authentication) were to intercept the request and return it early.
Maybe this should be a Valve and not a Filter.
WDYT?
-chris
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org