diff --git a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java index ac6e6fdc6..337dd43b3 100644 --- a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java +++ b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java @@ -27,6 +27,7 @@ package org.apache.http.client.methods; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -44,6 +45,8 @@ import org.apache.http.annotation.NotThreadSafe; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.ContentType; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.HeaderGroup; @@ -69,7 +72,7 @@ public class RequestBuilder { private URI uri; private HeaderGroup headergroup; private HttpEntity entity; - private LinkedList parameters; + private List parameters; private RequestConfig config; RequestBuilder(final String method) { @@ -77,6 +80,18 @@ public class RequestBuilder { this.method = method; } + RequestBuilder(final String method, final URI uri) { + super(); + this.method = method; + this.uri = uri; + } + + RequestBuilder(final String method, final String uri) { + super(); + this.method = method; + this.uri = uri != null ? URI.create(uri) : null; + } + RequestBuilder() { this(null); } @@ -90,30 +105,128 @@ public class RequestBuilder { return new RequestBuilder(HttpGet.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder get(final URI uri) { + return new RequestBuilder(HttpGet.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder get(final String uri) { + return new RequestBuilder(HttpGet.METHOD_NAME, uri); + } + public static RequestBuilder head() { return new RequestBuilder(HttpHead.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder head(final URI uri) { + return new RequestBuilder(HttpHead.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder head(final String uri) { + return new RequestBuilder(HttpHead.METHOD_NAME, uri); + } + public static RequestBuilder post() { return new RequestBuilder(HttpPost.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder post(final URI uri) { + return new RequestBuilder(HttpPost.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder post(final String uri) { + return new RequestBuilder(HttpPost.METHOD_NAME, uri); + } + public static RequestBuilder put() { return new RequestBuilder(HttpPut.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder put(final URI uri) { + return new RequestBuilder(HttpPut.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder put(final String uri) { + return new RequestBuilder(HttpPut.METHOD_NAME, uri); + } + public static RequestBuilder delete() { return new RequestBuilder(HttpDelete.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder delete(final URI uri) { + return new RequestBuilder(HttpDelete.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder delete(final String uri) { + return new RequestBuilder(HttpDelete.METHOD_NAME, uri); + } + public static RequestBuilder trace() { return new RequestBuilder(HttpTrace.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder trace(final URI uri) { + return new RequestBuilder(HttpTrace.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder trace(final String uri) { + return new RequestBuilder(HttpTrace.METHOD_NAME, uri); + } + public static RequestBuilder options() { return new RequestBuilder(HttpOptions.METHOD_NAME); } + /** + * @since 4.4 + */ + public static RequestBuilder options(final URI uri) { + return new RequestBuilder(HttpOptions.METHOD_NAME, uri); + } + + /** + * @since 4.4 + */ + public static RequestBuilder options(final String uri) { + return new RequestBuilder(HttpOptions.METHOD_NAME, uri); + } + public static RequestBuilder copy(final HttpRequest request) { Args.notNull(request, "HTTP request"); return new RequestBuilder().doCopy(request); @@ -125,27 +238,62 @@ public class RequestBuilder { } method = request.getRequestLine().getMethod(); version = request.getRequestLine().getProtocolVersion(); - if (request instanceof HttpUriRequest) { - uri = ((HttpUriRequest) request).getURI(); - } else { - uri = URI.create(request.getRequestLine().getMethod()); - } + if (headergroup == null) { headergroup = new HeaderGroup(); } headergroup.clear(); headergroup.setHeaders(request.getAllHeaders()); + + parameters = null; + entity = null; + if (request instanceof HttpEntityEnclosingRequest) { - entity = ((HttpEntityEnclosingRequest) request).getEntity(); - } else { - entity = null; + final HttpEntity originalEntity = ((HttpEntityEnclosingRequest) request).getEntity(); + final ContentType contentType = ContentType.get(originalEntity); + if (contentType != null && + contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { + try { + final List formParams = URLEncodedUtils.parse(originalEntity); + if (!formParams.isEmpty()) { + parameters = formParams; + } + } catch (IOException ignore) { + } + } else { + entity = originalEntity; + } } + + final URI originalUri; + if (request instanceof HttpUriRequest) { + originalUri = ((HttpUriRequest) request).getURI(); + } else { + originalUri = URI.create(request.getRequestLine().getUri()); + } + + final URIBuilder uriBuilder = new URIBuilder(originalUri); + if (parameters == null) { + final List queryParams = uriBuilder.getQueryParams(); + if (!queryParams.isEmpty()) { + parameters = queryParams; + uriBuilder.clearParameters(); + } else { + parameters = null; + } + } + try { + uri = uriBuilder.build(); + } catch (URISyntaxException ex) { + // Should never happen + uri = originalUri; + } + if (request instanceof Configurable) { - this.config = ((Configurable) request).getConfig(); + config = ((Configurable) request).getConfig(); } else { - this.config = null; + config = null; } - this.parameters = null; return this; } diff --git a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java index e27c6e3d7..f83926898 100644 --- a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java +++ b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java @@ -28,13 +28,16 @@ package org.apache.http.client.methods; import java.net.URI; +import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpVersion; +import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.entity.BasicHttpEntity; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; @@ -159,6 +162,46 @@ public class TestRequestBuilder { Assert.assertSame(config, ((Configurable) copy).getConfig()); } + @Test + public void testCopyWithQueryParams() throws Exception { + final HttpGet get = new HttpGet("/stuff?p1=this&p2=that"); + final RequestBuilder builder = RequestBuilder.copy(get); + final List parameters = builder.getParameters(); + Assert.assertNotNull(parameters); + Assert.assertEquals(2, parameters.size()); + Assert.assertEquals(new BasicNameValuePair("p1", "this"), parameters.get(0)); + Assert.assertEquals(new BasicNameValuePair("p2", "that"), parameters.get(1)); + Assert.assertEquals(new URI("/stuff"), builder.getUri()); + } + + @Test + public void testCopyWithFormParams() throws Exception { + final HttpPost post = new HttpPost("/stuff?p1=wtf"); + post.setEntity(new StringEntity("p1=this&p2=that", ContentType.APPLICATION_FORM_URLENCODED)); + final RequestBuilder builder = RequestBuilder.copy(post); + final List parameters = builder.getParameters(); + Assert.assertNotNull(parameters); + Assert.assertEquals(2, parameters.size()); + Assert.assertEquals(new BasicNameValuePair("p1", "this"), parameters.get(0)); + Assert.assertEquals(new BasicNameValuePair("p2", "that"), parameters.get(1)); + Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri()); + Assert.assertNull(builder.getEntity()); + } + + @Test + public void testCopyWithStringEntity() throws Exception { + final HttpPost post = new HttpPost("/stuff?p1=wtf"); + final HttpEntity entity = new StringEntity("p1=this&p2=that", ContentType.TEXT_PLAIN); + post.setEntity(entity); + final RequestBuilder builder = RequestBuilder.copy(post); + final List parameters = builder.getParameters(); + Assert.assertNotNull(parameters); + Assert.assertEquals(1, parameters.size()); + Assert.assertEquals(new BasicNameValuePair("p1", "wtf"), parameters.get(0)); + Assert.assertEquals(new URI("/stuff"), builder.getUri()); + Assert.assertSame(entity, builder.getEntity()); + } + @Test public void testGettersAndMutators() throws Exception { final HttpEntity entity = new StringEntity("stuff");