Skip to content

Commit

Permalink
jetty-httpclient-12: send method must pass along implemented response…
Browse files Browse the repository at this point in the history
… listeners

The super HttpRequest.send call says:

> The listener passed to this method may implement not only Response.CompleteListener
> but also other response listener interfaces, and all the events implemented will be notified.

The current implementation passes through a lambda that implements CompleteListener only, and
does not delegate-in-scope to any other callback methods you might provide
  • Loading branch information
stevenschlansker committed Sep 25, 2024
1 parent e4752ab commit f796e4c
Showing 1 changed file with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.client.Response;
import org.eclipse.jetty.client.Result;
import org.eclipse.jetty.client.transport.HttpConversation;
import org.eclipse.jetty.client.transport.HttpRequest;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.io.Content.Chunk;
import org.eclipse.jetty.io.Content.Source;

class TracingHttpRequest extends HttpRequest {

Expand All @@ -37,9 +41,84 @@ public void send(Response.CompleteListener listener) {
// start span and attach listeners.
JettyClientTracingListener.handleRequest(parentContext, this, instrumenter);
super.send(
result -> {
try (Scope scope = openScope()) {
listener.onComplete(result);
new Response.Listener() {
@Override
public void onBegin(Response response) {
if (listener instanceof Response.BeginListener l) {
try (Scope scope = openScope()) {
l.onBegin(response);
}
}
}

@Override
public void onComplete(Result result) {
try (Scope scope = openScope()) {
listener.onComplete(result);
}
}

@Override
public void onContent(Response response, ByteBuffer content) {
if (listener instanceof Response.ContentListener l) {
try (Scope scope = openScope()) {
l.onContent(response, content);
}
}
}

@Override
public void onContent(Response response, Chunk chunk, Runnable demander) {
if (listener instanceof Response.AsyncContentListener l) {
try (Scope scope = openScope()) {
l.onContent(response, chunk, demander);
}
}
}

@Override
public void onContentSource(Response response, Source contentSource) {
if (listener instanceof Response.ContentSourceListener l) {
try (Scope scope = openScope()) {
l.onContentSource(response, contentSource);
}
}
}

@Override
public void onFailure(Response response, Throwable failure) {
if (listener instanceof Response.FailureListener l) {
try (Scope scope = openScope()) {
l.onFailure(response, failure);
}
}
}

@Override
public void onHeaders(Response response) {
if (listener instanceof Response.HeadersListener l) {
try (Scope scope = openScope()) {
l.onHeaders(response);
}
}
}

@Override
public boolean onHeader(Response response, HttpField field) {
if (listener instanceof Response.HeaderListener l) {
try (Scope scope = openScope()) {
return l.onHeader(response, field);
}
}
}

@Override
public void onSuccess(Response response) {
if (listener instanceof Response.SuccessListener l) {
try (Scope scope = openScope()) {
l.onSuccess(response);
}
}
}
});
}
Expand Down

0 comments on commit f796e4c

Please # to comment.