Details
-
New Feature
-
Status: Resolved
-
Trivial
-
Resolution: Won't Fix
-
4.5.5, 4.5.6, 5.0 Beta1, 5.0 Beta2
-
None
-
Patch
Description
It would be convenient to have a removeParameter method added to class URIBuilder.
One rationale for this proposal is the use case when of reusing an URIBuilder to create multiple similar HMAC signed requests. In that scenario it is not sufficient to invoke setParameter with the values of the parameters that have changed. One also needs to remove the signature parameter from the previous request before the current request can be signed and a new signature parameter added to it.
This would be the implementation of the proposed method removeParameter:
/** * Remove parameter of URI query if set. The parameter name is expected to be unescaped and * may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> */ public URIBuilder removeParameter(final String param) { if (this.queryParams == null) { return this; } if (!this.queryParams.isEmpty()) { for (final Iterator<NameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) { final NameValuePair nvp = it.next(); if (nvp.getName().equals(param)) { it.remove(); } } } this.encodedQuery = null; this.encodedSchemeSpecificPart = null; this.query = null; return this; }
Since the proposed implementation above is the current implementation of setParameter minus the one line that actually adds the new parameter, then the implementation of setParameter could be simplified greatly to:
/** * Sets parameter of URI query overriding existing value if set. The parameter name and value * are expected to be unescaped and may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> */ public URIBuilder setParameter(final String param, final String value) { removeParameter(param); if (this.queryParams == null) { this.queryParams = new ArrayList<NameValuePair>(); } this.queryParams.add(new BasicNameValuePair(param, value)); return this; }