Skip to content

Commit

Permalink
Go back to use enum instead of exception, as discussed with Brian
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Mar 4, 2010
1 parent f41db9a commit 8cca15b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 93 deletions.
13 changes: 7 additions & 6 deletions README
Expand Up @@ -77,24 +77,25 @@ You can also mix Future with AsyncHandler to only retrieve part of the asynchron
private StringBuilder builder = new StringBuilder();

@Override
public void onStatusReceived(HttpResponseStatus status) throws Exception {
public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
int statusCode = status.getStatusCode();
// The Status have been read
// If you don't want to read the headers,body, or stop processing the response
throw new ResponseCompleted();
// If you don't want to read the headers,body or stop processing the response
return STATE.ABORT;
}

@Override
public void onHeadersReceived(HttpResponseHeaders h) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception {
Headers headers = h.getHeaders();
// The headers have been read
// If you don't want to read the body, or stop processing the response
throw new ResponseCompleted();
return STATE.ABORT;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
builder.append(new String(bodyPart.getBodyPartBytes()));
return STATE.CONTINU
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Expand Up @@ -17,11 +17,6 @@
<maven>2.0.9</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
Expand All @@ -37,6 +32,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
Expand Down
Expand Up @@ -27,21 +27,24 @@ public abstract class AsyncCompletionHandler<T> implements AsyncHandler<T>{
* {@inheritDoc}
*/
@Override
public void onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
return STATE.CONTINUE;
}

/**
* {@inheritDoc}
*/
@Override
public void onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
return STATE.CONTINUE;
}

/**
* {@inheritDoc}
*/
@Override
public void onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
return STATE.CONTINUE;
}

/**
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/ning/http/client/AsyncHandler.java
Expand Up @@ -31,14 +31,7 @@
*/
public interface AsyncHandler<T> {

/**
* Convenience exception to throw for interrupting the processing of the asynchronous response.
*/
public class ResponseCompleted extends Exception{
public ResponseCompleted(){
}
}

public static enum STATE {ABORT,CONTINUE}
/**
* Invoked when an unexpected exception occurs during the processing of the response
*
Expand All @@ -51,21 +44,21 @@ public ResponseCompleted(){
* @param bodyPart response's body part.
* @throws Exception
*/
void onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception;
STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception;

/**
* Invoked as soon as the HTTP status line has been received
* @param responseStatus the status code and test of the response
* @throws Exception
*/
void onStatusReceived(HttpResponseStatus responseStatus) throws Exception;
STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception;

/**
* Invoked as soon as the HTTP headers has been received.
* @param headers the HTTP headers.
* @throws Exception
*/
void onHeadersReceived(HttpResponseHeaders headers) throws Exception;
STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception;

/**
* Invoked once the HTTP response has been fully received
Expand Down
Expand Up @@ -16,7 +16,7 @@
package com.ning.http.client.providers;

import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHandler.ResponseCompleted;
import com.ning.http.client.AsyncHandler.STATE;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpProvider;
import com.ning.http.client.ByteArrayPart;
Expand All @@ -32,12 +32,7 @@
import com.ning.http.client.StringPart;
import com.ning.http.collection.Pair;
import com.ning.http.url.Url;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
Expand Down Expand Up @@ -517,30 +512,15 @@ private void finishUpdate(AsyncHandler<?> handler, NettyAsyncResponse<?> asyncRe
}

private final boolean updateStatusAndInterrupt(AsyncHandler<?> handler, HttpResponseStatus c) throws Exception {
try {
handler.onStatusReceived(c);
} catch (ResponseCompleted r) {
return true;
}
return false;
return (handler.onStatusReceived(c) == STATE.CONTINUE ? false : true);
}

private final boolean updateHeadersAndInterrupt(AsyncHandler<?> handler, HttpResponseHeaders c) throws Exception {
try {
handler.onHeadersReceived(c);
} catch (ResponseCompleted r) {
return true;
}
return false;
return (handler.onHeadersReceived(c) == STATE.CONTINUE ? false : true);
}

private final boolean updateBodyAndInterrupt(AsyncHandler<?> handler, HttpResponseBodyPart c) throws Exception {
try {
handler.onBodyPartReceived(c);
} catch (ResponseCompleted r) {
return true;
}
return false;
return (handler.onBodyPartReceived(c) == STATE.CONTINUE ? false : true);
}

//Simple marker for stopping publishing bytes.
Expand Down Expand Up @@ -603,28 +583,6 @@ HttpMethod map(RequestType type) {
}
}

/**
* Map Netty Method to CommonsHttp Method.
*
* @param m
* @return
*/
HttpMethodBase reverseMap(HttpMethod m) {
if (m == HttpMethod.GET) {
return new GetMethod();
} else if (m == HttpMethod.POST) {
return new PostMethod();
} else if (m == HttpMethod.DELETE) {
return new DeleteMethod();
} else if (m == HttpMethod.PUT) {
return new PutMethod();
} else if (m == HttpMethod.HEAD) {
return new HeadMethod();
} else {
return null;
}
}

/**
* This is quite ugly has the code is coming from the HTTPClient.
*
Expand Down
Expand Up @@ -168,15 +168,18 @@ public void onThrowable(Throwable t) {
}

@Override
public void onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
return STATE.CONTINUE;
}

@Override
public void onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
return STATE.CONTINUE;
}

@Override
public void onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
return STATE.CONTINUE;
}

@Override
Expand Down
Expand Up @@ -43,12 +43,12 @@ public void asyncStreamGETTest() throws Throwable {
c.prepareGet(TARGET_URL).execute(new AsyncHandlerAdapter() {

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
l.countDown();
throw new ResponseCompleted();
return STATE.ABORT;
}

@Override
Expand Down Expand Up @@ -82,15 +82,17 @@ public void asyncStreamPOSTTest() throws Throwable {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand Down Expand Up @@ -134,18 +136,18 @@ public void asyncStreamInterruptTest() throws Throwable {
c.preparePost(TARGET_URL).setParameters(m).execute(new AsyncHandlerAdapter() {

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
throw new ResponseCompleted();
return STATE.ABORT;
}

@Override
public void onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
a.set(false);
Assert.fail("Interrupted not working");
throw new ResponseCompleted();
return STATE.ABORT;
}

@Override
Expand Down Expand Up @@ -177,15 +179,17 @@ public void asyncStreamFutureTest() throws Throwable {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand Down Expand Up @@ -219,7 +223,7 @@ public void asyncStreamThrowableRefusedTest() throws Throwable {
c.prepareGet(TARGET_URL).execute(new AsyncHandlerAdapter() {

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
throw new RuntimeException("FOO");
}

Expand Down Expand Up @@ -256,15 +260,17 @@ public void asyncStreamReusePOSTTest() throws Throwable {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand All @@ -285,15 +291,17 @@ public String onCompleted() throws Exception {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand All @@ -317,15 +325,17 @@ public void asyncStream301WithBody() throws Throwable {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type").toLowerCase(), UTF8);
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand All @@ -350,15 +360,17 @@ public void asyncStream301RedirectWithBody() throws Throwable {
private StringBuilder builder = new StringBuilder();

@Override
public void onHeadersReceived(HttpResponseHeaders content) throws Exception {
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
Headers h = content.getHeaders();
Assert.assertNotNull(h);
Assert.assertEquals(h.getHeaderValue("content-type"), "text/html; charset=ISO-8859-1");
return STATE.CONTINUE;
}

@Override
public void onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
builder.append(new String(content.getBodyPartBytes()));
return STATE.CONTINUE;
}

@Override
Expand Down

0 comments on commit 8cca15b

Please sign in to comment.