Merged via cherry-pick branch 'master' into 'jetty-9.1'.

This commit is contained in:
Simone Bordet 2013-10-25 14:08:55 +02:00
parent 2e434ef1e8
commit b9fcd6695c
2 changed files with 262 additions and 26 deletions

View File

@ -23,6 +23,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Path;
import java.util.ArrayList;
@ -296,93 +297,184 @@ public class HttpRequest implements Request
}
@Override
public Request onRequestQueued(QueuedListener listener)
public Request onRequestQueued(final QueuedListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new QueuedListener()
{
@Override
public void onQueued(Request request)
{
listener.onQueued(request);
}
});
return this;
}
@Override
public Request onRequestBegin(BeginListener listener)
public Request onRequestBegin(final BeginListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new BeginListener()
{
@Override
public void onBegin(Request request)
{
listener.onBegin(request);
}
});
return this;
}
@Override
public Request onRequestHeaders(HeadersListener listener)
public Request onRequestHeaders(final HeadersListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new HeadersListener()
{
@Override
public void onHeaders(Request request)
{
listener.onHeaders(request);
}
});
return this;
}
@Override
public Request onRequestCommit(CommitListener listener)
public Request onRequestCommit(final CommitListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new CommitListener()
{
@Override
public void onCommit(Request request)
{
listener.onCommit(request);
}
});
return this;
}
@Override
public Request onRequestContent(ContentListener listener)
public Request onRequestContent(final ContentListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new ContentListener()
{
@Override
public void onContent(Request request, ByteBuffer content)
{
listener.onContent(request, content);
}
});
return this;
}
@Override
public Request onRequestSuccess(SuccessListener listener)
public Request onRequestSuccess(final SuccessListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new SuccessListener()
{
@Override
public void onSuccess(Request request)
{
listener.onSuccess(request);
}
});
return this;
}
@Override
public Request onRequestFailure(FailureListener listener)
public Request onRequestFailure(final FailureListener listener)
{
this.requestListeners.add(listener);
this.requestListeners.add(new FailureListener()
{
@Override
public void onFailure(Request request, Throwable failure)
{
listener.onFailure(request, failure);
}
});
return this;
}
@Override
public Request onResponseBegin(Response.BeginListener listener)
public Request onResponseBegin(final Response.BeginListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.BeginListener()
{
@Override
public void onBegin(Response response)
{
listener.onBegin(response);
}
});
return this;
}
@Override
public Request onResponseHeader(Response.HeaderListener listener)
public Request onResponseHeader(final Response.HeaderListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.HeaderListener()
{
@Override
public boolean onHeader(Response response, HttpField field)
{
return listener.onHeader(response, field);
}
});
return this;
}
@Override
public Request onResponseHeaders(Response.HeadersListener listener)
public Request onResponseHeaders(final Response.HeadersListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.HeadersListener()
{
@Override
public void onHeaders(Response response)
{
listener.onHeaders(response);
}
});
return this;
}
@Override
public Request onResponseContent(Response.ContentListener listener)
public Request onResponseContent(final Response.ContentListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.ContentListener()
{
@Override
public void onContent(Response response, ByteBuffer content)
{
listener.onContent(response, content);
}
});
return this;
}
@Override
public Request onResponseSuccess(Response.SuccessListener listener)
public Request onResponseSuccess(final Response.SuccessListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.SuccessListener()
{
@Override
public void onSuccess(Response response)
{
listener.onSuccess(response);
}
});
return this;
}
@Override
public Request onResponseFailure(Response.FailureListener listener)
public Request onResponseFailure(final Response.FailureListener listener)
{
this.responseListeners.add(listener);
this.responseListeners.add(new Response.FailureListener()
{
@Override
public void onFailure(Response response, Throwable failure)
{
listener.onFailure(response, failure);
}
});
return this;
}

View File

@ -863,4 +863,148 @@ public class HttpClientTest extends AbstractHttpClientServerTest
Assert.assertEquals(200, response.getStatus());
}
@Test
public void testRequestListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception
{
start(new EmptyServerHandler());
final AtomicInteger counter = new AtomicInteger();
Request.Listener listener = new Request.Listener()
{
@Override
public void onQueued(Request request)
{
counter.incrementAndGet();
}
@Override
public void onBegin(Request request)
{
counter.incrementAndGet();
}
@Override
public void onHeaders(Request request)
{
counter.incrementAndGet();
}
@Override
public void onCommit(Request request)
{
counter.incrementAndGet();
}
@Override
public void onContent(Request request, ByteBuffer content)
{
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onFailure(Request request, Throwable failure)
{
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onSuccess(Request request)
{
counter.incrementAndGet();
}
};
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.onRequestQueued(listener)
.onRequestBegin(listener)
.onRequestHeaders(listener)
.onRequestCommit(listener)
.onRequestContent(listener)
.onRequestSuccess(listener)
.onRequestFailure(listener)
.listener(listener)
.send();
Assert.assertEquals(200, response.getStatus());
int expectedEventsTriggeredByOnRequestXXXListeners = 5;
int expectedEventsTriggeredByListener = 5;
int expected = expectedEventsTriggeredByOnRequestXXXListeners + expectedEventsTriggeredByListener;
Assert.assertEquals(expected, counter.get());
}
@Test
public void testResponseListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception
{
start(new EmptyServerHandler());
final AtomicInteger counter = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
Response.Listener listener = new Response.Listener()
{
@Override
public void onBegin(Response response)
{
counter.incrementAndGet();
}
@Override
public boolean onHeader(Response response, HttpField field)
{
// Number of header may vary, so don't count
return true;
}
@Override
public void onHeaders(Response response)
{
counter.incrementAndGet();
}
@Override
public void onContent(Response response, ByteBuffer content)
{
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onSuccess(Response response)
{
counter.incrementAndGet();
}
@Override
public void onFailure(Response response, Throwable failure)
{
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onComplete(Result result)
{
Assert.assertEquals(200, result.getResponse().getStatus());
counter.incrementAndGet();
latch.countDown();
}
};
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.onResponseBegin(listener)
.onResponseHeader(listener)
.onResponseHeaders(listener)
.onResponseContent(listener)
.onResponseSuccess(listener)
.onResponseFailure(listener)
.send(listener);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
int expectedEventsTriggeredByOnResponseXXXListeners = 3;
int expectedEventsTriggeredByCompletionListener = 4;
int expected = expectedEventsTriggeredByOnResponseXXXListeners + expectedEventsTriggeredByCompletionListener;
Assert.assertEquals(expected, counter.get());
}
}