You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by smallufo <sm...@gmail.com> on 2008/12/25 19:38:37 UTC

WebPage for serving binary image data

Hi

I hope I can use wicket to serve image data.
I know I can extend org.apache.wicket.markup.html.image.Image and provide a
DynamicImageResource
but the generated image link is
http://localhost/app/?wicket:interface=:0:customImage::IResourceListener::
The image data is stored in the session and not bookmarkable, which is not
what I want.


I then created an ImagePage extends WebPage and override onBeforeRender()  ,
and coding below :
HttpServletResponse response = ((WebResponse)
getWebRequestCycle().getResponse()).getHttpServletResponse();
    try
    {
      response.setContentType("image/png");

      OutputStream responseOutputStream = response.getOutputStream();

      responseOutputStream.write(myImageBytes);
      responseOutputStream.flush();
      responseOutputStream.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

It works !!! And I can bookmark the image.

But there are warning output :

2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
org.apache.wicket.Component has not been properly rendered. Something in the
hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in the
override of onBeforeRender() method
java.lang.IllegalStateException: org.apache.wicket.Component has not been
properly rendered. Something in the hierarchy of foo.bar.ImagePage has not
called super.onBeforeRender() in the override of onBeforeRender() method
        at
org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
        at org.apache.wicket.Component.beforeRender(Component.java:1034)
        at org.apache.wicket.Component.prepareForRender(Component.java:2160)

Is this the "standard" way of outputing binary data ?
If not , what is the better way  (wicket 1.3.5) ?

thanks.

Re: WebPage for serving binary image data

Posted by Jonathan Locke <jo...@gmail.com>.

hmm... this really is a bit harder than it ought to be. 

below is a url encoding strategy and a sub-classable auto-mounting resource
that makes this a lot easier.  you can modify to suit your needs.

the use case looks like:

	// This dynamically rendered resource will auto-mount itself using the path
and resource name given
	private static final MountedDynamicImageResource redbox = new
MountedDynamicImageResource(
			"/images/redbox", "redbox", 100, 100) {

		private static final long serialVersionUID = 1021758684732149991L;

		@Override
		protected boolean render(Graphics2D graphics) {
			graphics.setColor(Color.RED);
			graphics.fillRect(0, 0, getWidth(), getHeight());
			return true;
		}
	};

        MyPage() {
     		add(redbox.getImage());
        }

the code for MountedDynamicImageResource looks like:

import java.awt.Graphics2D;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.image.Image;
import
org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource;
import org.apache.wicket.protocol.http.WebApplication;

public abstract class MountedDynamicImageResource extends
		RenderedDynamicImageResource {

	private static final long serialVersionUID = -9117628603222075688L;

	private static final Map<Class<? extends MountedDynamicImageResource>,
Boolean> mounted = new HashMap<Class<? extends MountedDynamicImageResource>,
Boolean>();

	private final String name;

	public MountedDynamicImageResource(final String path, final String name,
			int width, int height) {
		this(path, name, width, height, "png");
	}

	public MountedDynamicImageResource(final String path, final String name,
			int width, int height, String format) {
		super(width, height, format);
		this.name = name;
		synchronized (mounted) {
			if (mounted.get(getClass()) == null) {
				mounted.put(getClass(), Boolean.TRUE);
				WebApplication.get().getSharedResources().add(getClass(), name,
						getLocale(), null, this);
				WebApplication.get().mount(
						new MountedResourceRequestTargetUrlCodingStrategy(path
								+ "." + format, getClass(), name));
			}
		}
	}

	public Image getImage() {
		return new Image(name, new ResourceReference(getClass(), name));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected abstract boolean render(Graphics2D graphics);
}

and the url coding strategy:

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.request.RequestParameters;
import
org.apache.wicket.request.target.coding.AbstractRequestTargetUrlCodingStrategy;
import
org.apache.wicket.request.target.resource.ISharedResourceRequestTarget;
import
org.apache.wicket.request.target.resource.SharedResourceRequestTarget;

public class MountedResourceRequestTargetUrlCodingStrategy extends
		AbstractRequestTargetUrlCodingStrategy {

	private String keyPrefix;

	public MountedResourceRequestTargetUrlCodingStrategy(String mountPath,
			Class<?> scope) {
		this(mountPath, scope, mountPath.substring(1));
	}

	public MountedResourceRequestTargetUrlCodingStrategy(String mountPath,
			Class<?> scope, String namePrefix) {
		super(mountPath);
		this.keyPrefix = scope.getName() + "/" + namePrefix;
	}

	public IRequestTarget decode(RequestParameters requestParameters) {
		String name = requestParameters.getPath().substring(
				getMountPath().length());
		requestParameters.setResourceKey(keyPrefix + name);
		return new SharedResourceRequestTarget(requestParameters);
	}

	public CharSequence encode(IRequestTarget requestTarget) {
		String key = ((ISharedResourceRequestTarget) requestTarget)
				.getResourceKey();
		return getMountPath() + key.substring(keyPrefix.length());

	}

	public boolean matches(IRequestTarget requestTarget) {
		if (!(requestTarget instanceof ISharedResourceRequestTarget))
			return false;
		String key = ((ISharedResourceRequestTarget) requestTarget)
				.getResourceKey();
		return key.startsWith(keyPrefix);
	}
}

merry xmas!


smallufo wrote:
> 
> Hi
> 
> I hope I can use wicket to serve image data.
> I know I can extend org.apache.wicket.markup.html.image.Image and provide
> a
> DynamicImageResource
> but the generated image link is
> http://localhost/app/?wicket:interface=:0:customImage::IResourceListener::
> The image data is stored in the session and not bookmarkable, which is not
> what I want.
> 
> 
> I then created an ImagePage extends WebPage and override onBeforeRender() 
> ,
> and coding below :
> HttpServletResponse response = ((WebResponse)
> getWebRequestCycle().getResponse()).getHttpServletResponse();
>     try
>     {
>       response.setContentType("image/png");
> 
>       OutputStream responseOutputStream = response.getOutputStream();
> 
>       responseOutputStream.write(myImageBytes);
>       responseOutputStream.flush();
>       responseOutputStream.close();
>     }
>     catch (IOException e)
>     {
>       e.printStackTrace();
>     }
> 
> It works !!! And I can bookmark the image.
> 
> But there are warning output :
> 
> 2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
> org.apache.wicket.Component has not been properly rendered. Something in
> the
> hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in
> the
> override of onBeforeRender() method
> java.lang.IllegalStateException: org.apache.wicket.Component has not been
> properly rendered. Something in the hierarchy of foo.bar.ImagePage has not
> called super.onBeforeRender() in the override of onBeforeRender() method
>         at
> org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
>         at org.apache.wicket.Component.beforeRender(Component.java:1034)
>         at
> org.apache.wicket.Component.prepareForRender(Component.java:2160)
> 
> Is this the "standard" way of outputing binary data ?
> If not , what is the better way  (wicket 1.3.5) ?
> 
> thanks.
> 
> 

-- 
View this message in context: http://www.nabble.com/WebPage-for-serving-binary-image-data-tp21169289p21170251.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WebPage for serving binary image data

Posted by Jeremy Thomerson <je...@wickettraining.com>.
Although I haven't tested this theory, I used the ThreadLocal because I am
fairly sure that the "shared" resources are shared across threads (multiple
simultaneous requests) without synchronization.  Therefore, you need to keep
the parameters between method invocations, but can't keep it in a normal
field because multiple requests could be reading / writing them
simultaneously.

That code was not the best code - but it's a workaround because the
parameters are blown away before you are in your method that creates the
image.


-- 
Jeremy Thomerson
http://www.wickettraining.com


On Sat, Dec 27, 2008 at 1:31 AM, smallufo <sm...@gmail.com> wrote:

> Thank you, it works !
> Though I am not sure why ThreadLocal is needed here ...
>
> Anyway , the solution is much pretty than WebPage.
>
> BR
> --
>
>
>
> 2008/12/27 Jeremy Thomerson <je...@wickettraining.com>
>
> > It's not a WebPage because you're not serving a page - it is a mounted
> > resource.  Why do you want to force it to be a page?  It's a bookmarkable
> > resource - which is what you're serving.  The type is set by
> > DynamicImageResource - look in getResourceStream or getResourceState.
> >
> > On Fri, Dec 26, 2008 at 1:51 PM, smallufo <sm...@gmail.com> wrote:
> >
> > > Hi ,
> > > thank you for replying.....
> > >
> > >
> > > 2008/12/26 Jeremy Thomerson <je...@wickettraining.com>
> > > Then, you can include it in your page:
> > > JAVA: add(new Image("img", new
> > > ResourceReference( WicketApplication.IMAGE_KEY)));
> > > HTML: <img wicket:id="img" />
> > >
> > >
> > >
> > > >
> > > > http://localhost:8080/foo
> > > > http://localhost:8080/foo?text=fff&width=200
> > > >
> > > >
> > >
> > >
> > >
> > > My question is ...
> > > is "/foo" a bookmarkable mounted WebPage ?
> > > If it is , where did you set the ContentType to "image/png" ? I cannot
> > find
> > > such code ...
> > >
> > > If it is not a WebPage , how should I accomplish this by a WebPage ?
> > > I don't need the dynamic-generated image to be included in the
> WebPage's
> > > <img> tag.
> > >
> > > BR
> > > --
> > > smallufo
> > >
> >
> >
> >
> > --
> > Jeremy Thomerson
> > http://www.wickettraining.com
> >
>

Re: WebPage for serving binary image data

Posted by smallufo <sm...@gmail.com>.
Thank you, it works !
Though I am not sure why ThreadLocal is needed here ...

Anyway , the solution is much pretty than WebPage.

BR
--



2008/12/27 Jeremy Thomerson <je...@wickettraining.com>

> It's not a WebPage because you're not serving a page - it is a mounted
> resource.  Why do you want to force it to be a page?  It's a bookmarkable
> resource - which is what you're serving.  The type is set by
> DynamicImageResource - look in getResourceStream or getResourceState.
>
> On Fri, Dec 26, 2008 at 1:51 PM, smallufo <sm...@gmail.com> wrote:
>
> > Hi ,
> > thank you for replying.....
> >
> >
> > 2008/12/26 Jeremy Thomerson <je...@wickettraining.com>
> > Then, you can include it in your page:
> > JAVA: add(new Image("img", new
> > ResourceReference( WicketApplication.IMAGE_KEY)));
> > HTML: <img wicket:id="img" />
> >
> >
> >
> > >
> > > http://localhost:8080/foo
> > > http://localhost:8080/foo?text=fff&width=200
> > >
> > >
> >
> >
> >
> > My question is ...
> > is "/foo" a bookmarkable mounted WebPage ?
> > If it is , where did you set the ContentType to "image/png" ? I cannot
> find
> > such code ...
> >
> > If it is not a WebPage , how should I accomplish this by a WebPage ?
> > I don't need the dynamic-generated image to be included in the WebPage's
> > <img> tag.
> >
> > BR
> > --
> > smallufo
> >
>
>
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>

Re: WebPage for serving binary image data

Posted by Jeremy Thomerson <je...@wickettraining.com>.
It's not a WebPage because you're not serving a page - it is a mounted
resource.  Why do you want to force it to be a page?  It's a bookmarkable
resource - which is what you're serving.  The type is set by
DynamicImageResource - look in getResourceStream or getResourceState.

On Fri, Dec 26, 2008 at 1:51 PM, smallufo <sm...@gmail.com> wrote:

> Hi ,
> thank you for replying.....
>
>
> 2008/12/26 Jeremy Thomerson <je...@wickettraining.com>
> Then, you can include it in your page:
> JAVA: add(new Image("img", new
> ResourceReference( WicketApplication.IMAGE_KEY)));
> HTML: <img wicket:id="img" />
>
>
>
> >
> > http://localhost:8080/foo
> > http://localhost:8080/foo?text=fff&width=200
> >
> >
>
>
>
> My question is ...
> is "/foo" a bookmarkable mounted WebPage ?
> If it is , where did you set the ContentType to "image/png" ? I cannot find
> such code ...
>
> If it is not a WebPage , how should I accomplish this by a WebPage ?
> I don't need the dynamic-generated image to be included in the WebPage's
> <img> tag.
>
> BR
> --
> smallufo
>



-- 
Jeremy Thomerson
http://www.wickettraining.com

Re: WebPage for serving binary image data

Posted by smallufo <sm...@gmail.com>.
Hi ,
thank you for replying.....


2008/12/26 Jeremy Thomerson <je...@wickettraining.com>
Then, you can include it in your page:
JAVA: add(new Image("img", new
ResourceReference( WicketApplication.IMAGE_KEY)));
HTML: <img wicket:id="img" />



>
> http://localhost:8080/foo
> http://localhost:8080/foo?text=fff&width=200
>
>



My question is ...
is "/foo" a bookmarkable mounted WebPage ?
If it is , where did you set the ContentType to "image/png" ? I cannot find
such code ...

If it is not a WebPage , how should I accomplish this by a WebPage ?
I don't need the dynamic-generated image to be included in the WebPage's
<img> tag.

BR
-- 
smallufo

Re: WebPage for serving binary image data

Posted by Jeremy Thomerson <je...@wickettraining.com>.
Oh, sorry, mean to include that if you add this to a quickstart, you can
test with URL:

http://localhost:8080/foo
http://localhost:8080/foo?text=fff&width=200

-- 
Jeremy Thomerson
http://www.wickettraining.com


On Fri, Dec 26, 2008 at 12:48 AM, Jeremy Thomerson <
jeremy@wickettraining.com> wrote:

> I spent a while playing with this, and indeed - it is much more difficult
> than it should be (or I missed something that one of the core devs can point
> out.  I really hope one of them can review this and point me to an easier
> way.  But in the meantime, this should work for you.
>
> First, let's start with the basics.  In your web application class, you'll
> add a shared resource and mount it to whatever path you want:
>
> public static final String IMAGE_KEY = "jrtimage".intern();
>
> @Override
> protected void init() {
>     super.init();
>     getSharedResources().add(IMAGE_KEY, new MyImage());
>     mountSharedResource("foo", new
> ResourceReference(IMAGE_KEY).getSharedResourceKey());
> }
>
> Then, you can include it in your page:
> JAVA: add(new Image("img", new
> ResourceReference(WicketApplication.IMAGE_KEY)));
> HTML: <img wicket:id="img" />
>
> Okay, so what was that "MyImage"?  It is the class that creates your
> dynamic image.  Read the long comments in the class explaining the nuances
> of doing it this way.
> private static class MyImage extends DynamicImageResource {
>     private static final long serialVersionUID = 1L;
>     ThreadLocal<String> mText = new ThreadLocal<String>();
>     ThreadLocal<Integer> mWidth = new ThreadLocal<Integer>();
>     @Override
>     public IResourceStream getResourceStream() {
>      // see note below on why we get parameters in this method
>         int width = 300;
>         String text = "Hello World!";
>         String w = ((String[]) getParameters().get("width"))[0];
>         if (w != null && "".equals(w.trim()) == false) {
>             try {
>                 width = Integer.parseInt(w);
>             } catch(NumberFormatException nfe) {
>                 //no-op
>             }
>         }
>         String t = ((String[]) getParameters().get("text"))[0];
>         if (t != null && "".equals(t.trim()) == false) {
>             text = t;
>         }
>         mText.set(text);
>         mWidth.set(width);
>         return super.getResourceStream();
>     }
>     @Override
>     protected byte[] getImageData() {
>      /*
>       Unfortunately by the time we get here, the request target has already
>       been switched, and is no longer an instance of
> ISharedResourceRequestTarget
>       and the getParameters() method no longer returns any parameters,
> presumably
>       because the request target has been switched to a
> ResourceStreamRequestTarget
>
>       Therefore, we must override getResourceStream above (while params
> still available)
>       so that we can get the parameters there and store them in a
> ThreadLocal
>
>       NOTE: I haven't tested the ThreadLocal here at all.  Presumably it
> will be okay.
>       I don't think you could use regular member fields because multiple
> requests
>       could be operating on this object at the same time - I'm pretty sure,
> but haven't
>       double checked.
>      */
>         BufferedImage img = new BufferedImage(mWidth.get(), 100,
> BufferedImage.TYPE_INT_RGB);
>         Graphics gr = img.createGraphics();
>         gr.setColor(Color.BLACK);
>         gr.fillRect(0, 0, mWidth.get(), 100);
>         gr.setFont(new Font("Arial", Font.PLAIN, 16));
>         gr.setColor(Color.WHITE);
>         gr.drawString(mText.get(), 25, 25);
>         gr.drawString("Width: " + mWidth.get(), 25, 45);
>         mText.set(null);
>         mWidth.set(null);
>         return toImageData(img);
>     }
> }
>  --
> Jeremy Thomerson
> http://www.wickettraining.com
>
>
> On Thu, Dec 25, 2008 at 11:03 PM, smallufo <sm...@gmail.com> wrote:
>
>> Well ,
>> What I need is parsing URL and generating a corresponding image from
>> BufferedImage ,
>> not a bookmarkable link to internal file resource...
>>
>> for example :
>> http://localhost/app/myImage/text/Hello/width/500
>>
>> This will generate a 500x500 png , containing a "Hello" String.
>>
>
>
>
>

Re: WebPage for serving binary image data

Posted by Jeremy Thomerson <je...@wickettraining.com>.
I spent a while playing with this, and indeed - it is much more difficult
than it should be (or I missed something that one of the core devs can point
out.  I really hope one of them can review this and point me to an easier
way.  But in the meantime, this should work for you.

First, let's start with the basics.  In your web application class, you'll
add a shared resource and mount it to whatever path you want:

public static final String IMAGE_KEY = "jrtimage".intern();

@Override
protected void init() {
    super.init();
    getSharedResources().add(IMAGE_KEY, new MyImage());
    mountSharedResource("foo", new
ResourceReference(IMAGE_KEY).getSharedResourceKey());
}

Then, you can include it in your page:
JAVA: add(new Image("img", new
ResourceReference(WicketApplication.IMAGE_KEY)));
HTML: <img wicket:id="img" />

Okay, so what was that "MyImage"?  It is the class that creates your dynamic
image.  Read the long comments in the class explaining the nuances of doing
it this way.
private static class MyImage extends DynamicImageResource {
    private static final long serialVersionUID = 1L;
    ThreadLocal<String> mText = new ThreadLocal<String>();
    ThreadLocal<Integer> mWidth = new ThreadLocal<Integer>();
    @Override
    public IResourceStream getResourceStream() {
     // see note below on why we get parameters in this method
        int width = 300;
        String text = "Hello World!";
        String w = ((String[]) getParameters().get("width"))[0];
        if (w != null && "".equals(w.trim()) == false) {
            try {
                width = Integer.parseInt(w);
            } catch(NumberFormatException nfe) {
                //no-op
            }
        }
        String t = ((String[]) getParameters().get("text"))[0];
        if (t != null && "".equals(t.trim()) == false) {
            text = t;
        }
        mText.set(text);
        mWidth.set(width);
        return super.getResourceStream();
    }
    @Override
    protected byte[] getImageData() {
     /*
      Unfortunately by the time we get here, the request target has already
      been switched, and is no longer an instance of
ISharedResourceRequestTarget
      and the getParameters() method no longer returns any parameters,
presumably
      because the request target has been switched to a
ResourceStreamRequestTarget

      Therefore, we must override getResourceStream above (while params
still available)
      so that we can get the parameters there and store them in a
ThreadLocal

      NOTE: I haven't tested the ThreadLocal here at all.  Presumably it
will be okay.
      I don't think you could use regular member fields because multiple
requests
      could be operating on this object at the same time - I'm pretty sure,
but haven't
      double checked.
     */
        BufferedImage img = new BufferedImage(mWidth.get(), 100,
BufferedImage.TYPE_INT_RGB);
        Graphics gr = img.createGraphics();
        gr.setColor(Color.BLACK);
        gr.fillRect(0, 0, mWidth.get(), 100);
        gr.setFont(new Font("Arial", Font.PLAIN, 16));
        gr.setColor(Color.WHITE);
        gr.drawString(mText.get(), 25, 25);
        gr.drawString("Width: " + mWidth.get(), 25, 45);
        mText.set(null);
        mWidth.set(null);
        return toImageData(img);
    }
}
-- 
Jeremy Thomerson
http://www.wickettraining.com


On Thu, Dec 25, 2008 at 11:03 PM, smallufo <sm...@gmail.com> wrote:

> Well ,
> What I need is parsing URL and generating a corresponding image from
> BufferedImage ,
> not a bookmarkable link to internal file resource...
>
> for example :
> http://localhost/app/myImage/text/Hello/width/500
>
> This will generate a 500x500 png , containing a "Hello" String.
>

Re: WebPage for serving binary image data

Posted by smallufo <sm...@gmail.com>.
Well ,
What I need is parsing URL and generating a corresponding image from
BufferedImage ,
not a bookmarkable link to internal file resource...

for example :
http://localhost/app/myImage/text/Hello/width/500

This will generate a 500x500 png , containing a "Hello" String.

Re: WebPage for serving binary image data

Posted by Jeremy Thomerson <je...@wickettraining.com>.
There was also just a new strategy added in 1.4 (I know you're on 1.3.5 -
but just so you or others are aware of it) for creating very nice URLs for
resources.  See this commit:

http://fisheye6.atlassian.com/changelog/wicket/?cs=729078

-- 
Jeremy Thomerson
http://www.wickettraining.com

On Thu, Dec 25, 2008 at 7:18 PM, jWeekend <jw...@cabouge.com>wrote:

>
> Take a look at Wicket's shared resources if you need a stable URL.
> You may get some ideas
> http://www.nabble.com/Re%3A-Mounting-shared-resources-p15236047.html here
> too.
>
> Regards - Cemal
> http://www.jWeekend.co.uk <http://www.jweekend.co.uk/> jWeekend
>
>
> smallufo wrote:
> >
> > Hi
> >
> > I hope I can use wicket to serve image data.
> > I know I can extend org.apache.wicket.markup.html.image.Image and provide
> > a
> > DynamicImageResource
> > but the generated image link is
> > http://localhost/app/?wicket:interface=:0:customImage::IResourceListener
> ::
> > The image data is stored in the session and not bookmarkable, which is
> not
> > what I want.
> >
> >
> > I then created an ImagePage extends WebPage and override onBeforeRender()
> > ,
> > and coding below :
> > HttpServletResponse response = ((WebResponse)
> > getWebRequestCycle().getResponse()).getHttpServletResponse();
> >     try
> >     {
> >       response.setContentType("image/png");
> >
> >       OutputStream responseOutputStream = response.getOutputStream();
> >
> >       responseOutputStream.write(myImageBytes);
> >       responseOutputStream.flush();
> >       responseOutputStream.close();
> >     }
> >     catch (IOException e)
> >     {
> >       e.printStackTrace();
> >     }
> >
> > It works !!! And I can bookmark the image.
> >
> > But there are warning output :
> >
> > 2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
> > org.apache.wicket.Component has not been properly rendered. Something in
> > the
> > hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in
> > the
> > override of onBeforeRender() method
> > java.lang.IllegalStateException: org.apache.wicket.Component has not been
> > properly rendered. Something in the hierarchy of foo.bar.ImagePage has
> not
> > called super.onBeforeRender() in the override of onBeforeRender() method
> >         at
> > org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
> >         at org.apache.wicket.Component.beforeRender(Component.java:1034)
> >         at
> > org.apache.wicket.Component.prepareForRender(Component.java:2160)
> >
> > Is this the "standard" way of outputing binary data ?
> > If not , what is the better way  (wicket 1.3.5) ?
> >
> > thanks.
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/WebPage-for-serving-binary-image-data-tp21169289p21171044.html
>  Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: WebPage for serving binary image data

Posted by jWeekend <jw...@cabouge.com>.
Take a look at Wicket's shared resources if you need a stable URL.
You may get some ideas 
http://www.nabble.com/Re%3A-Mounting-shared-resources-p15236047.html here 
too.

Regards - Cemal
http://www.jWeekend.co.uk jWeekend 


smallufo wrote:
> 
> Hi
> 
> I hope I can use wicket to serve image data.
> I know I can extend org.apache.wicket.markup.html.image.Image and provide
> a
> DynamicImageResource
> but the generated image link is
> http://localhost/app/?wicket:interface=:0:customImage::IResourceListener::
> The image data is stored in the session and not bookmarkable, which is not
> what I want.
> 
> 
> I then created an ImagePage extends WebPage and override onBeforeRender() 
> ,
> and coding below :
> HttpServletResponse response = ((WebResponse)
> getWebRequestCycle().getResponse()).getHttpServletResponse();
>     try
>     {
>       response.setContentType("image/png");
> 
>       OutputStream responseOutputStream = response.getOutputStream();
> 
>       responseOutputStream.write(myImageBytes);
>       responseOutputStream.flush();
>       responseOutputStream.close();
>     }
>     catch (IOException e)
>     {
>       e.printStackTrace();
>     }
> 
> It works !!! And I can bookmark the image.
> 
> But there are warning output :
> 
> 2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
> org.apache.wicket.Component has not been properly rendered. Something in
> the
> hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in
> the
> override of onBeforeRender() method
> java.lang.IllegalStateException: org.apache.wicket.Component has not been
> properly rendered. Something in the hierarchy of foo.bar.ImagePage has not
> called super.onBeforeRender() in the override of onBeforeRender() method
>         at
> org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
>         at org.apache.wicket.Component.beforeRender(Component.java:1034)
>         at
> org.apache.wicket.Component.prepareForRender(Component.java:2160)
> 
> Is this the "standard" way of outputing binary data ?
> If not , what is the better way  (wicket 1.3.5) ?
> 
> thanks.
> 
> 

-- 
View this message in context: http://www.nabble.com/WebPage-for-serving-binary-image-data-tp21169289p21171044.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WebPage for serving binary image data

Posted by Jonathan Locke <jo...@gmail.com>.

ResourceReference reference;
WebApplication.mountSharedResource("/path/to/my/image",
reference.getSharedResourceKey())


smallufo wrote:
> 
> Hi
> 
> I hope I can use wicket to serve image data.
> I know I can extend org.apache.wicket.markup.html.image.Image and provide
> a
> DynamicImageResource
> but the generated image link is
> http://localhost/app/?wicket:interface=:0:customImage::IResourceListener::
> The image data is stored in the session and not bookmarkable, which is not
> what I want.
> 
> 
> I then created an ImagePage extends WebPage and override onBeforeRender() 
> ,
> and coding below :
> HttpServletResponse response = ((WebResponse)
> getWebRequestCycle().getResponse()).getHttpServletResponse();
>     try
>     {
>       response.setContentType("image/png");
> 
>       OutputStream responseOutputStream = response.getOutputStream();
> 
>       responseOutputStream.write(myImageBytes);
>       responseOutputStream.flush();
>       responseOutputStream.close();
>     }
>     catch (IOException e)
>     {
>       e.printStackTrace();
>     }
> 
> It works !!! And I can bookmark the image.
> 
> But there are warning output :
> 
> 2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
> org.apache.wicket.Component has not been properly rendered. Something in
> the
> hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in
> the
> override of onBeforeRender() method
> java.lang.IllegalStateException: org.apache.wicket.Component has not been
> properly rendered. Something in the hierarchy of foo.bar.ImagePage has not
> called super.onBeforeRender() in the override of onBeforeRender() method
>         at
> org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
>         at org.apache.wicket.Component.beforeRender(Component.java:1034)
>         at
> org.apache.wicket.Component.prepareForRender(Component.java:2160)
> 
> Is this the "standard" way of outputing binary data ?
> If not , what is the better way  (wicket 1.3.5) ?
> 
> thanks.
> 
> 

-- 
View this message in context: http://www.nabble.com/WebPage-for-serving-binary-image-data-tp21169289p21169962.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WebPage for serving binary image data

Posted by Nino Martinez <ni...@gmail.com>.
Search the list, use either a resource or a servlet are the conclusion...

Like so :

package zeuzgroup.web.icons;

import org.apache.log4j.Logger;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.model.Model;

import zeuzgroup.web.application.WicketApplication;

public class Icons {

    private static org.apache.log4j.Logger log = 
Logger.getLogger(Icons.class);

    /**
     * Call this when application loads
     *
     * @param wicketApp
     */
    public static void LoadImages() {

        for (IconType iconType : IconType.values()) {

            getResource(iconType);

        }

    }

    public static ResourceReference getResource(IconType iconType) {

        ResourceReference iconRef = new ResourceReference(Icons.class, 
iconType.getName());
        iconRef.bind(WicketApplication.get());
        log.debug("binding icon to:" + iconType.getName());

        return iconRef;

    }

    public static Image getImageForIcon(String id, IconType iconType) {
        log.debug("getting image for:" + iconType.getName());
        ResourceReference resource = new 
ResourceReference(Icons.class,iconType.getName());
        if (resource == null) {
            log.error("got null resource for :" + iconType.getName()
                    + " Will try to build new");

            resource = getResource(iconType);

        }

        Image image = new Image(id, resource);
        image.add(new AttributeModifier("alt", true, new Model(iconType
                .getDescription())));
        image.add(new AttributeModifier("title", true, new Model(iconType
                .getDescription())));

        return image;
    }

    public enum IconType {
        CONCERT("kguitar.png", "Concert"), MUSIC("music.png", "Music"), 
MONEY(
                "money.png", "Money"), INVITATION("page_white_text.png",
                "Invitation"), MONEY_ADD("money_add.png", "Money Add"), NEW(
                "new.png", "New"), DOOR_OPEN("door_open.png", "Open"), 
USERS(
                "system-users.png", "Users"), DRINK("drink.png", 
"Drink"), RAINBOW(
                "rainbow.png", "Rainbow"), LINK("link.png", "Link"), 
NOPICTURE("nopicture.png","No Picture");

        private IconType(String name, String description) {
            this.description = description;
            this.name = name;
        }

        private String description;
        private String name;

        public String getDescription() {
            return description;
        }

        public String getName() {
            return this.name;
        }
    }
}




smallufo wrote:
> Hi
>
> I hope I can use wicket to serve image data.
> I know I can extend org.apache.wicket.markup.html.image.Image and provide a
> DynamicImageResource
> but the generated image link is
> http://localhost/app/?wicket:interface=:0:customImage::IResourceListener::
> The image data is stored in the session and not bookmarkable, which is not
> what I want.
>
>
> I then created an ImagePage extends WebPage and override onBeforeRender()  ,
> and coding below :
> HttpServletResponse response = ((WebResponse)
> getWebRequestCycle().getResponse()).getHttpServletResponse();
>     try
>     {
>       response.setContentType("image/png");
>
>       OutputStream responseOutputStream = response.getOutputStream();
>
>       responseOutputStream.write(myImageBytes);
>       responseOutputStream.flush();
>       responseOutputStream.close();
>     }
>     catch (IOException e)
>     {
>       e.printStackTrace();
>     }
>
> It works !!! And I can bookmark the image.
>
> But there are warning output :
>
> 2008-12-26 02:20:42,919 ERROR wicket.RequestCycle -
> org.apache.wicket.Component has not been properly rendered. Something in the
> hierarchy of foo.bar.ImagePage has not called super.onBeforeRender() in the
> override of onBeforeRender() method
> java.lang.IllegalStateException: org.apache.wicket.Component has not been
> properly rendered. Something in the hierarchy of foo.bar.ImagePage has not
> called super.onBeforeRender() in the override of onBeforeRender() method
>         at
> org.apache.wicket.Component.internalBeforeRender(Component.java:1006)
>         at org.apache.wicket.Component.beforeRender(Component.java:1034)
>         at org.apache.wicket.Component.prepareForRender(Component.java:2160)
>
> Is this the "standard" way of outputing binary data ?
> If not , what is the better way  (wicket 1.3.5) ?
>
> thanks.
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org