|
| 1 | +package grails.async.web |
| 2 | + |
| 3 | +import groovy.transform.CompileStatic |
| 4 | +import org.grails.web.util.GrailsApplicationAttributes |
| 5 | +import org.grails.web.servlet.mvc.GrailsWebRequest |
| 6 | +import org.springframework.context.ApplicationContext |
| 7 | +import org.springframework.util.Assert |
| 8 | +import org.springframework.web.context.request.async.AsyncWebRequest |
| 9 | + |
| 10 | +import jakarta.servlet.AsyncContext |
| 11 | +import jakarta.servlet.AsyncEvent |
| 12 | +import jakarta.servlet.AsyncListener |
| 13 | +import jakarta.servlet.ServletContext |
| 14 | +import jakarta.servlet.http.HttpServletRequest |
| 15 | +import jakarta.servlet.http.HttpServletResponse |
| 16 | +import java.util.concurrent.atomic.AtomicBoolean |
| 17 | +import java.util.function.Consumer |
| 18 | + |
| 19 | +/** |
| 20 | + * Implementation of Spring 4.0 {@link AsyncWebRequest} interface for Grails |
| 21 | + * |
| 22 | + * @author Graeme Rocher |
| 23 | + * @since 3.0 |
| 24 | + */ |
| 25 | +@CompileStatic |
| 26 | +class AsyncGrailsWebRequest extends GrailsWebRequest implements AsyncWebRequest, AsyncListener{ |
| 27 | + static final String WEB_REQUEST = "org.grails.ASYNC_WEB_REQUEST" |
| 28 | + |
| 29 | + Long timeout |
| 30 | + AsyncContext asyncContext |
| 31 | + |
| 32 | + private AtomicBoolean asyncCompleted = new AtomicBoolean(false) |
| 33 | + |
| 34 | + List<Runnable> timeoutHandlers = [] |
| 35 | + List<Runnable> completionHandlers = [] |
| 36 | + List<Consumer<Throwable>> exceptionHandlers = [] |
| 37 | + |
| 38 | + AsyncGrailsWebRequest(HttpServletRequest request, HttpServletResponse response, GrailsApplicationAttributes attributes) { |
| 39 | + super(request, response, attributes) |
| 40 | + request.setAttribute(WEB_REQUEST, this) |
| 41 | + } |
| 42 | + |
| 43 | + AsyncGrailsWebRequest(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) { |
| 44 | + super(request, response, servletContext) |
| 45 | + request.setAttribute(WEB_REQUEST, this) |
| 46 | + } |
| 47 | + |
| 48 | + AsyncGrailsWebRequest(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, ApplicationContext applicationContext) { |
| 49 | + super(request, response, servletContext, applicationContext) |
| 50 | + request.setAttribute(WEB_REQUEST, this) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Looks up the GrailsWebRequest from the current request. |
| 55 | + * @param request The current request |
| 56 | + * @return The GrailsWebRequest |
| 57 | + */ |
| 58 | + static AsyncGrailsWebRequest lookup(HttpServletRequest request) { |
| 59 | + AsyncGrailsWebRequest webRequest = (AsyncGrailsWebRequest) request.getAttribute(WEB_REQUEST); |
| 60 | + return webRequest |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + void addTimeoutHandler(Runnable runnable) { |
| 65 | + timeoutHandlers << runnable |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + void addErrorHandler(Consumer<Throwable> exceptionHandler) { |
| 70 | + exceptionHandlers << exceptionHandler |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + void addCompletionHandler(Runnable runnable) { |
| 75 | + completionHandlers << runnable |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + void startAsync() { |
| 80 | + Assert.state(request.asyncSupported, "The current request does not support Async processing") |
| 81 | + if(!isAsyncStarted()) { |
| 82 | + asyncContext = request.startAsync(request, response) |
| 83 | + asyncContext.addListener(this) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + boolean isAsyncStarted() { |
| 89 | + asyncContext && request.asyncStarted |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + void dispatch() { |
| 94 | + Assert.notNull this.asyncContext, "Cannot dispatch without an AsyncContext" |
| 95 | + asyncContext.dispatch() |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + boolean isAsyncComplete() { |
| 100 | + return this.asyncCompleted.get() |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + void onComplete(AsyncEvent event) throws IOException { |
| 105 | + for(handler in completionHandlers) { |
| 106 | + handler.run() |
| 107 | + } |
| 108 | + asyncContext = null |
| 109 | + asyncCompleted.set true |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + void onTimeout(AsyncEvent event) throws IOException { |
| 114 | + for(handler in timeoutHandlers) { |
| 115 | + handler.run() |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + void onError(AsyncEvent event) throws IOException {} |
| 121 | + |
| 122 | + @Override |
| 123 | + void onStartAsync(AsyncEvent event) throws IOException {} |
| 124 | +} |
0 commit comments