Class RequestParams

java.lang.Object
java.util.AbstractMap<String,String>
org.elasticsearch.rest.RequestParams
All Implemented Interfaces:
Map<String,String>

public final class RequestParams extends AbstractMap<String,String>
A Map<String, String> for HTTP request parameters that preserves multiple values per key (e.g. repeated query parameters such as match[]=foo&match[]=bar).

Each key maps to a non-empty ordered list of values. The standard Map interface operates on the last value in that list: get(Object) returns the last value for a key, or null if absent, and put(String, String) sets a key to a single value (stored as a one-element list, so getAll(String) returns a singleton list after a put). Use getAll(String) to retrieve all values for a repeated key.

The value lists returned by getAll(String) are always non-empty and immutable, so List.getFirst() and List.getLast() are always safe to call on a non-null result.

  • Method Details

    • empty

      public static RequestParams empty()
      Returns an empty RequestParams instance.
    • fromQueryString

      public static RequestParams fromQueryString(String queryString)
      Parses a URL-encoded query string into a RequestParams, preserving all values for repeated parameters (e.g. match[]=foo&match[]=bar["foo", "bar"]).
      Parameters:
      queryString - the raw query string (the part after ?, without the ? itself)
      Returns:
      a RequestParams from parameter name to all its values, in encounter order
      Throws:
      IllegalArgumentException - if the query string cannot be decoded
    • fromUri

      public static RequestParams fromUri(String uri)
      Parses the query string from a URI string into a RequestParams, preserving all values for repeated parameters. Returns an empty map if the URI contains no ?.
      Parameters:
      uri - a URI string, e.g. /index/_search?pretty&size=10
      Returns:
      a RequestParams from parameter name to all its values, in encounter order
      Throws:
      IllegalArgumentException - if the query string cannot be decoded
    • from

      public static RequestParams from(URI uri)
      Parses the query string from a URI into a RequestParams, preserving all values for repeated parameters. Returns an empty map if the URI has no query.
      Parameters:
      uri - the URI whose raw query string is parsed
      Returns:
      a RequestParams from parameter name to all its values, in encounter order
      Throws:
      IllegalArgumentException - if the query string cannot be decoded
    • fromSingleValues

      public static RequestParams fromSingleValues(Map<String,String> singleValues)
      Creates a RequestParams from a plain single-value map. Each value is wrapped in a singleton list so that getAll(String) returns a one-element list rather than an empty one.
      Parameters:
      singleValues - a map whose values are treated as the sole value for each key
    • copyOf

      public static RequestParams copyOf(RequestParams source)
      Returns an independent copy of source, preserving all multi-values.
      Parameters:
      source - the RequestParams to copy
      Returns:
      a new mutable RequestParams with the same contents as source
    • putAll

      public void putAll(RequestParams source)
      Copies all entries from source into this map, preserving all multi-values. Unlike the inherited AbstractMap.putAll(Map), which only sees the last value per key, this overload copies every value in each key's list.
    • getAll

      public List<String> getAll(String key)
      Returns all values for key in the order they were added, or an empty list if the key is absent.
      Parameters:
      key - the parameter name
      Returns:
      an unmodifiable non-empty list of all values, or an empty list if absent; never null
    • requireSingle

      public String requireSingle(String key)
      Returns the single value for key, or null if absent.
      Parameters:
      key - the parameter name
      Returns:
      the single value, or null if absent
      Throws:
      RestRequest.BadParameterException - if the key has multiple values
    • get

      public String get(Object key)
      Returns the last value associated with key, or null if absent.

      When a query parameter appears multiple times (e.g. a=1&a=2), this method returns the last value ("2"). Use getAll(String) to retrieve all values, or requireSingle(String) to assert that only one value is present.

      Specified by:
      get in interface Map<String,String>
      Overrides:
      get in class AbstractMap<String,String>
    • put

      public String put(String key, String value)
      Sets key to a single value, replacing any previous values for that key. The value is stored as a one-element immutable list, so getAll(String) will return a singleton list and List.getFirst()/List.getLast() remain safe to call.
      Specified by:
      put in interface Map<String,String>
      Overrides:
      put in class AbstractMap<String,String>
      Returns:
      the previous last value for key, or null if absent
    • size

      public int size()
      Specified by:
      size in interface Map<String,String>
      Overrides:
      size in class AbstractMap<String,String>
    • remove

      public String remove(Object key)
      Specified by:
      remove in interface Map<String,String>
      Overrides:
      remove in class AbstractMap<String,String>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,String>
      Overrides:
      clear in class AbstractMap<String,String>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,String>
      Overrides:
      containsKey in class AbstractMap<String,String>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,String>
      Overrides:
      keySet in class AbstractMap<String,String>
    • entrySet

      public Set<Map.Entry<String,String>> entrySet()
      Returns an entry set where each entry's value is the last value for that key. Supports removal via the iterator, which removes the entire key from the underlying map.
      Specified by:
      entrySet in interface Map<String,String>
      Specified by:
      entrySet in class AbstractMap<String,String>