org.apache.wicket.resource.aggregation
Class AbstractResourceAggregatingHeaderResponse<R extends ResourceReferenceCollection,K>

java.lang.Object
  extended by org.apache.wicket.markup.html.DecoratingHeaderResponse
      extended by org.apache.wicket.resource.aggregation.AbstractResourceAggregatingHeaderResponse<R,K>
Type Parameters:
R - the type of ResourceReferenceCollection returned by newResourceReferenceCollection(Object) and passed to all the methods that take a ResourceReferenceCollection. You will typically just use ResourceReferenceCollection for this param, unless you are returning a specific type of ResourceReferenceCollection from your subclass.
K - the class of the key that you will create from newGroupingKey(ResourceReferenceAndStringData)
All Implemented Interfaces:
Closeable, IHeaderResponse
Direct Known Subclasses:
AbstractDependencyRespectingResourceAggregatingHeaderResponse

public abstract class AbstractResourceAggregatingHeaderResponse<R extends ResourceReferenceCollection,K>
extends DecoratingHeaderResponse

A header response that can be used to aggregate resources (primarily resource references) into groups that can be rendered after the entire hierarchy of IHeaderContributors have been traversed. A subclass of this could use that group to render a single URL to some aggregating servlet (for example) that could cut down on the number of HTTP requests the client must make. Resource references are aggregated according to the key that your subclass creates in the newGroupingKey(ResourceReferenceAndStringData). This key is used in a Map, so it needs to properly implement hashCode and equals. If your key does not implement Comparable<KeyClass>, you need to also return a Comparator for it from the getGroupingKeyComparator() method.

Author:
Jeremy Thomerson

Constructor Summary
AbstractResourceAggregatingHeaderResponse(IHeaderResponse real)
          Construct.
 
Method Summary
 void close()
          Mark Header rendering is completed and subsequent usage will be ignored.
protected  Comparator<K> getGroupingKeyComparator()
          This comparator is used to sort the grouping keys that you return from newGroupingKey(ResourceReferenceAndStringData).
protected abstract  K newGroupingKey(ResourceReferenceAndStringData ref)
          This key is what is used to determine how to group (or aggregate) your resources.
protected  R newResourceReferenceCollection(K key)
          creates a ResourceReferenceCollection.
protected  void onAllCollectionsRendered(List<ResourceReferenceAndStringData> allTopLevelReferences)
          After all the collections have been rendered, we call this callback so your subclass can add any other logic as needed.
protected  void render(ResourceReferenceAndStringData data)
          Renders a single resource reference.
protected  void renderCollection(Set<ResourceReferenceAndStringData> alreadyRendered, K key, R coll)
          When the entire hierarchy has been traversed and close() is called, we loop through the grouped collections and render them in this method.
 void renderCSS(CharSequence css, String media)
          Renders CSS code to the response, if the CSS has not already been rendered.
 void renderCSSReference(ResourceReference reference)
          Writes a CSS reference, if the specified reference hasn't been rendered yet.
 void renderCSSReference(ResourceReference reference, PageParameters pageParameters, String media)
          Writes a CSS reference with query parameters, if the specified reference hasn't been rendered yet.
 void renderCSSReference(ResourceReference reference, PageParameters pageParameters, String media, String condition)
          Writes a conditional IE comment with a CSS reference with query parameters, if the specified reference hasn't been rendered yet.
 void renderCSSReference(ResourceReference reference, String media)
          Writes a CSS reference, if the specified reference hasn't been rendered yet.
 void renderCSSReference(String url)
          Writes a CSS reference, if the specified reference hasn't been rendered yet.
 void renderCSSReference(String url, String media)
          Writes a link to a CSS resource, if the specified url hasn't been rendered yet.
 void renderCSSReference(String url, String media, String condition)
          Writes a conditional IE comment for a link to a CSS resource, if the specified url hasn't been rendered yet.
protected  void renderIfNotAlreadyRendered(Set<ResourceReferenceAndStringData> alreadyRendered, ResourceReferenceAndStringData data)
          Renders a single resource reference, only if it has not already been rendered.
 void renderJavaScript(CharSequence javascript, String id)
          Renders javascript code to the response, if the javascript has not already been rendered.
 void renderJavaScriptReference(ResourceReference reference)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(ResourceReference reference, PageParameters parameters, String id)
          Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(ResourceReference reference, PageParameters parameters, String id, boolean defer)
          Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(ResourceReference reference, PageParameters parameters, String id, boolean defer, String charset)
          Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(ResourceReference reference, String id)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(String url)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(String url, String id)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(String url, String id, boolean defer)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderJavaScriptReference(String url, String id, boolean defer, String charset)
          Writes a javascript reference, if the specified reference hasn't been rendered yet.
 void renderOnDomReadyJavaScript(String javascript)
          Renders javascript that is executed right after the DOM is built, before external resources (e.g.
 void renderOnEventJavaScript(String target, String event, String javascript)
          Renders javascript that is executed after the given event happens on specified target
 void renderOnLoadJavaScript(String javascript)
          Renders javascript that is executed after the entire page is loaded.
 void renderString(CharSequence string)
          Renders an arbitrary string to the header.
 
Methods inherited from class org.apache.wicket.markup.html.DecoratingHeaderResponse
getRealResponse, getResponse, isClosed, markRendered, wasRendered
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractResourceAggregatingHeaderResponse

public AbstractResourceAggregatingHeaderResponse(IHeaderResponse real)
Construct.

Parameters:
real - the wrapped header response
Method Detail

renderJavaScriptReference

public void renderJavaScriptReference(ResourceReference reference)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the javascript resource

renderJavaScriptReference

public void renderJavaScriptReference(ResourceReference reference,
                                      String id)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the javascript resource
id - id that will be used to filter duplicate reference (it's still filtered by URL too)

renderCSSReference

public void renderCSSReference(ResourceReference reference)
Description copied from interface: IHeaderResponse
Writes a CSS reference, if the specified reference hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the CSS resource

renderCSSReference

public void renderCSSReference(ResourceReference reference,
                               String media)
Description copied from interface: IHeaderResponse
Writes a CSS reference, if the specified reference hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the CSS resource
media - the media type for this CSS ("print", "screen", etc.)

close

public void close()
Description copied from interface: IHeaderResponse
Mark Header rendering is completed and subsequent usage will be ignored. If some kind of buffering is used internally, this action will mark that the contents has to be flushed out.

Specified by:
close in interface Closeable
Specified by:
close in interface IHeaderResponse
Overrides:
close in class DecoratingHeaderResponse

newResourceReferenceCollection

protected R newResourceReferenceCollection(K key)
creates a ResourceReferenceCollection. If you want a specific type of ResourceReferenceCollection for your subclass of AbstractResourceAggregatingHeaderResponse, override this method. Note that because of the generics definition, you will probably have to cast to R. R is the parameter used when creating your subclass defining the type of ResourceReferenceCollection this returns and is passed into all methods that take a ResourceReferenceCollection

Parameters:
key - the grouping key that will be used for this collection. all references added to it will have the same key
Returns:
a newly created collection to contain resource references

newGroupingKey

protected abstract K newGroupingKey(ResourceReferenceAndStringData ref)
This key is what is used to determine how to group (or aggregate) your resources. It must implement equals and hashCode correctly so that it can be used as a key in a HashMap. These methods must be implemented so that if two references are given over the course of the hierarchy traversal, and those two references should be grouped (or aggregated), the keys returned for each should equal each other and their hash codes should be equal as well. Typical implementations should use whether or not the resource reference is CSS as their first grouping parameter, since you don't want to render JS and CSS in the same tag (one needs to be in a link tag and one in a script tag). Note that if your grouping key class (K) does not implement Comparable<K>, you must also override getGroupingKeyComparator() and return a valid comparator that sorts keys in the order you want references rendered.

Parameters:
ref - the resource reference with associated data that came from the render*Reference methods
Returns:
a new key used to group the references.

getGroupingKeyComparator

protected Comparator<K> getGroupingKeyComparator()
This comparator is used to sort the grouping keys that you return from newGroupingKey(ResourceReferenceAndStringData). Note that if your grouping key class (K) implements Comparable<K>, you do not need to override this method.

Returns:
a Comparator for K

renderCollection

protected void renderCollection(Set<ResourceReferenceAndStringData> alreadyRendered,
                                K key,
                                R coll)
When the entire hierarchy has been traversed and close() is called, we loop through the grouped collections and render them in this method. This method is typically overridden to render your collection how you want to render them. For instance, if you want to aggregate your groups into a single HTTP request, you can override this method, create the URL to your aggregation servlet (or IResource), and then call getRealResponse().renderJavaScriptReference(yourUrl), or the appropriate method to render the URL for a group of CSS references.

Parameters:
alreadyRendered - a set of resource references that have already been rendered in other groups
key - they grouping key for this group
coll - the collection of resource references to render

renderIfNotAlreadyRendered

protected void renderIfNotAlreadyRendered(Set<ResourceReferenceAndStringData> alreadyRendered,
                                          ResourceReferenceAndStringData data)
Renders a single resource reference, only if it has not already been rendered. Note that you will typically not need to override this method. You should typically override render(ResourceReferenceAndStringData) directly, which is called from this method if the resource reference has not been rendered elsewhere.

Parameters:
alreadyRendered - the set of references that have already been rendered in other groups
data - the reference (and associated data) to conditionally render.

render

protected void render(ResourceReferenceAndStringData data)
Renders a single resource reference. This is called from renderIfNotAlreadyRendered(Set, ResourceReferenceAndStringData) for references that had not been rendered elsewhere.

Parameters:
data - the reference (and associated data) to conditionally render.

onAllCollectionsRendered

protected void onAllCollectionsRendered(List<ResourceReferenceAndStringData> allTopLevelReferences)
After all the collections have been rendered, we call this callback so your subclass can add any other logic as needed. For instance, if you are aggregating YUI resources, your renderCollection(Set, Object, ResourceReferenceCollection) method might have rendered only a YUI constructor that loaded all the JS files for each group. Then, you need to loop through the references again, and render any JS inside a sandboxed YUI.use() statement. You would render those here by creating the YUI.use statement, and call getHeaderResponse().renderJavaScript(yourJS, null)

Parameters:
allTopLevelReferences - all the references that were rendered by the developers

renderJavaScriptReference

public void renderJavaScriptReference(String url)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the the javascript resource

renderJavaScriptReference

public void renderJavaScriptReference(String url,
                                      String id)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the the javascript resource
id - id that will be used to filter duplicate reference (it's still filtered by URL too)

renderCSSReference

public void renderCSSReference(String url)
Description copied from interface: IHeaderResponse
Writes a CSS reference, if the specified reference hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the CSS resource

renderCSSReference

public void renderCSSReference(String url,
                               String media)
Description copied from interface: IHeaderResponse
Writes a link to a CSS resource, if the specified url hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the CSS resource
media - the media type for this CSS ("print", "screen", etc.)

renderJavaScriptReference

public void renderJavaScriptReference(ResourceReference reference,
                                      PageParameters parameters,
                                      String id)
Description copied from interface: IHeaderResponse
Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the javascript resource
parameters - the parameters for this Javascript resource reference
id - id that will be used to filter duplicate reference (it's still filtered by URL too)

renderJavaScriptReference

public void renderJavaScriptReference(ResourceReference reference,
                                      PageParameters parameters,
                                      String id,
                                      boolean defer)
Description copied from interface: IHeaderResponse
Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the javascript resource
parameters - the parameters for this Javascript resource reference
id - id that will be used to filter duplicate reference (it's still filtered by URL too)
defer - specifies that the execution of a script should be deferred (delayed) until after the page has been loaded.

renderJavaScriptReference

public void renderJavaScriptReference(ResourceReference reference,
                                      PageParameters parameters,
                                      String id,
                                      boolean defer,
                                      String charset)
Description copied from interface: IHeaderResponse
Writes a javascript reference with query parameters, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the javascript resource
parameters - the parameters for this Javascript resource reference
id - id that will be used to filter duplicate reference (it's still filtered by URL too)
defer - specifies that the execution of a script should be deferred (delayed) until after the page has been loaded.
charset - a non null value specifies the charset attribute of the script tag

renderJavaScriptReference

public void renderJavaScriptReference(String url,
                                      String id,
                                      boolean defer)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the the javascript resource
id - id that will be used to filter duplicate reference (it's still filtered by URL too)
defer - specifies that the execution of a script should be deferred (delayed) until after the page has been loaded.

renderJavaScriptReference

public void renderJavaScriptReference(String url,
                                      String id,
                                      boolean defer,
                                      String charset)
Description copied from interface: IHeaderResponse
Writes a javascript reference, if the specified reference hasn't been rendered yet.

Specified by:
renderJavaScriptReference in interface IHeaderResponse
Overrides:
renderJavaScriptReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the the javascript resource
id - id that will be used to filter duplicate reference (it's still filtered by URL too)
defer - specifies that the execution of a script should be deferred (delayed) until after the page has been loaded.
charset - a non null value specifies the charset attribute of the script tag

renderJavaScript

public void renderJavaScript(CharSequence javascript,
                             String id)
Description copied from interface: IHeaderResponse
Renders javascript code to the response, if the javascript has not already been rendered. the necessary surrounding script tags will be added to the output.

Specified by:
renderJavaScript in interface IHeaderResponse
Overrides:
renderJavaScript in class DecoratingHeaderResponse
Parameters:
javascript - javascript content to be rendered.
id - unique id for the javascript element. This can be null, however in that case the ajax header contribution can't detect duplicate script fragments.

renderCSS

public void renderCSS(CharSequence css,
                      String media)
Description copied from interface: IHeaderResponse
Renders CSS code to the response, if the CSS has not already been rendered. the necessary surrounding <style> tags will be added to the output.

Specified by:
renderCSS in interface IHeaderResponse
Overrides:
renderCSS in class DecoratingHeaderResponse
Parameters:
css - css content to be rendered.
media - unique id for the <style> element. This can be null, however in that case the ajax header contribution can't detect duplicate CSS fragments.

renderCSSReference

public void renderCSSReference(ResourceReference reference,
                               PageParameters pageParameters,
                               String media)
Description copied from interface: IHeaderResponse
Writes a CSS reference with query parameters, if the specified reference hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the CSS resource
pageParameters - the parameters for this CSS resource reference
media - the media type for this CSS ("print", "screen", etc.)

renderCSSReference

public void renderCSSReference(ResourceReference reference,
                               PageParameters pageParameters,
                               String media,
                               String condition)
Description copied from interface: IHeaderResponse
Writes a conditional IE comment with a CSS reference with query parameters, if the specified reference hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
reference - resource reference pointing to the CSS resource
pageParameters - the parameters for this CSS resource reference
media - the media type for this CSS ("print", "screen", etc.)
condition - the condition to use for Internet Explorer conditional comments. E.g. "IE 7".

renderCSSReference

public void renderCSSReference(String url,
                               String media,
                               String condition)
Description copied from interface: IHeaderResponse
Writes a conditional IE comment for a link to a CSS resource, if the specified url hasn't been rendered yet.

Specified by:
renderCSSReference in interface IHeaderResponse
Overrides:
renderCSSReference in class DecoratingHeaderResponse
Parameters:
url - context-relative url of the CSS resource
media - the media type for this CSS ("print", "screen", etc.)
condition - the condition to use for Internet Explorer conditional comments. E.g. "IE 7".

renderString

public void renderString(CharSequence string)
Description copied from interface: IHeaderResponse
Renders an arbitrary string to the header. The string is only rendered if the same string hasn't been rendered before.

Note: This method is kind of dangerous as users are able to write to the output whatever they like.

Specified by:
renderString in interface IHeaderResponse
Overrides:
renderString in class DecoratingHeaderResponse
Parameters:
string - string to be rendered to head

renderOnDomReadyJavaScript

public void renderOnDomReadyJavaScript(String javascript)
Description copied from interface: IHeaderResponse
Renders javascript that is executed right after the DOM is built, before external resources (e.g. images) are loaded.

Specified by:
renderOnDomReadyJavaScript in interface IHeaderResponse
Overrides:
renderOnDomReadyJavaScript in class DecoratingHeaderResponse

renderOnLoadJavaScript

public void renderOnLoadJavaScript(String javascript)
Description copied from interface: IHeaderResponse
Renders javascript that is executed after the entire page is loaded.

Specified by:
renderOnLoadJavaScript in interface IHeaderResponse
Overrides:
renderOnLoadJavaScript in class DecoratingHeaderResponse

renderOnEventJavaScript

public void renderOnEventJavaScript(String target,
                                    String event,
                                    String javascript)
Description copied from interface: IHeaderResponse
Renders javascript that is executed after the given event happens on specified target

Specified by:
renderOnEventJavaScript in interface IHeaderResponse
Overrides:
renderOnEventJavaScript in class DecoratingHeaderResponse


Copyright © 2006-2012 Apache Software Foundation. All Rights Reserved.