You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2010/12/19 21:41:59 UTC

svn commit: r1050942 - in /wicket/trunk/wicket/src/main/java/org/apache/wicket: ajax/AjaxRequestTarget.java resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java

Author: mgrigorov
Date: Sun Dec 19 20:41:59 2010
New Revision: 1050942

URL: http://svn.apache.org/viewvc?rev=1050942&view=rev
Log:
Merge r1049685 and r1050058 from 1.4.x

original-author: jthomerson

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java?rev=1050942&r1=1050941&r2=1050942&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java Sun Dec 19 20:41:59 2010
@@ -715,7 +715,51 @@ public class AjaxRequestTarget implement
 
 		if (header != null)
 		{
+			// some header responses buffer all calls to render*** until close is called.
+			// when they are closed, they do something (i.e. aggregate all JS resource urls to a
+			// single url), and then "flush" (by writing to the real response) before closing.
+			// to support this, we need to allow header contributions to be written in the close
+			// tag, which we do here:
+			headerRendering = true;
+			// save old response, set new
+			Response oldResponse = RequestCycle.get().setResponse(encodingHeaderResponse);
+			encodingHeaderResponse.reset();
+
+			// now, close the response (which may render things)
 			header.getHeaderResponse().close();
+
+			// revert to old response
+			RequestCycle.get().setResponse(oldResponse);
+
+			// write the XML tags and we're done
+			writeHeaderContribution(response);
+			headerRendering = false;
+		}
+	}
+
+	private void writeHeaderContribution(Response response)
+	{
+		if (encodingHeaderResponse.getContents().length() != 0)
+		{
+			response.write("<header-contribution");
+
+			if (encodingHeaderResponse.isContentsEncoded())
+			{
+				response.write(" encoding=\"");
+				response.write(getEncodingName());
+				response.write("\" ");
+			}
+
+			// we need to write response as CDATA and parse it on client,
+			// because
+			// konqueror crashes when there is a <script> element
+			response.write("><![CDATA[<head xmlns:wicket=\"http://wicket.apache.org\">");
+
+			response.write(encodingHeaderResponse.getContents());
+
+			response.write("</head>]]>");
+
+			response.write("</header-contribution>");
 		}
 	}
 
@@ -1080,7 +1124,7 @@ public class AjaxRequestTarget implement
 	 * 
 	 * Beware that only renderOnDomReadyJavaScript and renderOnLoadJavaScript can be called outside
 	 * the renderHeader(IHeaderResponse response) method. Calls to other render** methods will
-	 * result in an exception being thrown.
+	 * result in the call failing with a debug-level log statement to help you see why it failed.
 	 * 
 	 * @return header response
 	 */
@@ -1088,7 +1132,9 @@ public class AjaxRequestTarget implement
 	{
 		if (headerResponse == null)
 		{
-			headerResponse = Application.get().decorateHeaderResponse(new AjaxHeaderResponse());
+			// we don't need to decorate the header response here because this is called from
+			// within AjaxHtmlHeaderContainer, which decorates the response
+			headerResponse = new AjaxHeaderResponse();
 		}
 		return headerResponse;
 	}
@@ -1172,31 +1218,9 @@ public class AjaxRequestTarget implement
 		}
 
 		// revert to old response
-
 		RequestCycle.get().setResponse(oldResponse);
 
-		if (encodingHeaderResponse.getContents().length() != 0)
-		{
-			response.write("<header-contribution");
-
-			if (encodingHeaderResponse.isContentsEncoded())
-			{
-				response.write(" encoding=\"");
-				response.write(getEncodingName());
-				response.write("\" ");
-			}
-
-			// we need to write response as CDATA and parse it on client,
-			// because
-			// konqueror crashes when there is a <script> element
-			response.write("><![CDATA[<head xmlns:wicket=\"http://wicket.apache.org\">");
-
-			response.write(encodingHeaderResponse.getContents());
-
-			response.write("</head>]]>");
-
-			response.write("</header-contribution>");
-		}
+		writeHeaderContribution(response);
 
 		headerRendering = false;
 	}

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java?rev=1050942&r1=1050941&r2=1050942&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/filtering/HeaderResponseContainerFilteringHeaderResponse.java Sun Dec 19 20:41:59 2010
@@ -378,15 +378,47 @@ public class HeaderResponseContainerFilt
 		log.warn("css was rendered to the filtering header response, but did not match any filters, so it was effectively lost.  Make sure that you have filters that accept every possible case or else configure a default filter that returns true to all acceptance tests");
 	}
 
+	/**
+	 * If subclasses of this class have special cases where they force something into a particular
+	 * bucket, regardless of the filters, they can create a Runnable that renders to the real
+	 * response, and pass it to this method with the name of the filter (bucket) that they want it
+	 * to appear in.
+	 * 
+	 * Example: <code>
+	               public void renderJavascriptIntoHead(final String js, final String id) {
+	                       runWithFilter(new Runnable() {
+	                               public void run()
+	                               {
+	                                       getRealResponse().renderJavascript(js, id);
+	                               }
+	                       }, "headerBucket");
+	               }
+	        * </code>
+	 * 
+	 * @param runnable
+	 *            the runnable that renders to the real response.
+	 * @param filterName
+	 *            the name of the filter bucket that you want the runnable to render into
+	 */
+	protected final void runWithFilter(Runnable runnable, String filterName)
+	{
+		run(runnable, responseFilterMap.get(filterName));
+	}
+
 	private void run(Runnable runnable, IHeaderResponseFilter filter)
 	{
+		run(runnable, responseFilterMap.get(filter.getName()));
+	}
+
+	private void run(Runnable runnable, Response response)
+	{
 		if (AjaxRequestTarget.get() != null)
 		{
 			// we're in an ajax request, so we don't filter and separate stuff....
 			runnable.run();
 			return;
 		}
-		Response original = RequestCycle.get().setResponse(responseFilterMap.get(filter.getName()));
+		Response original = RequestCycle.get().setResponse(response);
 		try
 		{
 			runnable.run();