Skip to content

Commit

Permalink
#37 : added AsyncResolution.complete(Resolution) method that allows f…
Browse files Browse the repository at this point in the history
…or reuse of Resolution classes. As the name says, completes the async processing. You cannot use several Resolutions when async processing.
  • Loading branch information
vankeisb committed Jan 18, 2016
1 parent 7e2d164 commit 95fdf51
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.function.Consumer;
import java.util.function.Supplier;

public abstract class AsyncResolution implements Resolution {

Expand Down Expand Up @@ -53,4 +55,14 @@ protected void dispatch(String path) {
protected void complete() {
getAsyncContext().complete();
}

protected void complete(Resolution resolution) {
try {
resolution.execute(getRequest(), getResponse());
} catch (Exception e) {
throw new RuntimeException(e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,24 @@ public void execute(HttpServletRequest request, HttpServletResponse response)
String oldEvent = (String) request.getAttribute(StripesConstants.REQ_ATTR_EVENT_NAME);
request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, event);

// Figure out if we're inside an include, and use an include instead of a forward
if (autoInclude && request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH) != null) {
log.trace("Including URL: ", path);
request.getRequestDispatcher(path).include(request, response);
// are we asynchronous ?
if (request.isAsyncStarted()) {
// async started, dispatch...
log.trace("Async mode, dispatching to URL: ", path);
request.getAsyncContext().dispatch(path);
} else {
log.trace("Forwarding to URL: ", path);
request.getRequestDispatcher(path).forward(request, response);
// Figure out if we're inside an include, and use an include instead of a forward
if (autoInclude && request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH) != null) {
log.trace("Including URL: ", path);
request.getRequestDispatcher(path).include(request, response);
} else {
log.trace("Forwarding to URL: ", path);
request.getRequestDispatcher(path).forward(request, response);
}

// Revert event name to its original value
request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, oldEvent);
}

// Revert event name to its original value
request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, oldEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
response.setContentType("application/json");
builder.build(response.getWriter());
response.flushBuffer();
if (request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public void sendRedirect(String location) throws IOException {
log.trace("Redirecting ", this.beans == null ? "" : "(w/flashed bean) ", "to URL: ", url);

response.sendRedirect(url);

if (request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ final public void execute(HttpServletRequest request, HttpServletResponse respon

applyHeaders(response);
stream(response);

if (request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
writer.println("</p><h2>Validation errors</h2><p>");
sendErrors(request, response);
writer.println("</p></div>");

if (request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
response.setContentType("text/javascript");
this.builder.build(response.getWriter());
response.flushBuffer();
if (request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class MockAsyncContext implements AsyncContext {

Expand All @@ -18,7 +17,7 @@ public class MockAsyncContext implements AsyncContext {

private boolean completed = false;
private boolean timedOut = false;
private long timeout = 30000;
private long timeout = 5000;
private long startedOn;

public MockAsyncContext(ServletRequest request, ServletResponse response) {
Expand Down Expand Up @@ -136,4 +135,16 @@ public void waitForCompletion() throws Exception {
Thread.sleep(200);
}
}

public long getStartedOn() {
return startedOn;
}

public boolean isTimedOut() {
return timedOut;
}

public boolean isCompleted() {
return completed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ public void testAsyncException() throws Exception {
assertTrue(caught);
}

@Test
public void testCompleteWithForwardResolution() throws Exception {
MockRoundtrip trip = new MockRoundtrip(getMockServletContext(), AsyncActionBean.class);
trip.execute("doAsyncAndCompleteWithForwardResolution");
AsyncActionBean bean = trip.getActionBean(AsyncActionBean.class);
assertNotNull(bean);
}

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

private boolean completed = false;
private boolean executedForwardResolution;
private ActionBeanContext context;

public ActionBeanContext getContext() {
Expand Down Expand Up @@ -142,9 +151,21 @@ protected void executeAsync() throws Exception {
};
}

public Resolution doAsyncAndCompleteWithForwardResolution() {
return new AsyncResolution() {
@Override
protected void executeAsync() throws Exception {
System.out.println("hiya, I'm forwarding...");
complete(new ForwardResolution("/foo/bar.jsp"));
}
};
}


public boolean isCompleted() {
return completed;
}

}


Expand Down

0 comments on commit 95fdf51

Please sign in to comment.