Skip to content

Commit

Permalink
refactor: refactor http request code by http-facade
Browse files Browse the repository at this point in the history
Signed-off-by: moxiaoying <[email protected]>
  • Loading branch information
CennyMo committed Dec 8, 2024
1 parent 7f741c1 commit 5d2852c
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 291 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
<spotbugs-maven-plugin.version>4.8.6.2</spotbugs-maven-plugin.version>
<spotless-maven-plugin.version>2.43.0</spotless-maven-plugin.version>
<http-facade.version>0.3.0</http-facade.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -97,6 +98,11 @@
<version>${embedded-pulsar.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.openfacade</groupId>
<artifactId>http-facade</artifactId>
<version>${http-facade.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.List;

public class JacksonService {
Expand All @@ -19,25 +20,29 @@ public class JacksonService {
}

public static String toJson(Object o) throws JsonProcessingException {
return MAPPER.writeValueAsString(o);
return o == null ? null : MAPPER.writeValueAsString(o);
}

public static <T> T toObject(String json, Class<T> type) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
public static byte[] toBytes(Object o) throws JsonProcessingException {
return o == null ? null : MAPPER.writeValueAsBytes(o);
}

public static <T> T toObject(byte[] json, Class<T> type) throws IOException {
if (json == null || json.length == 0) {
return null;
}
return MAPPER.readValue(json, type);
}

public static <T> T toRefer(String json, TypeReference<T> ref) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
public static <T> T toRefer(byte[] json, TypeReference<T> ref) throws IOException {
if (json == null || json.length == 0) {
return null;
}
return MAPPER.readValue(json, ref);
}

public static <T> List<T> toList(String json, TypeReference<List<T>> typeRef) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
public static <T> List<T> toList(byte[] json, TypeReference<List<T>> typeRef) throws IOException {
if (json == null || json.length == 0) {
return List.of();
}
return MAPPER.readValue(json, typeRef);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.protocol.pulsar.admin.jdk;

import java.net.http.HttpResponse;
import io.github.openfacade.http.HttpResponse;

public class Brokers {
private final InnerHttpClient innerHttpClient;
Expand All @@ -15,12 +15,12 @@ public void healthcheck(TopicVersion topicVersion) throws PulsarAdminException {
url += "?topicVersion=" + topicVersion;
}
try {
HttpResponse<String> httpResponse = innerHttpClient.get(url);
HttpResponse httpResponse = innerHttpClient.get(url);
if (httpResponse.statusCode() != 200) {
throw new PulsarAdminException("healthcheck failed, status code: " + httpResponse.statusCode(),
httpResponse.statusCode());
}
if (!httpResponse.body().equals("ok")) {
if (!httpResponse.bodyAsString().equals("ok")) {
throw new PulsarAdminException("healthcheck failed, body: " + httpResponse.body());
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.protocol.pulsar.admin.jdk;

import com.fasterxml.jackson.core.type.TypeReference;
import io.github.openfacade.http.HttpResponse;
import io.github.protocol.pulsar.admin.common.JacksonService;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class Clusters {

Expand All @@ -17,15 +18,14 @@ public Clusters(InnerHttpClient httpClient) {

public List<String> getClusters() throws PulsarAdminException {
try {
HttpResponse<String> response = httpClient.get(UrlConst.CLUSTERS);
HttpResponse response = httpClient.get(UrlConst.CLUSTERS);
if (response.statusCode() != 200) {
throw new PulsarAdminException(
String.format("failed to get list of clusters, "
+ "status code %s, body : %s", response.statusCode(), response.body()));
}
return JacksonService.toRefer(response.body(), new TypeReference<List<String>>() {
});
} catch (IOException | InterruptedException e) {
return JacksonService.toRefer(response.body(), new TypeReference<>() {});
} catch (IOException | InterruptedException | ExecutionException e) {

Check warning on line 28 in pulsar-admin-jdk/src/main/java/io/github/protocol/pulsar/admin/jdk/Clusters.java

View check run for this annotation

Codecov / codecov/patch

pulsar-admin-jdk/src/main/java/io/github/protocol/pulsar/admin/jdk/Clusters.java#L28

Added line #L28 was not covered by tests
throw new PulsarAdminException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,128 +1,127 @@
package io.github.protocol.pulsar.admin.jdk;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.openfacade.http.HttpClient;
import io.github.openfacade.http.HttpClientConfig;
import io.github.openfacade.http.HttpClientEngine;
import io.github.openfacade.http.HttpClientFactory;
import io.github.openfacade.http.HttpMethod;
import io.github.openfacade.http.HttpRequest;
import io.github.openfacade.http.HttpResponse;
import io.github.openfacade.http.HttpSchema;
import io.github.openfacade.http.TlsConfig;
import io.github.openfacade.http.UrlBuilder;
import io.github.protocol.pulsar.admin.api.Configuration;
import io.github.protocol.pulsar.admin.common.JacksonService;

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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class InnerHttpClient {
private final Configuration conf;

private final HttpClient client;

private final String httpPrefix;
private UrlBuilder templateUrlBuilder;

public InnerHttpClient(Configuration conf) {
this.conf = conf;
HttpClient.Builder builder = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1);
HttpClientConfig.Builder clientConfigBuilder = new HttpClientConfig.Builder();
clientConfigBuilder.engine(HttpClientEngine.JAVA);
if (conf.tlsEnabled) {
builder = builder
.sslContext(SslContextUtil.build(conf.tlsConfig));
this.httpPrefix = "https://" + conf.host + ":" + conf.port;
} else {
this.httpPrefix = "http://" + conf.host + ":" + conf.port;
TlsConfig.Builder tlsConfigBuilder = new TlsConfig.Builder();
io.github.protocol.pulsar.admin.api.TlsConfig tlsConfig = conf.tlsConfig;
tlsConfigBuilder.cipherSuites(tlsConfig.cipherSuites);
tlsConfigBuilder.hostnameVerifyDisabled(tlsConfig.hostnameVerifyDisabled);
tlsConfigBuilder.keyStore(tlsConfig.keyStorePath, tlsConfig.keyStorePassword);
tlsConfigBuilder.trustStore(tlsConfig.trustStorePath, tlsConfig.trustStorePassword);
tlsConfigBuilder.verifyDisabled(tlsConfig.verifyDisabled);
clientConfigBuilder.tlsConfig(tlsConfigBuilder.build());
}
this.client = builder.build();
this.client = HttpClientFactory.createHttpClient(clientConfigBuilder.build());
templateUrlBuilder = new UrlBuilder();
templateUrlBuilder.setHttpSchema(conf.tlsEnabled ? HttpSchema.HTTPS : HttpSchema.HTTP).setHost(conf.host)
.setPort(conf.port);
}

public HttpResponse<String> get(String url) throws IOException, InterruptedException {
return this.get(url, new String[0]);
public HttpResponse post(String url) throws IOException, InterruptedException, ExecutionException {
return this.innerPost(url, new byte[0]);
}

public HttpResponse<String> get(String url, String... requestParams)
throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(getUri(url, requestParams))
.GET()
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
public HttpResponse post(String url, Object body) throws IOException, InterruptedException, ExecutionException {
return this.innerPost(url, objectToBytes(body));
}

public HttpResponse<String> post(String url, Object body, String... params)
throws IOException, InterruptedException {
return this.post(url, objectToString(body), params);
public HttpResponse post(String url, Object body, String... params)
throws IOException, ExecutionException, InterruptedException {
return this.innerPost(url, objectToBytes(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 == null ? "" : body))
.setHeader("Content-Type", "application/json")
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
private HttpResponse innerPost(String url, byte[] body, String... params) throws InterruptedException, ExecutionException {
Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", List.of("application/json"));
HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.POST, headers, body);
return client.send(request).get();
}

public HttpResponse<String> post(String url) throws IOException, InterruptedException {
return this.post(url, "");
public HttpResponse put(String url) throws IOException, InterruptedException, ExecutionException {
return this.innerPut(url, new byte[0]);
}

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

public HttpResponse<String> put(String url, Object body, String... params)
throws IOException, InterruptedException {
return this.put(url, objectToString(body), params);
public HttpResponse put(String url, Object body, String... params) throws IOException, InterruptedException, ExecutionException {
return this.innerPut(url, objectToBytes(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 == null ? "" : body))
.setHeader("Content-Type", "application/json")
.build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
private HttpResponse innerPut(String url, byte[] body, String... params) throws InterruptedException, ExecutionException {
Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", List.of("application/json"));
HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.PUT, headers, body);
return client.send(request).get();
}

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());
public HttpResponse delete(String url, String... params) throws IOException, InterruptedException, ExecutionException {
HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.DELETE);
return client.send(request).get();
}

private URI getUri(String url, String... params) {
return URI.create(this.httpPrefix + url + mapToParams(params));
public HttpResponse get(String url, String... params) throws IOException, InterruptedException, ExecutionException {
HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.GET);
return client.send(request).get();
}

static String mapToParams(String... requestParams) {
private List<UrlBuilder.Param> convertListToParams(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++]));
List<UrlBuilder.Param> queryParams = new ArrayList<>();
for (int i = 0; i < requestParams.length; i = i + 2) {
queryParams.add(new UrlBuilder.Param(requestParams[i], requestParams[i+1]));
}
return res.toString();
return queryParams;
}

private String objectToString(Object obj) throws JsonProcessingException {
return obj == null ? "" : JacksonService.toJson(obj);
private byte[] objectToBytes(Object obj) throws JsonProcessingException {
return obj == null ? new byte[0]: JacksonService.toBytes(obj);
}

@Deprecated
private static String encode(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}

private String concatUrlWithParams(String url, String... params) {
UrlBuilder urlBuilder = templateUrlBuilder.duplicate();
urlBuilder.setPath(url);
if (params != null && params.length > 0) {
urlBuilder.setQueryParams(convertListToParams(params));
}
return urlBuilder.build();
}
}
Loading

0 comments on commit 5d2852c

Please sign in to comment.