click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r964015 - in /click/trunk/click/framework: src/org/apache/click/ClickServlet.java src/org/apache/click/Page.java test/org/apache/click/PageTest.java test/org/apache/click/pages/HeaderTestPage.java test/web/header-test.htm
Date Wed, 14 Jul 2010 11:52:44 GMT
Author: sabob
Date: Wed Jul 14 11:52:43 2010
New Revision: 964015

URL: http://svn.apache.org/viewvc?rev=964015&view=rev
Log:
Improved page header support allowing headers to be set from page constructor. CLK-711

Added:
    click/trunk/click/framework/test/org/apache/click/pages/HeaderTestPage.java
    click/trunk/click/framework/test/web/header-test.htm
Modified:
    click/trunk/click/framework/src/org/apache/click/ClickServlet.java
    click/trunk/click/framework/src/org/apache/click/Page.java
    click/trunk/click/framework/test/org/apache/click/PageTest.java

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=964015&r1=964014&r2=964015&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/ClickServlet.java (original)
+++ click/trunk/click/framework/src/org/apache/click/ClickServlet.java Wed Jul 14 11:52:43
2010
@@ -57,6 +57,7 @@ import org.apache.click.util.HtmlStringB
 import org.apache.click.util.PageImports;
 import org.apache.click.util.PropertyUtils;
 import org.apache.click.util.RequestTypeConverter;
+import org.apache.commons.collections.MapUtils;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.lang.ClassUtils;
 import org.apache.commons.lang.StringUtils;
@@ -941,7 +942,7 @@ public class ClickServlet extends HttpSe
 
         Writer writer = getWriter(response);
 
-        if (page.getHeaders() != null) {
+        if (page.hasHeaders()) {
             setPageResponseHeaders(response, page.getHeaders());
         }
 
@@ -1264,8 +1265,19 @@ public class ClickServlet extends HttpSe
 
             activatePageInstance(newPage);
 
-            if (newPage.getHeaders() == null) {
-                newPage.setHeaders(configService.getPageHeaders(path));
+            Map<String, Object> defaultHeaders = configService.getPageHeaders(path);
+            if (newPage.hasHeaders()) {
+
+                // Don't override existing headers
+                Map pageHeaders = newPage.getHeaders();
+                for (Map.Entry entry : defaultHeaders.entrySet()) {
+                    if (!pageHeaders.containsKey(entry.getKey())) {
+                        pageHeaders.put(entry.getKey(), entry.getValue());
+                    }
+                }
+
+            } else {
+                newPage.getHeaders().putAll(defaultHeaders);
             }
 
             newPage.setPath(path);
@@ -1497,9 +1509,13 @@ public class ClickServlet extends HttpSe
                 long time = ((Date) value).getTime();
                 response.setDateHeader(name, time);
 
-            } else {
+            } else if (value instanceof Integer) {
                 int intValue = (Integer) value;
                 response.setIntHeader(name, intValue);
+
+            } else if (value != null) {
+                throw new IllegalStateException("Invalid Page header value type: "
+                    + value.getClass() + ". Header value must of type String, Date or Integer.");
             }
         }
     }

Modified: click/trunk/click/framework/src/org/apache/click/Page.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/Page.java?rev=964015&r1=964014&r2=964015&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/Page.java (original)
+++ click/trunk/click/framework/src/org/apache/click/Page.java Wed Jul 14 11:52:43 2010
@@ -174,9 +174,6 @@ public class Page implements Serializabl
     /** The HTTP response headers. */
     protected Map<String, Object> headers;
 
-    /** The headers have been edited flag, to support copy on write. */
-    protected boolean headersEdited;
-
     /** The map of localized page resource messages. **/
     protected transient Map<String, String> messages;
 
@@ -594,32 +591,38 @@ public class Page implements Serializabl
 
     /**
      * Return the map of HTTP header to be set in the HttpServletResponse.
-     * Note to edit header values use {@link #setHeader(String, Object)} as
-     * headers Map is initially unmodifiable.
      *
      * @return the map of HTTP header to be set in the HttpServletResponse
      */
     public Map<String, Object> getHeaders() {
+        if (headers == null) {
+            headers = new HashMap<String, Object>();
+        }
         return headers;
     }
 
     /**
-     * Set the named header with the given value. This method uses copy on
-     * write to the headers Map, as the initial loaded headers Map is
-     * unmodifiable.
+     * Return true if the page has headers, false otherwise.
+     *
+     * @return true if the page has headers, false otherwise
+     */
+    public boolean hasHeaders() {
+        return headers != null && !headers.isEmpty();
+    }
+
+    /**
+     * Set the named header with the given value. Value can be either a String,
+     * Date or Integer.
      *
      * @param name the name of the header
-     * @param value the value of the header
+     * @param value the value of the header, either a String, Date or Integer
      */
     public void setHeader(String name, Object value) {
         if (name == null) {
             throw new IllegalArgumentException("Null header name parameter");
         }
-        if (!headersEdited) {
-            headersEdited = true;
-            headers = new HashMap<String, Object>(headers);
-        }
-        headers.put(name, value);
+
+        getHeaders().put(name, value);
     }
 
     /**

Modified: click/trunk/click/framework/test/org/apache/click/PageTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/PageTest.java?rev=964015&r1=964014&r2=964015&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/PageTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/PageTest.java Wed Jul 14 11:52:43 2010
@@ -20,6 +20,7 @@ package org.apache.click;
 
 import junit.framework.TestCase;
 import org.apache.click.control.Form;
+import org.apache.click.pages.HeaderTestPage;
 import org.apache.click.pages.JspRedirectPage;
 import org.apache.click.pages.RedirectToHtm;
 import org.apache.click.pages.RedirectToJsp;
@@ -199,4 +200,42 @@ public class PageTest extends TestCase {
         assertSame(child1, page.getControls().get(0));
         assertSame(child2, page.getControls().get(1));
     }
+
+    /**
+     * Check that headers can be set during page constructor
+     *
+     * CLK-711
+     */
+    public void testSetPageHeaders() {
+        MockContext.initContext();
+
+        HeaderTestPage headerTestPage = new HeaderTestPage();
+        assertEquals(headerTestPage.getHeaders().get(headerTestPage.expiresHeader), headerTestPage.expiresValue);
+    }
+
+    /**
+     * Check that headers can be set during page constructor and that default
+     * headers won't override the explicitly set headers.
+     *
+     * CLK-711
+     */
+    public void testOverrideDefaultHeaders() {
+        MockContainer container = new MockContainer("web");
+        container.start();
+
+        HeaderTestPage headerTestPage = container.testPage(HeaderTestPage.class);
+
+        System.out.println(headerTestPage.getHeaders());
+
+        // Check that 'Expires' header (a default header) has been set by Page
+        // and not been overridden by ClickServlet
+        assertEquals(headerTestPage.getHeaders().get(headerTestPage.expiresHeader), headerTestPage.expiresValue);
+
+        // A Default header. In this test we check that this header is added to the page
headers
+        String pragmaHeader = "Pragma";
+        String pragmaValue = "no-cache";
+        assertEquals(headerTestPage.getHeaders().get(pragmaHeader), pragmaValue);
+
+        container.stop();
+    }
 }

Added: click/trunk/click/framework/test/org/apache/click/pages/HeaderTestPage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/pages/HeaderTestPage.java?rev=964015&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/pages/HeaderTestPage.java (added)
+++ click/trunk/click/framework/test/org/apache/click/pages/HeaderTestPage.java Wed Jul 14
11:52:43 2010
@@ -0,0 +1,32 @@
+/*
+ * 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 org.apache.click.Page;
+
+public class HeaderTestPage extends Page {
+
+    public String expiresHeader = "Expires";
+
+    public String expiresValue = "custom";
+
+    public HeaderTestPage() {
+        setHeader(expiresHeader, expiresValue);
+    }
+}

Added: click/trunk/click/framework/test/web/header-test.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/web/header-test.htm?rev=964015&view=auto
==============================================================================
--- click/trunk/click/framework/test/web/header-test.htm (added)
+++ click/trunk/click/framework/test/web/header-test.htm Wed Jul 14 11:52:43 2010
@@ -0,0 +1,18 @@
+<!--
+   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.
+-->
\ No newline at end of file



Mime
View raw message