Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
<src.dir>src/main/java</src.dir>
<!-- dependency -->
<embedded-bookkeeper.version>0.0.1</embedded-bookkeeper.version>
<jackson.version>2.14.2</jackson.version>
<junit.version>5.9.2</junit.version>
<log4j.version>2.20.0</log4j.version>
<!-- plugin -->
<compiler-plugin.version>3.10.1</compiler-plugin.version>
<checkstyle-plugin.version>3.2.0</checkstyle-plugin.version>
<javadoc-plugin.version>3.4.1</javadoc-plugin.version>
<lombok.version>1.18.26</lombok.version>
<puppycrawl.version>10.8.0</puppycrawl.version>
<release-plugin.version>3.0.0-M6</release-plugin.version>
<scm-provider-gitexe.version>1.13.0</scm-provider-gitexe.version>
<source-plugin.version>3.2.1</source-plugin.version>
<spotbugs-maven-plugin.version>4.7.2.0</spotbugs-maven-plugin.version>
<surefire-plugin.version>3.0.0-M9</surefire-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
</properties>

<dependencies>
Expand All @@ -36,6 +43,11 @@
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down Expand Up @@ -99,10 +111,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M6</version>
<version>${release-plugin.version}</version>
<configuration>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
Expand All @@ -113,14 +134,14 @@
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.13.0</version>
<version>${scm-provider-gitexe.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<version>${javadoc-plugin.version}</version>
<configuration>
<sourcepath>${src.dir}</sourcepath>
<doclint>all,-missing</doclint>
Expand All @@ -137,7 +158,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>${source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -150,7 +171,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<version>${nexus-staging-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/InnerHttpClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,4 +32,82 @@ public HttpResponse<String> get(String urlSuffix) throws Exception {
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
}

public HttpResponse<String> post(String url, Object body, String... params)
throws IOException, InterruptedException {
return this.post(url, objectToString(body), params);
}

public HttpResponse<String> 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<String> post(String url) throws IOException, InterruptedException {
return this.post(url, "");
}

public HttpResponse<String> put(String url) throws IOException, InterruptedException {
return this.put(url, "");
}

public HttpResponse<String> put(String url, Object body, String... params)
throws IOException, InterruptedException {
return this.put(url, objectToString(body), params);
}

public HttpResponse<String> 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<String> 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);
}

}
65 changes: 65 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/JacksonService.java
Original file line number Diff line number Diff line change
@@ -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> T toObject(String json, Class<T> type) throws JsonProcessingException {
return MAPPER.readValue(json, type);
}

public static <T> T toRefer(String json, TypeReference<T> ref) throws JsonProcessingException {
return MAPPER.readValue(json, ref);
}

public static <T> List<T> toList(String json, TypeReference<List<T>> 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();
}
}