Skip to content

Commit

Permalink
#37 : rollback to Java7 (no more lambdas). Still needs java8 to run w…
Browse files Browse the repository at this point in the history
…ebtests on jetty9...
  • Loading branch information
vankeisb committed Jan 26, 2016
1 parent 538c34f commit b1ac43b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.sourceforge.stripes.examples.bugzooky.ext.Public;
import net.sourceforge.stripes.validation.Validate;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;

import javax.servlet.AsyncContext;
import java.io.ByteArrayOutputStream;

@Public
Expand Down Expand Up @@ -43,7 +43,7 @@ public Resolution display() {
* asynchronously fetch data from a remote web service (github)
* and set instance fields for use in the view.
*/
public void asyncEvent(AsyncResolution async) {
public void asyncEvent(final AsyncResolution async) {

// we use an Async Http Client in order to call the github web service as a demo.
// the async http client calls back one of the lambdas when it's done, and
Expand All @@ -53,41 +53,44 @@ public void asyncEvent(AsyncResolution async) {
HttpHost host = new HttpHost("api.github.com", 443, "https");
new AsyncHttpClient(host)
.buildRequest("/repos/StripesFramework/stripes/commits")
.completed(result -> {

// response is returned, deserialize result
status = result.getStatusLine().getStatusCode();
if (status == 200) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
result.getEntity().writeTo(bos);
bos.close();
ghResponse = bos.toString("UTF-8");
} catch (Exception e) {
clientException = e;
.completed(new AsyncHttpClient.Consumer<HttpResponse>() {
@Override
public void accept(HttpResponse result) {
// response is returned, deserialize result
status = result.getStatusLine().getStatusCode();
if (status == 200) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
result.getEntity().writeTo(bos);
bos.close();
ghResponse = bos.toString("UTF-8");
} catch (Exception e) {
clientException = e;
}
async.complete(forwardResolution);
} else {
ghResponse = result.getStatusLine().getReasonPhrase();
async.complete(forwardResolution);
}
async.complete(forwardResolution);
} else {
ghResponse = result.getStatusLine().getReasonPhrase();
async.complete(forwardResolution);
}

})
.failed(ex -> {

// http client failure
clientException = ex;
async.complete(forwardResolution);

.failed(new AsyncHttpClient.Consumer<Exception>() {
@Override
public void accept(Exception e) {
// http client failure
clientException = e;
async.complete(forwardResolution);
}
})
.cancelled(() -> {

// just for demo, we never call it...
cancelled = true;
async.complete(forwardResolution);
.cancelled(new Runnable() {
@Override
public void run() {
// just for demo, we never call it...
cancelled = true;
async.complete(forwardResolution);

})
.get(); // trigger async request
}
}).get(); // trigger async request
}

@DontValidate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.http.nio.reactor.ConnectingIOReactor;

import java.io.IOException;
import java.util.function.Consumer;

/**
* Wrapper for non-blocking http client example. Avoids cluttering the action bean's code...
Expand Down Expand Up @@ -54,6 +53,10 @@ public AsyncRequest buildRequest(String uri) {
return new AsyncRequest(uri);
}

public interface Consumer<T> {
void accept(T t);
}

public class AsyncRequest {

private final String uri;
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javax.servlet.ServletContext;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -251,10 +250,10 @@ public String getHandledEvent(Method handler) {
public static boolean isAsyncEventHandler(Method handler) {
if (!Modifier.isAbstract(handler.getModifiers())
&& handler.getReturnType().equals(Void.TYPE)
&& handler.getParameterCount() == 1) {
&& handler.getParameterTypes().length == 1) {
// look at arg type
Parameter p = handler.getParameters()[0];
return AsyncResolution.class.isAssignableFrom(p.getType());
Class<?> pType = handler.getParameterTypes()[0];
return AsyncResolution.class.isAssignableFrom(pType);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MockAsyncContext implements AsyncContext {

private final ServletRequest request;
private final ServletResponse response;
private final List<AsyncListener> listeners = new ArrayList<>();
private final List<AsyncListener> listeners = new ArrayList<AsyncListener>();

private boolean completed = false;
private boolean timedOut = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ public void doAsync(AsyncResolution r) throws Exception {
r.complete();
}

public void doReallyAsync(AsyncResolution r) throws Exception {
new Thread(() -> {
System.out.println("Really Async !");
try {
r.getResponse().getWriter().write("DONE");
completed = true;
r.complete();
} catch (IOException e) {
// will timeout...
e.printStackTrace();
public void doReallyAsync(final AsyncResolution r) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Really Async !");
try {
r.getResponse().getWriter().write("DONE");
completed = true;
r.complete();
} catch (IOException e) {
// will timeout...
e.printStackTrace();
}
}
}).start();
}
Expand All @@ -144,10 +147,13 @@ public void doAsyncAndCompleteWithForwardResolution(AsyncResolution r) {
r.complete(new ForwardResolution("/foo/bar.jsp"));
}

public void doAsyncClassy(AsyncResolution callback) {
new Thread(() -> {
completed = true;
callback.complete(new ForwardResolution("/foo/bar"));
public void doAsyncClassy(final AsyncResolution callback) {
new Thread(new Runnable() {
@Override
public void run() {
completed = true;
callback.complete(new ForwardResolution("/foo/bar"));
}
}).start();
}

Expand Down

0 comments on commit b1ac43b

Please sign in to comment.