You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Leszek Gawron <lg...@apache.org> on 2007/09/17 11:51:24 UTC

serving images from db - direct url access

Hello,

I already know how to serve an image from database - dead easy. Thing is 
the image is a component. What i would like to achieve now is to have a 
very small image retrieving api:

http://host/mywicketapp/images/<id>
http://host/mywicketapp/images/<id>/next
http://host/mywicketapp/images/<id>/previous
http://host/mywicketapp/images/latest

how do I mount a Resource at specified path?
how do I pass parameters to it ? (/next /previous)


-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by Johan Compagner <jc...@gmail.com>.
The instance of CameraSnapshotResource is cached/stored
But internally what you do there again is up to you

johan


On 9/19/07, Leszek Gawron <lg...@apache.org> wrote:
>
> Johan Compagner wrote:
> > setCached causes wicket to send http cache headers. Resources it self
> > are cached in SharedResources. The resource itself is responsable what
> > is cached internally
>
> what does it mean that Resource is cached in SharedResources?
>
> In my case:
>
> getSharedResources().add(       "snapshot",
>                                 new CameraSnapshotResource() );
> mountSharedResource(    "snapshot",
>                                 new ResourceReference( "snapshot"
>                                  ).getSharedResourceKey() );
>
>
> there is a single resources but it produces different content basing on
> request parameters.
>
> --
> Leszek Gawron
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@apache.org>.
Johan Compagner wrote:
> setCached causes wicket to send http cache headers. Resources it self
> are cached in SharedResources. The resource itself is responsable what
> is cached internally

what does it mean that Resource is cached in SharedResources?

In my case:

getSharedResources().add(	"snapshot",
				new CameraSnapshotResource() );
mountSharedResource(	"snapshot",
				new ResourceReference( "snapshot"
                                 ).getSharedResourceKey() );


there is a single resources but it produces different content basing on 
request parameters.

-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by Johan Compagner <jc...@gmail.com>.
setCached causes wicket to send http cache headers. Resources it self
are cached in SharedResources. The resource itself is responsable what
is cached internally

On 9/17/07, Leszek Gawron <lg...@apache.org> wrote:
> Johan Compagner wrote:
> > ahh ok that seems a bug can you report this?
>
> sure can,
>
> one last question: there is a method set/getCached(). Does this tell the
> browser to cache the resource or does wicket cache it itself?
>
> --
> Leszek Gawron
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@apache.org>.
Johan Compagner wrote:
> ahh ok that seems a bug can you report this?

sure can,

one last question: there is a method set/getCached(). Does this tell the 
browser to cache the resource or does wicket cache it itself?

-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Johan Compagner wrote:
> ahh ok that seems a bug can you report this?

It looks like this is the problem:

> @Override
> public IResourceStream getResourceStream() {
> 	final ValueMap params = getParameters();
> 
> 	return new AbstractStringResourceStream() {
> 		@Override
> 		protected String getString() {
> 			// this wil work only with resource/id/1 
> 			CameraSnapshot current = cameraSnapshotService.findById( params.getLong( "id" ) );
> 			return "";
> 		}
> 	};
> }

> @Override
> public IResourceStream getResourceStream() {
> 	return new AbstractStringResourceStream() {
> 		@Override
> 		protected String getString() {
> 			// this wil work only with resource?id=1 
> 			CameraSnapshot current = cameraSnapshotService.findById( getParameters().getLong( "id" ) );
> 			return "";
> 		}
> 	};
> }

-- 
Leszek Gawron                         http://www.mobilebox.pl/krs.html
CTO at MobileBox Ltd.


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


Re: serving images from db - direct url access

Posted by Johan Compagner <jc...@gmail.com>.
ahh ok that seems a bug can you report this?


On 9/17/07, Leszek Gawron <lg...@apache.org> wrote:
>
> Johan Compagner wrote:
> > thats completely automatic.
> >
> > in your Resource that you have mounted under /images/ when you get a
> request
> > to it
> > you just do: Resource.getParameters()
>
> yes I did that. when requesting the resource like this:
>
> http://host/app/resource?id=1
> the id is visible in ValueMap
>
> when requesting
> http://host/app/resource/id/1
>
> the id is NOT visible (although properly decoded by url coding strategy)
>
> --
> Leszek Gawron
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@apache.org>.
Johan Compagner wrote:
> thats completely automatic.
> 
> in your Resource that you have mounted under /images/ when you get a request
> to it
> you just do: Resource.getParameters()

yes I did that. when requesting the resource like this:

http://host/app/resource?id=1
the id is visible in ValueMap

when requesting
http://host/app/resource/id/1

the id is NOT visible (although properly decoded by url coding strategy)

-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by Johan Compagner <jc...@gmail.com>.
thats completely automatic.

in your Resource that you have mounted under /images/ when you get a request
to it
you just do: Resource.getParameters()

johan


On 9/17/07, Leszek Gawron <lg...@apache.org> wrote:
>
> Johan Compagner wrote:
> > look at WebApplication.mountSharedResources()
> > and RequestCycle.urlFor(final ResourceReference resourceReference,
> ValueMap
> > parameters)
>
> I did the exact thing. Please see my others post about decoding request
> parameters.
>
> --
> Leszek Gawron
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@apache.org>.
Johan Compagner wrote:
> look at WebApplication.mountSharedResources()
> and RequestCycle.urlFor(final ResourceReference resourceReference, ValueMap
> parameters)

I did the exact thing. Please see my others post about decoding request 
parameters.

-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by Johan Compagner <jc...@gmail.com>.
look at WebApplication.mountSharedResources()
and RequestCycle.urlFor(final ResourceReference resourceReference, ValueMap
parameters)

johan


On 9/17/07, Leszek Gawron <lg...@apache.org> wrote:
>
> Hello,
>
> I already know how to serve an image from database - dead easy. Thing is
> the image is a component. What i would like to achieve now is to have a
> very small image retrieving api:
>
> http://host/mywicketapp/images/<id>
> http://host/mywicketapp/images/<id>/next
> http://host/mywicketapp/images/<id>/previous
> http://host/mywicketapp/images/latest
>
> how do I mount a Resource at specified path?
> how do I pass parameters to it ? (/next /previous)
>
>
> --
> Leszek Gawron
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: serving images from db - direct url access

Posted by David Bernard <dw...@free.fr>.
To fixe the decoding bug (I already submit a bug+testcase), I use MyMixedParamUrlCodingStrategy :

public class MyMixedParamUrlCodingStrategy extends MixedParamUrlCodingStrategy {

     public MyMixedParamUrlCodingStrategy(String mountPath, Class bookmarkablePageClass, String[] parameterNames) {
         super(mountPath, bookmarkablePageClass, parameterNames);
     }

     @Override
     public void appendParameters(AppendingStringBuffer url, Map parameters) {
         super.appendParameters(url, parameters);
     }

     @Override
     public ValueMap decodeParameters(String urlFragment, Map urlParameters) {
         ValueMap back = super.decodeParameters(urlFragment, urlParameters);
         for (Object key : back.keySet()) {
             back.put(key, urlDecode(back.getString((String)key)));
         }
         return back;
     }
}

May be, you could do the same for SharedResourceRequestTargetUrlCodingStrategy.

Regards.

Leszek Gawron wrote:
> David Bernard wrote:
>> Hi,
>>
>> You can mount with a coding strategy in Application.init, something 
>> like .. (I do it for attachment)
>>             mount(new MyMixedParamUrlCodingStrategy("/images", 
>> Page.class, new String[] { "id", "move" }) {
>>                 @Override
>>                 public IRequestTarget decode(RequestParameters 
>> requestParameters) {
>>                     try {
>>                         ValueMap params = 
>> decodeParameters(requestParameters.getPath().substring(getMountPath().length()), 
>> requestParameters.getParameters());
>>                         String id = params.getString("id");
>>                         String move = params.getString("move");
>>                         if (StringUtils.isNotBlank(id)) {
>>                             ...
>>                             return new ResourceStreamRequestTarget(...);
>>                         }
>>                         return new 
>> WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND);
> 
> nice to know how to return errors :)
> 
>>                         //return super.decode(requestParameters);
>>                     } catch (RuntimeException exc) {
>>                         throw exc;
>>                     } catch (Exception exc) {
>>                         throw new RuntimeException("wrap: " + 
>> exc.getMessage(), exc);
>>                     }
>>                 }
>>             });
> 
> Thanks, I managed to do something like this:
> 
>> getSharedResources().add(    "snapshot", new CameraSnapshotResource() );
>> mount( new SharedResourceRequestTargetUrlCodingStrategy( "/snapshot", 
>> new ResourceReference( "snapshot" ).getSharedResourceKey() ) );
> 
>> public class CameraSnapshotResource extends DynamicWebResource {
>>     @SpringBean
>>     private CameraSnapshotService    cameraSnapshotService;
>>
>>     public CameraSnapshotResource() {
>>         InjectorHolder.getInjector().inject( this );
>>     }
>>
>>     @Override
>>     protected ResourceState getResourceState() {
>>         ValueMap parameters = getParameters();
>>         Long id = parameters.getLong( "id" );
>>         final CameraSnapshot snapshot = 
>> cameraSnapshotService.findById( id );
>>
>>         return new ResourceState() {
>>             @Override
>>             public String getContentType() {
>>                 return "image/jpeg";
>>             }
>>
>>             @Override
>>             public byte[] getData() {
>>                 try {
>>                     return snapshot.getData().getBytes( 1,
>>                                                         
>> snapshot.getSize() );
>>                 } catch ( SQLException e ) {
>>                     throw new RuntimeException( "unable to fetch image 
>> data", e );
>>                 }
>>             }
>>         };
>>     }
>> }
> 
> Now I have a problem:
> 
> http://host/wicketapp/snapshot?id=1 works
> http://host/wicketapp/snapshot/id/1 does not (although when debugging I 
> see parameters get decoded).
> 

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


Re: serving images from db - direct url access

Posted by Leszek Gawron <lg...@apache.org>.
David Bernard wrote:
> Hi,
> 
> You can mount with a coding strategy in Application.init, something like 
> .. (I do it for attachment)
>             mount(new MyMixedParamUrlCodingStrategy("/images", 
> Page.class, new String[] { "id", "move" }) {
>                 @Override
>                 public IRequestTarget decode(RequestParameters 
> requestParameters) {
>                     try {
>                         ValueMap params = 
> decodeParameters(requestParameters.getPath().substring(getMountPath().length()), 
> requestParameters.getParameters());
>                         String id = params.getString("id");
>                         String move = params.getString("move");
>                         if (StringUtils.isNotBlank(id)) {
>                             ...
>                             return new ResourceStreamRequestTarget(...);
>                         }
>                         return new 
> WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND);

nice to know how to return errors :)

>                         //return super.decode(requestParameters);
>                     } catch (RuntimeException exc) {
>                         throw exc;
>                     } catch (Exception exc) {
>                         throw new RuntimeException("wrap: " + 
> exc.getMessage(), exc);
>                     }
>                 }
>             });

Thanks, I managed to do something like this:

> getSharedResources().add(	"snapshot", new CameraSnapshotResource() );
> mount( new SharedResourceRequestTargetUrlCodingStrategy( "/snapshot", new ResourceReference( "snapshot" ).getSharedResourceKey() ) );

> public class CameraSnapshotResource extends DynamicWebResource {
> 	@SpringBean
> 	private CameraSnapshotService	cameraSnapshotService;
> 
> 	public CameraSnapshotResource() {
> 		InjectorHolder.getInjector().inject( this );
> 	}
> 
> 	@Override
> 	protected ResourceState getResourceState() {
> 		ValueMap parameters = getParameters();
> 		Long id = parameters.getLong( "id" );
> 		final CameraSnapshot snapshot = cameraSnapshotService.findById( id );
> 
> 		return new ResourceState() {
> 			@Override
> 			public String getContentType() {
> 				return "image/jpeg";
> 			}
> 
> 			@Override
> 			public byte[] getData() {
> 				try {
> 					return snapshot.getData().getBytes( 1,
> 														snapshot.getSize() );
> 				} catch ( SQLException e ) {
> 					throw new RuntimeException( "unable to fetch image data", e );
> 				}
> 			}
> 		};
> 	}
> }

Now I have a problem:

http://host/wicketapp/snapshot?id=1 works
http://host/wicketapp/snapshot/id/1 does not (although when debugging I 
see parameters get decoded).

-- 
Leszek Gawron

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


Re: serving images from db - direct url access

Posted by David Bernard <dw...@free.fr>.
Hi,

You can mount with a coding strategy in Application.init, something like .. (I do it for attachment)
             mount(new MyMixedParamUrlCodingStrategy("/images", Page.class, new String[] { "id", "move" }) {
                 @Override
                 public IRequestTarget decode(RequestParameters requestParameters) {
                     try {
                         ValueMap params = decodeParameters(requestParameters.getPath().substring(getMountPath().length()), requestParameters.getParameters());
                         String id = params.getString("id");
                         String move = params.getString("move");
                         if (StringUtils.isNotBlank(id)) {
                             ...
                             return new ResourceStreamRequestTarget(...);
                         }
                         return new WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND);
                         //return super.decode(requestParameters);
                     } catch (RuntimeException exc) {
                         throw exc;
                     } catch (Exception exc) {
                         throw new RuntimeException("wrap: " + exc.getMessage(), exc);
                     }
                 }
             });


/david
Leszek Gawron wrote:
> Hello,
> 
> I already know how to serve an image from database - dead easy. Thing is 
> the image is a component. What i would like to achieve now is to have a 
> very small image retrieving api:
> 
> http://host/mywicketapp/images/<id>
> http://host/mywicketapp/images/<id>/next
> http://host/mywicketapp/images/<id>/previous
> http://host/mywicketapp/images/latest
> 
> how do I mount a Resource at specified path?
> how do I pass parameters to it ? (/next /previous)
> 
> 

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