add path method to http request builder

This commit is contained in:
Jörg Prante 2017-05-12 23:53:47 +02:00
parent b02014c744
commit 0a4cf575d6
6 changed files with 61 additions and 2 deletions

View file

@ -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

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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());
}
}