We started auditing our clients requests on the server by its CRUD operation.
Instead of calling the audit class to log the request, we want to use a CXF Filter to pre-process & post-process each request. What do I mean?
We want to audit every update request (which contains a JSON) with its old value (before the change, a GET operation to the DB), but log it into the logger only after the request completed successfully.
We are already using this method for changing the response body when an exception is thrown (by using a jaxsrs:providers tag and an ExceptionMapper implementation), but I can't understand how to use it for pre/post processing of requests.
Thanks!
UPDATE:
Well I kinda understood what I need to do to make a CXF Interceptor:
- Create a class extending
AbstractPhaseInterceptor
- In your class constructor call
super(Phase.[the-needed-pahse])
to decide when will your interceptor run (here is a list of all incoming/outgoing phases) - Override
handleMessage
as it is the inherited method, here you'll write your logic - Add the interceptor to the
web.xml
file of your project
For example:
public void handleMessage(Message request) throws Fault {
try {
InputStream is = request.getContent(InputStream.class);
String reqBodyAsString = org.apache.commons.io.IOUtils.toString(is, "UTF-8");
IOUtils.closeQuietly(is);
// Do the logic
is = org.apache.commons.io.IOUtils.toInputStream(reqBodyAsString, "UTF-8");
request.setContent(InputStream.class, is);
org.apache.commons.io.IOUtils.closeQuietly(is);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
And the web.xml
file:
<jaxrs:server id="[id]" address="[URL]">
<jaxrs:inInterceptors><!-- because we want it to run BEFORE the request logic -->
<ref bean="[interceptor-id]" />
</jaxrs:inInterceptors>
</jaxrs:server>
<bean id="[interceptor-id]" class="[package-path.interceptor-class-name]"/>
You can see that after reading the content I write it back again into the request. That is because I get an exception saying my body is null
.
And this is where I'm stuck, I have an interceptor but after reading the request-body, it seems that the logic can't be continued.