Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49726
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 23 Aug 2010 19:23:28 +0000 (19:23 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 23 Aug 2010 19:23:28 +0000 (19:23 +0000)
Specifying a default content type via a JSP property group should not prevent a page from setting some other content type
Includes test cases

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@988262 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/jasper/compiler/Compiler.java
test/org/apache/jasper/compiler/TestCompiler.java [new file with mode: 0644]
test/webapp-3.0/WEB-INF/web.xml
test/webapp-3.0/bug49726a.jsp [new file with mode: 0644]
test/webapp-3.0/bug49726b.jsp [new file with mode: 0644]
webapps/docs/changelog.xml

index 0c02fbd..7b9e03c 100644 (file)
@@ -140,9 +140,8 @@ public abstract class Compiler {
             pageInfo.setTrimDirectiveWhitespaces(JspUtil.booleanValue(jspProperty
                     .isTrimDirectiveWhitespaces()));
         }
-        if (jspProperty.getDefaultContentType() != null) {
-            pageInfo.setContentType(jspProperty.getDefaultContentType());
-        }
+        // Default ContentType processing is deferred until after the page has
+        // been parsed
         if (jspProperty.getBuffer() != null) {
             pageInfo.setBufferValue(jspProperty.getBuffer(), null,
                     errDispatcher);
@@ -197,6 +196,12 @@ public abstract class Compiler {
             // Pass 2 - the whole translation unit
             pageNodes = parserCtl.parse(ctxt.getJspFile());
 
+            // Leave this until now since it can only be set once - bug 49726
+            if (pageInfo.getContentType() == null &&
+                    jspProperty.getDefaultContentType() != null) {
+                pageInfo.setContentType(jspProperty.getDefaultContentType());
+            }
+
             if (ctxt.isPrototypeMode()) {
                 // generate prototype .java file for the tag file
                 writer = setupContextWriter(javaFileName);
diff --git a/test/org/apache/jasper/compiler/TestCompiler.java b/test/org/apache/jasper/compiler/TestCompiler.java
new file mode 100644 (file)
index 0000000..e9d01f6
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ */
+
+package org.apache.jasper.compiler;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestCompiler extends TomcatBaseTest {
+    
+    public void testBug49726a() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File("test/webapp-3.0");
+        tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
+        tomcat.start();
+
+        ByteChunk res = new ByteChunk();
+        Map<String,List<String>> headers = new HashMap<String,List<String>>();
+        
+        getUrl("http://localhost:" + getPort() + "/test/bug49726a.jsp", res,
+                headers);
+
+        // Check request completed
+        String result = res.toString();
+        assertEcho(result, "OK");
+        
+        // Check content type
+        assertTrue(headers.get("Content-Type").get(0).startsWith("text/html"));
+    }
+
+    public void testBug49726b() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File("test/webapp-3.0");
+        tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
+        tomcat.start();
+
+        ByteChunk res = new ByteChunk();
+        Map<String,List<String>> headers = new HashMap<String,List<String>>();
+        
+        getUrl("http://localhost:" + getPort() + "/test/bug49726b.jsp", res,
+                headers);
+
+        // Check request completed
+        String result = res.toString();
+        assertEcho(result, "OK");
+        
+        // Check content type
+        assertTrue(headers.get("Content-Type").get(0).startsWith("text/plain"));
+    }
+
+    /** Assertion for text printed by tags:echo */
+    private static void assertEcho(String result, String expected) {
+        assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
+    }
+}
index 947d4ea..ac2852f 100644 (file)
      Used as part of the Tomcat unit tests when a full web application is
      required.
   </description>
+  <jsp-config>
+    <jsp-property-group>
+      <default-content-type>text/plain</default-content-type>
+      <url-pattern>/bug49726a.jsp</url-pattern>
+      <url-pattern>/bug49726b.jsp</url-pattern>
+    </jsp-property-group>
+  </jsp-config>
 </web-app>
\ No newline at end of file
diff --git a/test/webapp-3.0/bug49726a.jsp b/test/webapp-3.0/bug49726a.jsp
new file mode 100644 (file)
index 0000000..a9e177b
--- /dev/null
@@ -0,0 +1,24 @@
+<%--
+ 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.
+--%>
+<%@ page contentType="text/html"%>
+<html>
+  <head><title>Bug 49726 test case</title></head>
+  <body>
+    <p>OK</p>
+  </body>
+</html>
+
diff --git a/test/webapp-3.0/bug49726b.jsp b/test/webapp-3.0/bug49726b.jsp
new file mode 100644 (file)
index 0000000..eaf7737
--- /dev/null
@@ -0,0 +1,23 @@
+<%--
+ 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.
+--%>
+<html>
+  <head><title>Bug 49726 test case</title></head>
+  <body>
+    <p>OK</p>
+  </body>
+</html>
+
index 0cd5ebf..fd73b0b 100644 (file)
       </add>
     </changelog>
   </subsection>
+  <subsection name="Jasper">
+    <changelog>
+      <fix>
+        <bug>49726</bug>: Specifying a default content type via a JSP property
+        group should not prevent a page from setting some other content type.
+        (markt)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Cluster">
     <changelog>
       <fix>