why not just create a dead simple servlet that streams images? in fact
there are tons of them online you can just take. it is a much simpler
solution then dealing with wicket's resources for this particular
usecase.
-igor
On Wed, Jun 25, 2008 at 7:59 AM, V. Jenks <[EMAIL PROTECTED]> wrote:
>
> I'm having non-stop issues w/ DynamicWebResource and trying to load images
> external to my .ear deployment now that I've moved to 1.3. First off, I'm
> hoping someone can suggest an easier way to do it because what I have now is
> code that someone was nice enough to give me, here on the Wicket mailing
> list. However, I don't entirely understand how it works and while it worked
> for 2 years in Wicket 1.2, it is now causing me problem after problem in
> Wicket 1.3.x - for whatever reason.
>
> First what I've got is a class called ImageResource which extends
> DynamicWebResource:
>
> /*****************************************************/
> public class ImageResource extends DynamicWebResource
> {
> private ImageService imgSrv;
>
> public ImageResource()
> {
> super();
> imgSrv = new ImageServiceEngine();
> }
>
> public String getImage(String image)
> {
> String retImg = null;
>
> try
> {
> retImg = imgSrv.getImage(image).toString();
> }
> catch (IOException exp)
> {
> LogProxy.saveEntry(exp);
> }
>
> return retImg;
> }
>
> protected ResourceState getResourceState()
> {
> ImageResourceState state = null;
>
> try
> {
> ValueMap params = getParameters();
>
> byte[] data = null;
> String imageId = params.getString("file");
>
> //Date lastModified = getImageLastMod(imageId);
> state = new ImageResourceState(Time.valueOf(new
> Date()));
> state.setContentType("image/jpeg");
>
> //SECURITY PRECAUTION - DO NOT ALLOW ANYTHING BUT
> IMAGES!
> if
> (imageId.contains(PropertyProxy.getExternalImagesRoot()) ||
>
> imageId.contains(PropertyProxy.getExternalImagesAltRoot()))
> {
> data = imgSrv.getImage(imageId);
> }
> else
> {
> //change image to "not found" warning image
> data =
> imgSrv.getImage(PropertyProxy.getImageNotFound());
>
> //TODO: send email to notify of inappropriate
> access
> }
>
> state.setData(data);
> }
> catch (FileNotFoundException exp)
> {
> LogProxy.saveEntry(exp);
> }
> catch (IOException exp)
> {
> LogProxy.saveEntry(exp);
> }
> catch (NullPointerException exp)
> {
> LogProxy.saveEntry(exp);
> }
>
> return state;
> }
>
> /**
> *
> * @author vjenks
> *
> */
> protected class ImageResourceState extends ResourceState
> {
> private String contentType;
> private byte[] data;
> private Time lastModified;
>
> ImageResourceState(Time lastModified)
> {
> super();
> this.lastModified = lastModified;
> }
>
> public String getContentType()
> {
> return contentType;
> }
>
> void setContentType(String contentType)
> {
> this.contentType = contentType;
> }
>
> public byte[] getData()
> {
> return data;
> }
>
> void setData(byte[] data)
> {
> this.data = data;
> }
>
> public int getLength()
> {
> if (data != null)
> return data.length;
> else
> return 0;
> }
>
> public Time lastModifiedTime()
> {
> return lastModified;
> }
> }
> }
> /*****************************************************/
>
>
> You can see that it uses a class called ImageServiceEngine:
>
> /*****************************************************/
> public class ImageServiceEngine implements ImageService, Serializable
> {
> private int thumbnailSize;
>
> public ImageServiceEngine()
> {
> super();
> }
>
> public int getThumbnailSize()
> {
> return this.thumbnailSize;
> }
>
> public void setThumbnailSize(int size)
> {
> this.thumbnailSize = size;
> }
>
> public byte[] getImage(String image) throws IOException
> {
> // Open the file, then read it in.
> ByteArrayOutputStream outStream = new
> ByteArrayOutputStream();
> InputStream inStream = new FileInputStream(new
> File(image));
> copy(inStream, outStream);
> inStream.close();
> outStream.close();
> return outStream.toByteArray();
> }
>
> public Date getLastModifyTime(String image)
> {
> File f = new File(image);
> return new Date(f.lastModified());
> }
>
> /* Spring Injected */
> private File imageDir;
>
> public void setImageDir(String dirName)
> {
> imageDir = new File(dirName);
> }
>
> public byte[] getThumbnail(String image) throws IOException
> {
> // Open the file, then read it in.
> ByteArrayOutputStream outStream = new
> ByteArrayOutputStream();
> InputStream inStream = new FileInputStream(new
> File(image));
> copy(inStream, outStream);
> inStream.close();
> outStream.close();
> return outStream.toByteArray();
> }
>
> /**
> * Copies data from src into dst.
> */
> private void copy(InputStream source, OutputStream destination)
> throws IOException
> {
> try
> {
> // Transfer bytes from source to destination
> byte[] buf = new byte[1024];
> int len;
> while ((len = source.read(buf)) > 0)
> {
> destination.write(buf, 0, len);
> }
> source.close();
> destination.close();
> }
> catch (IOException ioe)
> {
> throw ioe;
> }
> }
>
> private File createImageFile(String image)
> {
> File file = new File(imageDir, image);
> return file;
> }
>
>
> /** @see ImageService#save(ImageEntry) */
> public void save(String image, InputStream imageStream)
> throws IOException
> {
> // Read in the image data.
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> copy(imageStream, baos);
> baos.close();
> byte[] imageData = baos.toByteArray();
> baos = null;
>
> // Create a unique name for the file in the image directory and
> // write the image data into it.
> File newFile = createImageFile(image);
> OutputStream outStream = new FileOutputStream(newFile);
> outStream.write(imageData);
> outStream.close();
> }
> }
> /*****************************************************/
>
>
> ImageResource is setup in init() in the App class like so:
>
> /*****************************************************/
> public void init()
> {
> //create external images resource
> getSharedResources().add("imageResource", new ImageResource());
> }
> /*****************************************************/
>
> Now, if there's a nicer, cleaner, more simple way of loading external
> images, I'd really appreciate some suggestions.
>
> As for what is now broken, it seems to have something to do w/ the fact that
> the Wicket Filter is looking at "/*" to find the application because if I
> use this URL to view an image via my "imageResource" like so:
>
> http://localhost/MyApp/resources/wicket.Application/imageResource?file=C:\\Images\\assets\\images\\newsletter\\test_bkg.jpg
>
> ...I get a 404 not found. This worked fine when my URL had an alias in
> Wicket 1.2, i.e. "/products" instead of the "/*" that is used now. If I use
> "/products" in the URL above, as I would have prior to 1.3 (since I have it
> listed as a mountable URL in the app class) - it throws an exception saying
> "java.lang.IllegalStateException: URL fragment has unmatched key/value pair:
> resources/wicket.Application/imageResource"
>
> So, I need help in both areas....one, making this easier, if possible. And,
> most importantly - being able to restore production and be able to view
> external images ASAP.
>
> Thanks very much in advance!
> --
> View this message in context:
> http://www.nabble.com/More-issues-w--external-images-and-DynamicWebResource-tp18114584p18114584.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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]