|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.servlet.async; |
| 18 | + |
| 19 | +import org.glassfish.jersey.server.spi.ContainerResponseWriter; |
| 20 | +import org.glassfish.jersey.servlet.internal.ResponseWriter; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import javax.servlet.AsyncContext; |
| 24 | +import javax.servlet.AsyncListener; |
| 25 | +import javax.servlet.http.HttpServletRequest; |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | +import java.io.IOException; |
| 28 | +import java.lang.reflect.InvocationHandler; |
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.lang.reflect.Proxy; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +public class AsyncContextClosedTest { |
| 37 | + @Test |
| 38 | + public void testClosedAsyncContext() { |
| 39 | + List<AsyncListener> asyncListeners = new ArrayList<>(1); |
| 40 | + AsyncContext async = (AsyncContext) Proxy.newProxyInstance(getClass().getClassLoader(), |
| 41 | + new Class[]{AsyncContext.class}, new InvocationHandler() { |
| 42 | + @Override |
| 43 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 44 | + switch (method.getName()) { |
| 45 | + case "addListener": |
| 46 | + asyncListeners.add((AsyncListener) args[0]); |
| 47 | + case "complete": |
| 48 | + asyncListeners.forEach((asyncListener -> { |
| 49 | + try { |
| 50 | + asyncListener.onComplete(null); |
| 51 | + } catch (IOException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + })); |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + HttpServletRequest request = (HttpServletRequest) Proxy.newProxyInstance(getClass().getClassLoader(), |
| 61 | + new Class[]{HttpServletRequest.class}, new InvocationHandler() { |
| 62 | + boolean asyncStarted = false; |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 66 | + switch (method.getName()) { |
| 67 | + case "isAsyncStarted": |
| 68 | + return asyncStarted; |
| 69 | + case "startAsync": |
| 70 | + asyncStarted = true; |
| 71 | + return async; |
| 72 | + case "getAsyncContext": |
| 73 | + return async; |
| 74 | + |
| 75 | + } |
| 76 | + return null; |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + HttpServletResponse response = (HttpServletResponse) Proxy.newProxyInstance(getClass().getClassLoader(), |
| 81 | + new Class[]{HttpServletResponse.class}, new InvocationHandler() { |
| 82 | + @Override |
| 83 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 84 | + return null; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + // Create writer |
| 89 | + ResponseWriter writer = new ResponseWriter(false, false, response, |
| 90 | + new AsyncContextDelegateProviderImpl().createDelegate(request, response), |
| 91 | + Executors.newSingleThreadScheduledExecutor()); |
| 92 | + writer.suspend(10, TimeUnit.SECONDS, new ContainerResponseWriter.TimeoutHandler() { |
| 93 | + @Override |
| 94 | + public void onTimeout(ContainerResponseWriter responseWriter) { |
| 95 | + throw new IllegalStateException(); |
| 96 | + } |
| 97 | + }); |
| 98 | + // Simulate completion by the Servlet Container; |
| 99 | + request.getAsyncContext().complete(); |
| 100 | + // Check write is ignored |
| 101 | + writer.writeResponseStatusAndHeaders(10, null); |
| 102 | + } |
| 103 | +} |
0 commit comments