Skip to content

Commit

Permalink
Default callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn committed Jan 5, 2014
1 parent dbf00e8 commit 7a77a01
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/de/taimos/httputils/callbacks/HTTPByteCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.taimos.httputils.callbacks;

import org.apache.http.HttpResponse;

import de.taimos.httputils.WS;

public abstract class HTTPByteCallback extends HTTPStatusCheckCallback {

@Override
protected void checkedResponse(HttpResponse response) {
this.byteResponse(WS.getResponseAsBytes(response), response);
}

protected abstract void byteResponse(byte[] body, HttpResponse response);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.taimos.httputils.callbacks;

import org.apache.http.HttpResponse;

import de.taimos.httputils.HTTPResponseCallback;

public abstract class HTTPStatusCheckCallback implements HTTPResponseCallback {

@Override
public final void response(HttpResponse response) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != this.expectedStatus()) {
this.invalidStatus(statusCode, response);
}
this.checkedResponse(response);
}

protected abstract void checkedResponse(HttpResponse response);

protected abstract void invalidStatus(int status, HttpResponse response);

/**
* @return the expected status code of the {@link HttpResponse}
*/
protected int expectedStatus() {
return 200;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.taimos.httputils.callbacks;

import org.apache.http.HttpResponse;

import de.taimos.httputils.WS;

public abstract class HTTPStringCallback extends HTTPStatusCheckCallback {

@Override
protected void checkedResponse(HttpResponse response) {
this.stringResponse(WS.getResponseAsString(response), response);
}

protected abstract void stringResponse(String body, HttpResponse response);

}
35 changes: 35 additions & 0 deletions src/test/java/de/taimos/httputils/Tester1.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.junit.Assert;
import org.junit.Test;

import de.taimos.httputils.callbacks.HTTPStringCallback;

/**
* @author thoeger
*
Expand Down Expand Up @@ -63,6 +65,39 @@ public void fail(Exception e) {
Assert.assertTrue(cdl.await(10, TimeUnit.SECONDS));
}

/**
*
*/
@Test
public void testGetAsyncStringCB() throws InterruptedException {
final CountDownLatch cdl = new CountDownLatch(1);
WS.url("http://www.heise.de").getAsync(new HTTPStringCallback() {

@Override
public void fail(Exception e) {
System.out.println(e);
Assert.fail();
cdl.countDown();
}

@Override
protected void invalidStatus(int status, HttpResponse response) {
System.out.println("Invalid status: " + status);
Assert.fail();
cdl.countDown();
}

@Override
protected void stringResponse(String body, HttpResponse response) {
Assert.assertNotNull(body);
Assert.assertFalse(body.isEmpty());
cdl.countDown();
}

});
Assert.assertTrue(cdl.await(10, TimeUnit.SECONDS));
}

/**
*
*/
Expand Down

0 comments on commit 7a77a01

Please sign in to comment.