Re: [OT] Merb-Rails Merge

2008-12-25 Thread HHB

I'm not bashing Rails, I'm just got bored from the Rails guys and how they
underestimate Java culture (the language, frameworks and the performance !!)
When I say Rails is a good replacement of PHP, I'm not disdaining them.
Both are good for building public web sites (forums, e-commerce) but not for
the enterprise IMHO.  
Too late :)  Struts merged with Webworks.
This movement is logical since WW was an attempt to enhance Struts but you
can't expect Wicket to merge into SpringMVC for example.


Erik van Oosten wrote:
 
 Please, no Ruby bashing here (or no bashing whatsoever).
 
 The Ruby world has many more options besides Rails and Merb. Camping, 
 Sinatra, Ramaze, Nitro just to name a few. Its not such a ridiculous 
 long list as in the Java world, but hey, Ruby has not been popular for 
 that long.
 Rails should be a good replacement of PHP, nothing more.
 I think you severely underestimate both. (When given to the right people 
 of course.)
 
 I hope Java web frameworks never got merged together.
   
 Too late :)  Struts merged with Webworks.
 
 But I agree; choice is good.
 
 Erik.
 
 
 HHB wrote:
 I hope Java web frameworks never got merged together.
 Whenever my Rails dudes points toward how many Java has web frameworks
 and
 considering this as a bad thing, I smile.
 I smile because they don't have an option, just Rails.
 We (Java guys), have request/action frameworks, component-based
 frameworks,
 Java2JavaScript frameworks, Hybrid framework.
 DHH is a jerk, but a smart one.
 He tries so hard to convince every body on the planet that his Rails is
 the
 ultimate framework.
 Rails should be a good replacement of PHP, nothing more.
 The funniest thing when I hear Ruby/Rails guys talking about deploying
 Rails
 applications in the enterprise, What a good joke !!
   
 
 -- 
 Erik van Oosten
 http://www.day-to-day-stuff.blogspot.com/
 
 
 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org
 
 
 

-- 
View this message in context: 
http://www.nabble.com/-OT--Merb-Rails-Merge-tp21151511p21166311.html
Sent from the Wicket - User mailing list archive at Nabble.com.


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



WebPage for serving binary image data

2008-12-25 Thread smallufo
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

2008-12-25 Thread Nino Martinez

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-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: [OT] wicket users around the world

2008-12-25 Thread Jonathan Locke


Seattle, WA USA

i just finished writing a foreword for a Japanese book on Wicket
i'm constantly amazed to see how far Wicket is spreading in the world.
i've been invited to holland twice now (deventer and utrecht) and have 
clients in norway and new zealand.  and i suspect there will be more 
travel in my future at this rate!

merry xmas everyone!

best,

   jon


francisco treacy-2 wrote:
 
 to know a little bit more of our great (and vast) community, i was
 just wondering if you're keen on sharing where you come from and/or
 where you work with wicket...
 
 for instance, here argentinian/belgian working with wicket in antibes,
 france
 
 francisco
 
 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org
 
 
 

-- 
View this message in context: 
http://www.nabble.com/-OT--wicket-users-around-the-world-tp20962108p21169581.html
Sent from the Wicket - User mailing list archive at Nabble.com.


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



Submitting form via ajax using wicket tester

2008-12-25 Thread John Lasher
Hello,

I'm new to wicket and have been struggling with this problem for a few hours
now and any help is greatly appreciated. I created a form with ajaxbuttons
for submit and cancel.
When I try to submit the form via the FormTester, it does a regular submit
instead of an ajax submit and hence the call back methods in my ajaxButton,
the onError and onSubmit are never getting called.

I have also tried the executeAjaxEvent with onclick but this also does not
seem to be calling the callback methods

Has anyone else run into this kind of issue or am I doing something totally
wrong here.

Thanks,

John Lasher.


Re: WebPage for serving binary image data

2008-12-25 Thread Jonathan Locke


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-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: WebPage for serving binary image data

2008-12-25 Thread Jonathan Locke


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 MapClass? extends MountedDynamicImageResource,
Boolean mounted = new HashMapClass? 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);
 

Re: WebPage for serving binary image data

2008-12-25 Thread jWeekend

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-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: WebPage for serving binary image data

2008-12-25 Thread Jeremy Thomerson
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 jweekend_for...@cabouge.comwrote:


 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-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org




Re: WebPage for serving binary image data

2008-12-25 Thread smallufo
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

2008-12-25 Thread Jeremy Thomerson
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;
ThreadLocalString mText = new ThreadLocalString();
ThreadLocalInteger mWidth = new ThreadLocalInteger();
@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 small...@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

2008-12-25 Thread Jeremy Thomerson
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=fffwidth=200

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


On Fri, Dec 26, 2008 at 12:48 AM, Jeremy Thomerson 
jer...@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;
 ThreadLocalString mText = new ThreadLocalString();
 ThreadLocalInteger mWidth = new ThreadLocalInteger();
 @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 small...@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.