Skip to content

Commit

Permalink
#37 : rename AsyncResolution to AsyncResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
vankeisb committed Jan 27, 2016
1 parent ba5bac2 commit 60d7aaf
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.sourceforge.stripes.examples.async;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import net.sourceforge.stripes.examples.bugzooky.ext.Public;
import net.sourceforge.stripes.validation.Validate;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -44,7 +44,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(final AsyncResolution async) {
public void asyncEvent(final AsyncResponse 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 Down Expand Up @@ -95,14 +95,14 @@ public void run() {
}

@DontValidate
public void asyncEventThatTimeouts(AsyncResolution r) throws Exception {
public void asyncEventThatTimeouts(AsyncResponse r) throws Exception {
r.setTimeout(1000);
r.getResponse().getWriter().write("OK");
// never call complete/dispatch...
}

@DontValidate
public void asyncEventThatThrows(AsyncResolution r) {
public void asyncEventThatThrows(AsyncResponse r) {
throw new RuntimeException("BOOM");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package net.sourceforge.stripes.action;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -59,10 +59,10 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
} else {
response.sendError(errorCode);
}
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package net.sourceforge.stripes.action;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import net.sourceforge.stripes.controller.StripesConstants;
import net.sourceforge.stripes.util.Log;

Expand Down Expand Up @@ -146,11 +146,11 @@ public void execute(HttpServletRequest request, HttpServletResponse response)
request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, event);

// are we asynchronous ?
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, dispatch...
log.trace("Async mode, dispatching to URL: ", path);
asyncResolution.dispatch(path);
asyncResponse.dispatch(path);
} else {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package net.sourceforge.stripes.action;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -48,10 +48,10 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
response.setContentType("application/json");
builder.build(response.getWriter());
response.flushBuffer();
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package net.sourceforge.stripes.action;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import net.sourceforge.stripes.controller.FlashScope;
import net.sourceforge.stripes.controller.StripesConstants;
import net.sourceforge.stripes.util.Log;
Expand Down Expand Up @@ -224,10 +224,10 @@ public void sendRedirect(String location) throws IOException {

response.sendRedirect(url);

AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import net.sourceforge.stripes.exception.StripesRuntimeException;
import net.sourceforge.stripes.util.Log;
import net.sourceforge.stripes.util.Range;
Expand Down Expand Up @@ -240,10 +240,10 @@ final public void execute(HttpServletRequest request, HttpServletResponse respon
applyHeaders(response);
stream(response);

AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import net.sourceforge.stripes.controller.StripesFilter;
import net.sourceforge.stripes.exception.SourcePageNotFoundException;
import net.sourceforge.stripes.tag.ErrorsTag;
Expand Down Expand Up @@ -67,10 +67,10 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
sendErrors(request, response);
writer.println("</p></div>");

AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package net.sourceforge.stripes.ajax;

import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -75,10 +75,10 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
response.setContentType("text/javascript");
this.builder.build(response.getWriter());
response.flushBuffer();
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
AsyncResponse asyncResponse = AsyncResponse.get(request);
if (asyncResponse != null) {
// async started, complete
asyncResolution.complete();
asyncResponse.complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* do not want to depend on Servlet3 APIs at runtime, so that
* Stripes continues to run in Servlet2 containers.
*/
public abstract class AsyncResolution implements Resolution {
public abstract class AsyncResponse implements Resolution {

private static final Log log = Log.getInstance(AsyncResolution.class);
private static final Log log = Log.getInstance(AsyncResponse.class);

private static final String REQ_ATTR_NAME = "__Stripes_Async_Resolution";
private final HttpServletRequest request;
Expand All @@ -39,7 +39,7 @@ public abstract class AsyncResolution implements Resolution {
static {
try {
HttpServletRequest.class.getMethod("startAsync");
Class<?> impl = Class.forName("net.sourceforge.stripes.controller.AsyncResolutionServlet3");
Class<?> impl = Class.forName("net.sourceforge.stripes.controller.AsyncResponseServlet3");
ctor = impl.getDeclaredConstructor(
HttpServletRequest.class,
HttpServletResponse.class,
Expand All @@ -50,15 +50,15 @@ public abstract class AsyncResolution implements Resolution {
log.info("Container is not using Servlet3 : Async event handlers will throw runtime exceptions.");
} catch (Exception e) {
// should not happen unless we break internals (bad refactor etc).
log.error("Exception while initializing AsyncResolution implementation class.", e);
log.error("Exception while initializing AsyncResponse implementation class.", e);
throw new RuntimeException(e);
}

}

private Runnable cleanupCallback;

AsyncResolution(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
AsyncResponse(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
this.request = request;
this.response = response;
this.bean = bean;
Expand All @@ -68,14 +68,14 @@ public abstract class AsyncResolution implements Resolution {
}

/**
* Return the AsyncResolution bound to the request, if any.
* Return the AsyncResponse bound to the request, if any.
* Primarily used by Resolutions in order to complete processing
* accordingly when async is started.
* @param request the request
* @return the AsyncResolution or null
* @return the AsyncResponse or null
*/
public static AsyncResolution get(HttpServletRequest request) {
return (AsyncResolution)request.getAttribute(REQ_ATTR_NAME);
public static AsyncResponse get(HttpServletRequest request) {
return (AsyncResponse)request.getAttribute(REQ_ATTR_NAME);
}

void setCleanupCallback(Runnable cleanupCallback) {
Expand Down Expand Up @@ -134,13 +134,13 @@ public final void execute(HttpServletRequest request, HttpServletResponse respon
*/
public abstract void setTimeout(long timeout);

static AsyncResolution newInstance(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
static AsyncResponse newInstance(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
if (ctor == null) {
throw new StripesRuntimeException("Async events are not available in your container (requires Servlet3+).");
}
try {
Object o = ctor.newInstance(request, response, bean, handler);
return (AsyncResolution)o;
return (AsyncResponse)o;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.lang.reflect.Method;

/**
* Concrete implementation for AsyncResolution in Servlet3 containers.
* Concrete implementation for AsyncResponse in Servlet3 containers.
* Handles cleanup on completion, and delegates to servlet's AsyncContext methods.
*/
public class AsyncResolutionServlet3 extends AsyncResolution {
public class AsyncResponseServlet3 extends AsyncResponse {

private final static Log log = Log.getInstance(AsyncResolutionServlet3.class);
private final static Log log = Log.getInstance(AsyncResponseServlet3.class);

private final AsyncContext asyncContext;

public AsyncResolutionServlet3(HttpServletRequest request,
HttpServletResponse response,
ActionBean bean,
Method handler) {
public AsyncResponseServlet3(HttpServletRequest request,
HttpServletResponse response,
ActionBean bean,
Method handler) {
super(request, response, bean, handler);
this.asyncContext = request.startAsync(request, response);
// remove currentContext ThreadLocal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public Resolution intercept(ExecutionContext ctx) throws Exception {
final Object returnValue;
if (NameBasedActionResolver.isAsyncEventHandler(handler)) {
ActionBeanContext abc = ctx.getActionBeanContext();
returnValue = AsyncResolution.newInstance(abc.getRequest(), abc.getResponse(), bean, handler);
returnValue = AsyncResponse.newInstance(abc.getRequest(), abc.getResponse(), bean, handler);
} else {
returnValue = handler.invoke(bean);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon

// Whatever stage it came from, execute the resolution
if (resolution != null) {
if (resolution instanceof AsyncResolution) {
if (resolution instanceof AsyncResponse) {
// special handling for async resolutions : we defer
// cleanup to async processing. We register a
// "cleanup" callback that the async processing will
Expand All @@ -178,7 +178,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
// so that Stripes still works with Servlet2.x containers.)
async = true;
final PageContext pc = pageContext;
((AsyncResolution)resolution).setCleanupCallback(new Runnable() {
((AsyncResponse)resolution).setCleanupCallback(new Runnable() {
@Override
public void run() {
if (pc != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static boolean isAsyncEventHandler(Method handler) {
&& handler.getParameterTypes().length == 1) {
// look at arg type
Class<?> pType = handler.getParameterTypes()[0];
return AsyncResolution.class.isAssignableFrom(pType);
return AsyncResponse.class.isAssignableFrom(pType);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.sourceforge.stripes.action.*;
import static org.testng.Assert.*;

import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.controller.AsyncResponse;
import org.testng.annotations.Test;

import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -105,14 +105,14 @@ public void setContext(ActionBeanContext context) {
}

@DefaultHandler
public void doAsync(AsyncResolution r) throws Exception {
public void doAsync(AsyncResponse r) throws Exception {
System.out.println("Not Really Async...");
r.getResponse().getWriter().write("DONE");
completed = true;
r.complete();
}

public void doReallyAsync(final AsyncResolution r) throws Exception {
public void doReallyAsync(final AsyncResponse r) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -129,7 +129,7 @@ public void run() {
}).start();
}

public void doAsyncTimeout(AsyncResolution r) {
public void doAsyncTimeout(AsyncResponse r) {
r.setTimeout(1000);
// we never complete !
}
Expand All @@ -138,17 +138,17 @@ public Resolution doRegularException() {
throw new RuntimeException("boom");
}

public void doAsyncException(AsyncResolution r) {
public void doAsyncException(AsyncResponse r) {
throw new RuntimeException("Async boom");
}

public void doAsyncAndCompleteWithForwardResolution(AsyncResolution r) {
public void doAsyncAndCompleteWithForwardResolution(AsyncResponse r) {
System.out.println("hiya, I'm forwarding...");
completed = true;
r.complete(new ForwardResolution("/foo/bar.jsp"));
}

public void doAsyncClassy(final AsyncResolution callback) {
public void doAsyncClassy(final AsyncResponse callback) {
new Thread(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit 60d7aaf

Please sign in to comment.