Jersey + Jetty = less hair

This is mainly for future me when I run into this problem again. I needed to use Jersey 2.x and had setup an embedded Jetty server for some integration testing. One portion needed to get the parameters from a POST request and needed to pass on the HttpServletRequest. However, injecting the HttpServletRequest always returned empty values for the params. Jersey was consuming them before hand. In order to get around this, I had to implement a subclass of HttpServletRequestWrapper, here is the underlying code:

public class JerseyMultiValueMapWrapper extends HttpServletRequestWrapper {

   MultivaluedMap<String, String> oAuth2Params;
   public JerseyMultiValueMapWrapper(HttpServletRequest request) {
     super(request);
   }
	
   public JerseyMultiValueMapWrapper(MultivaluedMap<String, String> params, HttpServletRequest request) {
     super(request);
     oAuth2Params = params;
   }
	
   @Override
   public String getParameter(String name) {
     return oAuth2Params.getFirst(name);
   }
}

You can then pass this along to the API that expects the HttpServletRequest. I needed this mainly when working with the Apache Oltu api, as it only accepted HttpServletRequests and kept failing validation due to missing parameters.

Hopefully this helps some others.

Advertisements
This entry was posted in java, open source. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s