Resource/SharedResource - Display an image

View: New views
7 Messages — Rating Filter:   Alert me  

Resource/SharedResource - Display an image

by greeklinux :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am building a simple Page. The Page has a form and allows an user
to upload a profile picture.

If the user does not have a profile picture, then I want to display a standard
picture. This should be a shared resource, so the browser can cache it.

Now if the user uploads a picture, the new picture should be shown instead.

I tried and played with shared resources and WebResource but I cant
get it working.

This is my code:

Image image = null;
final byte[] pic = MyApplication.get().getUserPicFromDB(userid);

if (pic == null) {
    image = new Image("photo", new ResourceReference(MyApplication.PROFILE_STANDARD));
} else {
            WebResource resource = new WebResource() {
                public IResourceStream getResourceStream() {
                    IResourceStream resourceStream =  new AbstractResourceStream() {
                        private InputStream is;

                        public InputStream getInputStream() throws ResourceStreamNotFoundException {
                            is = new BufferedInputStream(new ByteArrayInputStream(pic));
                            return is;
                        }

                        public void close() throws IOException {
                            is.close();
                        }
                    };

                    return resourceStream;
                }
            };

            image = new Image("photo", resource);
        }
add(image)


If an user has no picture and he uploads one, then I still see the share resource.
Ok, because the Page was constructed with the shared resource.

Then I tried to test if a user picture is present in the getResourceStream() Method of WebResource:

WebResource resource = new WebResource() {
            public IResourceStream getResourceStream() {
                IResourceStream resourceStream;
                final byte[] pic = MyApplication.get().getUserPicFromDB(userid);

                if (pic != null) {
                    resourceStream =  new AbstractResourceStream() {
                        private InputStream is;

                        public InputStream getInputStream() throws ResourceStreamNotFoundException {
                            is = new BufferedInputStream(new ByteArrayInputStream(pic));
                            return is;
                        }

                        public void close() throws IOException {
                            is.close();
                        }
                    };
                } else {
                    resourceStream = new ResourceReference(MyApplication.PROFILE_STANDARD)
                                                .getResource().getResourceStream();
                }

                return resourceStream;
            }
        };
add(new Image("photo", resource));


But now I get a NullPointerException:
 at .....PhotoUploadPage$1.getResourceStream(PhotoUploadPage.java:66)
        at org.apache.wicket.Resource.init(Resource.java:199)
        at org.apache.wicket.Resource.onResourceRequested(Resource.java:105)
        at org.apache.wicket.markup.html.image.resource.LocalizedImageResource.onResourceRequested(LocalizedImageResource.java:198)
        at org.apache.wicket.markup.html.image.Image.onResourceRequested(Image.java:147)


I would be thankful for suggestions.

Thanks

Re: Resource/SharedResource - Display an image

by Roland Huss :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

greeklinux wrote:
....

But now I get a NullPointerException:
 at .....PhotoUploadPage$1.getResourceStream(PhotoUploadPage.java:66)
        at org.apache.wicket.Resource.init(Resource.java:199)
        at org.apache.wicket.Resource.onResourceRequested(Resource.java:105)
        at org.apache.wicket.markup.html.image.resource.LocalizedImageResource.onResourceRequested(LocalizedImageResource.java:198)
        at org.apache.wicket.markup.html.image.Image.onResourceRequested(Image.java:147)
Where in the code snippet that you posted is line 66 at which this NPE has occured ?


...roland

Re: Resource/SharedResource - Display an image

by greeklinux :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Roland,

the line where the NullPointerException is thrown is:

resourceStream = new ResourceReference(MyApplication.PROFILE_STANDARD)
                                                .getResource().getResourceStream();

If I am using only this ResourceReference then it works. But I dont know why
the Exception is thrown.

greetings


Roland Huss wrote:
greeklinux wrote:
....

But now I get a NullPointerException:
 at .....PhotoUploadPage$1.getResourceStream(PhotoUploadPage.java:66)
        at org.apache.wicket.Resource.init(Resource.java:199)
        at org.apache.wicket.Resource.onResourceRequested(Resource.java:105)
        at org.apache.wicket.markup.html.image.resource.LocalizedImageResource.onResourceRequested(LocalizedImageResource.java:198)
        at org.apache.wicket.markup.html.image.Image.onResourceRequested(Image.java:147)
Where in the code snippet that you posted is line 66 at which this NPE has occured ?


...roland

Re: Resource/SharedResource - Display an image

by Roland Huss :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

greeklinux wrote:
the line where the NullPointerException is thrown is:

resourceStream = new ResourceReference(MyApplication.PROFILE_STANDARD)
                                                .getResource().getResourceStream();

If I am using only this ResourceReference then it works. But I dont know why
the Exception is thrown.
You need to bind (==register) the ResourceReference to the application before you can get to the referenced Resource (which has been registered either explicitely or implicitely, if is a PackageResource):

resourceReference =  new ResourceReference(MyApplication.PROFILE_STANDARD);
resourceReference.bind(Application.get());
resourceStream = resourceReferece.getResource().getResourceStream();


...roland

Re: Resource/SharedResource - Display an image

by greeklinux :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Roland,

your solution works. But the problem is that I am already registering the resource in
an Initializer. But ok...

Now I am uploading the picture and after the reload, I see the standard pic. After a
site refresh I am seeing the uploaded pic.

Do I have to set any headers?

greetings




Roland Huss wrote:
greeklinux wrote:
the line where the NullPointerException is thrown is:

resourceStream = new ResourceReference(MyApplication.PROFILE_STANDARD)
                                                .getResource().getResourceStream();

If I am using only this ResourceReference then it works. But I dont know why
the Exception is thrown.
You need to bind (==register) the ResourceReference to the application before you can get to the referenced Resource (which has been registered either explicitely or implicitely, if is a PackageResource):

resourceReference =  new ResourceReference(MyApplication.PROFILE_STANDARD);
resourceReference.bind(Application.get());
resourceStream = resourceReferece.getResource().getResourceStream();


...roland

Re: Resource/SharedResource - Display an image

by Roland Huss :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

greeklinux wrote:
your solution works. But the problem is that I am already registering the resource in
an Initializer. But ok...
When you bind a resource reference to an application, it's not about registering the resource itself
but looking it up. My fault.
greeklinux wrote:
Now I am uploading the picture and after the reload, I see the standard pic. After a
site refresh I am seeing the uploaded pic.

Do I have to set any headers?
Though I don't know your setup, but you need to refresh your image component (i.e. if it is done via an Ajax request). No headers needs to be set here.

... roland

Re: Resource/SharedResource - Display an image

by greeklinux :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

no I am not using any AJAX with this form. I only get the resource
and set it in an Image component. Maybe the uploaded picture is not
persistet fast enough to be available on the response...

Ok, I thank you for your help.

greetings


Roland Huss wrote:
Hi,

greeklinux wrote:
your solution works. But the problem is that I am already registering the resource in
an Initializer. But ok...
When you bind a resource reference to an application, it's not about registering the resource itself
but looking it up. My fault.
greeklinux wrote:
Now I am uploading the picture and after the reload, I see the standard pic. After a
site refresh I am seeing the uploaded pic.

Do I have to set any headers?
Though I don't know your setup, but you need to refresh your image component (i.e. if it is done via an Ajax request). No headers needs to be set here.

... roland
LightInTheBox - Buy quality products at wholesale price