You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/07/09 15:07:11 UTC
svn commit: r1359140 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/client/utils/URIBuilder.java
test/java/org/apache/http/client/utils/TestURIBuilder.java
Author: olegk
Date: Mon Jul 9 13:07:10 2012
New Revision: 1359140
URL: http://svn.apache.org/viewvc?rev=1359140&view=rev
Log:
Make custom query component and form parameters mutually exclusive
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java?rev=1359140&r1=1359139&r2=1359140&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java Mon Jul 9 13:07:10 2012
@@ -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;
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java?rev=1359140&r1=1359139&r2=1359140&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java Mon Jul 9 13:07:10 2012
@@ -155,11 +155,9 @@ public class TestURIBuilder {
@Test
public void testQueryAndParameterEncoding() throws Exception {
- URI uri1 = new URI("https://somehost.com/stuff?this&that" +
- "¶m1=12345¶m2=67890");
+ URI uri1 = new URI("https://somehost.com/stuff?param1=12345¶m2=67890");
URI uri2 = new URIBuilder("https://somehost.com/stuff")
.setCustomQuery("this&that")
- .clearParameters()
.addParameter("param1","12345")
.addParameter("param2","67890").build();
Assert.assertEquals(uri1, uri2);