jamesfredley commented on code in PR #16786:
URL: https://github.com/apache/grails-core/pull/16786#discussion_r4401191320


##########
grails-async/plugin/src/main/groovy/org/grails/plugins/web/async/TaskRequest.groovy:
##########
@@ -0,0 +1,237 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.async
+
+import java.lang.reflect.InvocationHandler
+import java.lang.reflect.InvocationTargetException
+import java.lang.reflect.Method
+import java.lang.reflect.Modifier
+import java.lang.reflect.Proxy
+
+import groovy.transform.CompileStatic
+
+import jakarta.servlet.AsyncEvent
+import jakarta.servlet.AsyncListener
+import jakarta.servlet.http.HttpServletRequest
+
+import org.springframework.web.context.request.RequestAttributes
+
+import org.grails.web.servlet.mvc.GrailsWebRequest
+
+/**
+ * The request a task started during a web request sees.
+ *
+ * <p>Until the request completes, it is that request, with every public view 
the request offers, such as a
+ * multipart wrapper's, so a task the response waits for, such as one started 
with {@code WebPromises}, renders
+ * through it. The container recycles the request once it completes, including 
any asynchronous processing, so
+ * from then on it is a {@link CompletedHttpServletRequest copy} of the 
request taken as it completed, and the
+ * recycled request is released. One copy is taken for the request, however 
many tasks it has, and each task
+ * reads it through a view of its own, so each has the body to read.</p>
+ *
+ * @since 8.0.1
+ */
+@CompileStatic
+class TaskRequest implements InvocationHandler {
+
+    private static final String STATE = TaskRequest.name + '.STATE'
+    private static final String COMPLETION_CALLBACK = TaskRequest.name + 
'.COMPLETION'
+
+    private final RequestState state
+    private CompletedHttpServletRequest view
+
+    private TaskRequest(RequestState state) {
+        this.state = state
+    }
+
+    /**
+     * The request a task started during a web request sees. It has to be 
called while the web request is the
+     * current one, when the task is submitted.
+     *
+     * @param webRequest the web request the task is started during
+     * @return the request for the task
+     */
+    static HttpServletRequest of(GrailsWebRequest webRequest) {
+        HttpServletRequest current = webRequest.currentRequest
+        if (Proxy.isProxyClass(current.getClass())) {
+            InvocationHandler handler = Proxy.getInvocationHandler(current)
+            if (handler instanceof TaskRequest) {
+                // A task started from a task reads the same request through a 
view of its own
+                return (HttpServletRequest) 
Proxy.newProxyInstance(current.getClass().classLoader,
+                        current.getClass().interfaces, new 
TaskRequest(((TaskRequest) handler).state))
+            }
+        }
+        ClassLoader loader = current.getClass().classLoader ?: 
TaskRequest.classLoader
+        return (HttpServletRequest) Proxy.newProxyInstance(loader, 
interfacesOf(current),
+                new TaskRequest(stateOf(webRequest, current)))
+    }
+
+    // One state for the request, however many tasks it has, so completing it 
takes the one copy they all read
+    private static RequestState stateOf(GrailsWebRequest webRequest, 
HttpServletRequest current) {
+        RequestState existing = boundTo(current)
+        if (existing != null) {
+            return existing
+        }
+        RequestState state = new RequestState(current)
+        if (webRequest.isActive()) {
+            current.setAttribute(STATE, state)
+            webRequest.registerDestructionCallback(COMPLETION_CALLBACK, 
state::requestCompleted,
+                    RequestAttributes.SCOPE_REQUEST)
+        }
+        else {
+            state.requestCompleted()
+        }
+        return state
+    }
+
+    private static RequestState boundTo(HttpServletRequest current) {
+        try {
+            Object state = current.getAttribute(STATE)
+            return state instanceof RequestState ? (RequestState) state : null
+        }
+        catch (IllegalStateException ignored) {
+            // The container has already recycled the request
+            return null
+        }
+    }
+
+    // Every public view of the request, such as a multipart wrapper's, so the 
task finds what a filter resolved
+    private static Class<?>[] interfacesOf(HttpServletRequest request) {
+        Set<Class<?>> interfaces = new LinkedHashSet<>()
+        interfaces.add(HttpServletRequest)
+        for (Class<?> type = request.getClass(); type != null; type = 
type.superclass) {

Review Comment:
   This follows the class hierarchy only. A multipart request sitting inside an 
ordinary HttpServletRequestWrapper is not among those interfaces. The proxy is 
not a MultipartHttpServletRequest and it is not a ServletRequestWrapper, so 
WebUtils.resolveMultipartRequest cannot find the upload. A file parameter stays 
null while the request is still live. Walk the wrapped requests too.



##########
grails-async/plugin/src/main/groovy/org/grails/plugins/web/async/TaskRequest.groovy:
##########
@@ -0,0 +1,237 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.async
+
+import java.lang.reflect.InvocationHandler
+import java.lang.reflect.InvocationTargetException
+import java.lang.reflect.Method
+import java.lang.reflect.Modifier
+import java.lang.reflect.Proxy
+
+import groovy.transform.CompileStatic
+
+import jakarta.servlet.AsyncEvent
+import jakarta.servlet.AsyncListener
+import jakarta.servlet.http.HttpServletRequest
+
+import org.springframework.web.context.request.RequestAttributes
+
+import org.grails.web.servlet.mvc.GrailsWebRequest
+
+/**
+ * The request a task started during a web request sees.
+ *
+ * <p>Until the request completes, it is that request, with every public view 
the request offers, such as a
+ * multipart wrapper's, so a task the response waits for, such as one started 
with {@code WebPromises}, renders
+ * through it. The container recycles the request once it completes, including 
any asynchronous processing, so
+ * from then on it is a {@link CompletedHttpServletRequest copy} of the 
request taken as it completed, and the
+ * recycled request is released. One copy is taken for the request, however 
many tasks it has, and each task
+ * reads it through a view of its own, so each has the body to read.</p>
+ *
+ * @since 8.0.1
+ */
+@CompileStatic
+class TaskRequest implements InvocationHandler {
+
+    private static final String STATE = TaskRequest.name + '.STATE'
+    private static final String COMPLETION_CALLBACK = TaskRequest.name + 
'.COMPLETION'
+
+    private final RequestState state
+    private CompletedHttpServletRequest view
+
+    private TaskRequest(RequestState state) {
+        this.state = state
+    }
+
+    /**
+     * The request a task started during a web request sees. It has to be 
called while the web request is the
+     * current one, when the task is submitted.
+     *
+     * @param webRequest the web request the task is started during
+     * @return the request for the task
+     */
+    static HttpServletRequest of(GrailsWebRequest webRequest) {
+        HttpServletRequest current = webRequest.currentRequest
+        if (Proxy.isProxyClass(current.getClass())) {
+            InvocationHandler handler = Proxy.getInvocationHandler(current)
+            if (handler instanceof TaskRequest) {
+                // A task started from a task reads the same request through a 
view of its own
+                return (HttpServletRequest) 
Proxy.newProxyInstance(current.getClass().classLoader,
+                        current.getClass().interfaces, new 
TaskRequest(((TaskRequest) handler).state))
+            }
+        }
+        ClassLoader loader = current.getClass().classLoader ?: 
TaskRequest.classLoader
+        return (HttpServletRequest) Proxy.newProxyInstance(loader, 
interfacesOf(current),
+                new TaskRequest(stateOf(webRequest, current)))
+    }
+
+    // One state for the request, however many tasks it has, so completing it 
takes the one copy they all read
+    private static RequestState stateOf(GrailsWebRequest webRequest, 
HttpServletRequest current) {
+        RequestState existing = boundTo(current)
+        if (existing != null) {
+            return existing
+        }
+        RequestState state = new RequestState(current)
+        if (webRequest.isActive()) {
+            current.setAttribute(STATE, state)
+            webRequest.registerDestructionCallback(COMPLETION_CALLBACK, 
state::requestCompleted,
+                    RequestAttributes.SCOPE_REQUEST)
+        }
+        else {
+            state.requestCompleted()
+        }
+        return state
+    }
+
+    private static RequestState boundTo(HttpServletRequest current) {
+        try {
+            Object state = current.getAttribute(STATE)
+            return state instanceof RequestState ? (RequestState) state : null
+        }
+        catch (IllegalStateException ignored) {
+            // The container has already recycled the request
+            return null
+        }
+    }
+
+    // Every public view of the request, such as a multipart wrapper's, so the 
task finds what a filter resolved
+    private static Class<?>[] interfacesOf(HttpServletRequest request) {
+        Set<Class<?>> interfaces = new LinkedHashSet<>()
+        interfaces.add(HttpServletRequest)
+        for (Class<?> type = request.getClass(); type != null; type = 
type.superclass) {
+            addInterfaces(type.interfaces, interfaces)
+        }
+        return interfaces.toArray(new Class<?>[0])
+    }
+
+    private static void addInterfaces(Class<?>[] candidates, Set<Class<?>> 
interfaces) {
+        for (Class<?> candidate : candidates) {
+            // A proxy class can only implement interfaces it can access
+            if (Modifier.isPublic(candidate.modifiers) && 
interfaces.add(candidate)) {
+                addInterfaces(candidate.interfaces, interfaces)
+            }
+        }
+    }
+
+    @Override
+    Object invoke(Object proxy, Method method, Object[] args) throws Throwable 
{
+        if (method.name == 'equals' && method.parameterCount == 1) {
+            return proxy.is(args[0])
+        }
+        if (method.name == 'hashCode' && method.parameterCount == 0) {
+            return System.identityHashCode(proxy)
+        }
+        HttpServletRequest current = state.live
+        if (current == null) {
+            current = view()
+        }
+        if (!method.declaringClass.isInstance(current)) {

Review Comment:
   After completion this current is the copy, which is not a 
MultipartHttpServletRequest, so getMultiFileMap throws here. GrailsParameterMap 
calls getMultiFileMap once resolveMultipartRequest has found a multipart 
request, so reading params after completion throws instead of returning the 
copied parameter. Return the copied files, or an empty map, for that call.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to