users@jersey.java.net

Re: [Jersey] Jersey and UriInfo.getQueryParameters()

From: Wilhelmsen Tor Iver <TorIverW_at_arrive.no>
Date: Tue, 7 Apr 2009 11:36:07 +0200

> You could change your code to utilize the following Jersey feature:
>
> MultivaluedMap<String, String> queryParams =
> UriComponent.decodeQuery(uri, false);
>
> For example, sometimes you can have a query parameter with no
> value and your regex will will not match that case.

Great idea, the modified code works fine:

import java.net.URI;
import java.util.List;
import java.util.Map.Entry;

import javax.ws.rs.core.MultivaluedMap;

import com.meterware.httpunit.GetMethodWebRequest;
import com.sun.jersey.api.uri.UriComponent;

public class UnitTestUtil {

        public static final GetMethodWebRequest
bypassBugInServletUnit(String complete) {
                URI uri = URI.create(complete);
                // This is the culprit that strips away query...
                GetMethodWebRequest ret = new
GetMethodWebRequest(complete);
                // ... and forces us to add them afterwards
                String query = uri.getQuery();
                if (query != null) {
                        MultivaluedMap<String, String> queryParams =
UriComponent.decodeQuery(uri, false);
                        for (Entry<String, List<String>> entry:
queryParams.entrySet()) {
                                final List<String> values =
entry.getValue();
                                final String[] array = new
String[values.size()];
                                ret.setParameter(entry.getKey(),
values.toArray(array));
                        }
                }
                return ret;
        }
}