Make custom query component and form parameters mutually exclusive

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1359140 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-07-09 13:07:10 +00:00
parent d545fdd180
commit ba18ddbaca
2 changed files with 26 additions and 15 deletions

View File

@ -137,17 +137,10 @@ public class URIBuilder {
}
if (this.encodedQuery != null) {
sb.append("?").append(this.encodedQuery);
} else if (this.query != null || this.queryParams != null) {
sb.append("?");
if (this.query != null) {
sb.append(encodeUric(this.query));
}
if (this.queryParams != null) {
if (this.query != null) {
sb.append("&");
}
sb.append(encodeUrlForm(this.queryParams));
}
} else if (this.queryParams != null) {
sb.append("?").append(encodeUrlForm(this.queryParams));
} else if (this.query != null) {
sb.append("?").append(encodeUric(this.query));
}
}
if (this.encodedFragment != null) {
@ -280,6 +273,9 @@ public class URIBuilder {
/**
* Sets URI query parameters. The parameter name / values 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.
*
* @since 4.3
*/
@ -292,12 +288,16 @@ public class URIBuilder {
this.queryParams.addAll(nvps);
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
return this;
}
/**
* Sets URI query parameters. The parameter name / values 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.
*
* @since 4.3
*/
@ -312,12 +312,16 @@ public class URIBuilder {
}
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
return this;
}
/**
* Adds parameter to URI query. 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.
*/
public URIBuilder addParameter(final String param, final String value) {
if (this.queryParams == null) {
@ -326,12 +330,16 @@ public class URIBuilder {
this.queryParams.add(new BasicNameValuePair(param, value));
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
return this;
}
/**
* 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.
*/
public URIBuilder setParameter(final String param, final String value) {
if (this.queryParams == null) {
@ -348,6 +356,7 @@ public class URIBuilder {
this.queryParams.add(new BasicNameValuePair(param, value));
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
return this;
}
@ -365,7 +374,10 @@ public class URIBuilder {
/**
* Sets custom URI query. The value is expected to be unescaped and may contain non ASCII
* characters. Please note, this method does NOT override query parameters if set.
* characters.
* <p/>
* Please note query parameters and custom query component are mutually exclusive. This method
* will remove query parameters if present.
*
* @since 4.3
*/
@ -373,6 +385,7 @@ public class URIBuilder {
this.query = query;
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.queryParams = null;
return this;
}

View File

@ -155,11 +155,9 @@ public class TestURIBuilder {
@Test
public void testQueryAndParameterEncoding() throws Exception {
URI uri1 = new URI("https://somehost.com/stuff?this&that" +
"&param1=12345&param2=67890");
URI uri1 = new URI("https://somehost.com/stuff?param1=12345&param2=67890");
URI uri2 = new URIBuilder("https://somehost.com/stuff")
.setCustomQuery("this&that")
.clearParameters()
.addParameter("param1","12345")
.addParameter("param2","67890").build();
Assert.assertEquals(uri1, uri2);