click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r922761 - in /click/trunk/click: documentation/docs/roadmap-changes.html framework/src/org/apache/click/ClickServlet.java framework/test/org/apache/click/ClickServletTest.java framework/test/org/apache/click/pages/BinaryPage.java
Date Sun, 14 Mar 2010 04:43:29 GMT
Author: sabob
Date: Sun Mar 14 04:43:28 2010
New Revision: 922761

URL: http://svn.apache.org/viewvc?rev=922761&view=rev
Log:
fallback to the outputstream if writer cannot be retrieved. CLK-644

Added:
    click/trunk/click/framework/test/org/apache/click/pages/BinaryPage.java
Modified:
    click/trunk/click/documentation/docs/roadmap-changes.html
    click/trunk/click/framework/src/org/apache/click/ClickServlet.java
    click/trunk/click/framework/test/org/apache/click/ClickServletTest.java

Modified: click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/click/trunk/click/documentation/docs/roadmap-changes.html?rev=922761&r1=922760&r2=922761&view=diff
==============================================================================
--- click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ click/trunk/click/documentation/docs/roadmap-changes.html Sun Mar 14 04:43:28 2010
@@ -163,6 +163,11 @@ includes improved Ajax support and @Bind
         This issue was raised by Andrey Rybin
         [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-620">CLK-620</a>].
       </li>
+      <li class="change">
+        Improved ClickServlet to use the response's OutputStream if the Writer
+        cannot be retrieved
+        [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-644">CLK-644</a>].
+      </li>
     </ul>
   </dd>
 

Modified: click/trunk/click/framework/src/org/apache/click/ClickServlet.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/ClickServlet.java?rev=922761&r1=922760&r2=922761&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/ClickServlet.java (original)
+++ click/trunk/click/framework/src/org/apache/click/ClickServlet.java Sun Mar 14 04:43:28
2010
@@ -19,6 +19,7 @@
 package org.apache.click;
 
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.lang.reflect.Field;
 import java.util.Collections;
@@ -820,11 +821,12 @@ public class ClickServlet extends HttpSe
 
         final Map model = createTemplateModel(page);
 
-        HttpServletResponse response = page.getContext().getResponse();
+        Context context = page.getContext();
+        HttpServletResponse response = context.getResponse();
 
         response.setContentType(page.getContentType());
 
-        Writer writer = response.getWriter();
+        Writer writer = getWriter(context);
 
         if (page.getHeaders() != null) {
             setPageResponseHeaders(response, page.getHeaders());
@@ -1748,6 +1750,25 @@ public class ClickServlet extends HttpSe
         THREAD_LOCAL_INTERCEPTORS.set(listeners);
     }
 
+    /**
+     * Retrieve a writer instance for the given context.
+     *
+     * @param context the request context
+     * @return a writer instance
+     * @throws IOException if an input or output exception occurred
+     */
+    Writer getWriter(Context context) throws IOException {
+        try {
+
+            return context.getResponse().getWriter();
+
+        } catch (IllegalStateException ignore) {
+            // If writer cannot be retrieved fallback to OutputStream. CLK-644
+            return new OutputStreamWriter(context.getResponse().getOutputStream(),
+                 context.getResponse().getCharacterEncoding());
+        }
+    }
+
     // ---------------------------------------------------------- Inner Classes
 
     /**

Modified: click/trunk/click/framework/test/org/apache/click/ClickServletTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/ClickServletTest.java?rev=922761&r1=922760&r2=922761&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/ClickServletTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/ClickServletTest.java Sun Mar 14 04:43:28
2010
@@ -21,6 +21,7 @@ package org.apache.click;
 import junit.framework.Assert;
 import junit.framework.TestCase;
 import org.apache.click.control.Form;
+import org.apache.click.pages.BinaryPage;
 import org.apache.click.pages.ListenerPage;
 
 /**
@@ -47,4 +48,17 @@ public class ClickServletTest extends Te
         Assert.assertTrue(page.success);
         container.stop();
     }
+
+    /**
+     * Check that ClickServlet still renders an ErrorPage for cases where the
+     * response outputStream has been retrieved and an exception occurs.
+     */
+    public void testBinaryExceptionHandling() {
+        MockContainer container = new MockContainer("web");
+        container.start();
+
+        container.testPage(BinaryPage.class);
+
+        container.stop();
+    }
 }

Added: click/trunk/click/framework/test/org/apache/click/pages/BinaryPage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/pages/BinaryPage.java?rev=922761&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/pages/BinaryPage.java (added)
+++ click/trunk/click/framework/test/org/apache/click/pages/BinaryPage.java Sun Mar 14 04:43:28
2010
@@ -0,0 +1,44 @@
+/*
+ * 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.click.pages;
+
+import java.io.IOException;
+import org.apache.click.Context;
+import org.apache.click.Page;
+
+/**
+ * Page that renders binary content.
+ */
+public class BinaryPage extends Page {
+
+    public void onInit() {
+        try {
+            Context context = getContext();
+
+            // Retrieve the response outputStream. The servlet container will
+            // throw an exception if Click tries to retrieve the response writer.
+            // CLK-644 fixes the problem by using the response outputStream if
+            // writer cannot be used
+            context.getResponse().getOutputStream();
+
+        } catch (IOException ioe) {
+            throw new RuntimeException(ioe);
+        }
+    }
+}



Mime
View raw message