Class QueryParams
- java.lang.Object
-
- io.grpc.QueryParams
-
@Internal public final class QueryParams extends Object
A parser and mutable container class forapplication/x-www-form-urlencoded-style URL parameters as conceived by RFC 1866 Section 8.2.1.For example, a URI like
"http://who?name=John+Doe&role=admin&role=user&active"has:- A key
namewith valueJohn Doe - A key
rolewith valueadmin - A second key named
rolewith valueuser - "Lone" key
activewithout a value.
This class is meant to be used with
Uri. For example:Uri uri = Uri.parse("http://who?name=John+Doe&role=admin&role=user&active"); QueryParams params = QueryParams.fromRawQuery(uri.getRawQuery()); params.asList().removeIf(e -> "role".equals(e.getKey()) && "admin".equals(e.getValue())); Uri modifiedUri = uri.toBuilder().setRawQuery(params.toRawQuery()).build();Note that the empty collection is encoded as a null raw query string, which means "absent" to
Uri.Builder.setRawQuery(java.lang.String). An empty string query component (""), on the other hand, is modeled as an instance of QueryParams containing a single lone (empty) key. It must be this way if we are to simultaneously 1) support lone keys, 2) have parse/toRawQuery round-trip transparency, and 3) never fail to parse a valid RFC 3986 query component.This container and its
QueryParams.Entrytake the same position asUrion equality: raw keys and values must match exactly to be equal. Most callers won't care about how keys and values are encoded on the wire and will work with the getters for cooked keys and values instead.Instances are not safe for concurrent access by multiple threads, including by way of the
asList()view method. - A key
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryParams.EntryA single query parameter entry.
-
Constructor Summary
Constructors Constructor Description QueryParams()Creates a new, emptyQueryParamsinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<QueryParams.Entry>asList()Returns a mutable list view of the query parameters.booleanequals(Object o)static QueryParamsfromRawQuery(String rawQuery)Parses a raw query string into aQueryParamsinstance.inthashCode()StringtoRawQuery()Returns the "raw" query string representation of these parameters, suitable for passing to theUri.Builder.setRawQuery(java.lang.String)method.StringtoString()
-
-
-
Method Detail
-
fromRawQuery
public static QueryParams fromRawQuery(@Nullable String rawQuery)
Parses a raw query string into aQueryParamsinstance.The input is split on
'&'and each parameter is parsed as either a key/value pair (if it contains an equals sign) or a "lone" key (if it does not).No valid RFC 3986 query component will fail to parse. For example,
===is parsed as a single parameter with "" as the key and "==" as the value.&&&is parsed as three lone keys named "". And so on. IfrawQueryis not a valid RFC 3986 query component, the behavior is undefined. But if you are starting with aUri, passing the value returned byUri.getRawQuery()is always well-defined and will never fail.Calling
toRawQuery()on the returned object is guaranteed to return exactlyrawQuery.- Parameters:
rawQuery- the raw query component to parse, or null to return an empty container- Returns:
- a new instance of
QueryParamsrepresenting the input
-
asList
public List<QueryParams.Entry> asList()
Returns a mutable list view of the query parameters.- Returns:
- the mutable list of entries
-
toRawQuery
@Nullable public String toRawQuery()
Returns the "raw" query string representation of these parameters, suitable for passing to theUri.Builder.setRawQuery(java.lang.String)method.- Returns:
- the raw query string
-
-