Repository: wicket Updated Branches: refs/heads/master b48042da7 -> 6497ee079
InlineImage Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/64f936d9 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/64f936d9 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/64f936d9 Branch: refs/heads/master Commit: 64f936d9e26a43af13e384f4e9a97a14b64cebab Parents: b48042d Author: Tobias Soloschenko <tsolosche...@apache.org> Authored: Wed Jun 3 21:58:14 2015 +0200 Committer: Tobias Soloschenko <tsolosche...@apache.org> Committed: Wed Jun 3 22:04:48 2015 +0200 ---------------------------------------------------------------------- .../wicket/markup/html/image/InlineImage.java | 95 +++++++++++++++++++ .../apache/wicket/resource/CssUrlReplacer.java | 38 +------- .../org/apache/wicket/util/image/ImageUtil.java | 68 +++++++++++++ .../wicket/markup/html/image/InlineImage.gif | Bin 0 -> 25903 bytes .../markup/html/image/InlineImageTest.java | 53 +++++++++++ .../markup/html/image/InlineImageTestPage.html | 8 ++ .../markup/html/image/InlineImageTestPage.java | 31 ++++++ .../org/apache/wicket/examples/images/Home.html | 1 + .../org/apache/wicket/examples/images/Home.java | 3 + .../src/docs/guide/resources/resources_3.gdoc | 12 +++ 10 files changed, 273 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/main/java/org/apache/wicket/markup/html/image/InlineImage.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/InlineImage.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/InlineImage.java new file mode 100644 index 0000000..15ae5b4 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/InlineImage.java @@ -0,0 +1,95 @@ +/* + * 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.image; + +import java.io.IOException; + +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.markup.ComponentTag; +import org.apache.wicket.markup.html.WebComponent; +import org.apache.wicket.model.IModel; +import org.apache.wicket.request.resource.PackageResourceReference; +import org.apache.wicket.util.image.ImageUtil; +import org.apache.wicket.util.resource.ResourceStreamNotFoundException; + +/** + * The inline image is used to embed the complete image content within a HTML document. + * + * @author Tobias Soloschenko + * @since 6.20.0 + * + */ +public class InlineImage extends WebComponent +{ + + private static final long serialVersionUID = 1L; + + private PackageResourceReference packageResourceReference; + + /** + * Creates an inline image + * + * @param id + * the id + * @param packageResourceReference + * the package resource reference of the image + */ + public InlineImage(String id, PackageResourceReference packageResourceReference) + { + this(id, null, packageResourceReference); + } + + /** + * Creates an inline image + * + * @param id + * the id + * @param model + * the model of the inline image + * @param packageResourceReference + * the package resource reference of the image + */ + public InlineImage(String id, IModel<?> model, PackageResourceReference packageResourceReference) + { + super(id, model); + this.packageResourceReference = packageResourceReference; + } + + /** + * Renders the complete image tag with the base64 encoded content. + */ + @Override + protected void onComponentTag(ComponentTag tag) + { + super.onComponentTag(tag); + + checkComponentTag(tag, "img"); + + try + { + tag.put("src", ImageUtil.createBase64EncodedImage(packageResourceReference, false)); + } + catch (ResourceStreamNotFoundException e) + { + throw new WicketRuntimeException("Couldn't find the resource stream", e); + } + catch (IOException e) + { + throw new WicketRuntimeException("Error while reading the resource stream", e); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java b/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java index 2a03228..534e89a 100644 --- a/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java +++ b/wicket-core/src/main/java/org/apache/wicket/resource/CssUrlReplacer.java @@ -16,8 +16,6 @@ */ package org.apache.wicket.resource; -import java.io.IOException; -import java.io.InputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,10 +24,7 @@ import org.apache.wicket.css.ICssCompressor; import org.apache.wicket.request.Url; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.resource.PackageResourceReference; -import org.apache.wicket.util.crypt.Base64; -import org.apache.wicket.util.io.IOUtils; -import org.apache.wicket.util.resource.IResourceStream; -import org.apache.wicket.util.resource.ResourceStreamNotFoundException; +import org.apache.wicket.util.image.ImageUtil; /** * This compressor is used to replace URLs within CSS files with URLs created from @@ -97,7 +92,7 @@ public class CssUrlReplacer implements IScopeAwareTextResourceProcessor, ICssCom cssUrlCopy.toString().replace("?" + EMBED_BASE64, "")); try { - processedUrl = createBase64EncodedImage(imageReference); + processedUrl = ImageUtil.createBase64EncodedImage(imageReference, true); } catch (Exception e) { @@ -120,35 +115,6 @@ public class CssUrlReplacer implements IScopeAwareTextResourceProcessor, ICssCom 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(); - InputStream inputStream = resourceStream.getInputStream(); - try - { - byte[] bytes = IOUtils.toByteArray(inputStream); - String base64EncodedImage = Base64.encodeBase64String(bytes); - return "data:" + resourceStream.getContentType() + ";base64," + - base64EncodedImage.replaceAll("\\s", ""); - } - finally - { - IOUtils.closeQuietly(inputStream); - } - } - @Override public String compress(String original) { http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/main/java/org/apache/wicket/util/image/ImageUtil.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/image/ImageUtil.java b/wicket-core/src/main/java/org/apache/wicket/util/image/ImageUtil.java new file mode 100644 index 0000000..6c14867 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/util/image/ImageUtil.java @@ -0,0 +1,68 @@ +/* + * 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.util.image; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.wicket.request.resource.PackageResourceReference; +import org.apache.wicket.util.crypt.Base64; +import org.apache.wicket.util.io.IOUtils; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.ResourceStreamNotFoundException; + +/** + * Util class to provide basic image functionality like converting image data to base64 content + * + * @author Tobias Soloschenko + * @since 6.20.0 + * + */ +public class ImageUtil +{ + + /** + * 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 + * @param removeWhitespaces + * if whitespaces should be removed from the output + * @return the base64 encoded image string + * @throws ResourceStreamNotFoundException + * if the resource couldn't be found + * @throws IOException + * if the stream couldn't be read + */ + public static CharSequence createBase64EncodedImage(PackageResourceReference imageReference, + boolean removeWhitespaces) throws ResourceStreamNotFoundException, IOException + { + IResourceStream resourceStream = imageReference.getResource().getResourceStream(); + InputStream inputStream = resourceStream.getInputStream(); + try + { + byte[] bytes = IOUtils.toByteArray(inputStream); + String base64EncodedImage = Base64.encodeBase64String(bytes); + return "data:" + resourceStream.getContentType() + ";base64," + + (removeWhitespaces ? base64EncodedImage.replaceAll("\\s", "") : base64EncodedImage); + } + finally + { + IOUtils.closeQuietly(inputStream); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImage.gif ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImage.gif b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImage.gif new file mode 100644 index 0000000..98cc51a Binary files /dev/null and b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImage.gif differ http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTest.java new file mode 100644 index 0000000..04acb09 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTest.java @@ -0,0 +1,53 @@ +/* + * 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.image; + + +import org.apache.wicket.util.tester.WicketTester; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InlineImageTest +{ + + private WicketTester wicketTester; + + @Before + public void setup() + { + wicketTester = new WicketTester(); + } + + @After + public void tearDown() + { + wicketTester.destroy(); + } + + @Test + public void inlineImageTest() + { + wicketTester.startPage(InlineImageTestPage.class); + String lastResponseAsString = wicketTester.getLastResponse().getDocument(); + Assert.assertTrue( + "inline image is in html", + lastResponseAsString.contains("<img wicket:id=\"inlineimage\" src=\"data:image/gif;base64,R0lGODlhQAHwAPf8AAAAAAwMDAsNABUZABUXBRIS")); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.html ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.html b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.html new file mode 100644 index 0000000..7f24711 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.html @@ -0,0 +1,8 @@ +<html> + <head> + <title>inlineimage</title> + </head> + <body> + <img wicket:id="inlineimage" /> + </body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.java new file mode 100644 index 0000000..47a6d67 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/InlineImageTestPage.java @@ -0,0 +1,31 @@ +/* + * 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.image; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.request.resource.PackageResourceReference; + +public class InlineImageTestPage extends WebPage +{ + private static final long serialVersionUID = 1L; + + public InlineImageTestPage() + { + add(new InlineImage("inlineimage", new PackageResourceReference(InlineImageTest.class, + "InlineImage.gif"))); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.html ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.html b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.html index 39ca830..32a658a 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.html +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.html @@ -23,5 +23,6 @@ <source wicket:id="sourcemedium" /> <img wicket:id="image7"/> </picture> + <img wicket:id="inline"/> </body> </html> http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java index 42a6a9e..9f97dea 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java @@ -23,6 +23,7 @@ import java.util.Random; import org.apache.wicket.examples.WicketExamplePage; import org.apache.wicket.markup.html.image.Image; +import org.apache.wicket.markup.html.image.InlineImage; import org.apache.wicket.markup.html.image.Picture; import org.apache.wicket.markup.html.image.Source; import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource; @@ -126,6 +127,8 @@ public final class Home extends WicketExamplePage "Image2_small.gif")); picture.add(image3); this.add(picture); + + add(new InlineImage("inline", new PackageResourceReference(getClass(),"image2.gif"))); } /** http://git-wip-us.apache.org/repos/asf/wicket/blob/64f936d9/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc ---------------------------------------------------------------------- diff --git a/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc b/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc index 7315892..cd5e8f6 100644 --- a/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc +++ b/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc @@ -125,6 +125,18 @@ The component @org.apache.wicket.markup.html.image.Picture@ is used to provide a ... {code} +h3. Inline Image - embedded resource reference content + +In some components like in the inline image resource references are going to be translated to other representations like base64 content. + +*Java Code:* +{code} +... + add(new InlineImage("inline", new PackageResourceReference(getClass(),"image2.gif"))); +... +{code} + + h3. Media tags - resource references with content range support Since Wicket 7.0.0 the PackageResource and the PackageResourceReference support "Range" HTTP header for the request and "Content-Range" / "Accept-Range" HTTP headers for the response, which are used for videos / audio tags. The "Range" header allows the client to only request a specific byte range of the resource. The server provides the "Content-Range" and tells the client which bytes are going to be send.