forked from browserup/browserup-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace OkHttp client with native Java HTTP client (#443)
Co-authored-by: Valery Yatsynovich <[email protected]>
- Loading branch information
Showing
4 changed files
with
58 additions
and
44 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
94 changes: 57 additions & 37 deletions
94
...-proxy-core/src/main/java/com/browserup/bup/mitmproxy/management/AddonsManagerClient.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,81 +1,101 @@ | ||
package com.browserup.bup.mitmproxy.management; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import okhttp3.*; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
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.List; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
public class AddonsManagerClient { | ||
private final HttpClient httpClient = HttpClient.newHttpClient(); | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private final int port; | ||
private final String host = "localhost"; | ||
|
||
public AddonsManagerClient(int port) { | ||
this.port = port; | ||
} | ||
|
||
public <T> T putRequestToAddonsManager(String addOnPath, | ||
String operation, | ||
List<Pair<String, String>> queryParams, | ||
RequestBody requestBody, | ||
String requestBodyAsJson, | ||
Class<T> responseClass) { | ||
return requestToAddonsManager(addOnPath, operation, queryParams, "PUT", requestBody, responseClass); | ||
return requestToAddonsManager(addOnPath, operation, List.of(), | ||
requestBuilder -> requestBuilder | ||
.method("PUT", HttpRequest.BodyPublishers.ofString(requestBodyAsJson)) | ||
.header("Content-Type", "application/json; charset=utf-8"), | ||
responseClass); | ||
} | ||
|
||
public <T> T getRequestToAddonsManager(String addOnPath, | ||
String operation, | ||
List<Pair<String, String>> queryParams, | ||
Class<T> responseClass) { | ||
return requestToAddonsManager(addOnPath, operation, queryParams, "GET", null, responseClass); | ||
return requestToAddonsManager(addOnPath, operation, queryParams, | ||
requestBuilder -> requestBuilder | ||
.method("GET", HttpRequest.BodyPublishers.noBody()), | ||
responseClass); | ||
} | ||
|
||
public <T> T requestToAddonsManager(String addOnPath, | ||
private <T> T requestToAddonsManager(String addOnPath, | ||
String operation, | ||
List<Pair<String, String>> queryParams, | ||
String method, | ||
RequestBody requestBody, | ||
Consumer<HttpRequest.Builder> requestConfigurer, | ||
Class<T> responseClass) { | ||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder() | ||
.url(buildRequestUrl(addOnPath, operation, queryParams)) | ||
.method(method, requestBody) | ||
.build(); | ||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() | ||
.uri(buildRequestUrl(addOnPath, operation, queryParams)); | ||
|
||
requestConfigurer.accept(requestBuilder); | ||
|
||
Response response; | ||
HttpRequest request = requestBuilder.build(); | ||
|
||
HttpResponse<byte[]> response; | ||
try { | ||
response = client.newCall(request).execute(); | ||
} catch (IOException ex) { | ||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofByteArray()); | ||
} catch (IOException | InterruptedException ex) { | ||
throw new RuntimeException("Failed to request manager API", ex); | ||
} | ||
|
||
try { | ||
if (responseClass.equals(Void.class)) return null; | ||
if (responseClass.equals(Void.class)) { | ||
return null; | ||
} | ||
|
||
if (responseClass.equals(String.class)) return (T) response.body().string(); | ||
if (responseClass.equals(String.class)) { | ||
//noinspection unchecked | ||
return (T) new String(response.body(), StandardCharsets.UTF_8); | ||
} | ||
|
||
return new ObjectMapper().readerFor(responseClass).readValue(Objects.requireNonNull(response.body()).byteStream()); | ||
try { | ||
return objectMapper.readerFor(responseClass).readValue(response.body()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to parse response from manager API", e); | ||
} finally { | ||
ResponseBody body = response.body(); | ||
if (body != null) { | ||
body.close(); | ||
} | ||
} | ||
} | ||
|
||
private HttpUrl buildRequestUrl(String addOnPath, String operation, List<Pair<String, String>> queryParams) { | ||
HttpUrl.Builder builder = new HttpUrl.Builder() | ||
.host(host) | ||
.port(port) | ||
.scheme("http") | ||
.addPathSegment(addOnPath) | ||
.addPathSegment(operation); | ||
|
||
queryParams.forEach(p -> builder.addQueryParameter(p.getKey(), p.getValue())); | ||
private URI buildRequestUrl(String addOnPath, String operation, List<Pair<String, String>> queryParams) { | ||
String uri = String.format("http://localhost:%d/%s/%s", port, addOnPath, operation); | ||
if (!queryParams.isEmpty()) { | ||
String query = queryParams.stream() | ||
.map(pair -> buildUriQueryPart(pair.getKey(), pair.getValue())) | ||
.collect(Collectors.joining("&")); | ||
uri = uri + "?" + query; | ||
} | ||
return URI.create(uri); | ||
} | ||
|
||
return builder.build(); | ||
private static String buildUriQueryPart(String name, String value) { | ||
StringBuilder queryPart = new StringBuilder(URLEncoder.encode(name, StandardCharsets.UTF_8)); | ||
if (value != null) { | ||
queryPart.append("=").append(URLEncoder.encode(value, StandardCharsets.UTF_8)); | ||
} | ||
return queryPart.toString(); | ||
} | ||
} |
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