Repository: wicket Updated Branches: refs/heads/wicket-6.x b7620f499 -> f30f07a03
InlineImage Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6bb4f53c Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6bb4f53c Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6bb4f53c Branch: refs/heads/wicket-6.x Commit: 6bb4f53c67e02908371ffcb17d70fd40823677a0 Parents: b7620f4 Author: Tobias Soloschenko <[email protected]> Authored: Wed Jun 3 22:16:52 2015 +0200 Committer: Tobias Soloschenko <[email protected]> Committed: Wed Jun 3 22:18: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 | 10 ++ 10 files changed, 271 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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/6bb4f53c/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 7624089..61d6be9 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 @@ -17,5 +17,6 @@ <wicket:link> <img src="Image2.gif" /> </wicket:link> + <img wicket:id="inline"/> </body> </html> http://git-wip-us.apache.org/repos/asf/wicket/blob/6bb4f53c/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 670cc48..8f8095e 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.resource.BufferedDynamicImageResource; import org.apache.wicket.markup.html.image.resource.DefaultButtonImageResource; import org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource; @@ -93,6 +94,8 @@ public final class Home extends WicketExamplePage // image loaded as resource via model. add(new Image("imageModelResource", new Model<CircleDynamicImageResource>( new CircleDynamicImageResource(100, 100)))); + + add(new InlineImage("inline", new PackageResourceReference(getClass(),"image2.gif"))); } http://git-wip-us.apache.org/repos/asf/wicket/blob/6bb4f53c/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 c227256..ac93b5a 100644 --- a/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc +++ b/wicket-user-guide/src/docs/guide/resources/resources_3.gdoc @@ -65,6 +65,16 @@ Package resources can be localized following the same rules seen for resource bu In the example illustrated in the picture above, if we try to retrieve package resource calendar.jpg when the current locale is set to French, the actual file returned will be calendar_fr.jpg. {note} +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. Using package resources with tag <wicket:link>
