All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spark.QueryParamsMap Maven / Gradle / Ivy

There is a newer version: 1.1.2-fork-u1
Show newest version
package spark;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * This objects represent the parameters sent on a Http Request. 
* Parses parameters keys like in Sinatra.
*
* For a querystring like:
* * user[name]=federico&user[lastname]=dayan *
*
* We get would get a structure like:
* * user : {name: federico, lastname: dayan} * * *
*
* That is:
* * queryParamsMapInstance.get("user).get("name").value();
* queryParamsMapInstance.get("user).get("lastname").value(); * * *

* * It is null safe, meaning that if a key does not exist, it does not throw NullPointerExcetpion * , it just returns null. * * @author fddayan * */ public class QueryParamsMap { private static final QueryParamsMap NULL = new NullQueryParamsMap(); /** Holds the nested keys */ private Map queryMap = new HashMap(); /** Value(s) for this key */ private String[] values; private Pattern p = Pattern.compile("\\A[\\[\\]]*([^\\[\\]]+)\\]*"); /** * Creates a new QueryParamsMap from and HttpServletRequest.
* Parses the parameters from request.getParameterMap()
* No need to decode, since HttpServletRequest does it for us. * * @param request */ public QueryParamsMap(HttpServletRequest request) { if (request == null) { throw new IllegalArgumentException("HttpServletRequest cannot be null."); } loadQueryString(request.getParameterMap()); } // Just for testing protected QueryParamsMap() { } /** * Parses the key and creates the child QueryParamMaps * * user[info][name] creates 3 nested QueryParamMaps. For user, info and * name. * * @param key * The key in the formar fo key1[key2][key3] (for example: * user[info][name]). * @param values */ protected QueryParamsMap(String key, String... values) { loadKeys(key, values); } protected QueryParamsMap(Map params) { loadQueryString(params); } protected final void loadQueryString(Map params) { for (Map.Entry param : params.entrySet()) { loadKeys(param.getKey(), param.getValue()); } } protected final void loadKeys(String key, String[] value) { String[] parsed = parseKey(key); if (parsed == null) { return; } if (!queryMap.containsKey(parsed[0])) { queryMap.put(parsed[0], new QueryParamsMap()); } if (!parsed[1].isEmpty()) { queryMap.get(parsed[0]).loadKeys(parsed[1], value); } else { queryMap.get(parsed[0]).values = value.clone(); } } protected final String[] parseKey(String key) { Matcher m = p.matcher(key); if (m.find()) { return new String[] { cleanKey(m.group()), key.substring(m.end()) }; } else { return null; // NOSONAR } } protected static final String cleanKey(String group) { if (group.startsWith("[")) { return group.substring(1, group.length() - 1); } else { return group; } } /** * Retruns and element fro the specified key.
* For querystring:
*
* * user[name]=fede *
*
* get("user").get("name").value() # fede *
* or *
* get("user","name").value() # fede * *
* * @param key * The paramater nested key * @return */ public QueryParamsMap get(String... keys) { QueryParamsMap ret = this; for (String key : keys) { if (ret.queryMap.containsKey(key)) { ret = ret.queryMap.get(key); } else { ret = NULL; } } return ret; } /** * Returns the value for this key.
* If this key has nested elements and does not have a value returns null. * * @return */ public String value() { if (hasValue()) { return values[0]; } else { return null; } } /** * Returns the value for that key.
* * It is a shortcut for:
*
* * get("user").get("name").value() * get("user").value("name") * * * @param key * @return */ public String value(String... keys) { return get(keys).value(); } public boolean hasKeys() { return !this.queryMap.isEmpty(); } public boolean hasValue() { return this.values != null && this.values.length > 0; } public Boolean booleanValue() { return hasValue() ? Boolean.valueOf(value()) : null; } public Integer integerValue() { return hasValue() ? Integer.valueOf(value()) : null; } public Long longValue() { return hasValue() ? Long.valueOf(value()) : null; } public Float floatValue() { return hasValue() ? Float.valueOf(value()) : null; } public Double doubleValue() { return hasValue() ? Double.valueOf(value()) : null; } public String[] values() { return this.values.clone(); } /** * @return the queryMap */ Map getQueryMap() { return queryMap; } /** * @return the values */ String[] getValues() { return values; } private static class NullQueryParamsMap extends QueryParamsMap { public NullQueryParamsMap() { super(); } } public Map toMap() { Map map = new HashMap(); for (Entry key : this.queryMap.entrySet()) { map.put(key.getKey(), key.getValue().values); } return map; } }