Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WICKET-4161 AbstractResourceAggregatingHeaderResponse does not order …

…javascript properly

Make it possible to group js/css url or content, not just resource references.



git-svn-id: https://svn.apache.org/repos/asf/wicket/trunk@1189195 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 29a82fd3754bd156e4666b7fa1c829c2e03d0280 1 parent 25cea0f
@martin-g martin-g authored
View
58 wicket-core/src/main/java/org/apache/wicket/resource/ResourceUtil.java
@@ -22,7 +22,9 @@
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.resource.aggregation.ResourceReferenceAndStringData;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
@@ -53,7 +55,9 @@ private ResourceUtil()
* @param string
* the string argument to pass to those methods that accept it (js = id / css =
* media)
+ * @deprecated Will be removed in the next major release
*/
+ @Deprecated
public static void renderTo(IHeaderResponse resp, ResourceReference ref, boolean css,
String string)
{
@@ -82,6 +86,60 @@ public static void renderTo(IHeaderResponse resp, ResourceReference ref, boolean
}
/**
+ * Helper that calls the proper IHeaderResponse.render*Reference method based on the input.
+ *
+ * @param resp
+ * the response to call render*Reference methods on
+ * @param data
+ */
+ // TODO Wicket.next - make this method work with an improved version of
+// ResourceReferenceAndStringData
+ public static void renderTo(IHeaderResponse resp, ResourceReferenceAndStringData data)
+ {
+ boolean css = data.isCss();
+ ResourceReference reference = data.getReference();
+ PageParameters parameters = data.getParameters();
+ String idOrMedia = data.getIdOrMedia();
+ CharSequence content = data.getContent();
+
+ if (css)
+ {
+ String condition = data.getCssCondition();
+
+ if (Strings.isEmpty(content) == false)
+ {
+ resp.renderCSS(content, idOrMedia);
+ }
+ else if (reference == null)
+ {
+ resp.renderCSSReference(data.getUrl(), idOrMedia, condition);
+ }
+ else
+ {
+ resp.renderCSSReference(reference, parameters, idOrMedia, condition);
+ }
+ }
+ else
+ {
+ boolean defer = data.isJsDefer();
+ String charset = data.getCharset();
+
+ if (Strings.isEmpty(content) == false)
+ {
+ resp.renderJavaScript(content, idOrMedia);
+ }
+ else if (reference == null)
+ {
+ resp.renderJavaScriptReference(data.getUrl(), idOrMedia, defer, charset);
+ }
+ else
+ {
+ resp.renderJavaScriptReference(reference, parameters, idOrMedia, defer, charset);
+ }
+ }
+ }
+
+ /**
* read string with platform default encoding from resource stream
*
* @param resourceStream
View
9 ...rg/apache/wicket/resource/aggregation/AbstractDependencyRespectingResourceAggregatingHeaderResponse.java
@@ -80,10 +80,11 @@ protected void renderCollection(Set<ResourceReferenceAndStringData> alreadyRende
}
private static ResourceReferenceAndStringData toData(
- AbstractResourceDependentResourceReference child)
+ AbstractResourceDependentResourceReference reference)
{
- boolean css = ResourceType.CSS.equals(child.getResourceType());
- String string = css ? child.getMedia() : child.getUniqueId();
- return new ResourceReferenceAndStringData(child, string, css);
+ boolean css = ResourceType.CSS.equals(reference.getResourceType());
+ String string = css ? reference.getMedia() : reference.getUniqueId();
+ return new ResourceReferenceAndStringData(reference, null, null, string, css, false, null,
+ null);
}
}
View
108 ...core/src/main/java/org/apache/wicket/resource/aggregation/AbstractResourceAggregatingHeaderResponse.java
@@ -27,6 +27,8 @@
import org.apache.wicket.markup.html.DecoratingHeaderResponse;
import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.resource.ResourceUtil;
@@ -74,25 +76,29 @@ public AbstractResourceAggregatingHeaderResponse(IHeaderResponse real)
@Override
public void renderJavaScriptReference(ResourceReference reference)
{
- topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, false));
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, null, null,
+ false, false, null, null));
}
@Override
public void renderJavaScriptReference(ResourceReference reference, String id)
{
- topLevelReferences.add(new ResourceReferenceAndStringData(reference, id, false));
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, null, id, false,
+ false, null, null));
}
@Override
public void renderCSSReference(ResourceReference reference)
{
- topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, true));
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, null, null,
+ true, false, null, null));
}
@Override
public void renderCSSReference(ResourceReference reference, String media)
{
- topLevelReferences.add(new ResourceReferenceAndStringData(reference, media, true));
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, null, media,
+ true, false, null, null));
}
@Override
@@ -188,7 +194,7 @@ protected R newResourceReferenceCollection(K key)
* 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 {@link Resource}), and
+ * override this method, create the URL to your aggregation servlet (or {@link IResource}), and
* then call <tt>getRealResponse().renderJavaScriptReference(yourUrl)</tt>, or the appropriate
* method to render the URL for a group of CSS references.
*
@@ -239,8 +245,7 @@ protected void renderIfNotAlreadyRendered(Set<ResourceReferenceAndStringData> al
*/
protected void render(ResourceReferenceAndStringData data)
{
- ResourceUtil.renderTo(getRealResponse(), data.getReference(), data.isCss(),
- data.getString());
+ ResourceUtil.renderTo(getRealResponse(), data);
}
/**
@@ -265,29 +270,102 @@ protected void onAllCollectionsRendered(
@Override
public void renderJavaScriptReference(String url)
{
- // TODO: can we aggregate this? probably shouldn't...
- getRealResponse().renderJavaScriptReference(url);
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, null, false,
+ false, null, null));
}
@Override
public void renderJavaScriptReference(String url, String id)
{
- // TODO: can we aggregate this? probably shouldn't...
- getRealResponse().renderJavaScriptReference(url, id);
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, id, false,
+ false, null, null));
}
@Override
public void renderCSSReference(String url)
{
- // TODO: can we aggregate this? probably shouldn't...
- getRealResponse().renderCSSReference(url);
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, null, true,
+ false, null, null));
}
@Override
public void renderCSSReference(String url, String media)
{
- // TODO: can we aggregate this? probably shouldn't...
- getRealResponse().renderCSSReference(url, media);
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, media, true,
+ false, null, null));
+ }
+
+ @Override
+ public void renderJavaScriptReference(ResourceReference reference, PageParameters parameters,
+ String id)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, parameters, null, id,
+ false, false, null, null));
+ }
+
+ @Override
+ public void renderJavaScriptReference(ResourceReference reference, PageParameters parameters,
+ String id, boolean defer)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, parameters, null, id,
+ false, defer, null, null));
+ }
+
+ @Override
+ public void renderJavaScriptReference(ResourceReference reference, PageParameters parameters,
+ String id, boolean defer, String charset)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, parameters, null, id,
+ false, defer, charset, null));
+ }
+
+ @Override
+ public void renderJavaScriptReference(String url, String id, boolean defer)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, null, false,
+ defer, null, null));
+ }
+
+ @Override
+ public void renderJavaScriptReference(String url, String id, boolean defer, String charset)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, id, false,
+ defer, charset, null));
+ }
+
+ @Override
+ public void renderJavaScript(CharSequence javascript, String id)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(javascript, false, id));
+ }
+
+ @Override
+ public void renderCSS(CharSequence css, String media)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(css, true, media));
+ }
+
+ @Override
+ public void renderCSSReference(ResourceReference reference, PageParameters pageParameters,
+ String media)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, pageParameters, null,
+ media, true, false, null, null));
+ }
+
+ @Override
+ public void renderCSSReference(ResourceReference reference, PageParameters pageParameters,
+ String media, String condition)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(reference, null, null, media,
+ true, false, null, condition));
+ }
+
+ @Override
+ public void renderCSSReference(String url, String media, String condition)
+ {
+ topLevelReferences.add(new ResourceReferenceAndStringData(null, null, url, media, true,
+ false, null, condition));
}
}
View
205 wicket-core/src/main/java/org/apache/wicket/resource/aggregation/ResourceReferenceAndStringData.java
@@ -16,6 +16,7 @@
*/
package org.apache.wicket.resource.aggregation;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ResourceReference;
/**
@@ -30,25 +31,88 @@
*
* @author Jeremy Thomerson
*/
+// TODO Wicket.next - Improve this class by splitting it in more specialized ones (one for JS,
+// another for CSS, ...)
public class ResourceReferenceAndStringData
{
private final ResourceReference reference;
- private final String string;
- private final boolean css;
+ private final PageParameters parameters;
+ private final String url;
+ private final String idOrMedia;
+ private final boolean isCss;
+ private final boolean jsDefer;
+ private final String charset;
+ private final String cssCondition;
+ private final CharSequence content;
/**
- * Construct with fields.
+ * Construct.
*
* @param reference
- * @param string
- * @param css
+ * @param parameters
+ * @param url
+ * @param idOrMedia
+ * @param isCss
+ * @param jsDefer
+ * @param charset
+ * @param cssCondition
*/
- public ResourceReferenceAndStringData(ResourceReference reference, String string, boolean css)
+ public ResourceReferenceAndStringData(ResourceReference reference, PageParameters parameters,
+ String url, String idOrMedia, boolean isCss, boolean jsDefer, String charset,
+ String cssCondition)
{
- super();
this.reference = reference;
- this.string = string;
- this.css = css;
+ this.parameters = parameters;
+ this.url = url;
+ this.idOrMedia = idOrMedia;
+ this.isCss = isCss;
+ this.jsDefer = jsDefer;
+ this.charset = charset;
+ this.cssCondition = cssCondition;
+ content = null;
+ }
+
+ /**
+ * Construct.
+ *
+ * @param reference
+ * @param idOrMedia
+ * @param isCss
+ * @deprecated use the other constructors instead
+ */
+ @Deprecated
+ public ResourceReferenceAndStringData(ResourceReference reference, String idOrMedia,
+ boolean isCss)
+ {
+ this.reference = reference;
+ parameters = null;
+ url = null;
+ this.idOrMedia = idOrMedia;
+ this.isCss = isCss;
+ jsDefer = false;
+ charset = null;
+ cssCondition = null;
+ content = null;
+ }
+
+ /**
+ * Construct.
+ *
+ * @param content
+ * @param isCss
+ * @param idOrMedia
+ */
+ public ResourceReferenceAndStringData(CharSequence content, boolean isCss, String idOrMedia)
+ {
+ this.content = content;
+ this.isCss = isCss;
+ reference = null;
+ parameters = null;
+ url = null;
+ this.idOrMedia = idOrMedia;
+ jsDefer = false;
+ charset = null;
+ cssCondition = null;
}
/**
@@ -60,11 +124,37 @@ public ResourceReference getReference()
}
/**
+ * @return the parameters for the resource reference
+ */
+ public PageParameters getParameters()
+ {
+ return parameters;
+ }
+
+ /**
+ * @return the resource reference that the user rendered
+ */
+ public String getUrl()
+ {
+ return url;
+ }
+
+ /**
+ * @return the string representing media (if this isCss()), or id (if not, meaning it's js)
+ */
+ public String getIdOrMedia()
+ {
+ return idOrMedia;
+ }
+
+ /**
* @return the string representing media (if this isCss()), or id (if not, meaning it's js)
+ * @deprecated use {@link #getIdOrMedia()} instead
*/
+ @Deprecated
public String getString()
{
- return string;
+ return getIdOrMedia();
}
/**
@@ -72,7 +162,39 @@ public String getString()
*/
public boolean isCss()
{
- return css;
+ return isCss;
+ }
+
+ /**
+ * @return whether the script should be deferred
+ */
+ public boolean isJsDefer()
+ {
+ return jsDefer;
+ }
+
+ /**
+ * @return the charset to use when loading the script
+ */
+ public String getCharset()
+ {
+ return charset;
+ }
+
+ /**
+ * @return the IE CSS condition
+ */
+ public String getCssCondition()
+ {
+ return cssCondition;
+ }
+
+ /**
+ * @return inline content of CSS or JS contribution
+ */
+ public CharSequence getContent()
+ {
+ return content;
}
@Override
@@ -80,9 +202,15 @@ public int hashCode()
{
final int prime = 31;
int result = 1;
- result = prime * result + (css ? 1231 : 1237);
+ result = prime * result + ((charset == null) ? 0 : charset.hashCode());
+ result = prime * result + ((content == null) ? 0 : content.hashCode());
+ result = prime * result + ((cssCondition == null) ? 0 : cssCondition.hashCode());
+ result = prime * result + ((idOrMedia == null) ? 0 : idOrMedia.hashCode());
+ result = prime * result + (isCss ? 1231 : 1237);
+ result = prime * result + (jsDefer ? 1231 : 1237);
+ result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
result = prime * result + ((reference == null) ? 0 : reference.hashCode());
- result = prime * result + ((string == null) ? 0 : string.hashCode());
+ result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
}
@@ -96,7 +224,44 @@ public boolean equals(Object obj)
if (getClass() != obj.getClass())
return false;
ResourceReferenceAndStringData other = (ResourceReferenceAndStringData)obj;
- if (css != other.css)
+ if (charset == null)
+ {
+ if (other.charset != null)
+ return false;
+ }
+ else if (!charset.equals(other.charset))
+ return false;
+ if (content == null)
+ {
+ if (other.content != null)
+ return false;
+ }
+ else if (!content.equals(other.content))
+ return false;
+ if (cssCondition == null)
+ {
+ if (other.cssCondition != null)
+ return false;
+ }
+ else if (!cssCondition.equals(other.cssCondition))
+ return false;
+ if (idOrMedia == null)
+ {
+ if (other.idOrMedia != null)
+ return false;
+ }
+ else if (!idOrMedia.equals(other.idOrMedia))
+ return false;
+ if (isCss != other.isCss)
+ return false;
+ if (jsDefer != other.jsDefer)
+ return false;
+ if (parameters == null)
+ {
+ if (other.parameters != null)
+ return false;
+ }
+ else if (!parameters.equals(other.parameters))
return false;
if (reference == null)
{
@@ -105,12 +270,12 @@ public boolean equals(Object obj)
}
else if (!reference.equals(other.reference))
return false;
- if (string == null)
+ if (url == null)
{
- if (other.string != null)
+ if (other.url != null)
return false;
}
- else if (!string.equals(other.string))
+ else if (!url.equals(other.url))
return false;
return true;
}
@@ -118,7 +283,9 @@ else if (!string.equals(other.string))
@Override
public String toString()
{
- return "ResourceReferenceAndStringData [reference=" + reference + ", string=" + string +
- ", css=" + css + "]";
+ return "ResourceReferenceAndStringData [reference=" + reference + ", parameters=" +
+ parameters + ", url=" + url + ", idOrMedia=" + idOrMedia + ", isCss=" + isCss +
+ ", jsDefer=" + jsDefer + ", charset=" + charset + ", cssCondition=" + cssCondition +
+ ", content=" + content + "]";
}
}
View
2  wicket-core/src/test/java/org/apache/wicket/markup/html/DecoratingHeaderResponseTest.java
@@ -112,7 +112,7 @@ public IHeaderResponse decorate(IHeaderResponse response)
@Override
protected Integer newGroupingKey(ResourceReferenceAndStringData ref)
{
- return Integer.parseInt(ref.getString()) % 2;
+ return Integer.parseInt(ref.getIdOrMedia()) % 2;
}
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.