-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor http request code by http-facade
Signed-off-by: moxiaoying <[email protected]>
- Loading branch information
Showing
9 changed files
with
273 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 63 additions & 62 deletions
125
pulsar-admin-jdk/src/main/java/io/github/protocol/pulsar/admin/jdk/BaseTopicsImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 75 additions & 76 deletions
151
pulsar-admin-jdk/src/main/java/io/github/protocol/pulsar/admin/jdk/InnerHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.