Author: mgrigorov Date: Tue Nov 9 19:00:26 2010 New Revision: 1033158 URL: http://svn.apache.org/viewvc?rev=1033158&view=rev Log: WICKET-1382 Image could be made ajax aware
Add 'anti cache' parameter to Image components when rerendering in Ajax requests. Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java (with props) wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html (with props) wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java (with props) Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/Image.java wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/NonCachingImage.java Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/Image.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/Image.java?rev=1033158&r1=1033157&r2=1033158&view=diff ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/Image.java (original) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/Image.java Tue Nov 9 19:00:26 2010 @@ -18,6 +18,7 @@ package org.apache.wicket.markup.html.im import org.apache.wicket.Component; import org.apache.wicket.IResourceListener; +import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.markup.MarkupStream; import org.apache.wicket.markup.html.WebComponent; @@ -237,6 +238,25 @@ public class Image extends WebComponent localizedImageResource.setResourceReference(resourceReference); } localizedImageResource.setSrcAttribute(tag); + + if (AjaxRequestTarget.get() != null) + { + addAntiCacheParameter(tag); + } + } + + /** + * Adds random noise to the url every request to prevent the browser from caching the image. + * + * @param tag + */ + protected final void addAntiCacheParameter(final ComponentTag tag) + { + String url = tag.getAttributes().getString("src"); + url = url + ((url.indexOf("?") >= 0) ? "&" : "?"); + url = url + "wicket:antiCache=" + System.currentTimeMillis(); + + tag.put("src", url); } /** Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/NonCachingImage.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/NonCachingImage.java?rev=1033158&r1=1033157&r2=1033158&view=diff ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/NonCachingImage.java (original) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/NonCachingImage.java Tue Nov 9 19:00:26 2010 @@ -16,6 +16,7 @@ */ package org.apache.wicket.markup.html.image; +import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.model.IModel; import org.apache.wicket.request.mapper.parameter.PageParameters; @@ -124,11 +125,11 @@ public class NonCachingImage extends Ima { super.onComponentTag(tag); - String url = tag.getAttributes().getString("src"); - url = url + ((url.indexOf("?") >= 0) ? "&" : "?"); - url = url + "wicket:antiCache=" + System.currentTimeMillis(); - - tag.put("src", url); + // the parameter is already added for Ajax requests by the super call + if (AjaxRequestTarget.get() == null) + { + addAntiCacheParameter(tag); + } } } Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java?rev=1033158&view=auto ============================================================================== --- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java (added) +++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java Tue Nov 9 19:00:26 2010 @@ -0,0 +1,56 @@ +/* + * 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 static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.wicket.util.tester.TagTester; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Test; + +/** + * Test for WICKET-1382 + */ +public class AjaxUpdatedImageTest +{ + /** + * Tests that Image re-rendered in Ajax request have 'wicket:antiCache' parameter in its 'src' + * attribute value + */ + @Test + public void wicket1382() + { + WicketTester tester = new WicketTester(); + AjaxyImagesPage page = (AjaxyImagesPage)tester.startPage(AjaxyImagesPage.class); + + TagTester tagTester = tester.getTagById(page.image.getMarkupId()); + final String srcAttr = tagTester.getAttribute("src"); + assertFalse( + "Image has not be rendered in Ajax request so it has no wicket:antiCache' parameter", + srcAttr.contains("wicket:antiCache")); + + // make an ajax call + tester.clickLink("link", true); + page = (AjaxyImagesPage)tester.getLastRenderedPage(); + tagTester = tester.getTagById(page.image.getMarkupId()); + final String imageAjaxComponent = tagTester.getValue(); + assertTrue( + "Image has not be rendered in Ajax request so it has no wicket:antiCache' parameter", + imageAjaxComponent.contains("wicket:antiCache")); + } +} Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxUpdatedImageTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html?rev=1033158&view=auto ============================================================================== --- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html (added) +++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html Tue Nov 9 19:00:26 2010 @@ -0,0 +1,7 @@ +<html> + + <body> + <img wicket:id="image"/> + <a wicket:id="link">link</a> + </body> +</html> \ No newline at end of file Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.html ------------------------------------------------------------------------------ svn:eol-style = native Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java?rev=1033158&view=auto ============================================================================== --- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java (added) +++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java Tue Nov 9 19:00:26 2010 @@ -0,0 +1,45 @@ +/* + * 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.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.AjaxLink; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.request.resource.PackageResourceReference; + +public final class AjaxyImagesPage extends WebPage +{ + public final Image image; + + public AjaxyImagesPage() + { + image = new Image("image", new PackageResourceReference(AjaxyImagesPage.class, "Beer.gif")); + image.setOutputMarkupId(true); + + add(image); + + AjaxLink<Void> link = new AjaxLink<Void>("link") + { + @Override + public void onClick(AjaxRequestTarget target) + { + target.add(image); + } + }; + add(link); + } +} Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/image/AjaxyImagesPage.java ------------------------------------------------------------------------------ svn:eol-style = native