Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly wait for entire result in JettyClientCall #1101

Open
wants to merge 1 commit into
base: 2.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import java.io.OutputStream;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;

import org.eclipse.jetty.client.HttpRequest;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.InputStreamContentProvider;
import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.http.HttpField;
Expand Down Expand Up @@ -70,9 +70,9 @@ public class JettyClientCall extends ClientCall {
private final HttpRequest httpRequest;

/**
* The wrapped HTTP response.
* The wrapped HTTP result.
*/
private volatile org.eclipse.jetty.client.api.Response httpResponse;
private volatile Result result;

/**
* The wrapped input stream response listener.
Expand Down Expand Up @@ -128,7 +128,7 @@ public HttpRequest getHttpRequest() {
* @return The HTTP response.
*/
public org.eclipse.jetty.client.api.Response getHttpResponse() {
return this.httpResponse;
return (this.result != null) ? this.result.getResponse() : null;
}

/**
Expand Down Expand Up @@ -203,22 +203,22 @@ public Representation getResponseEntity(Response response) {
*/
@Override
public Series<Header> getResponseHeaders() {
final Series<Header> result = super.getResponseHeaders();
final Series<Header> responseHeaders = super.getResponseHeaders();

if (!this.responseHeadersAdded) {
final org.eclipse.jetty.client.api.Response httpResponse = getHttpResponse();
if (httpResponse != null) {
final HttpFields headers = httpResponse.getHeaders();
if (headers != null) {
for (HttpField header : headers)
result.add(header.getName(), header.getValue());
responseHeaders.add(header.getName(), header.getValue());
}
}

this.responseHeadersAdded = true;
}

return result;
return responseHeaders;
}

/**
Expand Down Expand Up @@ -253,7 +253,7 @@ public int getStatusCode() {
*/
@Override
public Status sendRequest(Request request) {
Status result = null;
Status status = null;

try {
final Representation entity = request.getEntity();
Expand Down Expand Up @@ -282,41 +282,34 @@ public Status sendRequest(Request request) {
// Ensure that the connection is active
this.inputStreamResponseListener = new InputStreamResponseListener();
this.httpRequest.send(this.inputStreamResponseListener);
this.httpResponse = this.inputStreamResponseListener.get(
this.result = this.inputStreamResponseListener.await(
clientHelper.getIdleTimeout(), TimeUnit.MILLISECONDS);

result = new Status(getStatusCode(), getReasonPhrase());
status = new Status(getStatusCode(), this.result.getFailure(), getReasonPhrase());
} catch (IOException e) {
this.clientHelper.getLogger().log(Level.WARNING,
"An error occurred while reading the request entity.", e);
result = new Status(Status.CONNECTOR_ERROR_INTERNAL, e);
status = new Status(Status.CONNECTOR_ERROR_INTERNAL, e);

// Release the connection
getHttpRequest().abort(e);
} catch (TimeoutException e) {
this.clientHelper.getLogger().log(Level.WARNING,
"The HTTP request timed out.", e);
result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, e);
status = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, e);

// Release the connection
getHttpRequest().abort(e);
} catch (InterruptedException e) {
this.clientHelper.getLogger().log(Level.WARNING,
"The HTTP request thread was interrupted.", e);
result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, e);

// Release the connection
getHttpRequest().abort(e);
} catch (ExecutionException e) {
this.clientHelper.getLogger().log(Level.WARNING,
"An error occurred while processing the HTTP request.", e);
result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, e);
status = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, e);

// Release the connection
getHttpRequest().abort(e);
}

return result;
return status;
}

@Override
Expand Down