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() 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:
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()
          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 renderCSSReference(ResourceReference reference)
          Writes a CSS reference, 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 CSS reference, if the specified reference 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 renderJavascriptReference(ResourceReference reference)
          Writes a javascript reference, 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.
 
Methods inherited from class org.apache.wicket.markup.html.DecoratingHeaderResponse
getRealResponse, getResponse, isClosed, markRendered, renderJavascript, renderOnDomReadyJavascript, renderOnEventJavascript, renderOnLoadJavascript, renderString, 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 IHeaderResponse
Overrides:
close in class DecoratingHeaderResponse

newResourceReferenceCollection

protected R newResourceReferenceCollection()
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

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 Resource), 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 - 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 - 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 - url of the CSS resource

renderCSSReference

public void renderCSSReference(String url,
                               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:
url - url of the CSS resource
media - the media type for this CSS ("print", "screen", etc.)


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.