You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2008/12/21 11:40:20 UTC

svn commit: r728423 - /wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java

Author: jdonnerstag
Date: Sun Dec 21 02:40:20 2008
New Revision: 728423

URL: http://svn.apache.org/viewvc?rev=728423&view=rev
Log:
wicket-1718 fixed: WebPage#onAfterRender erroneously reports missing header

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java?rev=728423&r1=728422&r2=728423&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java Sun Dec 21 02:40:20 2008
@@ -64,6 +64,11 @@
  */
 public class WebPage extends Page implements INewBrowserWindowListener
 {
+	/** log. */
+	private static final Logger log = LoggerFactory.getLogger(WebPage.class);
+
+	private static final long serialVersionUID = 1L;
+
 	/**
 	 * Tries to determine whether this page was opened in a new window or tab. If it is (and this
 	 * checker were able to recognize that), a new page map is created for this page instance, so
@@ -162,15 +167,10 @@
 		}
 	}
 
-	/** log. */
-	private static final Logger log = LoggerFactory.getLogger(WebPage.class);
-
 	/** The resource references used for new window/tab support */
 	private static ResourceReference cookiesResource = new ResourceReference(WebPage.class,
 		"cookies.js");
 
-	private static final long serialVersionUID = 1L;
-
 	/**
 	 * The url compressor that will compress the urls by collapsing the component path and listener
 	 * interface
@@ -374,7 +374,6 @@
 		return (WebRequestCycle)getRequestCycle();
 	}
 
-
 	/**
 	 * Creates and returns a bookmarkable link to this application's home page.
 	 * 
@@ -387,78 +386,99 @@
 		return new BookmarkablePageLink(id, getApplication().getHomePage());
 	}
 
+	/**
+	 * 
+	 * @see org.apache.wicket.Component#onAfterRender()
+	 */
 	@Override
 	protected void onAfterRender()
 	{
 		super.onAfterRender();
+
+		// only in development mode validate the headers
 		if (Application.DEVELOPMENT.equals(getApplication().getConfigurationType()))
 		{
-			HtmlHeaderContainer header = (HtmlHeaderContainer)visitChildren(new IVisitor<Component>()
+			// Ignore if an exception and a redirect happened in between (e.g.
+			// RestartResponseAtInterceptPageException)
+			if (getRequestCycle().getResponsePage() == this)
 			{
-				public Object component(Component component)
+				validateHeaders();
+			}
+		}
+	}
+
+	/**
+	 * Validate that each component which wanted to contribute to the header section actually was
+	 * able to do so.
+	 */
+	private void validateHeaders()
+	{
+		HtmlHeaderContainer header = (HtmlHeaderContainer)visitChildren(new IVisitor<Component>()
+		{
+			public Object component(Component component)
+			{
+				if (component instanceof HtmlHeaderContainer)
 				{
-					if (component instanceof HtmlHeaderContainer)
-					{
-						return component;
-					}
-					return IVisitor.CONTINUE_TRAVERSAL;
+					return component;
 				}
-			});
-			if (header == null)
-			{
-				// the markup must at least contain a <body> tag for wicket to automatically
-				// create a HtmlHeaderContainer. Log an error if no header container
-				// was created but any of the components or behavior want to contribute
-				// something to the header.
-				header = new HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
-				add(header);
+				return IVisitor.CONTINUE_TRAVERSAL;
+			}
+		});
 
-				Response orgResponse = getRequestCycle().getResponse();
-				try
-				{
-					final StringResponse response = new StringResponse();
-					getRequestCycle().setResponse(response);
+		if (header == null)
+		{
+			// the markup must at least contain a <body> tag for wicket to automatically
+			// create a HtmlHeaderContainer. Log an error if no header container
+			// was created but any of the components or behaviors want to contribute
+			// something to the header.
+			header = new HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
+			add(header);
 
-					// Render all header sections of all components on the page
-					renderHead(header);
+			Response orgResponse = getRequestCycle().getResponse();
+			try
+			{
+				final StringResponse response = new StringResponse();
+				getRequestCycle().setResponse(response);
 
-					// Make sure all Components interested in contributing to the header
-					// and there attached behaviors are asked.
-					final HtmlHeaderContainer finalHeader = header;
-					visitChildren(new IVisitor<Component>()
-					{
-						/**
-						 * @see org.apache.wicket.Component.IVisitor#component(org.apache.wicket.Component)
-						 */
-						public Object component(Component component)
-						{
-							component.renderHead(finalHeader);
-							return CONTINUE_TRAVERSAL;
-						}
-					});
-					response.close();
+				// Render all header sections of all components on the page
+				renderHead(header);
 
-					if (response.getBuffer().length() > 0)
+				// Make sure all Components interested in contributing to the header
+				// and there attached behaviors are asked.
+				final HtmlHeaderContainer finalHeader = header;
+				visitChildren(new IVisitor<Component>()
+				{
+					/**
+					 * @see org.apache.wicket.Component.IVisitor#component(org.apache.wicket.Component)
+					 */
+					public Object component(Component component)
 					{
-						// @TODO it is not yet working properly. JDo to fix it
-						log.error("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
-						log.error("You probably forgot to add a <body> or <header> tag to your markup since no Header Container was \n" +
-							"found but components where found which want to write to the <head> section.\n" +
-							response.getBuffer());
-						log.error("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
+						component.renderHead(finalHeader);
+						return CONTINUE_TRAVERSAL;
 					}
-				}
-				catch (Exception e)
-				{
-					// just swallow this exception, there isn't much we can do about.
-					log.error("header/body check throws exception", e);
-				}
-				finally
+				});
+				response.close();
+
+				if (response.getBuffer().length() > 0)
 				{
-					this.remove(header);
-					getRequestCycle().setResponse(orgResponse);
+					// @TODO it is not yet working properly. JDo to fix it
+					log.error("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
+					log.error("You probably forgot to add a <body> or <header> tag to your markup since no Header Container was \n" +
+						"found but components where found which want to write to the <head> section.\n" +
+						response.getBuffer());
+					log.error("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
 				}
 			}
+			catch (Exception e)
+			{
+				// just swallow this exception, there isn't much we can do about.
+				log.error("header/body check throws exception", e);
+			}
+			finally
+			{
+				this.remove(header);
+				getRequestCycle().setResponse(orgResponse);
+			}
 		}
 	}
 }