Sanyifejű
Sanyifejű

Reputation: 2740

Java. How to get all the parameters from URL, but nothing more

I want to get the parameters with their values that are part of the URL:

https://ga.acriss.com:8844/arranger/CTX4434001/HTML/CORE/maintain/schedule?dateFrom=2015-03-27&dateTo=2015-04-27#!schedule

The parameter names are arbitrary, and can be zero or many. In the case above I am interested in "dateFrom" and "dateTo". The closest thing I found is this:

  VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
    for(String key: vaadinRequest.getParameterMap().keySet()){
        vaadinRequest.getParameterMap().get(key); 
    }

Indeed it gives back the parameters I want but unfortunately also a lot of other stuff like theme, v-appId, etc..

Is there an elegant solution that gives back all parameters present in the URL(even if I don't know their name) but do not give anything that is not part of the URL? Of course I can take all the stuff and eliminate those that are not present in the URL but I wonder if there is anything better.

Upvotes: 1

Views: 1085

Answers (3)

Vic
Vic

Reputation: 437

Not familiar with VaadinRequest, but the Documentation says that the Framework's internal init parameters have prefix "v-".

You could have a method that filters out keys with "v-". Something like (Note: untested code):

private Map<String,String[]> getQueryStringParams(Map<String,String[]> parameterMap) {

    Map<String,String[]> results;

    if (parameterMap == null || parameterMap.size() == 0)
        return null;
    else
        results = new HashMap<String, String[]>();

    Iterator<String> pMapKeysIt = parameterMap.keySet().iterator();
    while (pMapKeysIt.hasNext()) {
        String key = pMapKeysIt.next();
        if (!key.contains("v-"))
            results.put(key, parameterMap.get(key));
    }

    return results;
}

Upvotes: 1

Anand saga
Anand saga

Reputation: 1503

you can get all parameter value as a key value pair.

           for(Entry temp:vaadinRequest.getParameterMap()){
                temp.getKey();
                temp.getValue();
            }

Upvotes: -1

Abhishek
Abhishek

Reputation: 7045

Here you go,

public static Map<String, String> getQueryMap(String query)  
{  
    String[] params = query.split("&");  
    Map<String, String> map = new HashMap<String, String>();  
    for (String param : params)  
    {  
        String name = param.split("=")[0];  
        String value = param.split("=")[1];  
        map.put(name, value);  
    }  
    return map;  
}

Map keys has all the parameters and Map value has all the values. Retrieve whatever you need. In your case, get keys as map.keySet().

Upvotes: 2

Related Questions