Skip to content

Commit

Permalink
#37 : refactor AsyncResolution for better API (getters for servlet ob…
Browse files Browse the repository at this point in the history
…jects, shortcut methods
  • Loading branch information
vankeisb committed Jan 13, 2016
1 parent 4ccf2a9 commit 9354905
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import org.apache.http.HttpHost;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

@Public
@UrlBinding("/async")
public class AsyncActionBean implements ActionBean {

private static final String JSP_PATH = "/WEB-INF/async/async.jsp";
private ActionBeanContext context;
public ActionBeanContext getContext() {
return context;
Expand Down Expand Up @@ -50,7 +49,8 @@ public Resolution asyncEvent() {
return new AsyncResolution() {

// only this method to implement. you must complete() or dispatch() yourself.
public void execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
@Override
protected void executeAsync() throws Exception {

// we use an Async Http Client in order to call the github web service as a demo.
// the async http client calls on of the lambdas when he's done, and
Expand All @@ -72,25 +72,25 @@ public void execute(final HttpServletRequest request, final HttpServletResponse
} catch (Exception e) {
clientException = e;
}
dispatchToJsp(getAsyncContext());
dispatch(JSP_PATH);
} else {
ghResponse = result.getStatusLine().getReasonPhrase();
dispatchToJsp(getAsyncContext());
dispatch(JSP_PATH);
}

})
.failed(ex -> {

// http client failure
clientException = ex;
dispatchToJsp(getAsyncContext());
dispatch(JSP_PATH);

})
.cancelled(() -> {

// just for demo, we never call it...
cancelled = true;
dispatchToJsp(getAsyncContext());
dispatch(JSP_PATH);

})
.get(); // trigger async request
Expand All @@ -101,9 +101,10 @@ public void execute(final HttpServletRequest request, final HttpServletResponse
@DontValidate
public Resolution asyncEventThatTimeouts() {
return new AsyncResolution() {
public void execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
@Override
protected void executeAsync() throws Exception {
getAsyncContext().setTimeout(1000);
getAsyncContext().getResponse().getWriter().write("OK");
getResponse().getWriter().write("OK");
// never call complete/dispatch...
}
};
Expand All @@ -112,17 +113,13 @@ public void execute(final HttpServletRequest request, final HttpServletResponse
@DontValidate
public Resolution asyncEventThatThrows() {
return new AsyncResolution() {
public void execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
@Override
protected void executeAsync() throws Exception {
throw new RuntimeException("WTF");
}
};
}

// helper dispatch method
private void dispatchToJsp(AsyncContext asyncContext) {
asyncContext.dispatch("/WEB-INF/async/async.jsp");
}

// getters for instance fields that have been set by event method

public boolean isCancelled() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.sourceforge.stripes.action;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class AsyncResolution implements Resolution {

Expand All @@ -14,4 +16,41 @@ public void setAsyncContext(AsyncContext asyncContext) {
this.asyncContext = asyncContext;
}

private ActionBeanContext context;

public ActionBeanContext getContext() {
return context;
}

public void setContext(ActionBeanContext context) {
this.context = context;
}

private HttpServletRequest request;
private HttpServletResponse response;

public HttpServletRequest getRequest() {
return request;
}

public HttpServletResponse getResponse() {
return response;
}

@Override
public final void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
this.request = request;
this.response = response;
executeAsync();
}

protected abstract void executeAsync() throws Exception;

protected void dispatch(String path) {
getAsyncContext().dispatch(path);
}

protected void complete() {
getAsyncContext().complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void onStartAsync(AsyncEvent event) throws IOException {
});
AsyncResolution asyncResolution = (AsyncResolution)resolution;
asyncResolution.setAsyncContext(asyncContext);
asyncResolution.setContext(ctx.getActionBeanContext());
}
executeResolution(ctx, resolution);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public void setContext(ActionBeanContext context) {
@DefaultHandler
public Resolution doAsync() {
return new AsyncResolution() {
public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
@Override
protected void executeAsync() throws Exception {
Thread.sleep(5000);
AsyncContext asyncContext = getAsyncContext();
asyncContext.getResponse().getWriter().write("DONE");
asyncContext.complete();
getResponse().getWriter().write("DONE");
complete();
}
};
}
Expand Down

0 comments on commit 9354905

Please sign in to comment.