You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by va...@apache.org on 2022/11/01 10:16:48 UTC

[incubator-hugegraph-commons] branch master updated: support custom content-type (#113)

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

vaughn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git


The following commit(s) were added to refs/heads/master by this push:
     new 16602cc  support custom content-type (#113)
16602cc is described below

commit 16602cc8394ac9c2e3ee194cd48ea26f0061e784
Author: 青年 <10...@qq.com>
AuthorDate: Tue Nov 1 18:16:44 2022 +0800

    support custom content-type (#113)
    
    * support custom content-type
    
    * support custom content-type v2
---
 .../apache/hugegraph/rest/AbstractRestClient.java  | 24 ++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
index fd9fdab..0ccf1ad 100644
--- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
+++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java
@@ -26,11 +26,13 @@ import java.security.SecureRandom;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.collections.MapUtils;
 import org.apache.hugegraph.util.E;
 import org.apache.hugegraph.util.ExecutorUtil;
 import jakarta.ws.rs.client.Client;
@@ -396,17 +398,35 @@ public abstract class AbstractRestClient implements RestClient {
          * because Entity.json() method will reset "content encoding =
          * null" that has been set up by headers before.
          */
+        MediaType customContentType = parseCustomContentType(headers);
         Entity<?> entity;
         if (encoding == null) {
-            entity = Entity.json(object);
+            entity = Entity.entity(object, customContentType);
         } else {
-            Variant variant = new Variant(MediaType.APPLICATION_JSON_TYPE,
+            Variant variant = new Variant(customContentType,
                                           (String) null, encoding);
             entity = Entity.entity(object, variant);
         }
         return Pair.of(builder, entity);
     }
 
+    /**
+     * parse user custom content-type, returns MediaType.APPLICATION_JSON_TYPE default.
+     * @param headers
+     * @return
+     */
+    public static MediaType parseCustomContentType(MultivaluedMap<String, Object> headers) {
+        String customContentType = null;
+        if (MapUtils.isNotEmpty(headers) && headers.get("Content-Type") != null) {
+            Object contentTypeObj = headers.get("Content-Type");
+            if (contentTypeObj instanceof List) {
+                customContentType = ((List<?>) contentTypeObj).get(0).toString();
+            }
+            return MediaType.valueOf(customContentType);
+        }
+        return MediaType.APPLICATION_JSON_TYPE;
+    }
+
     private static void configConnectionManager(String url, ClientConfig conf) {
         /*
          * Using httpclient with connection pooling, and configuring the