Github user martin-g commented on a diff in the pull request:
https://github.com/apache/wicket/pull/97#discussion_r26283760
--- Diff:
wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java ---
@@ -71,21 +82,67 @@ else if (imageCandidateUrl.isContextAbsolute())
// relativize against the url for the
containing CSS file
Url cssUrlCopy = new Url(cssUrl);
cssUrlCopy.resolveRelative(imageCandidateUrl);
- PackageResourceReference imageReference = new
PackageResourceReference(scope,
- cssUrlCopy.toString());
- processedUrl = cycle.urlFor(imageReference,
null);
+
+ // if the image should be processed as URL or
base64 embedded
+ if (cssUrlCopy.getQueryString() != null &&
+
cssUrlCopy.getQueryString().contains(EMBED_BASE64))
+ {
+ embedded = true;
+ PackageResourceReference imageReference
= new PackageResourceReference(scope,
+
cssUrlCopy.toString().replace("?" + EMBED_BASE64, ""));
+ try
+ {
+ processedUrl =
createBase64EncodedImage(imageReference);
+ }
+ catch (Exception e)
+ {
+ throw new
WicketRuntimeException(
+ "Error while embedding
an image into the css: " +
+
imageReference.toString(), e);
+ }
+ }
+ else
+ {
+ PackageResourceReference imageReference
= new PackageResourceReference(scope,
+ cssUrlCopy.toString());
+ processedUrl =
cycle.urlFor(imageReference, null);
+ }
}
- matcher.appendReplacement(output, "url('" +
processedUrl + "')");
+ matcher.appendReplacement(output, embedded ? "url(" +
processedUrl + ")" : "url('" +
+ processedUrl + "')");
}
matcher.appendTail(output);
return output.toString();
}
+ /**
+ * Creates a base64 encoded image string based on the given image
reference
+ *
+ * @param imageReference
+ * the image reference to create the base64 encoded image
string of
+ * @return the base64 encoded image string
+ * @throws ResourceStreamNotFoundException
+ * if the resource couldn't be found
+ * @throws IOException
+ * if the stream couldn't be read
+ */
+ private CharSequence createBase64EncodedImage(PackageResourceReference
imageReference)
+ throws ResourceStreamNotFoundException, IOException
+ {
+ IResourceStream resourceStream =
imageReference.getResource().getResourceStream();
+ byte[] bytes = new byte[(int)resourceStream.length().bytes()];
+ DataInputStream dataInputStream = new
DataInputStream(resourceStream.getInputStream());
+ dataInputStream.readFully(bytes);
--- End diff --
You can use
org.apache.wicket.util.io.IOUtils#toByteArray(java.io.InputStream) instead.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---