From 2aa43e5c5485a5a5113989c82cc65bcd6fac56ed Mon Sep 17 00:00:00 2001 From: z7658329 <1043706593@qq.com> Date: Mon, 31 Oct 2022 11:22:12 +0800 Subject: [PATCH 1/2] support custom content-type --- .../hugegraph/rest/AbstractRestClient.java | 26 +++++++++++++++++-- 1 file changed, 24 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 276a8fe0..47158708 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.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,37 @@ private Pair> buildRequest( * 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 + */ + private MediaType parseCustomContentType(MultivaluedMap 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(); + } else if (contentTypeObj instanceof String) { + customContentType = contentTypeObj.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 From 66c5a1815009049f73a04f9ca050a14334df1c46 Mon Sep 17 00:00:00 2001 From: z7658329 <1043706593@qq.com> Date: Tue, 1 Nov 2022 10:02:10 +0800 Subject: [PATCH 2/2] support custom content-type v2 --- .../java/org/apache/hugegraph/rest/AbstractRestClient.java | 6 ++---- 1 file changed, 2 insertions(+), 4 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 47158708..abaa25d9 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 @@ -415,14 +415,12 @@ private Pair> buildRequest( * @param headers * @return */ - private MediaType parseCustomContentType(MultivaluedMap headers) { + public static MediaType parseCustomContentType(MultivaluedMap 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(); - } else if (contentTypeObj instanceof String) { - customContentType = contentTypeObj.toString(); + customContentType = ((List) contentTypeObj).get(0).toString(); } return MediaType.valueOf(customContentType); }