diff --git a/gradle.properties b/gradle.properties index f49d695..4b74532 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = netty-http-client -version = 4.1.11.0 +version = 4.1.11.1 netty.version = 4.1.11.Final tcnative.version = 2.0.1.Final diff --git a/src/main/java/org/xbib/netty/http/client/HttpClientBuilder.java b/src/main/java/org/xbib/netty/http/client/HttpClientBuilder.java index 6b5a486..ee2b047 100644 --- a/src/main/java/org/xbib/netty/http/client/HttpClientBuilder.java +++ b/src/main/java/org/xbib/netty/http/client/HttpClientBuilder.java @@ -104,6 +104,11 @@ public class HttpClientBuilder implements HttpClientChannelContextDefaults { private Socks5ProxyHandler socks5ProxyHandler; + /** + * Set byte buf allocator for payload in HTTP requests. + * @param byteBufAllocator the byte buf allocator + * @return this builder + */ public HttpClientBuilder withByteBufAllocator(ByteBufAllocator byteBufAllocator) { this.byteBufAllocator = byteBufAllocator; return this; diff --git a/src/main/java/org/xbib/netty/http/client/HttpClientRequestBuilder.java b/src/main/java/org/xbib/netty/http/client/HttpClientRequestBuilder.java index f1660fd..ba3cd64 100644 --- a/src/main/java/org/xbib/netty/http/client/HttpClientRequestBuilder.java +++ b/src/main/java/org/xbib/netty/http/client/HttpClientRequestBuilder.java @@ -79,7 +79,7 @@ public class HttpClientRequestBuilder implements HttpRequestBuilder, HttpRequest private int maxRedirects = DEFAULT_MAX_REDIRECT; - private URI uri; + private URI uri = DEFAULT_URI; private QueryStringEncoder queryStringEncoder; @@ -163,6 +163,16 @@ public class HttpClientRequestBuilder implements HttpRequestBuilder, HttpRequest return this; } + @Override + public HttpRequestBuilder path(String path) { + if (this.uri != null) { + setURL(this.uri.resolve(path).toString()); + } else { + setURL(path); + } + return this; + } + @Override public HttpRequestBuilder addHeader(String name, Object value) { headers.add(name, value); diff --git a/src/main/java/org/xbib/netty/http/client/HttpRequestBuilder.java b/src/main/java/org/xbib/netty/http/client/HttpRequestBuilder.java index 129893f..63d218c 100644 --- a/src/main/java/org/xbib/netty/http/client/HttpRequestBuilder.java +++ b/src/main/java/org/xbib/netty/http/client/HttpRequestBuilder.java @@ -41,6 +41,8 @@ public interface HttpRequestBuilder { HttpRequestBuilder setURL(String url); + HttpRequestBuilder path(String path); + HttpRequestBuilder setHeader(String name, Object value); HttpRequestBuilder addHeader(String name, Object value); diff --git a/src/main/java/org/xbib/netty/http/client/HttpRequestDefaults.java b/src/main/java/org/xbib/netty/http/client/HttpRequestDefaults.java index 2b52a64..db53686 100644 --- a/src/main/java/org/xbib/netty/http/client/HttpRequestDefaults.java +++ b/src/main/java/org/xbib/netty/http/client/HttpRequestDefaults.java @@ -17,6 +17,8 @@ package org.xbib.netty.http.client; import io.netty.handler.codec.http.HttpVersion; +import java.net.URI; + /** */ public interface HttpRequestDefaults { @@ -25,6 +27,8 @@ public interface HttpRequestDefaults { String DEFAULT_USER_AGENT = HttpClientUserAgent.getUserAgent(); + URI DEFAULT_URI = URI.create("http://localhost"); + boolean DEFAULT_GZIP = true; boolean DEFAULT_FOLLOW_REDIRECT = true; diff --git a/src/test/java/org/xbib/netty/http/client/test/URITest.java b/src/test/java/org/xbib/netty/http/client/test/URITest.java new file mode 100644 index 0000000..76b8aa7 --- /dev/null +++ b/src/test/java/org/xbib/netty/http/client/test/URITest.java @@ -0,0 +1,38 @@ +package org.xbib.netty.http.client.test; + +import io.netty.handler.codec.http.HttpMethod; +import org.junit.Test; +import org.xbib.netty.http.client.HttpClientRequestBuilder; +import org.xbib.netty.http.client.HttpRequestBuilder; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; + +/** + */ +public class URITest { + + @Test + public void testURIResolve() { + URI uri = URI.create("http://localhost"); + URI uri2 = uri.resolve("/path"); + assertEquals("http://localhost/path", uri2.toString()); + uri = URI.create("http://localhost/path1?a=b"); + uri2 = uri.resolve("path2?c=d"); + assertEquals("http://localhost/path2?c=d", uri2.toString()); + } + + @Test + public void testClientRequestURIs() { + HttpRequestBuilder httpRequestBuilder = HttpClientRequestBuilder.builder(HttpMethod.GET); + httpRequestBuilder.setURL("https://localhost").path("/path"); + assertEquals("/path", httpRequestBuilder.build().uri()); + httpRequestBuilder.path("/foobar"); + assertEquals("/foobar", httpRequestBuilder.build().uri()); + httpRequestBuilder.path("/path1?a=b"); + assertEquals("/path1?a=b", httpRequestBuilder.build().uri()); + httpRequestBuilder.path("/path2?c=d"); + assertEquals("/path2?c=d", httpRequestBuilder.build().uri()); + } +}