Author: jrthomerson
Date: Wed Nov  3 19:24:07 2010
New Revision: 1030625

URL: http://svn.apache.org/viewvc?rev=1030625&view=rev
Log:
add the ability to decorate all IHeaderResponses created by Wicket.

This gives you a convenient place to wrap an IHeaderResponse in a separate 
implementation
that incrementally adds functionality.  Examples of functionality that can be 
added:

1 - you could wrap all calls to renderJavascriptReference and catch any JS 
reference that is
    added to the header response, and see if those resource references 
(externally, in some 
    ancillary configuration service) are dependent on other resource 
references.  Then, you can
    add the original reference and all dependent references to the real header 
response that
    you are delegating to.

2 - you also could wrap all calls that append javascript and cache them rather 
than immediately
    writing them out, and then write them in the footer instead of the header, 
without having to
    add a new notion of IFooterListener or similar

NOTE: these functionalities are NOT part of this commit (and likely wouldn't go 
in core).  This
commit is simply about adding a hook that makes it possible for you to do these 
thing.


Added:
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/DecoratingHeaderResponse.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/IHeaderResponseDecorator.java
Modified:
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HeaderResponse.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java?rev=1030625&r1=1030624&r2=1030625&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
 Wed Nov  3 19:24:07 2010
@@ -38,6 +38,7 @@ import org.apache.wicket.markup.IMarkupC
 import org.apache.wicket.markup.html.EmptySrcAttributeCheckFilter;
 import org.apache.wicket.markup.html.IHeaderContributor;
 import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.markup.html.IHeaderResponseDecorator;
 import 
org.apache.wicket.markup.html.image.resource.DefaultButtonImageResourceFactory;
 import org.apache.wicket.markup.parser.filter.RelativePathPrefixHandler;
 import org.apache.wicket.markup.parser.filter.WicketMessageTagHandler;
@@ -158,6 +159,11 @@ public abstract class Application
        private List<IHeaderContributor> renderHeadListeners;
 
        /**
+        * The decorator this application uses to decorate any header responses 
created by Wicket
+        */
+       private IHeaderResponseDecorator headerResponseDecorator;
+
+       /**
         * Checks if the <code>Application</code> threadlocal is set in this 
thread
         * 
         * @return true if {...@link Application#get()} can return the instance 
of application, false
@@ -238,7 +244,7 @@ public abstract class Application
        private IComponentInstantiationListener[] 
componentInstantiationListeners = new IComponentInstantiationListener[0];
 
        /** list of {...@link IComponentInitializationListener}s. */
-       private CopyOnWriteArrayList<IComponentInitializationListener> 
componentInitializationListeners = new 
CopyOnWriteArrayList<IComponentInitializationListener>();
+       private final CopyOnWriteArrayList<IComponentInitializationListener> 
componentInitializationListeners = new 
CopyOnWriteArrayList<IComponentInitializationListener>();
 
        /** The converter locator instance. */
        private IConverterLocator converterLocator;
@@ -1299,4 +1305,37 @@ public abstract class Application
                        }
                }
        }
+
+       /**
+        * Sets an {...@link IHeaderResponseDecorator} that you want your 
application to use to decorate
+        * header responses.
+        * 
+        * @param headerResponseDecorator
+        *            your custom decorator
+        */
+       public void setHeaderResponseDecorator(IHeaderResponseDecorator 
headerResponseDecorator)
+       {
+               this.headerResponseDecorator = headerResponseDecorator;
+       }
+
+       /**
+        * INTERNAL METHOD - You shouldn't need to call this. This is called 
every time Wicket creates
+        * an IHeaderResponse. It gives you the ability to incrementally add 
features to an
+        * IHeaderResponse implementation by wrapping it in another 
implementation.
+        * 
+        * To decorate an IHeaderResponse in your application, set the 
{...@link IHeaderResponseDecorator}
+        * on the application.
+        * 
+        * @see IHeaderResponseDecorator
+        * @param response
+        *            the response Wicket created
+        * @return the response Wicket should use in IHeaderContributor 
traversal
+        */
+       public final IHeaderResponse decorateHeaderResponse(IHeaderResponse 
response)
+       {
+               IHeaderResponse hr = headerResponseDecorator == null ? response
+                       : headerResponseDecorator.decorate(response);
+               notifyRenderHeadListener(hr);
+               return hr;
+       }
 }

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java?rev=1030625&r1=1030624&r2=1030625&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
 Wed Nov  3 19:24:07 2010
@@ -36,7 +36,6 @@ import org.apache.wicket.Page;
 import org.apache.wicket.RequestCycle;
 import org.apache.wicket.ResourceReference;
 import org.apache.wicket.Response;
-import org.apache.wicket.ajax.AjaxRequestTarget.IListener;
 import org.apache.wicket.behavior.IBehavior;
 import org.apache.wicket.markup.html.IHeaderResponse;
 import org.apache.wicket.markup.html.internal.HeaderResponse;
@@ -1061,7 +1060,7 @@ public class AjaxRequestTarget implement
        {
                if (headerResponse == null)
                {
-                       headerResponse = new AjaxHeaderResponse();
+                       headerResponse = 
Application.get().decorateHeaderResponse(new AjaxHeaderResponse());
                }
                return headerResponse;
        }

Added: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/DecoratingHeaderResponse.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/DecoratingHeaderResponse.java?rev=1030625&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/DecoratingHeaderResponse.java
 (added)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/DecoratingHeaderResponse.java
 Wed Nov  3 19:24:07 2010
@@ -0,0 +1,138 @@
+/*
+ * 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.wicket.markup.html;
+
+import org.apache.wicket.ResourceReference;
+import org.apache.wicket.Response;
+
+/**
+ * This is simply a helper implementation of IHeaderResponse that really 
delegates all of its method
+ * calls to the IHeaderResponse that is passed into the constructor. It is 
defined as abstract
+ * because it's only meant to be extended and not used a la carte. You can 
extend it and override
+ * only the methods that you want to change the functionality of.
+ * 
+ * @see IHeaderResponseDecorator
+ * @see IHeaderResponse
+ * @author Jeremy Thomerson
+ */
+public abstract class DecoratingHeaderResponse implements IHeaderResponse
+{
+
+       private final IHeaderResponse realResponse;
+
+       /**
+        * Create a header response that simply delegates all methods to the 
one that is passed in here.
+        * 
+        * @param real
+        *            the actual response that this class delegates to by 
default
+        */
+       public DecoratingHeaderResponse(IHeaderResponse real)
+       {
+               realResponse = real;
+       }
+
+       public void renderJavascriptReference(ResourceReference reference)
+       {
+               realResponse.renderJavascriptReference(reference);
+       }
+
+       public void renderJavascriptReference(ResourceReference reference, 
String id)
+       {
+               realResponse.renderJavascriptReference(reference, id);
+       }
+
+       public void renderJavascriptReference(String url)
+       {
+               realResponse.renderJavascriptReference(url);
+       }
+
+       public void renderJavascriptReference(String url, String id)
+       {
+               realResponse.renderJavascriptReference(url, id);
+       }
+
+       public void renderJavascript(CharSequence javascript, String id)
+       {
+               realResponse.renderJavascript(javascript, id);
+       }
+
+       public void renderCSSReference(ResourceReference reference)
+       {
+               realResponse.renderCSSReference(reference);
+       }
+
+       public void renderCSSReference(String url)
+       {
+               realResponse.renderCSSReference(url);
+       }
+
+       public void renderCSSReference(ResourceReference reference, String 
media)
+       {
+               realResponse.renderCSSReference(reference, media);
+       }
+
+       public void renderCSSReference(String url, String media)
+       {
+               realResponse.renderCSSReference(url, media);
+       }
+
+       public void renderString(CharSequence string)
+       {
+               realResponse.renderString(string);
+       }
+
+       public void markRendered(Object object)
+       {
+               realResponse.markRendered(object);
+       }
+
+       public boolean wasRendered(Object object)
+       {
+               return realResponse.wasRendered(object);
+       }
+
+       public Response getResponse()
+       {
+               return realResponse.getResponse();
+       }
+
+       public void renderOnDomReadyJavascript(String javascript)
+       {
+               realResponse.renderOnDomReadyJavascript(javascript);
+       }
+
+       public void renderOnLoadJavascript(String javascript)
+       {
+               realResponse.renderOnLoadJavascript(javascript);
+       }
+
+       public void renderOnEventJavascript(String target, String event, String 
javascript)
+       {
+               realResponse.renderOnEventJavascript(target, event, javascript);
+       }
+
+       public void close()
+       {
+               realResponse.close();
+       }
+
+       public boolean isClosed()
+       {
+               return realResponse.isClosed();
+       }
+
+}

Added: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/IHeaderResponseDecorator.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/IHeaderResponseDecorator.java?rev=1030625&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/IHeaderResponseDecorator.java
 (added)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/IHeaderResponseDecorator.java
 Wed Nov  3 19:24:07 2010
@@ -0,0 +1,42 @@
+/*
+ * 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.wicket.markup.html;
+
+/**
+ * Setting an IHeaderResponseDecorator on an application allows you to wrap 
any IHeaderResponse
+ * created by Wicket in a separate implementation that incrementally adds 
functionality to the
+ * IHeaderResponse that is used by all IHeaderContributor components or 
behaviors.
+ * 
+ * Everywhere that Wicket creates an instance of IHeaderResponse, it will call 
to your application
+ * and give it the opportunity to decorate that IHeaderResponse before using 
it.
+ * 
+ * @see IHeaderResponse
+ * @see DecoratingHeaderResponse
+ * @author Jeremy Thomerson
+ */
+public interface IHeaderResponseDecorator
+{
+
+       /**
+        * The method that does the decorating of the IHeaderResponse.
+        * 
+        * @param response
+        *            the original response created by Wicket
+        * @return the response to be used by IHeaderContributors
+        */
+       IHeaderResponse decorate(IHeaderResponse response);
+}

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HeaderResponse.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HeaderResponse.java?rev=1030625&r1=1030624&r2=1030625&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HeaderResponse.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HeaderResponse.java
 Wed Nov  3 19:24:07 2010
@@ -21,7 +21,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.wicket.Application;
 import org.apache.wicket.RequestCycle;
 import org.apache.wicket.ResourceReference;
 import org.apache.wicket.Response;
@@ -51,10 +50,6 @@ public abstract class HeaderResponse imp
         */
        public HeaderResponse()
        {
-               if (Application.exists())
-               {
-                       Application.get().notifyRenderHeadListener(this);
-               }
        }
 
        /**

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java?rev=1030625&r1=1030624&r2=1030625&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java
 Wed Nov  3 19:24:07 2010
@@ -321,7 +321,7 @@ public class HtmlHeaderContainer extends
        {
                if (headerResponse == null)
                {
-                       headerResponse = newHeaderResponse();
+                       headerResponse = 
getApplication().decorateHeaderResponse(newHeaderResponse());
                }
                return headerResponse;
        }


Reply via email to