6

I want to be able pass to my server a GET request suh as:

 http://example.com/?a[foo]=1&a[bar]=15&b=3

And when I get the query param 'a', it should be parsed as HashMap like so:

{'foo':1, 'bar':15}

EDIT: Ok, to be clear, here is what I am trying to do, but in Java, instead of PHP:

Pass array with keys via HTTP GET

Any ideas how to accomplish this?

Community
  • 1
  • 1
Martin
  • 1,038
  • 14
  • 16

3 Answers3

1

There is no standard way to do that.

Wink supports javax.ws.rs.core.MultivaluedMap. So if you send something like http://example.com/?a=1&a=15&b=3 you will receive: key a values 1, 15; key b value 3.

If you need to parse something like ?a[1]=1&a[gv]=15&b=3 you need to take the javax.ws.rs.core.MultivaluedMap.entrySet() and perform an additional parsing of the keys.

Here's example of code you can use (didn't test it, so it may contain some minor errors):

String getArrayParameter(String key, String index,  MultivaluedMap<String, String> queryParameters) {
    for (Entry<String, List<String>> entry : queryParameters.entrySet()) {
        String qKey = entry.getKey();
        int a = qKey.indexOf('[');
        if (a < 0) {
            // not an array parameter
            continue;
        }
        int b = qKey.indexOf(']');
        if (b <= a) {
            // not an array parameter
            continue;
        }
        if (qKey.substring(0, a).equals(key)) {
            if (qKey.substring(a + 1, b).equals(index)) {
                return entry.getValue().get(0);
            }
        }
    }
    return null;
}

In your resource you should call it like this:

@GET
public void getResource(@Context UriInfo uriInfo) {
    MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
    getArrayParameter("a", "foo", queryParameters);
}
Tarlog
  • 9,679
  • 2
  • 41
  • 66
  • This is not what I want, because keys are also important to me. So when I get 'a' , I want to it's value to be: {'1':1, 'gv':1} – Martin Mar 11 '13 at 11:10
  • Your proposed format is not usual query parameters format: you mix the key name with index. Anyway you can get the keySet() from the map and parse it yourself. – Tarlog Mar 17 '13 at 08:20
  • This was the initial idea - is there something which I can use instead of parsing it on my own :) – Martin Mar 18 '13 at 08:51
  • The format of a query path is ?key=value&key=value&key=value... this is the normal format and this how it should be used. If you are trying to use it differently, it's up to you, but it's not standard. – Tarlog Mar 18 '13 at 08:54
  • Anyway, using a MultivaluedMap.entrySet() will help you. You'll get the parameters already parsed. You will only need to go over the keys and see if they contain '[]' so you can build your own map. – Tarlog Mar 18 '13 at 09:00
  • I edited the post. Once again - I am trying to avoid that custom parsing. I gave a link with such thing done in PHP, I have seen it in Rails too, my question is - "Is there such thing in Java?" – Martin Mar 18 '13 at 09:21
  • I also edited the answer. The very short answer is "No". The little longer: "No standard way" Sorry if the answer doesn't help you much. – Tarlog Mar 18 '13 at 09:24
1

You can pass JSON Object as String by appending it to URL. But befor that you need to encode it you can use the method given in this link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm for encoding and then just append the json object as any other string

0

Alternatively if you are using HttpServletRequest then you can use getParameterMap() method which gives you all the parameters and its values as map.

e.g.

public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws IOException {

   System.out.println("Parameters : \n"+request.getParameterMap()+"\n End");

}
Community
  • 1
  • 1
  • I don't think you understand what I want. Please read the post again. Also, the linked answer I gave - the implementation in PHP – Martin Mar 18 '13 at 10:00
  • The URL which you are mentioning is formed by your code or you are just receiving it? As if you can change the URL creation as http://example.com/?foo=1&bar=15&b=3 and then use above code you will get the map with keys as foo, bar..etc and values as 1, 15...etc – Kishor Raskar Mar 19 '13 at 06:54
  • Ok, but how can I distinguish that foo and bar are keys of `a` and b is not? – Martin Mar 19 '13 at 07:48
  • one way is keep fix format i.e. the last parameter is b or first parameter is b. – Kishor Raskar Mar 19 '13 at 09:39
  • Another way is pass 'a' as JSONObject(e.g.www.example.com/?a={'foo':1, 'bar':15}) and them use request.getParameter("a") and convert the json into Map using the code given in link : http://www.techtamasha.com/convert-json-to-map-in-java/168 – Kishor Raskar Mar 19 '13 at 09:55
  • Well, the purpose was to not use any third party library, or parse it in anyway after I got the parameter. The idea was to get it already parsed by just calling .get('a') and receive a HashMap. Anyway, it doesn't seem to exist such way in Java. – Martin Mar 19 '13 at 11:24
  • Well then if you know the URL format then you can do that. like if example.com/?foo=1&bar=15&b=3 this is the URL then call Map map= request.getParameterMap() and then map.remove(b), then the remaining map is what you needed. – Kishor Raskar Mar 20 '13 at 08:07
  • Consider that I do not know the format, just that a hashmap 'a' will be given. – Martin Mar 21 '13 at 08:22
  • Then there is no standard way.either use json or pass the request parameter with specific format and remove the parameter from the map which do not satisfy the pattern like create url example.com/?a#foo=1&a#bar=15&b=3 then then after Map map= request.getParameterMap() iterate over the map and remove the entries whose keys does not satisfy the pattern start with "a#" – Kishor Raskar Mar 21 '13 at 12:08
  • Thanks, I was already told there is no standard way. My research ends here. – Martin Mar 22 '13 at 08:07