From 0dfb5b5915c197ba3cbb1fb1933c57adb59904e8 Mon Sep 17 00:00:00 2001 From: Lico-Tom <583566138@qq.com> Date: Mon, 13 Mar 2023 20:15:44 +0800 Subject: [PATCH 1/2] add jackson component and surefire plugin. --- pom.xml | 31 +++++-- .../protocol/bookkeeper/InnerHttpClient.java | 84 +++++++++++++++++++ .../protocol/bookkeeper/JacksonService.java | 65 ++++++++++++++ 3 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/protocol/bookkeeper/JacksonService.java diff --git a/pom.xml b/pom.xml index 61b4124..c79eee2 100644 --- a/pom.xml +++ b/pom.xml @@ -15,14 +15,21 @@ src/main/java 0.0.1 + 2.14.2 5.9.2 2.20.0 3.10.1 3.2.0 + 3.4.1 1.18.26 10.8.0 + 3.0.0-M6 + 1.13.0 + 3.2.1 4.7.2.0 + 3.0.0-M9 + 1.6.13 @@ -36,6 +43,11 @@ log4j-slf4j-impl ${log4j.version} + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + org.junit.jupiter junit-jupiter-engine @@ -99,10 +111,19 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire-plugin.version} + true + + false + + org.apache.maven.plugins maven-release-plugin - 3.0.0-M6 + ${release-plugin.version} true false @@ -113,14 +134,14 @@ org.apache.maven.scm maven-scm-provider-gitexe - 1.13.0 + ${scm-provider-gitexe.version} org.apache.maven.plugins maven-javadoc-plugin - 3.4.1 + ${javadoc-plugin.version} ${src.dir} all,-missing @@ -137,7 +158,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + ${source-plugin.version} attach-sources @@ -150,7 +171,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.13 + ${nexus-staging-maven-plugin.version} true ossrh diff --git a/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java b/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java index 79b77db..90ad1ed 100644 --- a/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java +++ b/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java @@ -1,9 +1,14 @@ package io.github.protocol.bookkeeper; +import com.fasterxml.jackson.core.JsonProcessingException; + +import java.io.IOException; import java.net.URI; +import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; public class InnerHttpClient { private final Configuration conf; @@ -27,4 +32,83 @@ public HttpResponse get(String urlSuffix) throws Exception { .build(); return client.send(request, HttpResponse.BodyHandlers.ofString()); } + + + public HttpResponse post(String url, Object body, String... params) + throws IOException, InterruptedException { + return this.post(url, objectToString(body), params); + } + + public HttpResponse post(String url, String body, String... params) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(getUri(url, params)) + .POST(HttpRequest.BodyPublishers.ofString(body)) + .build(); + return client.send(request, HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse post(String url) throws IOException, InterruptedException { + return this.post(url, ""); + } + + public HttpResponse put(String url) throws IOException, InterruptedException { + return this.put(url, ""); + } + + public HttpResponse put(String url, Object body, String... params) + throws IOException, InterruptedException { + return this.put(url, objectToString(body), params); + } + + public HttpResponse put(String url, String body, String... params) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(getUri(url, params)) + .PUT(HttpRequest.BodyPublishers.ofString(body)) + .build(); + return client.send(request, HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse delete(String url, String... requestParams) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(getUri(url, requestParams)) + .DELETE() + .build(); + return client.send(request, HttpResponse.BodyHandlers.ofString()); + } + + private URI getUri(String url, String... params) { + return URI.create(this.httpPrefix + url + mapToParams(params)); + } + + static String mapToParams(String... requestParams) { + if (requestParams.length % 2 != 0) { + throw new IllegalArgumentException("params list length cannot be odd"); + } + if (requestParams.length == 0) { + return ""; + } + StringBuilder res = new StringBuilder("?"); + res.append(requestParams[0]); + res.append('='); + res.append(requestParams[1]); + for (int i = 2; i < requestParams.length; ) { + res.append('&'); + res.append(encode(requestParams[i++])); + res.append('='); + res.append(encode(requestParams[i++])); + } + return res.toString(); + } + + private String objectToString(Object obj) throws JsonProcessingException { + return obj == null ? "" : JacksonService.toJson(obj); + } + + private static String encode(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8); + } + } diff --git a/src/main/java/io/github/protocol/bookkeeper/JacksonService.java b/src/main/java/io/github/protocol/bookkeeper/JacksonService.java new file mode 100644 index 0000000..e554567 --- /dev/null +++ b/src/main/java/io/github/protocol/bookkeeper/JacksonService.java @@ -0,0 +1,65 @@ +/* + * 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. + */ +package io.github.protocol.bookkeeper; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import java.util.List; + +public class JacksonService { + private static final ObjectMapper MAPPER = new ObjectMapper(); + + static { + MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); + } + + public static String toJson(Object o) throws JsonProcessingException { + return MAPPER.writeValueAsString(o); + } + + public static T toObject(String json, Class type) throws JsonProcessingException { + return MAPPER.readValue(json, type); + } + + public static T toRefer(String json, TypeReference ref) throws JsonProcessingException { + return MAPPER.readValue(json, ref); + } + + public static List toList(String json, TypeReference> typeRef) throws JsonProcessingException { + return MAPPER.readValue(json, typeRef); + } + + public static JsonNode toJsonNode(String json) throws JsonProcessingException { + return MAPPER.readTree(json); + } + + public static ObjectNode createObjectNode() { + return MAPPER.createObjectNode(); + } + + public static ArrayNode createArrayNode() { + return MAPPER.createArrayNode(); + } +} From b0a833ad2030d1cbd567050adcf87a9f2d3695e7 Mon Sep 17 00:00:00 2001 From: ZhangJian He Date: Mon, 13 Mar 2023 22:01:40 +0800 Subject: [PATCH 2/2] Update InnerHttpClient.java --- src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java b/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java index 90ad1ed..21ab6ac 100644 --- a/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java +++ b/src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java @@ -33,7 +33,6 @@ public HttpResponse get(String urlSuffix) throws Exception { return client.send(request, HttpResponse.BodyHandlers.ofString()); } - public HttpResponse post(String url, Object body, String... params) throws IOException, InterruptedException { return this.post(url, objectToString(body), params);