I'm currently using below interceptor (implementing both
MessageBodyReaderInterceptor and PreProcessInterceptor) to dump incoming
requests to a log file.

I'm implementing them both because:
a) MessageBodyReaderInterceptor is not intercepting requests without body
(like a GET or OPTIONS request)
b) PreProcessInterceptor has no option to retrieve the request body.

Would there be a way to more or less 'combine' the 2 and only once call the
LOG.debug statement like
LOG.debug("Request:" + NEW_LINE + httpServletRequest + NEW_LINE + new
String(body));


@Provider
@ServerInterceptor
@Precedence("REQUEST_DUMPER")
public class RequestDumperInterceptor implements
MessageBodyReaderInterceptor, PreProcessInterceptor {

private static final Log LOG =
LogFactory.getLog(RequestDumperInterceptor.class);

private static final String NEW_LINE = System.getProperty("line.separator");

@Context
private HttpServletRequest httpServletRequest;

@Override
public Object read(MessageBodyReaderContext context) throws IOException,
WebApplicationException {
if (LOG.isDebugEnabled()) {
InputStream old = context.getInputStream();
try {
InputStreamToByteArray stream = new InputStreamToByteArray(old);
context.setInputStream(stream);
Object proceed = context.proceed();
byte[] body = stream.toByteArray();
LOG.debug("Body:" + NEW_LINE + new String(body));
return proceed;
} finally {
context.setInputStream(old);
}
}
return context.proceed();
}

@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod
method) throws Failure, WebApplicationException {
if (LOG.isDebugEnabled()) {
LOG.debug("Request:" + NEW_LINE + httpServletRequest);
}
return null;
}
}
------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second 
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users

Reply via email to