Updated to the new HttpCore API

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@446853 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2006-09-16 11:12:45 +00:00
parent 8de8066f83
commit 5e97770df4
2 changed files with 17 additions and 6 deletions

View File

@ -35,6 +35,7 @@ import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpConnection;
import org.apache.http.HttpResponse;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.protocol.HttpContext;
/**
* <p>
@ -50,9 +51,12 @@ public class DefaultResponseConsumedWatcher
private final HttpConnection conn;
private final HttpResponse response;
private final HttpContext context;
public DefaultResponseConsumedWatcher(
final HttpConnection conn, final HttpResponse response) {
final HttpConnection conn,
final HttpResponse response,
final HttpContext context) {
super();
if (conn == null) {
throw new IllegalArgumentException("HTTP connection may not be null");
@ -62,11 +66,12 @@ public class DefaultResponseConsumedWatcher
}
this.conn = conn;
this.response = response;
this.context = context;
}
public void responseConsumed() {
ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
if (!s.keepAlive(this.conn, this.response)) {
if (!s.keepAlive(this.response, this.context)) {
try {
this.conn.close();
} catch (IOException ex) {

View File

@ -38,6 +38,8 @@ import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.DefaultHttpParams;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.mockup.HttpConnectionMockup;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import junit.framework.Test;
import junit.framework.TestCase;
@ -63,14 +65,14 @@ public class TestDefaultResponseConsumedWatcher extends TestCase {
public void testIllegalResponseArg() throws Exception {
try {
new DefaultResponseConsumedWatcher(null, null);
new DefaultResponseConsumedWatcher(null, null, null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
new DefaultResponseConsumedWatcher(
new HttpConnectionMockup(), null);
new HttpConnectionMockup(), null, null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
@ -78,6 +80,7 @@ public class TestDefaultResponseConsumedWatcher extends TestCase {
}
public void testConnectionAutoClose() throws Exception {
HttpContext context = new HttpExecutionContext(null);
byte[] data = new byte[] {'1', '2', '3'};
HttpConnection conn = new HttpConnectionMockup();
BasicHttpEntity entity = new BasicHttpEntity();
@ -91,7 +94,8 @@ public class TestDefaultResponseConsumedWatcher extends TestCase {
response.setEntity(entity);
// Wrap the entity input stream
ResponseConsumedWatcher watcher = new DefaultResponseConsumedWatcher(conn, response);
ResponseConsumedWatcher watcher = new DefaultResponseConsumedWatcher(
conn, response, context);
InputStream content = new AutoCloseInputStream(entity.getContent(), watcher);
assertTrue(conn.isOpen());
while (content.read() != -1) {}
@ -99,6 +103,7 @@ public class TestDefaultResponseConsumedWatcher extends TestCase {
}
public void testConnectionKeepAlive() throws Exception {
HttpContext context = new HttpExecutionContext(null);
byte[] data = new byte[] {'1', '2', '3'};
HttpConnection conn = new HttpConnectionMockup();
BasicHttpEntity entity = new BasicHttpEntity();
@ -112,7 +117,8 @@ public class TestDefaultResponseConsumedWatcher extends TestCase {
response.setEntity(entity);
// Wrap the entity input stream
ResponseConsumedWatcher watcher = new DefaultResponseConsumedWatcher(conn, response);
ResponseConsumedWatcher watcher = new DefaultResponseConsumedWatcher(
conn, response, context);
InputStream content = new AutoCloseInputStream(entity.getContent(), watcher);
assertTrue(conn.isOpen());