We're using JBoss and Resteasy to implement a web service, and one requirement is that we log parameters and attributes passed to the web service before processing starts. the PreProcessInterceptor is ideal for this. I have it working and logging the request parameters just fine, but some of the information is going to be passed as attributes, and I can't work out how to access them.
Below is the code I use to log the request parameters; can anyone tell me how to get the attributes as well?
@Provider
@ServerInterceptor
public class ParameterLoggingInterceptor implements PreProcessInterceptor {
@Inject
private Logger logger;
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws UnauthorizedException {
StringBuilder sb = new StringBuilder();
MultivaluedMap<String, String> mMap = request.getUri().getQueryParameters();
Set<String> keys = mMap.keySet();
int mapSize = keys.size();
if (mapSize > 0) {
sb.append("Incoming parameters: ");
int processedParams = 1;
for (String key : keys) {
sb.append(key).append("=");
sb.append(mMap.get(key));
if (processedParams < mapSize) {
sb.append(", ");
}
processedParams++;
}
logger.debug(sb.toString());
}
return null;
}
Edited to add the following in response to Eiden's answer:
Thanks, Eiden. I just gave it a try. Do you mean something like the following? If so, it doesn't seem to work (and if not, please clarify). What attributeName provides is: "org.jboss.weld.context.AbstractConversationContext", and attributeValue is the same. This is the same result I got when using the org.jboss.resteasy.spi.HttpRequest object.
@Provider
@ServerInterceptor
public class ParameterLoggingInterceptor implements PreProcessInterceptor {
@Context HttpServletRequest servletRequest;
@Inject
private Logger logger;
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws UnauthorizedException {
Enumeration<String> attributeNames = servletRequest.getAttributeNames();
StringBuilder sb = new StringBuilder();
while (attributeNames.hasMoreElements()) {
String attributeName = (String) attributeNames.nextElement();
String attributeValue = (String) servletRequest.getAttribute(attributeName);
// Do something with attributeValue and attributeName
}
}