You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2020/01/28 22:18:29 UTC

[httpcomponents-client] branch master updated (538a60b -> 95dbbf0)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git.


 discard 538a60b  Now that ClassicHttpRequests is no longer an enum, we need to way to (#205)
    omit ca61e60  Build requests from method names in ClassicHttpRequests #204.
    omit 776ecda  Now that ClassicHttpRequests is no longer an enum, I need to way to (#204)
     new 95dbbf0  Now that ClassicHttpRequests is no longer an enum, we need to way to generically build requests from method names. Update all factory classes with matching APIs for Method and String method name inputs.

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (538a60b)
            \
             N -- N -- N   refs/heads/master (95dbbf0)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


[httpcomponents-client] 01/01: Now that ClassicHttpRequests is no longer an enum, we need to way to generically build requests from method names. Update all factory classes with matching APIs for Method and String method name inputs.

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 95dbbf099b3b5cc4bcb9de160fc5a339aecec91c
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Tue Jan 28 12:17:46 2020 -0500

    Now that ClassicHttpRequests is no longer an enum, we need to way to
    generically build requests from method names. Update all factory classes
    with matching APIs for Method and String method name inputs.
    
    (#204), (#205)
---
 RELEASE_NOTES.txt                                  | 20 +++++-
 .../http/async/methods/BasicHttpRequests.java      | 34 ++++++++++
 .../http/async/methods/SimpleHttpRequests.java     | 34 ++++++++++
 .../http/classic/methods/ClassicHttpRequests.java  | 77 ++++++++++++++++++++++
 ...pRequests.java => SimpleBasicHttpRequests.java} | 24 +++++--
 .../http/async/methods/TestBasicHttpRequests.java  | 15 ++++-
 .../classic/methods/TestClassicHttpRequests.java   | 10 +++
 7 files changed, 206 insertions(+), 8 deletions(-)

diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 3da16a0..ceecf89 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 <ggregory at apache.org>
+
+* 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 <ggregory at apache.org>
+
+
+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 ab6b446..02e2366 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 050f373..eb44815 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 5f3a0a0..02287c2 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/TestBasicHttpRequests.java b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/SimpleBasicHttpRequests.java
similarity index 69%
copy from httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/TestBasicHttpRequests.java
copy to httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/SimpleBasicHttpRequests.java
index a1b0080..473cdbc 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/SimpleBasicHttpRequests.java
@@ -32,7 +32,6 @@ import java.net.URI;
 import java.util.Arrays;
 
 import org.apache.hc.core5.http.HttpRequest;
-import org.apache.hc.core5.http.message.BasicHttpRequest;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,9 +39,10 @@ import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
 @RunWith(Parameterized.class)
-public class TestBasicHttpRequests {
+public class SimpleBasicHttpRequests {
 
-  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<Object[]> data() {
@@ -64,16 +64,28 @@ public class TestBasicHttpRequests {
 
   private final String expectedMethod;
 
-  public TestBasicHttpRequests(final String methodName, 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 = BasicHttpRequests.class.getMethod(methodName, URI.class);
+    final Method httpMethod = SimpleHttpRequests.class.getMethod(methodName, URI.class);
     final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE);
-    Assert.assertEquals(BasicHttpRequest.class, basicHttpRequest.getClass());
+    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 a1b0080..f0e7a9b 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<Object[]> data() {
@@ -70,6 +71,18 @@ public class TestBasicHttpRequests {
   }
 
   @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);
     final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE);
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 7996cdd..63998a6 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
@@ -71,6 +71,16 @@ public class TestClassicHttpRequests {
   }
 
   @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);
       Assert.assertEquals(expectedClass,