diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 3da16a026..ceecf89b9 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,4 +1,22 @@ -Release 5.0-BETA7 +Release 5.0-BETA8 +----------------- + +* GitHub #204: Build requests from method names in ClassicHttpRequests: + ClassicHttpRequests.create(String, String) + ClassicHttpRequests.create(String, URI) + Contributed by Gary Gregory + +* GitHub #205: Update request factory classes with matching APIs for Method and String method name inputs: + BasicHttpRequests.create(String, URI) + BasicHttpRequests.create(String, String) + ClassicHttpRequests.create(Method, String) + ClassicHttpRequests.create(Method, URI) + SimpleHttpRequests.create(String, URI) + SimpleHttpRequests.create(String, String) + Contributed by Gary Gregory + + +Release 5.0-BETA7 ----------------- This BETA release upgrades HttpCore to the latest version and addresses a number of issues found diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/BasicHttpRequests.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/BasicHttpRequests.java index ab6b446a7..02e236626 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/BasicHttpRequests.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/BasicHttpRequests.java @@ -28,10 +28,12 @@ package org.apache.hc.client5.http.async.methods; import java.net.URI; +import java.util.Locale; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.Method; import org.apache.hc.core5.http.message.BasicHttpRequest; +import org.apache.hc.core5.util.Args; /** * Common HTTP methods using {@link BasicHttpRequest} as a HTTP request message representation. @@ -40,6 +42,38 @@ import org.apache.hc.core5.http.message.BasicHttpRequest; */ public final class BasicHttpRequests { + // TODO Next version of HttpCore: + // Method.normalizedValueOf(method) + private static Method normalizedValueOf(final String method) { + return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT)); + } + + /** + * Creates a new BasicHttpRequest for the given {@code method} and {@code String} URI. + * + * @param method A method supported by this class. + * @param uri a non-null request string URI. + * @return A new BasicHttpRequest. + */ + public static BasicHttpRequest create(final String method, final String uri) { + // TODO Next version of HttpCore: + // return create(Method.normalizedValueOf(method), uri); + return create(normalizedValueOf(method), uri); + } + + /** + * Creates a new BasicHttpRequest for the given {@code method} and {@code URI}. + * + * @param method A method supported by this class. + * @param uri a non-null request URI. + * @return A new BasicHttpRequest. + */ + public static BasicHttpRequest create(final String method, final URI uri) { + // TODO Next version of HttpCore: + // return create(Method.normalizedValueOf(method), uri); + return create(normalizedValueOf(method), uri); + } + public static BasicHttpRequest delete(final String uri) { return delete(URI.create(uri)); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleHttpRequests.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleHttpRequests.java index 050f373c9..eb448158c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleHttpRequests.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleHttpRequests.java @@ -28,9 +28,11 @@ package org.apache.hc.client5.http.async.methods; import java.net.URI; +import java.util.Locale; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.Method; +import org.apache.hc.core5.util.Args; /** * Common HTTP methods using {@link SimpleHttpRequest} as a HTTP request message representation. @@ -39,6 +41,38 @@ import org.apache.hc.core5.http.Method; */ public final class SimpleHttpRequests { + // TODO Next version of HttpCore: + // Method.normalizedValueOf(method) + private static Method normalizedValueOf(final String method) { + return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT)); + } + + /** + * Creates a new BasicHttpRequest for the given {@code method} and {@code String} URI. + * + * @param method A method supported by this class. + * @param uri a non-null request string URI. + * @return A new BasicHttpRequest. + */ + public static SimpleHttpRequest create(final String method, final String uri) { + // TODO Next version of HttpCore: + // return create(Method.normalizedValueOf(method), uri); + return create(normalizedValueOf(method), uri); + } + + /** + * Creates a new BasicHttpRequest for the given {@code method} and {@code URI}. + * + * @param method A method supported by this class. + * @param uri a non-null request URI. + * @return A new BasicHttpRequest. + */ + public static SimpleHttpRequest create(final String method, final URI uri) { + // TODO Next version of HttpCore: + // return create(Method.normalizedValueOf(method), uri); + return create(normalizedValueOf(method), uri); + } + public static SimpleHttpRequest delete(final String uri) { return delete(URI.create(uri)); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java index 5f3a0a076..02287c2bc 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java @@ -28,6 +28,10 @@ package org.apache.hc.client5.http.classic.methods; import java.net.URI; +import java.util.Locale; + +import org.apache.hc.core5.http.Method; +import org.apache.hc.core5.util.Args; /** @@ -40,6 +44,79 @@ import java.net.URI; */ public final class ClassicHttpRequests { + private static Method normalizedValueOf(final String method) { + // TODO Next version of HttpCore: + // Method.normalizedValueOf(method) + return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT)); + } + + /** + * Creates a new HttpUriRequest for the given {@code Method} and {@code String} URI. + * + * @param method A method. + * @param uri a URI. + * @return a new HttpUriRequest. + */ + public static HttpUriRequest create(final Method method, final String uri) { + return create(method, URI.create(uri)); + } + + /** + * Creates a new HttpUriRequest for the given {@code Method} and {@code URI}. + * + * @param method A method. + * @param uri a URI. + * @return a new HttpUriRequest. + */ + public static HttpUriRequest create(final Method method, final URI uri) { + switch (Args.notNull(method, "method")) { + case DELETE: + return delete(uri); + case GET: + return get(uri); + case HEAD: + return head(uri); + case OPTIONS: + return options(uri); + case PATCH: + return patch(uri); + case POST: + return post(uri); + case PUT: + return put(uri); + case TRACE: + return trace(uri); + default: + throw new IllegalArgumentException(method.toString()); + } + } + + /** + * Creates a new HttpUriRequest for the given {@code method} and {@code String} URI. + * + * @param method A method supported by this class. + * @param uri a non-null request string URI. + * @throws IllegalArgumentException if the method is not supported. + * @throws IllegalArgumentException if the string uri is null. + * @return A new HttpUriRequest. + */ + public static HttpUriRequest create(final String method, final String uri) { + return create(normalizedValueOf(method), uri); + } + + /** + * Creates a new HttpUriRequest for the given {@code method} and {@code URI}. + * + * @param method A method supported by this class. + * @param uri a non-null request URI. + * @throws IllegalArgumentException if the method is not supported. + * @throws IllegalArgumentException if the uri is null. + * @return A new HttpUriRequest. + */ + public static HttpUriRequest create(final String method, final URI uri) { + return create(normalizedValueOf(method), uri); + } + public static HttpUriRequest delete(final String uri) { return delete(URI.create(uri)); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/SimpleBasicHttpRequests.java b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/SimpleBasicHttpRequests.java new file mode 100644 index 000000000..473cdbcc2 --- /dev/null +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/SimpleBasicHttpRequests.java @@ -0,0 +1,91 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.hc.client5.http.async.methods; + +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Arrays; + +import org.apache.hc.core5.http.HttpRequest; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class SimpleBasicHttpRequests { + + private static final String URI_STRING_FIXTURE = "http://localhost"; + private static final URI URI_FIXTURE = URI.create(URI_STRING_FIXTURE); + + @Parameters(name = "{index}: {0} => {1}") + public static Iterable data() { + return Arrays.asList(new Object[][] { + // @formatter:off + { "delete", "DELETE" }, + { "get", "GET" }, + { "head", "HEAD" }, + { "options", "OPTIONS" }, + { "patch", "PATCH" }, + { "post", "POST" }, + { "put", "PUT" }, + { "trace", "TRACE" } + // @formatter:on + }); + } + + private final String methodName; + + private final String expectedMethod; + + public SimpleBasicHttpRequests(final String methodName, final String expectedMethod) { + this.methodName = methodName; + this.expectedMethod = expectedMethod; + } + + @Test + public void testCreateMethodUri() { + Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_FIXTURE).getClass()); + Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_FIXTURE).getClass()); + } + + @Test + public void testCreateMethodUriString() { + Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass()); + Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_STRING_FIXTURE).getClass()); + } + + @Test + public void testCreateClassicHttpRequest() throws Exception { + final Method httpMethod = SimpleHttpRequests.class.getMethod(methodName, URI.class); + final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE); + Assert.assertEquals(SimpleHttpRequest.class, basicHttpRequest.getClass()); + Assert.assertEquals(expectedMethod, basicHttpRequest.getMethod()); + } +} diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/TestBasicHttpRequests.java b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/TestBasicHttpRequests.java index a1b00801d..f0e7a9bd8 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/TestBasicHttpRequests.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/TestBasicHttpRequests.java @@ -42,7 +42,8 @@ import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class TestBasicHttpRequests { - private static final URI URI_FIXTURE = URI.create("http://localhost"); + private static final String URI_STRING_FIXTURE = "http://localhost"; + private static final URI URI_FIXTURE = URI.create(URI_STRING_FIXTURE); @Parameters(name = "{index}: {0} => {1}") public static Iterable data() { @@ -69,6 +70,18 @@ public class TestBasicHttpRequests { this.expectedMethod = expectedMethod; } + @Test + public void testCreateMethodUri() { + Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(methodName, URI_FIXTURE).getClass()); + Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(expectedMethod, URI_FIXTURE).getClass()); + } + + @Test + public void testCreateMethodUriString() { + Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass()); + Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(expectedMethod, URI_STRING_FIXTURE).getClass()); + } + @Test public void testCreateClassicHttpRequest() throws Exception { final Method httpMethod = BasicHttpRequests.class.getMethod(methodName, URI.class); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java b/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java index 7996cddac..63998a684 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java @@ -70,6 +70,16 @@ public class TestClassicHttpRequests { this.expectedClass = expectedClass; } + @Test + public void testCreateMethodUri() { + Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_FIXTURE).getClass()); + } + + @Test + public void testCreateMethodUriString() { + Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass()); + } + @Test public void testCreateFromString() throws Exception { final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);