Skip to content

Commit 128cfde

Browse files
artur-ciocanuArtur Ciocanu
and
Artur Ciocanu
authored
Removing OkHttp3 dependencies (#1313)
Signed-off-by: Artur Ciocanu <[email protected]> Co-authored-by: Artur Ciocanu <[email protected]>
1 parent ef1fc22 commit 128cfde

File tree

11 files changed

+75
-119
lines changed

11 files changed

+75
-119
lines changed

dapr-spring/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@
111111
<version>${springboot.version}</version>
112112
<scope>test</scope>
113113
</dependency>
114-
<dependency>
115-
<groupId>com.github.gmazzo.okhttp.mock</groupId>
116-
<artifactId>mock-client</artifactId>
117-
<version>2.0.0</version>
118-
<scope>test</scope>
119-
</dependency>
120114
</dependencies>
121115

122116
<build>

daprdocs/content/en/java-sdk-docs/_index.md

-12
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,6 @@ dependencies {
8888

8989
{{< /tabs >}}
9090

91-
If you are also using Spring Boot, you may run into a common issue where the `OkHttp` version that the Dapr SDK uses conflicts with the one specified in the Spring Boot _Bill of Materials_.
92-
93-
You can fix this by specifying a compatible `OkHttp` version in your project to match the version that the Dapr SDK uses:
94-
95-
```xml
96-
<dependency>
97-
<groupId>com.squareup.okhttp3</groupId>
98-
<artifactId>okhttp</artifactId>
99-
<version>1.14.0</version>
100-
</dependency>
101-
```
102-
10391
## Try it out
10492

10593
Put the Dapr Java SDK to the test. Walk through the Java quickstarts and tutorials to see Dapr in action:

examples/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@
133133
<artifactId>protobuf-java</artifactId>
134134
<version>${protobuf.version}</version>
135135
</dependency>
136-
<dependency>
137-
<groupId>com.squareup.okhttp3</groupId>
138-
<artifactId>okhttp</artifactId>
139-
<version>4.12.0</version>
140-
</dependency>
141136
</dependencies>
142137

143138
<build>

examples/src/main/java/io/dapr/examples/tracing/Validation.java

+17-22
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import com.evanlennick.retry4j.config.RetryConfigBuilder;
2020
import com.jayway.jsonpath.DocumentContext;
2121
import com.jayway.jsonpath.JsonPath;
22-
import okhttp3.HttpUrl;
23-
import okhttp3.OkHttpClient;
24-
import okhttp3.Request;
25-
import okhttp3.Response;
2622

23+
import java.net.URI;
24+
import java.net.http.HttpClient;
25+
import java.net.http.HttpRequest;
26+
import java.net.http.HttpResponse;
2727
import java.util.List;
2828
import java.util.Map;
2929

@@ -34,7 +34,11 @@
3434
*/
3535
final class Validation {
3636

37-
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
37+
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
38+
.version(HttpClient.Version.HTTP_1_1)
39+
.build();
40+
41+
private static final String TRACES_URL = "http://localhost:9411/api/v2/traces";
3842

3943
private static final RetryConfig RETRY_CONFIG = new RetryConfigBuilder()
4044
.withMaxNumberOfTries(5)
@@ -56,27 +60,16 @@ final class Validation {
5660

5761
static void validate() {
5862
Status<Void> result = new CallExecutorBuilder().config(RETRY_CONFIG).build().execute(() -> doValidate());
63+
5964
if (!result.wasSuccessful()) {
6065
throw new RuntimeException(result.getLastExceptionThatCausedRetry());
6166
}
6267
}
6368

6469
private static Void doValidate() throws Exception {
65-
System.out.println("Performing validation of tracing events ...");
66-
67-
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
68-
urlBuilder.scheme("http")
69-
.host("localhost")
70-
.port(9411);
71-
urlBuilder.addPathSegments("api/v2/traces");
72-
Request.Builder requestBuilder = new Request.Builder()
73-
.url(urlBuilder.build());
74-
requestBuilder.method("GET", null);
75-
76-
Request request = requestBuilder.build();
77-
78-
Response response = HTTP_CLIENT.newCall(request).execute();
79-
DocumentContext documentContext = JsonPath.parse(response.body().string());
70+
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(TRACES_URL)).build();
71+
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
72+
DocumentContext documentContext = JsonPath.parse(response.body());
8073
String mainSpanId = readOne(documentContext, "$..[?(@.name == \"example's main\")]['id']").toString();
8174

8275
// Validate echo
@@ -104,13 +97,14 @@ private static Void doValidate() throws Exception {
10497
.toString();
10598
readOne(documentContext,
10699
String.format(JSONPATH_SLEEP_SPAN_ID, proxySleepSpanId2));
107-
System.out.println("Validation of tracing events has succeeded.");
100+
108101
return null;
109102
}
110103

111104
private static Object readOne(DocumentContext documentContext, String path) {
112105
List<Map<String, Object>> arr = documentContext.read(path);
113-
if (arr.size() == 0) {
106+
107+
if (arr.isEmpty()) {
114108
throw new RuntimeException("No record found for " + path);
115109
}
116110

@@ -119,6 +113,7 @@ private static Object readOne(DocumentContext documentContext, String path) {
119113

120114
private static void assertCount(DocumentContext documentContext, String path, int expectedCount) {
121115
List<Map<String, Object>> arr = documentContext.read(path);
116+
122117
if (arr.size() != expectedCount) {
123118
throw new RuntimeException(
124119
String.format("Unexpected count %d vs expected %d for %s", arr.size(), expectedCount, path));

sdk-actors/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
<artifactId>mockito-core</artifactId>
3232
<scope>test</scope>
3333
</dependency>
34-
<dependency>
35-
<groupId>com.github.gmazzo.okhttp.mock</groupId>
36-
<artifactId>mock-client</artifactId>
37-
<version>2.0.0</version>
38-
<scope>test</scope>
39-
</dependency>
4034
<dependency>
4135
<groupId>org.junit.jupiter</groupId>
4236
<artifactId>junit-jupiter</artifactId>

sdk-springboot/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@
7171
<artifactId>spring-boot-starter-test</artifactId>
7272
<scope>test</scope>
7373
</dependency>
74-
<dependency>
75-
<groupId>com.github.gmazzo.okhttp.mock</groupId>
76-
<artifactId>mock-client</artifactId>
77-
<version>2.0.0</version>
78-
<scope>test</scope>
79-
</dependency>
8074
</dependencies>
8175

8276
<build>

sdk-tests/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
</dependency>
143143
<dependency>
144144
<groupId>io.zipkin.reporter2</groupId>
145-
<artifactId>zipkin-sender-okhttp3</artifactId>
145+
<artifactId>zipkin-sender-urlconnection</artifactId>
146146
<version>3.4.0</version>
147147
</dependency>
148148
<dependency>

sdk-tests/src/test/java/io/dapr/it/DaprRun.java

+18-19
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
import io.dapr.v1.AppCallbackHealthCheckGrpc;
2525
import io.grpc.ManagedChannel;
2626
import io.grpc.ManagedChannelBuilder;
27-
import okhttp3.OkHttpClient;
28-
import okhttp3.Request;
29-
import okhttp3.Response;
3027
import org.apache.commons.lang3.tuple.ImmutablePair;
3128

3229
import java.io.IOException;
30+
import java.net.URI;
31+
import java.net.http.HttpClient;
32+
import java.net.http.HttpRequest;
33+
import java.net.http.HttpResponse;
34+
import java.time.Duration;
3335
import java.util.Collections;
3436
import java.util.HashMap;
3537
import java.util.Map;
3638
import java.util.UUID;
37-
import java.util.concurrent.TimeUnit;
3839
import java.util.concurrent.atomic.AtomicBoolean;
3940
import java.util.function.Supplier;
4041

@@ -231,23 +232,21 @@ public void waitForAppHealth(int maxWaitMilliseconds) throws InterruptedExceptio
231232
channel.shutdown();
232233
}
233234
} else {
234-
// Create an OkHttpClient instance with a custom timeout
235-
OkHttpClient client = new OkHttpClient.Builder()
236-
.connectTimeout(maxWaitMilliseconds, TimeUnit.MILLISECONDS)
237-
.readTimeout(maxWaitMilliseconds, TimeUnit.MILLISECONDS)
238-
.build();
239-
240-
// Define the URL to probe
241-
String url = "http://127.0.0.1:" + this.getAppPort() + "/health"; // Change to your specific URL
235+
Duration waitDuration = Duration.ofMillis(maxWaitMilliseconds);
236+
HttpClient client = HttpClient.newBuilder()
237+
.version(HttpClient.Version.HTTP_1_1)
238+
.connectTimeout(waitDuration)
239+
.build();
240+
String url = "http://127.0.0.1:" + this.getAppPort() + "/health";
241+
HttpRequest request = HttpRequest.newBuilder()
242+
.GET()
243+
.uri(URI.create(url))
244+
.build();
242245

243-
// Create a request to the URL
244-
Request request = new Request.Builder()
245-
.url(url)
246-
.build();
246+
try {
247+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
247248

248-
// Execute the request
249-
try (Response response = client.newCall(request).execute()) {
250-
if (!response.isSuccessful()) {
249+
if (response.statusCode() != 200) {
251250
throw new RuntimeException("error: HTTP service is not healthy.");
252251
}
253252
} catch (IOException e) {

sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerIT.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121

2222
import io.dapr.config.Properties;
2323
import io.dapr.testcontainers.DaprContainer;
24-
import okhttp3.OkHttpClient;
25-
import okhttp3.Request;
26-
import okhttp3.Response;
2724
import org.junit.jupiter.api.BeforeEach;
2825
import org.junit.jupiter.api.Tag;
2926
import org.junit.jupiter.api.Test;
@@ -34,6 +31,10 @@
3431
import org.testcontainers.shaded.org.awaitility.core.ConditionTimeoutException;
3532

3633
import java.io.IOException;
34+
import java.net.URI;
35+
import java.net.http.HttpClient;
36+
import java.net.http.HttpRequest;
37+
import java.net.http.HttpResponse;
3738
import java.util.Map;
3839
import java.util.concurrent.TimeUnit;
3940

@@ -51,7 +52,6 @@
5152
import static io.dapr.it.testcontainers.ContainerConstants.DAPR_IMAGE_TAG;
5253
import static org.junit.jupiter.api.Assertions.assertEquals;
5354
import static org.junit.jupiter.api.Assertions.assertNotNull;
54-
import static org.junit.jupiter.api.Assertions.assertTrue;
5555
import static org.junit.jupiter.api.Assertions.fail;
5656

5757

@@ -155,20 +155,21 @@ public void testPlacement() throws Exception {
155155

156156
}
157157

158-
private String checkSidecarMetadata() throws IOException {
159-
OkHttpClient okHttpClient = new OkHttpClient.Builder()
160-
.build();
161-
Request request = new Request.Builder()
162-
.url(DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata")
158+
private String checkSidecarMetadata() throws IOException, InterruptedException {
159+
String url = DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata";
160+
HttpClient client = HttpClient.newBuilder()
161+
.version(HttpClient.Version.HTTP_1_1)
162+
.build();
163+
HttpRequest request = HttpRequest.newBuilder()
164+
.uri(URI.create(url))
163165
.build();
166+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
164167

165-
try (Response response = okHttpClient.newCall(request).execute()) {
166-
if (response.isSuccessful() && response.body() != null) {
167-
return response.body().string();
168-
} else {
169-
throw new IOException("Unexpected response: " + response.code());
170-
}
168+
if (response.statusCode() != 200) {
169+
throw new IOException("Unexpected response: " + response.statusCode());
171170
}
171+
172+
return response.body();
172173
}
173174

174175
@Test

sdk-tests/src/test/java/io/dapr/it/tracing/Validation.java

+23-21
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import com.jayway.jsonpath.DocumentContext;
1717
import com.jayway.jsonpath.JsonPath;
1818
import net.minidev.json.JSONArray;
19-
import okhttp3.HttpUrl;
20-
import okhttp3.OkHttpClient;
21-
import okhttp3.Request;
22-
import okhttp3.Response;
2319

20+
import java.net.URI;
21+
import java.net.http.HttpClient;
22+
import java.net.http.HttpRequest;
23+
import java.net.http.HttpResponse;
24+
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2426
import static org.junit.jupiter.api.Assertions.assertNotNull;
25-
import static org.junit.jupiter.api.Assertions.assertTrue;
2627

2728
/**
2829
* Class used to verify that traces are present as expected.
@@ -31,7 +32,11 @@
3132
*/
3233
public final class Validation {
3334

34-
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
35+
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
36+
.version(HttpClient.Version.HTTP_1_1)
37+
.build();
38+
39+
private static final String TRACES_URL = "http://localhost:9411/api/v2/traces?limit=100";
3540

3641
/**
3742
* JSON Path for main span Id.
@@ -47,31 +52,28 @@ public final class Validation {
4752
public static void validate(String spanName, String sleepSpanName) throws Exception {
4853
// Must wait for some time to make sure Zipkin receives all spans.
4954
Thread.sleep(10000);
50-
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
51-
urlBuilder.scheme("http")
52-
.host("localhost")
53-
.port(9411)
54-
.addPathSegments("api/v2/traces")
55-
.addQueryParameter("limit", "100");
56-
Request.Builder requestBuilder = new Request.Builder()
57-
.url(urlBuilder.build());
58-
requestBuilder.method("GET", null);
59-
60-
Request request = requestBuilder.build();
61-
62-
Response response = HTTP_CLIENT.newCall(request).execute();
63-
DocumentContext documentContext = JsonPath.parse(response.body().string());
55+
56+
HttpRequest request = HttpRequest.newBuilder()
57+
.GET()
58+
.uri(URI.create(TRACES_URL))
59+
.build();
60+
61+
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
62+
DocumentContext documentContext = JsonPath.parse(response.body());
6463
String mainSpanId = readOne(documentContext, String.format(JSONPATH_MAIN_SPAN_ID, spanName)).toString();
64+
6565
assertNotNull(mainSpanId);
6666

6767
String sleepSpanId = readOne(documentContext, String.format(JSONPATH_SLEEP_SPAN_ID, mainSpanId, sleepSpanName))
6868
.toString();
69+
6970
assertNotNull(sleepSpanId);
7071
}
7172

7273
private static Object readOne(DocumentContext documentContext, String path) {
7374
JSONArray arr = documentContext.read(path);
74-
assertTrue(arr.size() > 0);
75+
76+
assertFalse(arr.isEmpty());
7577

7678
return arr.get(0);
7779
}

sdk/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@
6161
<version>1.9.0</version>
6262
<scope>test</scope>
6363
</dependency>
64-
<dependency>
65-
<groupId>com.github.gmazzo.okhttp.mock</groupId>
66-
<artifactId>mock-client</artifactId>
67-
<version>2.0.0</version>
68-
<scope>test</scope>
69-
</dependency>
7064
<dependency>
7165
<groupId>com.github.stefanbirkner</groupId>
7266
<artifactId>system-rules</artifactId>

0 commit comments

Comments
 (0)