Allow async customization of long polling request
@@ -32,6 +32,7 @@ import java.util.regex.Pattern;
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
+import org.cometd.bayeux.Promise;
import org.cometd.common.TransportException;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
@@ -138,16 +139,15 @@ public class LongPollingTransport extends HttpClientTransport {
request.content(new StringContentProvider(generateJSON(messages)));
- customize(request);
-
+ customize(request, Promise.from(customizedRequest -> {
synchronized (this) {
if (_aborted) {
throw new IllegalStateException("Aborted");
}
- _requests.add(request);
+ _requests.add(customizedRequest);
}
- request.listener(new Request.Listener.Adapter() {
+ customizedRequest.listener(new Request.Listener.Adapter() {
@Override
public void onHeaders(Request request) {
listener.onSending(messages);
@@ -174,9 +174,9 @@ public class LongPollingTransport extends HttpClientTransport {
}
// Set the idle timeout for this request larger than the total timeout
// so there are no races between the two timeouts
- request.idleTimeout(maxNetworkDelay * 2, TimeUnit.MILLISECONDS);
- request.timeout(maxNetworkDelay, TimeUnit.MILLISECONDS);
- request.send(new BufferingResponseListener(_maxMessageSize) {
+ customizedRequest.idleTimeout(maxNetworkDelay * 2, TimeUnit.MILLISECONDS);
+ customizedRequest.timeout(maxNetworkDelay, TimeUnit.MILLISECONDS);
+ customizedRequest.send(new BufferingResponseListener(_maxMessageSize) {
@Override
public boolean onHeader(Response response, HttpField field) {
HttpHeader header = field.getHeader();
@@ -250,11 +250,21 @@ public class LongPollingTransport extends HttpClientTransport {
}
}
});
+ }, error -> {
+ listener.onFailure(error, messages);
+ }));
+
+
}
protected void customize(Request request) {
}
+ protected void customize(Request request, Promise<Request> promise) {
+ customize(request);
+ promise.succeed(request);
+ }
+
public static class Factory extends ContainerLifeCycle implements ClientTransport.Factory {
private final HttpClient httpClient;
