I want to log all user JSF requests (including AJAX), to do this I have implemented an @Observer for the @RequestScoped
This works however it also fires for all static content GET requests including CSS/JS/Images etc. which I do not want to log.
Is there a convenient way to determine if the request was initiated from the user and ignore static file GET's? I thought of checking the requested files extension but some of the static files seem to end with .xhtml so that won't work
I am using Omnifaces if that helps (JSF 2.3 / CDI)
public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
// logging request here
}
Provided that you're handling all resources via the therefor provided JSF components such as <h:outputScript>
, <h:outputStylesheet>
and <h:graphicImage>
, then simply check if the request path starts with ResourceHandler.RESOURCE_IDENTIFIER
.
public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
HttpServletRequest request = (HttpServletRequest) payload;
if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/")) {
return; // It's a JSF resource request.
}
// Log request here.
}
I am using Omnifaces if that helps (JSF 2.3 / CDI)
Yes, that definitely helps. There's since OmniFaces version 2.0 already a helper method which does exactly that, hereby reducing the boilerplate. It's the Servlets#isFacesResourceRequest()
:
public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
HttpServletRequest request = (HttpServletRequest) payload;
if (Servlets.isFacesResourceRequest(request)) {
return; // It's a JSF resource request.
}
// Log request here.
}