1

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
    }
}
Redboots
  • 115
  • 3
  • 11

1 Answers1

4

RestEasy can inject the HttpServletRequest into your interceptor:

@Provider
@ServerInterceptor
public class ParameterLoggingInterceptor implements PreProcessInterceptor {
    @javax.ws.rs.core.Context HttpServletRequest servletRequest;

}

Then you can access the HttpServletRequest directly from the preProcess-method and extract the data necessary to fulfill your logging requirements.

eiden
  • 2,329
  • 19
  • 19
  • Thanks, Eiden. I've edited my question, above, to show what happened when I tried this; not sure if I've correctly implemented your answer, so please take a look. – Redboots Aug 23 '12 at 13:47
  • See also this: http://stackoverflow.com/questions/3699549/resteasy-security-interceptor-how-to-obtain-client-ip-address-inside-intercept – Redboots Aug 23 '12 at 13:57
  • PreProcessInterceptor is deprecated now, what is the new way? http://stackoverflow.com/q/31240026/1694323 – Reza Jul 06 '15 at 18:05