This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 8d7a0102c4 Fix a TCK failure for JSP include then forward
8d7a0102c4 is described below

commit 8d7a0102c4953a62728b3a45443902b975272af9
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Mar 12 12:26:56 2024 +0000

    Fix a TCK failure for JSP include then forward
    
    Handle the case where the JSP engine forwards a request/response to a
    Servlet that uses an OutputStream rather than a Writer. This was
    triggering an IllegalStateException on code paths where there was a
    subsequent attempt to obtain a Writer.
    
    This had been returning the correct body but with a 500 response. The
    older TCKs didn't notice the 500 response. The refactored TCK used in
    Jakarta EE 11 did.
---
 java/org/apache/jasper/runtime/JspWriterImpl.java     | 12 +++++++++++-
 .../apache/jasper/runtime/TestPageContextImpl.java    | 17 +++++++++++++++++
 test/webapp/jsp/forward.jsp                           | 17 +++++++++++++++++
 test/webapp/jsp/includeThenForward.jsp                | 19 +++++++++++++++++++
 test/webapp/jsp/ok.html                               |  5 +++++
 webapps/docs/changelog.xml                            | 11 +++++++++++
 6 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/jasper/runtime/JspWriterImpl.java 
b/java/org/apache/jasper/runtime/JspWriterImpl.java
index a2b1632295..06b14c1b3b 100644
--- a/java/org/apache/jasper/runtime/JspWriterImpl.java
+++ b/java/org/apache/jasper/runtime/JspWriterImpl.java
@@ -17,6 +17,7 @@
 package org.apache.jasper.runtime;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.io.Writer;
 
 import jakarta.servlet.ServletResponse;
@@ -115,7 +116,16 @@ public class JspWriterImpl extends JspWriter {
 
     private void initOut() throws IOException {
         if (out == null) {
-            out = response.getWriter();
+            try {
+                out = response.getWriter();
+            } catch (IllegalStateException e) {
+                /*
+                 * At some point in the processing something (most likely the 
default servlet as the target of a
+                 * <jsp:forward ... /> action) wrote directly to the 
OutputStream rather than the Writer. Wrap the
+                 * OutputStream in a Writer so the JSp engine can use the 
Writer it is expecting to use.
+                 */
+                out = new PrintWriter(response.getOutputStream());
+            }
         }
     }
 
diff --git a/test/org/apache/jasper/runtime/TestPageContextImpl.java 
b/test/org/apache/jasper/runtime/TestPageContextImpl.java
index 4d409f0219..ee326ec3b2 100644
--- a/test/org/apache/jasper/runtime/TestPageContextImpl.java
+++ b/test/org/apache/jasper/runtime/TestPageContextImpl.java
@@ -120,4 +120,21 @@ public class TestPageContextImpl extends TomcatBaseTest {
         }
 
     }
+
+
+    @Test
+    public void testIncludeThenForward() throws Exception {
+        getTomcatInstanceTestWebapp(false, true);
+
+        ByteChunk res = new ByteChunk();
+
+        int rc = getUrl("http://localhost:"; + getPort() +
+                "/test/jsp/includeThenForward.jsp", res, null);
+
+        Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+
+        String body = res.toString();
+        Assert.assertTrue(body.contains("OK"));
+        Assert.assertFalse(body.contains("FAIL"));
+    }
 }
diff --git a/test/webapp/jsp/forward.jsp b/test/webapp/jsp/forward.jsp
new file mode 100644
index 0000000000..eb87acd5fe
--- /dev/null
+++ b/test/webapp/jsp/forward.jsp
@@ -0,0 +1,17 @@
+<%--
+ 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
+
+      http://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.
+--%>
+<jsp:forward page="ok.html" />
diff --git a/test/webapp/jsp/includeThenForward.jsp 
b/test/webapp/jsp/includeThenForward.jsp
new file mode 100644
index 0000000000..9c17bcce37
--- /dev/null
+++ b/test/webapp/jsp/includeThenForward.jsp
@@ -0,0 +1,19 @@
+<%--
+ 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
+
+      http://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.
+--%>
+<p>FAIL</p>
+<jsp:include page="forward.jsp" />
+<p>FAIL</p>
\ No newline at end of file
diff --git a/test/webapp/jsp/ok.html b/test/webapp/jsp/ok.html
new file mode 100644
index 0000000000..35b67d7844
--- /dev/null
+++ b/test/webapp/jsp/ok.html
@@ -0,0 +1,5 @@
+<html>
+  <body>
+    <p>OK</p>
+  </body>
+</html>
\ No newline at end of file
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index b921c7ccfe..59c19636c5 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -120,6 +120,17 @@
       </add>
     </changelog>
   </subsection>
+  <subsection name="Jasper">
+    <changelog>
+      <fix>
+        Handle the case where the JSP engine forwards a request/response to a
+        Servlet that uses an <code>OutputStream</code> rather than a
+        <code>Writer</code>. This was triggering an
+        <code>IllegalStateException</code> on code paths where there was a
+        subsequent attempt to obtain a <code>Writer</code>. (markt)
+      </fix>
+    </changelog>
+  </subsection>
 </section>
 <section name="Tomcat 11.0.0-M18 (markt)" rtext="release in progress">
   <subsection name="General">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to