|
1 | 1 | package io.github.protocol.pulsar.admin.jdk; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import io.github.openfacade.http.HttpClient; |
| 5 | +import io.github.openfacade.http.HttpClientConfig; |
| 6 | +import io.github.openfacade.http.HttpClientEngine; |
| 7 | +import io.github.openfacade.http.HttpClientFactory; |
| 8 | +import io.github.openfacade.http.HttpMethod; |
| 9 | +import io.github.openfacade.http.HttpRequest; |
| 10 | +import io.github.openfacade.http.HttpResponse; |
| 11 | +import io.github.openfacade.http.HttpSchema; |
| 12 | +import io.github.openfacade.http.TlsConfig; |
| 13 | +import io.github.openfacade.http.UrlBuilder; |
4 | 14 | import io.github.protocol.pulsar.admin.api.Configuration; |
5 | 15 | import io.github.protocol.pulsar.admin.common.JacksonService; |
6 | 16 |
|
7 | 17 | import java.io.IOException; |
8 | | -import java.net.URI; |
9 | | -import java.net.URLEncoder; |
10 | | -import java.net.http.HttpClient; |
11 | | -import java.net.http.HttpRequest; |
12 | | -import java.net.http.HttpResponse; |
13 | | -import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.ExecutionException; |
14 | 23 |
|
15 | 24 | public class InnerHttpClient { |
16 | | - private final Configuration conf; |
17 | | - |
18 | 25 | private final HttpClient client; |
19 | 26 |
|
20 | | - private final String httpPrefix; |
| 27 | + private UrlBuilder templateUrlBuilder; |
21 | 28 |
|
22 | 29 | public InnerHttpClient(Configuration conf) { |
23 | | - this.conf = conf; |
24 | | - HttpClient.Builder builder = HttpClient.newBuilder() |
25 | | - .version(HttpClient.Version.HTTP_1_1); |
| 30 | + HttpClientConfig.Builder clientConfigBuilder = new HttpClientConfig.Builder(); |
| 31 | + clientConfigBuilder.engine(HttpClientEngine.JAVA); |
26 | 32 | if (conf.tlsEnabled) { |
27 | | - builder = builder |
28 | | - .sslContext(SslContextUtil.build(conf.tlsConfig)); |
29 | | - this.httpPrefix = "https://" + conf.host + ":" + conf.port; |
30 | | - } else { |
31 | | - this.httpPrefix = "http://" + conf.host + ":" + conf.port; |
| 33 | + TlsConfig.Builder tlsConfigBuilder = new TlsConfig.Builder(); |
| 34 | + io.github.protocol.pulsar.admin.api.TlsConfig tlsConfig = conf.tlsConfig; |
| 35 | + tlsConfigBuilder.cipherSuites(tlsConfig.cipherSuites); |
| 36 | + tlsConfigBuilder.hostnameVerifyDisabled(tlsConfig.hostnameVerifyDisabled); |
| 37 | + tlsConfigBuilder.keyStore(tlsConfig.keyStorePath, tlsConfig.keyStorePassword); |
| 38 | + tlsConfigBuilder.trustStore(tlsConfig.trustStorePath, tlsConfig.trustStorePassword); |
| 39 | + tlsConfigBuilder.verifyDisabled(tlsConfig.verifyDisabled); |
| 40 | + clientConfigBuilder.tlsConfig(tlsConfigBuilder.build()); |
32 | 41 | } |
33 | | - this.client = builder.build(); |
| 42 | + this.client = HttpClientFactory.createHttpClient(clientConfigBuilder.build()); |
| 43 | + templateUrlBuilder = new UrlBuilder(); |
| 44 | + templateUrlBuilder.setHttpSchema(conf.tlsEnabled ? HttpSchema.HTTPS : HttpSchema.HTTP).setHost(conf.host) |
| 45 | + .setPort(conf.port); |
34 | 46 | } |
35 | 47 |
|
36 | | - public HttpResponse<String> get(String url) throws IOException, InterruptedException { |
37 | | - return this.get(url, new String[0]); |
| 48 | + public HttpResponse post(String url) throws IOException, InterruptedException, ExecutionException { |
| 49 | + return this.innerPost(url, new byte[0]); |
38 | 50 | } |
39 | 51 |
|
40 | | - public HttpResponse<String> get(String url, String... requestParams) |
41 | | - throws IOException, InterruptedException { |
42 | | - HttpRequest request = HttpRequest.newBuilder() |
43 | | - .uri(getUri(url, requestParams)) |
44 | | - .GET() |
45 | | - .build(); |
46 | | - return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 52 | + public HttpResponse post(String url, Object body) throws IOException, InterruptedException, ExecutionException { |
| 53 | + return this.innerPost(url, objectToBytes(body)); |
47 | 54 | } |
48 | 55 |
|
49 | | - public HttpResponse<String> post(String url, Object body, String... params) |
50 | | - throws IOException, InterruptedException { |
51 | | - return this.post(url, objectToString(body), params); |
| 56 | + public HttpResponse post(String url, Object body, String... params) |
| 57 | + throws IOException, ExecutionException, InterruptedException { |
| 58 | + return this.innerPost(url, objectToBytes(body), params); |
52 | 59 | } |
53 | 60 |
|
54 | | - public HttpResponse<String> post(String url, String body, String... params) |
55 | | - throws IOException, InterruptedException { |
56 | | - HttpRequest request = HttpRequest.newBuilder() |
57 | | - .uri(getUri(url, params)) |
58 | | - .POST(HttpRequest.BodyPublishers.ofString(body == null ? "" : body)) |
59 | | - .setHeader("Content-Type", "application/json") |
60 | | - .build(); |
61 | | - return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 61 | + private HttpResponse innerPost(String url, byte[] body, String... params) |
| 62 | + throws InterruptedException, ExecutionException { |
| 63 | + Map<String, List<String>> headers = new HashMap<>(); |
| 64 | + headers.put("Content-Type", List.of("application/json")); |
| 65 | + HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.POST, headers, body); |
| 66 | + return client.send(request).get(); |
62 | 67 | } |
63 | 68 |
|
64 | | - public HttpResponse<String> post(String url) throws IOException, InterruptedException { |
65 | | - return this.post(url, ""); |
| 69 | + public HttpResponse put(String url) throws IOException, InterruptedException, ExecutionException { |
| 70 | + return this.innerPut(url, new byte[0]); |
66 | 71 | } |
67 | 72 |
|
68 | | - public HttpResponse<String> put(String url) throws IOException, InterruptedException { |
69 | | - return this.put(url, ""); |
| 73 | + public HttpResponse put(String url, Object body) throws IOException, InterruptedException, ExecutionException { |
| 74 | + return this.innerPut(url, objectToBytes(body)); |
70 | 75 | } |
71 | 76 |
|
72 | | - public HttpResponse<String> put(String url, Object body, String... params) |
73 | | - throws IOException, InterruptedException { |
74 | | - return this.put(url, objectToString(body), params); |
| 77 | + public HttpResponse put(String url, Object body, String... params) |
| 78 | + throws IOException, InterruptedException, ExecutionException { |
| 79 | + return this.innerPut(url, objectToBytes(body), params); |
75 | 80 | } |
76 | 81 |
|
77 | | - public HttpResponse<String> put(String url, String body, String... params) |
78 | | - throws IOException, InterruptedException { |
79 | | - HttpRequest request = HttpRequest.newBuilder() |
80 | | - .uri(getUri(url, params)) |
81 | | - .PUT(HttpRequest.BodyPublishers.ofString(body == null ? "" : body)) |
82 | | - .setHeader("Content-Type", "application/json") |
83 | | - .build(); |
84 | | - return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 82 | + private HttpResponse innerPut(String url, byte[] body, String... params) |
| 83 | + throws InterruptedException, ExecutionException { |
| 84 | + Map<String, List<String>> headers = new HashMap<>(); |
| 85 | + headers.put("Content-Type", List.of("application/json")); |
| 86 | + HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.PUT, headers, body); |
| 87 | + return client.send(request).get(); |
85 | 88 | } |
86 | 89 |
|
87 | | - public HttpResponse<String> delete(String url, String... requestParams) |
88 | | - throws IOException, InterruptedException { |
89 | | - HttpRequest request = HttpRequest.newBuilder() |
90 | | - .uri(getUri(url, requestParams)) |
91 | | - .DELETE() |
92 | | - .build(); |
93 | | - return client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 90 | + public HttpResponse delete(String url, String... params) |
| 91 | + throws IOException, InterruptedException, ExecutionException { |
| 92 | + HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.DELETE); |
| 93 | + return client.send(request).get(); |
94 | 94 | } |
95 | 95 |
|
96 | | - private URI getUri(String url, String... params) { |
97 | | - return URI.create(this.httpPrefix + url + mapToParams(params)); |
| 96 | + public HttpResponse get(String url, String... params) throws IOException, InterruptedException, ExecutionException { |
| 97 | + HttpRequest request = new HttpRequest(concatUrlWithParams(url, params), HttpMethod.GET); |
| 98 | + return client.send(request).get(); |
98 | 99 | } |
99 | 100 |
|
100 | | - static String mapToParams(String... requestParams) { |
| 101 | + private List<UrlBuilder.Param> convertListToParams(String... requestParams) { |
101 | 102 | if (requestParams.length % 2 != 0) { |
102 | 103 | throw new IllegalArgumentException("params list length cannot be odd"); |
103 | 104 | } |
104 | | - if (requestParams.length == 0) { |
105 | | - return ""; |
106 | | - } |
107 | | - StringBuilder res = new StringBuilder("?"); |
108 | | - res.append(requestParams[0]); |
109 | | - res.append('='); |
110 | | - res.append(requestParams[1]); |
111 | | - for (int i = 2; i < requestParams.length; ) { |
112 | | - res.append('&'); |
113 | | - res.append(encode(requestParams[i++])); |
114 | | - res.append('='); |
115 | | - res.append(encode(requestParams[i++])); |
| 105 | + List<UrlBuilder.Param> queryParams = new ArrayList<>(); |
| 106 | + for (int i = 0; i < requestParams.length; i = i + 2) { |
| 107 | + queryParams.add(new UrlBuilder.Param(requestParams[i], requestParams[i + 1])); |
116 | 108 | } |
117 | | - return res.toString(); |
| 109 | + return queryParams; |
118 | 110 | } |
119 | 111 |
|
120 | | - private String objectToString(Object obj) throws JsonProcessingException { |
121 | | - return obj == null ? "" : JacksonService.toJson(obj); |
| 112 | + private byte[] objectToBytes(Object obj) throws JsonProcessingException { |
| 113 | + return obj == null ? new byte[0] : JacksonService.toBytes(obj); |
122 | 114 | } |
123 | 115 |
|
124 | | - private static String encode(String value) { |
125 | | - return URLEncoder.encode(value, StandardCharsets.UTF_8); |
| 116 | + private String concatUrlWithParams(String url, String... params) { |
| 117 | + UrlBuilder urlBuilder = templateUrlBuilder.duplicate(); |
| 118 | + urlBuilder.setPath(url); |
| 119 | + if (params != null && params.length > 0) { |
| 120 | + urlBuilder.setQueryParams(convertListToParams(params)); |
| 121 | + } |
| 122 | + return urlBuilder.build(); |
126 | 123 | } |
127 | | - |
128 | 124 | } |
0 commit comments