Thanks Ernesto, Jonas, Riyad, Don and Thomas.
I implemented all of your solutions.
I ended up with this one :
1) In the application init method I call this method, which add a directory to
the WebApplicationPath
private void initPath() {
WebApplicationPath resourceFinder = (WebApplicationPath)
getResourceSettings().getResourceFinder();
String dir = "/usr/ext/img"; // the images are under this dir
resourceFinder.add( imp );
getResourceSettings().setResourceFinder( resourceFinder );
}
2) in the panel I add
add(new Image("imageid", "/test/test.gif"));
3) the image url is
resources/xxx.yyy.zzz.TestPage3//test/test.gif
François
Le 28 janv. 2010 à 05:54, Ernesto Reinaldo Barreiro a écrit :
> Sure it is overhead but he wanted to serve images from a folder not
> under application
> context root directory... Then, you have to serve them somehow? The options
> I see are
>
> 1-A dedicated servlet?
> 2-With Wicket... and thats what the code shows... and for sure it can be
> done in a simpler way...
>
> A would try to use 1. As then Wicket would not have to serve the images.
>
> Regards,
>
> Ernesto
>
> On Wed, Jan 27, 2010 at 9:43 PM, Riyad Kalla <[email protected]> wrote:
>
>> This seems like adding a large amount of overhead to an image-heavy site
>> (e.g. image blog or something), I thought I read in Wicket in Action that
>> WicketFilter ignored HTTP requests for non-wicket resources now and passed
>> them through to the underlying server to handle avoiding the need to remap
>> your wicket URLs to something like /app/* so you could have /images and
>> other resources under root and not have them go through the filter.
>>
>> Is this not the case?
>>
>> On Wed, Jan 27, 2010 at 1:38 PM, Ernesto Reinaldo Barreiro <
>> [email protected]> wrote:
>>
>>> Hi Francois,
>>>
>>> Following example works.....
>>>
>>> 1-Create this class anywhere you want need.
>>>
>>> package com.antilia.demo.manager.img;
>>>
>>> import java.io.ByteArrayOutputStream;
>>> import java.io.File;
>>> import java.io.FileInputStream;
>>> import java.io.IOException;
>>> import java.io.InputStream;
>>> import java.io.OutputStream;
>>>
>>> import org.apache.wicket.AttributeModifier;
>>> import org.apache.wicket.markup.html.image.Image;
>>> import org.apache.wicket.markup.html.image.resource.DynamicImageResource;
>>> import org.apache.wicket.model.Model;
>>> import org.apache.wicket.protocol.http.WebApplication;
>>> import org.apache.wicket.protocol.http.WebRequestCycle;
>>> import org.apache.wicket.util.file.Folder;
>>>
>>> /**
>>> *
>>> * @author Ernesto Reinaldo Barreiro ([email protected])
>>> *
>>> */
>>> public abstract class MountedImageFactory {
>>>
>>>
>>> static int BUFFER_SIZE = 10*1024;
>>> /**
>>> * Copies one stream into the other..
>>> * @param is source Stream
>>> * @param os destination Stream
>>> * */
>>> static public void copy(InputStream is, OutputStream os) throws
>> IOException
>>> {
>>> byte[] buf = new byte[BUFFER_SIZE];
>>> while (true) {
>>> int tam = is.read(buf);
>>> if (tam == -1) {
>>> return;
>>> }
>>> os.write(buf, 0, tam);
>>> }
>>> }
>>> public static byte[] bytes(InputStream is) throws IOException {
>>> ByteArrayOutputStream out = new ByteArrayOutputStream();
>>> copy(is, out);
>>> return out.toByteArray();
>>> }
>>> private static ImageFromFolderWebResource dynamicResource;
>>> private static class ImageFromFolderWebResource extends
>>> DynamicImageResource {
>>> private static final long serialVersionUID = 1L;
>>>
>>> private File folder;
>>> public ImageFromFolderWebResource(File folder, String mountPoint) {
>>> this.folder = folder;
>>> WebApplication.get().getSharedResources().add(mountPoint, this);
>>> WebApplication.get().mountSharedResource(mountPoint,
>>> "org.apache.wicket.Application/"+mountPoint);
>>> }
>>> @Override
>>> protected byte[] getImageData() {
>>> try {
>>> String name = WebRequestCycle.get().getRequest().getParameter("name");
>>> return bytes(new FileInputStream(new File(getFolder().getAbsolutePath() +
>>> System.getProperty("file.separator")+(name))));
>>> } catch (Exception e) {
>>> //TODO: do this properly
>>> return null;
>>> }
>>> }
>>>
>>> public File getFolder() {
>>> return folder;
>>> }
>>> }
>>> /**
>>> * @return Folder from where images will be retrieved.
>>> */
>>> protected abstract Folder getFolder();
>>> /**
>>> * @return the URL to mount the dynamic WEB resource.e.g.
>>> */
>>> protected abstract String getMountPoint();
>>> public Image createImage(String id, final String imageName) {
>>> if(dynamicResource == null)
>>> dynamicResource = new ImageFromFolderWebResource(getFolder(),
>>> getMountPoint());
>>> return new Image(id) {
>>> private static final long serialVersionUID = 1L;
>>>
>>> @Override
>>> protected void onBeforeRender() {
>>> String path = WebRequestCycle.get().getRequest().getURL();
>>> path = path.substring(0, path.indexOf('/'));
>>> add(new AttributeModifier("src",true, new
>>> Model<String>("/"+path+"/"+getMountPoint()+"?name="+imageName)));
>>> super.onBeforeRender();
>>> }
>>> };
>>> }
>>> }
>>>
>>> 2- Create a test page.
>>>
>>> import org.apache.wicket.markup.html.WebPage;
>>> import org.apache.wicket.markup.html.image.Image;
>>> import org.apache.wicket.util.file.Folder;
>>>
>>> /**
>>> * @author Ernesto Reinaldo Barreiro ([email protected])
>>> *
>>> */
>>> public class TestPage extends WebPage {
>>>
>>> private static final MountedImageFactory IMAGE_FACTORY = new
>>> MountedImageFactory() {
>>> @Override
>>> protected Folder getFolder() {
>>> return new Folder("C:/temp/images");
>>> }
>>> @Override
>>> protected String getMountPoint() {
>>> return "test";
>>> }
>>> };
>>> /**
>>> *
>>> */
>>> public TestPage() {
>>> Image img = IMAGE_FACTORY.createImage("img", "test.png");
>>> add(img);
>>> }
>>> }
>>>
>>> and the HTML markup
>>>
>>> <html xmlns:wicket="org.apache.wicket">
>>> <head></head>
>>> <body>
>>> <img wicket:id="img" alt="Test"/>
>>> </body>
>>> </html>
>>>
>>> 3- If you place a "test.png" on your "C:/temp/images" then you should be
>>> able to see the image when you hit the page.
>>>
>>> Hope you can adapt this to your needs?
>>>
>>> Regards,
>>>
>>> Ernesto
>>>
>>> 2010/1/27 François Meillet <[email protected]>
>>>
>>>> Thank for yours posts.
>>>> I try the solutions, but I can't figure out how to serve images as
>>> static
>>>> images.
>>>> F.
>>>>
>>>> Le 27 janv. 2010 à 16:10, Thomas Kappler a écrit :
>>>>
>>>>> On 01/27/10 15:57, Jonas wrote:
>>>>>> Have you tried the following:
>>>>>>
>>>>>> WebComponent image = new WebComponent("someWicketId");
>>>>>> image.add(new SimpleAttributeModifier("src", "http://.....jpg"));
>>>>>> add(image);
>>>>>>
>>>>>> with markup
>>>>>>
>>>>>> <img wicket:id="someWicketId" />
>>>>>>
>>>>>>
>>>>>> that should work just fine...
>>>>>>
>>>>>> if you cannot hardcode the image url, you can use the following
>>>>>> instead of SimpleAttributeModifier
>>>>>> image.add(new AttributeModifier("src", true new
>>>>>> AbstractReadOnlyModel<String>() {
>>>>>> public String getObject() {
>>>>>> String url = ... (fetch the image url from anywhere else)
>>>>>> // e.g. '/xxx/yyyy/image893748.png'
>>>>>> return url;
>>>>>> }
>>>>>> ));
>>>>>
>>>>> Or, maybe a bit nicer, encapsulate it into a component and let the
>> URI
>>>> come from a Model, as usual in Wicket:
>>>>>
>>>>> class ExternalImageUri
>>>>> extends WebComponent
>>>>> {
>>>>> public ExternalImageUri(String id, IModel<String> uri)
>>>>> {
>>>>> super(id, uri);
>>>>> add(new AttributeModifier("src", true, uri));
>>>>> }
>>>>>
>>>>> @Override
>>>>> protected void onComponentTag(ComponentTag tag)
>>>>> {
>>>>> super.onComponentTag(tag);
>>>>> checkComponentTag(tag, "img");
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>> This in the Wiki at
>>>> http://cwiki.apache.org/WICKET/how-to-load-an-external-image.html.
>>>>>
>>>>>
>>>>> -- Thomas
>>>>>
>>>>>
>>>>>> 2010/1/27 François Meillet<[email protected]>:
>>>>>>> Hi Wicketers,
>>>>>>>
>>>>>>> I have a directory, /xxx/images with uploaded images, which is not
>>>> under the application context root directory.
>>>>>>> How can I serve them as static images ?
>>>>>>>
>>>>>>> I tried the StaticImage class I found in the forum (
>>>> http://old.nabble.com/Plain-IMG-src-urls-td21547371.html#a21547543 )
>>>>>>> but it doesn't work for me. It just work if the image files are
>> under
>>>> the context root directory.
>>>>>>>
>>>>>>> Thanks for your help.
>>>>>>>
>>>>>>> François
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe, e-mail: [email protected]
>>>>>>> For additional commands, e-mail: [email protected]
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: [email protected]
>>>>>> For additional commands, e-mail: [email protected]
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> -------------------------------------------------------------------
>>>>> Thomas Kappler [email protected]
>>>>> Swiss Institute of Bioinformatics Tel: +41 22 379 51 89
>>>>> CMU, rue Michel Servet 1
>>>>> 1211 Geneve 4
>>>>> Switzerland http://www.uniprot.org
>>>>> -------------------------------------------------------------------
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [email protected]
>>>>> For additional commands, e-mail: [email protected]
>>>>>
>>>>
>>>> François Meillet
>>>> [email protected]
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [email protected]
>>>> For additional commands, e-mail: [email protected]
>>>>
>>>>
>>>
>>
François Meillet
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]