Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1916

Add method removeParameter to URIBuilder

    XMLWordPrintableJSON

Details

    • 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;
          }
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            jmoraleda Jorge Moraleda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 0.5h Original Estimate - 0.5h
                0.5h
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 20m
                1h 20m