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

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


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

commit ae91e0b564ebe3610ab187792605fec0cc65797c
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                            |  7 +++++++
 6 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/jasper/runtime/JspWriterImpl.java 
b/java/org/apache/jasper/runtime/JspWriterImpl.java
index 2b3d34c5c7..263df987dc 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 javax.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 d385d5263a..7f67009630 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 8d0427b3de..501626ccd7 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -160,6 +160,13 @@
         a warning will be logged and the default will used.
         (markt)
       </add>
+      <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>
   <subsection name="Other">


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

Reply via email to