-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
203 additions
and
1 deletion.
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
143 changes: 143 additions & 0 deletions
143
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelAsyncHttpClientWrapper.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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.httpclient.JavaHttpClientTelemetry; | ||
import org.apache.http.HttpException; | ||
import org.apache.http.concurrent.FutureCallback; | ||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | ||
import org.apache.http.nio.protocol.HttpAsyncRequestProducer; | ||
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
|
||
public class OtelAsyncHttpClientWrapper extends CloseableHttpAsyncClient { | ||
private final HttpClient telemetryHttpClient; | ||
|
||
public OtelAsyncHttpClientWrapper() { | ||
this.telemetryHttpClient = | ||
JavaHttpClientTelemetry.builder(GlobalOpenTelemetry.get()) | ||
.build() | ||
.newHttpClient(HttpClient.newBuilder().build()); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> execute( | ||
HttpAsyncRequestProducer requestProducer, | ||
HttpAsyncResponseConsumer<T> responseConsumer, | ||
HttpContext context, | ||
FutureCallback<T> callback) { | ||
|
||
try { | ||
HttpRequest request = convertToHttpRequest(requestProducer); | ||
|
||
CompletableFuture<HttpResponse<String>> futureResponse = | ||
telemetryHttpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
// Adapt future to CXF's response handling expectations | ||
return futureResponse.handle( | ||
(response, throwable) -> { | ||
if (throwable != null) { | ||
if (callback != null) { | ||
callback.failed((Exception) throwable); | ||
} | ||
throw new RuntimeException(throwable); | ||
} | ||
try { | ||
// Pass the response to the consumer and return the result | ||
T result = | ||
adaptAndConsumeResponse(response, responseConsumer, callback); | ||
if (callback != null) { | ||
callback.completed(result); | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
if (callback != null) { | ||
callback.failed(e); | ||
} | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
// Handle synchronous conversion errors | ||
if (callback != null) { | ||
callback.failed(e); | ||
} | ||
CompletableFuture<T> failedFuture = new CompletableFuture<>(); | ||
failedFuture.completeExceptionally(e); | ||
return failedFuture; | ||
} | ||
} | ||
|
||
private HttpRequest convertToHttpRequest(HttpAsyncRequestProducer requestProducer) | ||
throws HttpException, IOException { | ||
return HttpRequest.newBuilder() | ||
.uri(URI.create(requestProducer.generateRequest().getRequestLine().getUri())) | ||
.build(); | ||
} | ||
|
||
private <T> T adaptAndConsumeResponse( | ||
HttpResponse<String> response, | ||
HttpAsyncResponseConsumer<T> responseConsumer, | ||
FutureCallback<T> callback) | ||
throws Exception { | ||
|
||
var apacheResponse = | ||
new org.apache.http.message.BasicHttpResponse( | ||
new org.apache.http.ProtocolVersion("HTTP", 1, 1), | ||
response.statusCode(), | ||
response.body()); | ||
|
||
response.headers() | ||
.map() | ||
.forEach( | ||
(name, values) -> | ||
values.forEach(value -> apacheResponse.addHeader(name, value))); | ||
|
||
try { | ||
responseConsumer.responseReceived(apacheResponse); | ||
T result = responseConsumer.getResult(); | ||
|
||
if (callback != null) { | ||
callback.completed(result); | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
if (callback != null) { | ||
callback.failed(e); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// No action needed for java.net.http.HttpClient | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
// java.net.http.HttpClient does not have a "running" state | ||
return true; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
// No explicit start action needed for java.net.http.HttpClient | ||
} | ||
|
||
@Override | ||
public <T> Future<T> execute( | ||
HttpAsyncRequestProducer requestProducer, | ||
HttpAsyncResponseConsumer<T> responseConsumer, | ||
FutureCallback<T> callback) { | ||
// Use the main execute method with null context | ||
return execute(requestProducer, responseConsumer, null, callback); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelAsyncHttpConduit.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import org.apache.cxf.Bus; | ||
import org.apache.cxf.service.model.EndpointInfo; | ||
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit; | ||
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory; | ||
import org.apache.cxf.ws.addressing.EndpointReferenceType; | ||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | ||
|
||
import java.io.IOException; | ||
|
||
public class OtelAsyncHttpConduit extends AsyncHTTPConduit { | ||
|
||
public OtelAsyncHttpConduit( | ||
Bus bus, | ||
EndpointInfo endpointInfo, | ||
EndpointReferenceType endpointReferenceType, | ||
AsyncHTTPConduitFactory factory) | ||
throws IOException { | ||
super(bus, endpointInfo, endpointReferenceType, factory); | ||
} | ||
|
||
@Override | ||
public CloseableHttpAsyncClient getHttpAsyncClient() { | ||
return new OtelAsyncHttpClientWrapper(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelAsyncHttpConduitFactory.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import org.apache.cxf.Bus; | ||
import org.apache.cxf.service.model.EndpointInfo; | ||
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit; | ||
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory; | ||
import org.apache.cxf.ws.addressing.EndpointReferenceType; | ||
|
||
import java.io.IOException; | ||
|
||
public class OtelAsyncHttpConduitFactory extends AsyncHTTPConduitFactory { | ||
public OtelAsyncHttpConduitFactory(Bus b) { | ||
super(b); | ||
} | ||
|
||
@Override | ||
public AsyncHTTPConduit createConduit( | ||
Bus bus, EndpointInfo endpointInfo, EndpointReferenceType endpointReferenceType) | ||
throws IOException { | ||
|
||
return new OtelAsyncHttpConduit(bus, endpointInfo, endpointReferenceType, this); | ||
} | ||
} |
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