Skip to content

Commit

Permalink
#37 : disable async actions on servlet < 3. Will throw an exception a…
Browse files Browse the repository at this point in the history
…t runtime.
  • Loading branch information
vankeisb committed Jan 27, 2016
1 parent 1aa62ae commit 11140f7
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.sourceforge.stripes.examples.async;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.AsyncResolution;
import net.sourceforge.stripes.examples.bugzooky.ext.Public;
import net.sourceforge.stripes.validation.Validate;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void run() {

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

This file was deleted.

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

import net.sourceforge.stripes.controller.AsyncResolution;

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

Expand Down Expand Up @@ -57,6 +59,11 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
} else {
response.sendError(errorCode);
}
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
// async started, complete
asyncResolution.complete();
}
}

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

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

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

// are we asynchronous ?
if (request.isAsyncStarted()) {
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
// async started, dispatch...
log.trace("Async mode, dispatching to URL: ", path);
request.getAsyncContext().dispatch(path);
asyncResolution.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,6 +15,8 @@
*/
package net.sourceforge.stripes.action;

import net.sourceforge.stripes.controller.AsyncResolution;

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

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

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

response.sendRedirect(url);

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

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

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

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

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

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

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

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

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -74,8 +75,10 @@ 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();
AsyncResolution asyncResolution = AsyncResolution.get(request);
if (asyncResolution != null) {
// async started, complete
asyncResolution.complete();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.sourceforge.stripes.controller;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.Resolution;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public abstract class AsyncResolution implements Resolution {

private static final String REQ_ATTR_NAME = "__Stripes_Async_Resolution";
private final HttpServletRequest request;
private final HttpServletResponse response;
private final ActionBean bean;
private final Method handler;

private Runnable cleanupCallback;
private long timeout;

protected AsyncResolution(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
this.request = request;
this.response = response;
this.bean = bean;
this.handler = handler;
request.setAttribute(REQ_ATTR_NAME, this);
}

public static AsyncResolution get(HttpServletRequest request) {
return (AsyncResolution)request.getAttribute(REQ_ATTR_NAME);
}

public void setCleanupCallback(Runnable cleanupCallback) {
this.cleanupCallback = cleanupCallback;
}

public void cleanup() {
if (cleanupCallback != null) {
cleanupCallback.run();
}
}

public HttpServletRequest getRequest() {
return request;
}

public HttpServletResponse getResponse() {
return response;
}

@Override
public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// invoke the handler (start async has been done already) and let it complete...
handler.invoke(bean, this);
}

public abstract void complete();

public abstract void complete(Resolution resolution);

public abstract void dispatch(String path);

public abstract long getTimeout();

public abstract void setTimeout(long timeout);

public static AsyncResolution newInstance(HttpServletRequest request, HttpServletResponse response, ActionBean bean, Method handler) {
// check wether or not we're using servlet3
try {
HttpServletRequest.class.getMethod("startAsync");
Class<?> impl = Class.forName("net.sourceforge.stripes.controller.AsyncResolutionServlet3");
Constructor<?> ctor = impl.getDeclaredConstructor(
HttpServletRequest.class,
HttpServletResponse.class,
ActionBean.class,
Method.class);
Object o = ctor.newInstance(request, response, bean, handler);
return (AsyncResolution)o;
} catch (NoSuchMethodException e) {
// not using servlet 3, throw exception for the moment
throw new UnsupportedOperationException("Async action beans require Servlet3+");
} catch (ClassNotFoundException e) {
// should never happen unless bad refactor
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 11140f7

Please sign in to comment.