Convert convenience builders form enums to final classes

Turn exhaustive enum builder pattern to a final class with builder
methods serving HTTP request objects.

This closes #202
This commit is contained in:
Michael Osipov 2020-01-19 20:23:33 +01:00
parent 303e435d79
commit 5a8a6d0fc8
26 changed files with 457 additions and 451 deletions

View File

@ -94,67 +94,67 @@ public class Request {
}
public static Request Get(final URI uri) {
return new Request(ClassicHttpRequests.GET.create(uri));
return new Request(ClassicHttpRequests.get(uri));
}
public static Request Get(final String uri) {
return new Request(ClassicHttpRequests.GET.create(uri));
return new Request(ClassicHttpRequests.get(uri));
}
public static Request Head(final URI uri) {
return new Request(ClassicHttpRequests.HEAD.create(uri));
return new Request(ClassicHttpRequests.head(uri));
}
public static Request Head(final String uri) {
return new Request(ClassicHttpRequests.HEAD.create(uri));
return new Request(ClassicHttpRequests.head(uri));
}
public static Request Post(final URI uri) {
return new Request(ClassicHttpRequests.POST.create(uri));
return new Request(ClassicHttpRequests.post(uri));
}
public static Request Post(final String uri) {
return new Request(ClassicHttpRequests.POST.create(uri));
return new Request(ClassicHttpRequests.post(uri));
}
public static Request Patch(final URI uri) {
return new Request(ClassicHttpRequests.PATCH.create(uri));
return new Request(ClassicHttpRequests.patch(uri));
}
public static Request Patch(final String uri) {
return new Request(ClassicHttpRequests.PATCH.create(uri));
return new Request(ClassicHttpRequests.patch(uri));
}
public static Request Put(final URI uri) {
return new Request(ClassicHttpRequests.PUT.create(uri));
return new Request(ClassicHttpRequests.put(uri));
}
public static Request Put(final String uri) {
return new Request(ClassicHttpRequests.PUT.create(uri));
return new Request(ClassicHttpRequests.put(uri));
}
public static Request Trace(final URI uri) {
return new Request(ClassicHttpRequests.TRACE.create(uri));
return new Request(ClassicHttpRequests.trace(uri));
}
public static Request Trace(final String uri) {
return new Request(ClassicHttpRequests.TRACE.create(uri));
return new Request(ClassicHttpRequests.trace(uri));
}
public static Request Delete(final URI uri) {
return new Request(ClassicHttpRequests.DELETE.create(uri));
return new Request(ClassicHttpRequests.delete(uri));
}
public static Request Delete(final String uri) {
return new Request(ClassicHttpRequests.DELETE.create(uri));
return new Request(ClassicHttpRequests.delete(uri));
}
public static Request Options(final URI uri) {
return new Request(ClassicHttpRequests.OPTIONS.create(uri));
return new Request(ClassicHttpRequests.options(uri));
}
public static Request Options(final String uri) {
return new Request(ClassicHttpRequests.OPTIONS.create(uri));
return new Request(ClassicHttpRequests.options(uri));
}
Request(final ClassicHttpRequest request) {

View File

@ -164,7 +164,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -191,7 +191,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -218,7 +218,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -245,7 +245,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final SimpleHttpRequest put = SimpleHttpRequests.PUT.create(target, "/");
final SimpleHttpRequest put = SimpleHttpRequests.put(target, "/");
put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get();
@ -275,7 +275,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
context.setCredentialsProvider(credsProvider);
context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build());
final SimpleHttpRequest put = SimpleHttpRequests.PUT.create(target, "/");
final SimpleHttpRequest put = SimpleHttpRequests.put(target, "/");
put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get();
@ -302,7 +302,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
context.setCredentialsProvider(credsProvider);
context.setRequestConfig(RequestConfig.custom().setExpectContinueEnabled(true).build());
final SimpleHttpRequest put = SimpleHttpRequests.PUT.create(target, "/");
final SimpleHttpRequest put = SimpleHttpRequests.put(target, "/");
put.setBodyText("Some important stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(put, context, null);
final HttpResponse response = future.get();
@ -345,12 +345,12 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future1 = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future1 = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
Assert.assertEquals(HttpStatus.SC_OK, response1.getCode());
final Future<SimpleHttpResponse> future2 = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future2 = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
Assert.assertEquals(HttpStatus.SC_OK, response2.getCode());
@ -372,7 +372,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target.getSchemeName() + "://test:test@" + target.toHostString() + "/"), context, null);
SimpleHttpRequests.get(target.getSchemeName() + "://test:test@" + target.toHostString() + "/"), context, null);
final SimpleHttpResponse response = future.get();
Assert.assertNotNull(response);
@ -393,7 +393,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target.getSchemeName() + "://test:all-worng@" + target.toHostString() + "/"), context, null);
SimpleHttpRequests.get(target.getSchemeName() + "://test:all-worng@" + target.toHostString() + "/"), context, null);
final SimpleHttpResponse response = future.get();
Assert.assertNotNull(response);
@ -431,7 +431,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target.getSchemeName() + "://test:test@" + target.toHostString() + "/thatway"), context, null);
SimpleHttpRequests.get(target.getSchemeName() + "://test:test@" + target.toHostString() + "/thatway"), context, null);
final SimpleHttpResponse response = future.get();
Assert.assertNotNull(response);
@ -511,7 +511,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
context.setCredentialsProvider(credsProvider);
for (int i = 0; i < 10; i++) {
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, "/");
final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/");
request.setConfig(config);
final Future<SimpleHttpResponse> future = httpclient.execute(request, context, null);
final SimpleHttpResponse response = future.get();
@ -552,7 +552,7 @@ public abstract class AbstractHttpAsyncClientAuthentication<T extends CloseableH
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final SimpleHttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getCode());

View File

@ -66,7 +66,7 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
final HttpHost target = start();
for (int i = 0; i < 3; i++) {
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/random/2048"), null);
SimpleHttpRequests.get(target, "/random/2048"), null);
final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
@ -81,7 +81,7 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
final HttpHost target = start();
for (int i = 0; i < 3; i++) {
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.HEAD.create(target, "/random/2048"), null);
SimpleHttpRequests.head(target, "/random/2048"), null);
final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
@ -154,7 +154,7 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
try {
resultQueue.add(result);
if (count.decrementAndGet() > 0) {
httpclient.execute(SimpleHttpRequests.GET.create(target, "/random/2048"), this);
httpclient.execute(SimpleHttpRequests.get(target, "/random/2048"), this);
}
} finally {
countDownLatch.countDown();
@ -180,7 +180,7 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
@Override
public void run() {
if (!Thread.currentThread().isInterrupted()) {
httpclient.execute(SimpleHttpRequests.GET.create(target, "/random/2048"), callback);
httpclient.execute(SimpleHttpRequests.get(target, "/random/2048"), callback);
}
}
@ -205,10 +205,10 @@ public abstract class AbstractHttpAsyncFundamentalsTest<T extends CloseableHttpA
public void testBadRequest() throws Exception {
final HttpHost target = start();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/random/boom"), null);
SimpleHttpRequests.get(target, "/random/boom"), null);
final SimpleHttpResponse response = future.get();
Assert.assertThat(response, CoreMatchers.notNullValue());
Assert.assertThat(response.getCode(), CoreMatchers.equalTo(400));
}
}
}

View File

@ -217,7 +217,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -241,7 +241,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -265,7 +265,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -297,7 +297,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -320,7 +320,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -344,7 +344,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -367,7 +367,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -390,7 +390,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -417,7 +417,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
.setCircularRedirectsAllowed(true)
.setMaxRedirects(5).build();
try {
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, "/circular-oldlocation/");
final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/circular-oldlocation/");
request.setConfig(config);
final Future<SimpleHttpResponse> future = httpclient.execute(request, null);
future.get();
@ -443,7 +443,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
.setCircularRedirectsAllowed(false)
.build();
try {
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, "/circular-oldlocation/");
final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/circular-oldlocation/");
request.setConfig(config);
final Future<SimpleHttpResponse> future = httpclient.execute(request, null);
future.get();
@ -467,7 +467,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final SimpleHttpRequest post = SimpleHttpRequests.POST.create(target, "/oldlocation/");
final SimpleHttpRequest post = SimpleHttpRequests.post(target, "/oldlocation/");
post.setBodyText("stuff", ContentType.TEXT_PLAIN);
final Future<SimpleHttpResponse> future = httpclient.execute(post, context, null);
final HttpResponse response = future.get();
@ -495,7 +495,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -521,7 +521,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/test/oldlocation"), context, null);
SimpleHttpRequests.get(target, "/test/oldlocation"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -579,7 +579,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
try {
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), null);
SimpleHttpRequests.get(target, "/oldlocation/"), null);
future.get();
} catch (final ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof HttpException);
@ -601,7 +601,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
try {
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), null);
SimpleHttpRequests.get(target, "/oldlocation/"), null);
future.get();
} catch (final ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof ProtocolException);
@ -632,7 +632,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
cookieStore.addCookie(cookie);
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -711,7 +711,7 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final Queue<Future<SimpleHttpResponse>> queue = new ConcurrentLinkedQueue<>();
for (int i = 0; i < 1; i++) {
queue.add(httpclient.execute(SimpleHttpRequests.GET.create(initialTarget, "/redirect/anywhere"), null));
queue.add(httpclient.execute(SimpleHttpRequests.get(initialTarget, "/redirect/anywhere"), null));
}
while (!queue.isEmpty()) {
final Future<SimpleHttpResponse> future = queue.remove();
@ -763,12 +763,12 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future1 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/rome"), context, null);
SimpleHttpRequests.get(target, "/rome"), context, null);
final HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
final Future<SimpleHttpResponse> future2 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/rome"), context, null);
SimpleHttpRequests.get(target, "/rome"), context, null);
final HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
@ -794,12 +794,12 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future1 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/lille"), context, null);
SimpleHttpRequests.get(target, "/lille"), context, null);
final HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
final Future<SimpleHttpResponse> future2 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/lille"), context, null);
SimpleHttpRequests.get(target, "/lille"), context, null);
final HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
@ -825,12 +825,12 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future1 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/alian"), context, null);
SimpleHttpRequests.get(target, "/alian"), context, null);
final HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
final Future<SimpleHttpResponse> future2 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/lille"), context, null);
SimpleHttpRequests.get(target, "/lille"), context, null);
final HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
@ -842,4 +842,4 @@ public abstract class AbstractHttpAsyncRedirectsTest <T extends CloseableHttpAsy
Assert.assertEquals(target, new HttpHost(request.getScheme(), request.getAuthority()));
}
}
}

View File

@ -121,7 +121,7 @@ public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableH
public void testSequenctialGetRequestsCloseConnection() throws Exception {
final HttpHost target = start();
for (int i = 0; i < 3; i++) {
final SimpleHttpRequest get = SimpleHttpRequests.GET.create(target, "/random/2048");
final SimpleHttpRequest get = SimpleHttpRequests.get(target, "/random/2048");
get.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
final Future<SimpleHttpResponse> future = httpclient.execute(get, null);
final SimpleHttpResponse response = future.get();
@ -151,7 +151,7 @@ public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableH
public void testSharedPool() throws Exception {
final HttpHost target = start();
final Future<SimpleHttpResponse> future1 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/random/2048"), null);
SimpleHttpRequests.get(target, "/random/2048"), null);
final SimpleHttpResponse response1 = future1.get();
Assert.assertThat(response1, CoreMatchers.notNullValue());
Assert.assertThat(response1.getCode(), CoreMatchers.equalTo(200));
@ -166,7 +166,7 @@ public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableH
.build()) {
httpclient2.start();
final Future<SimpleHttpResponse> future2 = httpclient2.execute(
SimpleHttpRequests.GET.create(target, "/random/2048"), null);
SimpleHttpRequests.get(target, "/random/2048"), null);
final SimpleHttpResponse response2 = future2.get();
Assert.assertThat(response2, CoreMatchers.notNullValue());
Assert.assertThat(response2.getCode(), CoreMatchers.equalTo(200));
@ -176,7 +176,7 @@ public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableH
}
final Future<SimpleHttpResponse> future3 = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/random/2048"), null);
SimpleHttpRequests.get(target, "/random/2048"), null);
final SimpleHttpResponse response3 = future3.get();
Assert.assertThat(response3, CoreMatchers.notNullValue());
Assert.assertThat(response3.getCode(), CoreMatchers.equalTo(200));

View File

@ -179,7 +179,7 @@ public class TestHttp1AsyncRedirects extends AbstractHttpAsyncRedirectsTest<Clos
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -203,7 +203,7 @@ public class TestHttp1AsyncRedirects extends AbstractHttpAsyncRedirectsTest<Clos
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -234,7 +234,7 @@ public class TestHttp1AsyncRedirects extends AbstractHttpAsyncRedirectsTest<Clos
final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = httpclient.execute(
SimpleHttpRequests.GET.create(target, "/oldlocation/"), context, null);
SimpleHttpRequests.get(target, "/oldlocation/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
@ -247,4 +247,4 @@ public class TestHttp1AsyncRedirects extends AbstractHttpAsyncRedirectsTest<Clos
Assert.assertEquals("my-test-client", header.getValue());
}
}
}

View File

@ -216,7 +216,7 @@ public class TestHttp1AsyncStatefulConnManagement extends AbstractIntegrationTes
try {
context.setAttribute("user", uid);
for (int r = 0; r < requestCount; r++) {
final SimpleHttpRequest httpget = SimpleHttpRequests.GET.create(target, "/");
final SimpleHttpRequest httpget = SimpleHttpRequests.get(target, "/");
final Future<SimpleHttpResponse> future = httpclient.execute(httpget, null);
future.get();
@ -277,7 +277,7 @@ public class TestHttp1AsyncStatefulConnManagement extends AbstractIntegrationTes
final HttpContext context1 = new BasicHttpContext();
context1.setAttribute("user", "stuff");
final Future<SimpleHttpResponse> future1 = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context1, null);
final Future<SimpleHttpResponse> future1 = httpclient.execute(SimpleHttpRequests.get(target, "/"), context1, null);
final HttpResponse response1 = future1.get();
Assert.assertNotNull(response1);
Assert.assertEquals(200, response1.getCode());
@ -292,7 +292,7 @@ public class TestHttp1AsyncStatefulConnManagement extends AbstractIntegrationTes
// Send it to another route. Must be a keepalive.
final HttpContext context2 = new BasicHttpContext();
final Future<SimpleHttpResponse> future2 = httpclient.execute(SimpleHttpRequests.GET.create(
final Future<SimpleHttpResponse> future2 = httpclient.execute(SimpleHttpRequests.get(
new HttpHost(target.getSchemeName(), "127.0.0.1", target.getPort()),"/"), context2, null);
final HttpResponse response2 = future2.get();
Assert.assertNotNull(response2);
@ -310,10 +310,10 @@ public class TestHttp1AsyncStatefulConnManagement extends AbstractIntegrationTes
// The killed conn is the oldest, which means the first HTTPGet ([localhost][stuff]).
// When this happens, the RouteSpecificPool becomes empty.
final HttpContext context3 = new BasicHttpContext();
final Future<SimpleHttpResponse> future3 = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context3, null);
final Future<SimpleHttpResponse> future3 = httpclient.execute(SimpleHttpRequests.get(target, "/"), context3, null);
final HttpResponse response3 = future3.get();
Assert.assertNotNull(response3);
Assert.assertEquals(200, response3.getCode());
}
}
}

View File

@ -166,7 +166,7 @@ public class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuthen
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.GET.create(target, "/"), context, null);
final Future<SimpleHttpResponse> future = httpclient.execute(SimpleHttpRequests.get(target, "/"), context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);

View File

@ -126,7 +126,7 @@ public class TestHttp1Reactive extends AbstractHttpReactiveFundamentalsTest<Clos
public void testSequentialGetRequestsCloseConnection() throws Exception {
final HttpHost target = start();
for (int i = 0; i < 3; i++) {
final SimpleHttpRequest get = SimpleHttpRequests.GET.create(target, "/random/2048");
final SimpleHttpRequest get = SimpleHttpRequests.get(target, "/random/2048");
get.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
final AsyncRequestProducer request = AsyncRequestBuilder.get(target + "/random/2048").build();
final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();

View File

@ -131,7 +131,7 @@ public class CachingHttpAsyncClientCompatibilityTest {
// Initial ping
{
final HttpCacheContext context = HttpCacheContext.create();
final SimpleHttpRequest options = SimpleHttpRequests.OPTIONS.create(target, "*");
final SimpleHttpRequest options = SimpleHttpRequests.options(target, "*");
final Future<SimpleHttpResponse> future = client.execute(options, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -155,7 +155,7 @@ public class CachingHttpAsyncClientCompatibilityTest {
final Pattern linkPattern = Pattern.compile("^<(.*)>;rel=preload$");
final List<String> links = new ArrayList<>();
final SimpleHttpRequest getRoot1 = SimpleHttpRequests.GET.create(target, "/");
final SimpleHttpRequest getRoot1 = SimpleHttpRequests.get(target, "/");
final Future<SimpleHttpResponse> future1 = client.execute(getRoot1, context, null);
try {
final SimpleHttpResponse response = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -179,7 +179,7 @@ public class CachingHttpAsyncClientCompatibilityTest {
logResult(TestResult.NOK, getRoot1, "(time out)");
}
for (final String link: links) {
final SimpleHttpRequest getLink = SimpleHttpRequests.GET.create(target, link);
final SimpleHttpRequest getLink = SimpleHttpRequests.get(target, link);
final Future<SimpleHttpResponse> linkFuture = client.execute(getLink, context, null);
try {
final SimpleHttpResponse response = linkFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -198,7 +198,7 @@ public class CachingHttpAsyncClientCompatibilityTest {
}
}
final SimpleHttpRequest getRoot2 = SimpleHttpRequests.GET.create(target, "/");
final SimpleHttpRequest getRoot2 = SimpleHttpRequests.get(target, "/");
final Future<SimpleHttpResponse> future2 = client.execute(getRoot2, context, null);
try {
final SimpleHttpResponse response = future2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -216,7 +216,7 @@ public class CachingHttpAsyncClientCompatibilityTest {
logResult(TestResult.NOK, getRoot2, "(time out)");
}
for (final String link: links) {
final SimpleHttpRequest getLink = SimpleHttpRequests.GET.create(target, link);
final SimpleHttpRequest getLink = SimpleHttpRequests.get(target, link);
final Future<SimpleHttpResponse> linkFuture = client.execute(getLink, context, null);
try {
final SimpleHttpResponse response = linkFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());

View File

@ -168,7 +168,7 @@ public class HttpAsyncClientCompatibilityTest {
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
final SimpleHttpRequest options = SimpleHttpRequests.OPTIONS.create(target, "*");
final SimpleHttpRequest options = SimpleHttpRequests.options(target, "*");
final Future<SimpleHttpResponse> future = client.execute(options, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -193,7 +193,7 @@ public class HttpAsyncClientCompatibilityTest {
final String[] requestUris = new String[] {"/", "/news.html", "/status.html"};
for (final String requestUri: requestUris) {
final SimpleHttpRequest httpGet = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest httpGet = SimpleHttpRequests.get(target, requestUri);
final Future<SimpleHttpResponse> future = client.execute(httpGet, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -220,7 +220,7 @@ public class HttpAsyncClientCompatibilityTest {
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.GET.create(target, "/private/big-secret.txt");
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.get(target, "/private/big-secret.txt");
final Future<SimpleHttpResponse> future = client.execute(httpGetSecret, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -246,7 +246,7 @@ public class HttpAsyncClientCompatibilityTest {
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.GET.create(target, "/private/big-secret.txt");
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.get(target, "/private/big-secret.txt");
final Future<SimpleHttpResponse> future = client.execute(httpGetSecret, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -272,7 +272,7 @@ public class HttpAsyncClientCompatibilityTest {
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.GET.create(target, "/private/big-secret.txt");
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.get(target, "/private/big-secret.txt");
final Future<SimpleHttpResponse> future = client.execute(httpGetSecret, context, null);
try {
final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
@ -299,7 +299,7 @@ public class HttpAsyncClientCompatibilityTest {
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.GET.create(target, "/private/big-secret.txt");
final SimpleHttpRequest httpGetSecret = SimpleHttpRequests.get(target, "/private/big-secret.txt");
httpGetSecret.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
final Future<SimpleHttpResponse> future = client.execute(httpGetSecret, context, null);
try {

View File

@ -0,0 +1,170 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.async.methods;
import java.net.URI;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.BasicHttpRequest;
/**
* Common HTTP methods using {@link BasicHttpRequest} as a HTTP request message representation.
*
* @since 5.0
*/
public final class BasicHttpRequests {
public static BasicHttpRequest delete(final String uri) {
return delete(URI.create(uri));
}
public static BasicHttpRequest delete(final URI uri) {
return create(Method.DELETE, uri);
}
public static BasicHttpRequest delete(final HttpHost host, final String path) {
return create(Method.DELETE, host, path);
}
public static BasicHttpRequest get(final String uri) {
return get(URI.create(uri));
}
public static BasicHttpRequest get(final URI uri) {
return create(Method.GET, uri);
}
public static BasicHttpRequest get(final HttpHost host, final String path) {
return create(Method.GET, host, path);
}
public static BasicHttpRequest head(final String uri) {
return head(URI.create(uri));
}
public static BasicHttpRequest head(final URI uri) {
return create(Method.HEAD, uri);
}
public static BasicHttpRequest head(final HttpHost host, final String path) {
return create(Method.HEAD, host, path);
}
public static BasicHttpRequest options(final String uri) {
return options(URI.create(uri));
}
public static BasicHttpRequest options(final URI uri) {
return create(Method.OPTIONS, uri);
}
public static BasicHttpRequest options(final HttpHost host, final String path) {
return create(Method.OPTIONS, host, path);
}
public static BasicHttpRequest patch(final String uri) {
return patch(URI.create(uri));
}
public static BasicHttpRequest patch(final URI uri) {
return create(Method.PATCH, uri);
}
public static BasicHttpRequest patch(final HttpHost host, final String path) {
return create(Method.PATCH, host, path);
}
public static BasicHttpRequest post(final String uri) {
return post(URI.create(uri));
}
public static BasicHttpRequest post(final URI uri) {
return create(Method.POST, uri);
}
public static BasicHttpRequest post(final HttpHost host, final String path) {
return create(Method.POST, host, path);
}
public static BasicHttpRequest put(final String uri) {
return put(URI.create(uri));
}
public static BasicHttpRequest put(final URI uri) {
return create(Method.PUT, uri);
}
public static BasicHttpRequest put(final HttpHost host, final String path) {
return create(Method.PUT, host, path);
}
public static BasicHttpRequest trace(final String uri) {
return trace(URI.create(uri));
}
public static BasicHttpRequest trace(final URI uri) {
return create(Method.TRACE, uri);
}
public static BasicHttpRequest trace(final HttpHost host, final String path) {
return create(Method.TRACE, host, path);
}
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param uri a non-null URI String.
* @return a new subclass of BasicHttpRequest
*/
public static BasicHttpRequest create(final Method method, final String uri) {
return create(method, URI.create(uri));
}
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param uri a non-null URI.
* @return a new subclass of BasicHttpRequest
*/
public static BasicHttpRequest create(final Method method, final URI uri) {
return new BasicHttpRequest(method, uri);
}
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param host HTTP host.
* @param path request path.
* @return a new subclass of BasicHttpRequest
*/
public static BasicHttpRequest create(final Method method, final HttpHost host, final String path) {
return new BasicHttpRequest(method, host, path);
}
}

View File

@ -1,166 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.async.methods;
import java.net.URI;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.BasicHttpRequest;
/**
* Common HTTP methods using {@link BasicHttpRequest} as a HTTP request message representation.
*
* @since 5.0
*/
public enum HttpRequests {
DELETE {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.DELETE, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.DELETE, host, path);
}
},
GET {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.GET, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.GET, host, path);
}
},
HEAD {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.HEAD, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.HEAD, host, path);
}
},
OPTIONS {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.OPTIONS, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.OPTIONS, host, path);
}
},
PATCH {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.PATCH, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.PATCH, host, path);
}
},
POST {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.POST, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.POST, host, path);
}
},
PUT {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.PUT, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.PUT, host, path);
}
},
TRACE {
@Override
public BasicHttpRequest create(final URI uri) {
return new BasicHttpRequest(Method.TRACE, uri);
}
@Override
public BasicHttpRequest create(final HttpHost host, final String path) {
return new BasicHttpRequest(Method.TRACE, host, path);
}
};
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param uri a non-null URI String.
* @return a new subclass of BasicHttpRequest
*/
public BasicHttpRequest create(final String uri) {
return create(URI.create(uri));
}
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param uri a non-null URI.
* @return a new subclass of BasicHttpRequest
*/
public abstract BasicHttpRequest create(URI uri);
/**
* Creates a request object of the exact subclass of {@link BasicHttpRequest}.
*
* @param host HTTP host.
* @param path request path.
* @return a new subclass of BasicHttpRequest
*/
public abstract BasicHttpRequest create(final HttpHost host, final String path);
}

View File

@ -37,103 +37,103 @@ import org.apache.hc.core5.http.Method;
*
* @since 5.0
*/
public enum SimpleHttpRequests {
public final class SimpleHttpRequests {
DELETE {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.DELETE, uri);
}
public static SimpleHttpRequest delete(final String uri) {
return delete(URI.create(uri));
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.DELETE, host, path);
}
},
public static SimpleHttpRequest delete(final URI uri) {
return create(Method.DELETE, uri);
}
GET {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.GET, uri);
}
public static SimpleHttpRequest delete(final HttpHost host, final String path) {
return create(Method.DELETE, host, path);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.GET, host, path);
}
},
public static SimpleHttpRequest get(final String uri) {
return get(URI.create(uri));
}
HEAD {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.HEAD, uri);
}
public static SimpleHttpRequest get(final URI uri) {
return create(Method.GET, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.HEAD, host, path);
}
},
public static SimpleHttpRequest get(final HttpHost host, final String path) {
return create(Method.GET, host, path);
}
OPTIONS {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.OPTIONS, uri);
}
public static SimpleHttpRequest head(final String uri) {
return head(URI.create(uri));
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.OPTIONS, host, path);
}
},
public static SimpleHttpRequest head(final URI uri) {
return create(Method.HEAD, uri);
}
PATCH {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.PATCH, uri);
}
public static SimpleHttpRequest head(final HttpHost host, final String path) {
return create(Method.HEAD, host, path);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.PATCH, host, path);
}
},
public static SimpleHttpRequest options(final String uri) {
return options(URI.create(uri));
}
POST {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.POST, uri);
}
public static SimpleHttpRequest options(final URI uri) {
return create(Method.OPTIONS, uri);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.POST, host, path);
}
},
public static SimpleHttpRequest options(final HttpHost host, final String path) {
return create(Method.OPTIONS, host, path);
}
PUT {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.PUT, uri);
}
public static SimpleHttpRequest patch(final String uri) {
return patch(URI.create(uri));
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.PUT, host, path);
}
},
public static SimpleHttpRequest patch(final URI uri) {
return create(Method.PATCH, uri);
}
TRACE {
@Override
public SimpleHttpRequest create(final URI uri) {
return new SimpleHttpRequest(Method.TRACE, uri);
}
public static SimpleHttpRequest patch(final HttpHost host, final String path) {
return create(Method.PATCH, host, path);
}
@Override
public SimpleHttpRequest create(final HttpHost host, final String path) {
return new SimpleHttpRequest(Method.TRACE, host, path);
}
};
public static SimpleHttpRequest post(final String uri) {
return post(URI.create(uri));
}
public static SimpleHttpRequest post(final URI uri) {
return create(Method.POST, uri);
}
public static SimpleHttpRequest post(final HttpHost host, final String path) {
return create(Method.POST, host, path);
}
public static SimpleHttpRequest put(final String uri) {
return put(URI.create(uri));
}
public static SimpleHttpRequest put(final URI uri) {
return create(Method.PUT, uri);
}
public static SimpleHttpRequest put(final HttpHost host, final String path) {
return create(Method.PUT, host, path);
}
public static SimpleHttpRequest trace(final String uri) {
return trace(URI.create(uri));
}
public static SimpleHttpRequest trace(final URI uri) {
return create(Method.TRACE, uri);
}
public static SimpleHttpRequest trace(final HttpHost host, final String path) {
return create(Method.TRACE, host, path);
}
/**
* Creates a request object of the exact subclass of {@link SimpleHttpRequest}.
@ -141,8 +141,8 @@ public enum SimpleHttpRequests {
* @param uri a non-null URI String.
* @return a new subclass of SimpleHttpRequest
*/
public SimpleHttpRequest create(final String uri) {
return create(URI.create(uri));
public static SimpleHttpRequest create(final Method method, final String uri) {
return create(method, URI.create(uri));
}
/**
@ -151,7 +151,9 @@ public enum SimpleHttpRequests {
* @param uri a non-null URI.
* @return a new subclass of SimpleHttpRequest
*/
public abstract SimpleHttpRequest create(URI uri);
public static SimpleHttpRequest create(final Method method, final URI uri) {
return new SimpleHttpRequest(method, uri);
}
/**
* Creates a request object of the exact subclass of {@link SimpleHttpRequest}.
@ -160,6 +162,8 @@ public enum SimpleHttpRequests {
* @param path request path.
* @return a new subclass of SimpleHttpRequest
*/
public abstract SimpleHttpRequest create(final HttpHost host, final String path);
public static SimpleHttpRequest create(final Method method, final HttpHost host, final String path) {
return new SimpleHttpRequest(method, host, path);
}
}

View File

@ -32,85 +32,76 @@ import java.net.URI;
/**
* Common HTTP methods using {@link HttpUriRequest} as a HTTP request message representation.
* <p>
* Each static method creates a request object of the exact subclass of {@link HttpUriRequest}
* with a non-null URI.
*
* @since 5.0
*/
public enum ClassicHttpRequests {
public final class ClassicHttpRequests {
DELETE {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpDelete(uri);
}
},
GET {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpGet(uri);
}
},
HEAD {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpHead(uri);
}
},
OPTIONS {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpOptions(uri);
}
},
PATCH {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpPatch(uri);
}
},
POST {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpPost(uri);
}
},
PUT {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpPut(uri);
}
},
TRACE {
@Override
public HttpUriRequest create(final URI uri) {
return new HttpTrace(uri);
}
};
/**
* Creates a request object of the exact subclass of {@link HttpUriRequest}.
*
* @param uri
* a non-null URI String.
* @return a new subclass of HttpUriRequest
*/
public HttpUriRequest create(final String uri) {
return create(URI.create(uri));
public static HttpUriRequest delete(final String uri) {
return delete(URI.create(uri));
}
/**
* Creates a request object of the exact subclass of {@link HttpUriRequest}.
*
* @param uri
* a non-null URI.
* @return a new subclass of HttpUriRequest
*/
public abstract HttpUriRequest create(URI uri);
public static HttpUriRequest delete(final URI uri) {
return new HttpDelete(uri);
}
public static HttpUriRequest get(final String uri) {
return get(URI.create(uri));
}
public static HttpUriRequest get(final URI uri) {
return new HttpGet(uri);
}
public static HttpUriRequest head(final String uri) {
return head(URI.create(uri));
}
public static HttpUriRequest head(final URI uri) {
return new HttpHead(uri);
}
public static HttpUriRequest options(final String uri) {
return options(URI.create(uri));
}
public static HttpUriRequest options(final URI uri) {
return new HttpOptions(uri);
}
public static HttpUriRequest patch(final String uri) {
return patch(URI.create(uri));
}
public static HttpUriRequest patch(final URI uri) {
return new HttpPatch(uri);
}
public static HttpUriRequest post(final String uri) {
return post(URI.create(uri));
}
public static HttpUriRequest post(final URI uri) {
return new HttpPost(uri);
}
public static HttpUriRequest put(final String uri) {
return put(URI.create(uri));
}
public static HttpUriRequest put(final URI uri) {
return new HttpPut(uri);
}
public static HttpUriRequest trace(final String uri) {
return trace(URI.create(uri));
}
public static HttpUriRequest trace(final URI uri) {
return new HttpTrace(uri);
}
}

View File

@ -27,6 +27,7 @@
package org.apache.hc.client5.http.async.methods;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
@ -39,7 +40,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class TestHttpRequests {
public class TestBasicHttpRequests {
private static final URI URI_FIXTURE = URI.create("http://localhost");
@ -47,30 +48,32 @@ public class TestHttpRequests {
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
// @formatter:off
{ HttpRequests.DELETE, "DELETE" },
{ HttpRequests.GET, "GET" },
{ HttpRequests.HEAD, "HEAD" },
{ HttpRequests.OPTIONS, "OPTIONS" },
{ HttpRequests.PATCH, "PATCH" },
{ HttpRequests.POST, "POST" },
{ HttpRequests.PUT, "PUT" }
{ "delete", "DELETE" },
{ "get", "GET" },
{ "head", "HEAD" },
{ "options", "OPTIONS" },
{ "patch", "PATCH" },
{ "post", "POST" },
{ "put", "PUT" },
{ "trace", "TRACE" }
// @formatter:on
});
}
private final HttpRequests httpRequest;
private final String methodName;
private final String expectedMethod;
public TestHttpRequests(final HttpRequests classicHttpRequests, final String expectedMethod) {
this.httpRequest = classicHttpRequests;
public TestBasicHttpRequests(final String methodName, final String expectedMethod) {
this.methodName = methodName;
this.expectedMethod = expectedMethod;
}
@Test
public void testCreateClassicHttpRequest() {
final HttpRequest classicHttpRequest = httpRequest.create(URI_FIXTURE);
Assert.assertEquals(BasicHttpRequest.class, classicHttpRequest.getClass());
Assert.assertEquals(expectedMethod, classicHttpRequest.getMethod());
public void testCreateClassicHttpRequest() throws Exception {
final Method httpMethod = BasicHttpRequests.class.getMethod(methodName, URI.class);
final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE);
Assert.assertEquals(BasicHttpRequest.class, basicHttpRequest.getClass());
Assert.assertEquals(expectedMethod, basicHttpRequest.getMethod());
}
}

View File

@ -27,6 +27,7 @@
package org.apache.hc.client5.http.classic.methods;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
@ -47,36 +48,39 @@ public class TestClassicHttpRequests {
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
// @formatter:off
{ ClassicHttpRequests.DELETE, HttpDelete.class },
{ ClassicHttpRequests.GET, HttpGet.class },
{ ClassicHttpRequests.HEAD, HttpHead.class },
{ ClassicHttpRequests.OPTIONS, HttpOptions.class },
{ ClassicHttpRequests.PATCH, HttpPatch.class },
{ ClassicHttpRequests.POST, HttpPost.class },
{ ClassicHttpRequests.PUT, HttpPut.class }
{ "delete", HttpDelete.class },
{ "get", HttpGet.class },
{ "head", HttpHead.class },
{ "options", HttpOptions.class },
{ "patch", HttpPatch.class },
{ "post", HttpPost.class },
{ "put", HttpPut.class },
{ "trace", HttpTrace.class }
// @formatter:on
});
}
private final ClassicHttpRequests classicHttpRequests;
private final String methodName;
private final Class<ClassicHttpRequest> expectedClass;
public TestClassicHttpRequests(final ClassicHttpRequests classicHttpRequests,
public TestClassicHttpRequests(final String methodName,
final Class<ClassicHttpRequest> expectedClass) {
this.classicHttpRequests = classicHttpRequests;
this.methodName = methodName;
this.expectedClass = expectedClass;
}
@Test
public void testCreateFromString() {
Assert.assertEquals(expectedClass,
classicHttpRequests.create(URI_STRING_FIXTURE).getClass());
public void testCreateFromString() throws Exception {
final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);
Assert.assertEquals(expectedClass,
httpMethod.invoke(null, URI_STRING_FIXTURE).getClass());
}
@Test
public void testCreateFromURI() {
public void testCreateFromURI() throws Exception {
final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, URI.class);
Assert.assertEquals(expectedClass,
classicHttpRequests.create(URI_FIXTURE).getClass());
httpMethod.invoke(null, URI_FIXTURE).getClass());
}
}

View File

@ -56,7 +56,7 @@ public class AsyncClientAuthentication {
httpclient.start();
final String requestUri = "http://httpbin.org/basic-auth/user/passwd";
final SimpleHttpRequest httpget = SimpleHttpRequests.GET.create(requestUri);
final SimpleHttpRequest httpget = SimpleHttpRequests.get(requestUri);
System.out.println("Executing request " + requestUri);
final Future<SimpleHttpResponse> future = httpclient.execute(

View File

@ -65,7 +65,7 @@ public class AsyncClientConnectionEviction {
final HttpHost target = new HttpHost("httpbin.org");
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, "/");
final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/");
final Future<SimpleHttpResponse> future1 = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),

View File

@ -101,7 +101,7 @@ public class AsyncClientCustomSSL {
final String requestUri = "/";
final HttpClientContext clientContext = HttpClientContext.create();
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
final Future<SimpleHttpResponse> future = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),

View File

@ -71,7 +71,7 @@ public class AsyncClientH2Multiplexing {
final CountDownLatch latch = new CountDownLatch(requestUris.length);
for (final String requestUri: requestUris) {
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
endpoint.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),

View File

@ -70,7 +70,7 @@ public class AsyncClientHttp1Pipelining {
final CountDownLatch latch = new CountDownLatch(requestUris.length);
for (final String requestUri: requestUris) {
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
endpoint.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),

View File

@ -60,7 +60,7 @@ public class AsyncClientHttpExchange {
final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
for (final String requestUri: requestUris) {
final SimpleHttpRequest httpget = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest httpget = SimpleHttpRequests.get(target, requestUri);
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
final Future<SimpleHttpResponse> future = client.execute(
httpget,

View File

@ -123,7 +123,7 @@ public class AsyncClientInterceptors {
final String requestUri = "http://httpbin.org/get";
for (int i = 0; i < 20; i++) {
final SimpleHttpRequest httpget = SimpleHttpRequests.GET.create(requestUri);
final SimpleHttpRequest httpget = SimpleHttpRequests.get(requestUri);
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
final Future<SimpleHttpResponse> future = client.execute(
httpget,

View File

@ -83,7 +83,7 @@ public class AsyncClientTlsAlpn {
final String requestUri = "/httpbin/";
final HttpClientContext clientContext = HttpClientContext.create();
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(target, requestUri);
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
final Future<SimpleHttpResponse> future = client.execute(
SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),

View File

@ -55,7 +55,7 @@ public class AsyncQuickStart {
httpclient.start();
// Execute request
final SimpleHttpRequest request1 = SimpleHttpRequests.GET.create("http://httpbin.org/get");
final SimpleHttpRequest request1 = SimpleHttpRequests.get("http://httpbin.org/get");
final Future<SimpleHttpResponse> future = httpclient.execute(request1, null);
// and wait until response is received
final SimpleHttpResponse response1 = future.get();
@ -63,7 +63,7 @@ public class AsyncQuickStart {
// One most likely would want to use a callback for operation result
final CountDownLatch latch1 = new CountDownLatch(1);
final SimpleHttpRequest request2 = SimpleHttpRequests.GET.create("http://httpbin.org/get");
final SimpleHttpRequest request2 = SimpleHttpRequests.get("http://httpbin.org/get");
httpclient.execute(request2, new FutureCallback<SimpleHttpResponse>() {
@Override
@ -148,4 +148,4 @@ public class AsyncQuickStart {
}
}
}
}