Skip to content

Commit 56fb7d9

Browse files
committed
Change com.squareup.okhttp3:okhttp:5.1.0 to Apache HttpClient5
1 parent 3557787 commit 56fb7d9

File tree

3 files changed

+278
-247
lines changed

3 files changed

+278
-247
lines changed

modules/couchbase/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ description = "Testcontainers :: Couchbase"
22

33
dependencies {
44
api project(':testcontainers')
5-
// TODO use JDK's HTTP client and/or Apache HttpClient5
6-
shaded 'com.squareup.okhttp3:okhttp:5.1.0'
75

86
testImplementation 'com.couchbase.client:java-client:3.8.3'
97
testImplementation 'org.awaitility:awaitility:4.3.0'
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.testcontainers.couchbase;
2+
3+
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.EntityDetails;
4+
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpException;
5+
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpRequest;
6+
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpRequestInterceptor;
7+
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.protocol.HttpContext;
8+
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Base64;
12+
13+
/**
14+
* HTTP request interceptor that adds Basic Authentication headers to HTTP requests.
15+
* <p>
16+
* This interceptor checks if the "Authorization" header is already present in the request.
17+
* If not, it adds a Basic Authentication header using the provided username and password.
18+
* The credentials are Base64 encoded in the format "username:password".
19+
* </p>
20+
*
21+
* <p><b>Example usage:</b></p>
22+
* <pre>
23+
* {@code
24+
* AuthInterceptor interceptor = new AuthInterceptor("admin", "password");
25+
* // Register with HTTP client to automatically add auth headers
26+
* }
27+
* </pre>
28+
*
29+
* @see HttpRequestInterceptor
30+
*/
31+
class AuthInterceptor implements HttpRequestInterceptor {
32+
33+
private final String usr;
34+
35+
private final String pass;
36+
37+
/**
38+
* Constructs a new AuthInterceptor with the specified credentials.
39+
*
40+
* @param usr the username for Basic Authentication
41+
* @param pass the password for Basic Authentication
42+
*/
43+
public AuthInterceptor(final String usr, final String pass) {
44+
this.usr = usr;
45+
this.pass = pass;
46+
}
47+
48+
/**
49+
* Processes the HTTP request by adding a Basic Authentication header if not already present.
50+
* <p>
51+
* The method encodes the username and password using Base64 encoding and sets the
52+
* "Authorization" header with the value "Basic {base64-encoded-credentials}".
53+
* </p>
54+
*
55+
* @param httpRequest the HTTP request to process
56+
* @param entityDetails the entity details (if any)
57+
* @param httpContext the HTTP context for the request
58+
* @throws HttpException if an HTTP protocol error occurs
59+
* @throws IOException if an I/O error occurs
60+
*/
61+
@Override
62+
public void process(HttpRequest httpRequest, EntityDetails entityDetails, HttpContext httpContext)
63+
throws HttpException, IOException {
64+
if (!httpRequest.containsHeader("Authorization")) {
65+
String authValue = "Basic " + Base64.getEncoder()
66+
.encodeToString((usr + ":" + pass).getBytes(StandardCharsets.UTF_8));
67+
httpRequest.setHeader("Authorization", authValue);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)