Serve static content using SpringBeanRouter?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Serve static content using SpringBeanRouter?

Jorge Gallardo
Hello all,

Im trying to serve static content from the filesystem but I cant. I was able to do it with a plain and simple Reslet application but not using SpringBeanRouter. 
Here is the code:


@Component("/v1/images/{imageId}")

@Scope("prototype")

public class ImagesResource extends DirectoryServerResource {

private String imageId;

@Override

public void doInit() throws ResourceException {

Directory directory = new Directory(getContext(),

"file:///path/images");

getRequestAttributes().put("org.restlet.directory", directory);

super.doInit();

}

}

And here the xml mapping:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>

<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<property name="name" value="REST API" />

<property name="inboundRoot" ref="ServerHeaderFilter" />

<property name="encoderService.enabled" value="true" />

</bean>

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


Im facing two issues:

1- the URL mapping. If i use "/v1/images", SpringBeanRouter does not match it, so I went for "/v1/images/{imageId}"

2- If i dont put the Directory in the map, I get a NullPointerException. It looks odd, is there any other/better way of doing it?

Thanks!

--
Jorge Gallardo
----------------------------------------
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Serve static content using SpringBeanRouter?

Jerome Louvel-3
Hi Jorge,

Normally, you shouldn't embed a Directory inside a ServerResource but directly attach it to your SpringBeanRouter.
To not have to use the {imageId} path variable (for your file names I guess), you can set the route's matchingMode to Template.STARTS_WITH. I'm not sure exactly how to do it with SpringBeanRouter, but with a regular router you would do this:

    myRouter.attach("/v1/images/", myDirectory).setMatchingMode(Template.STARTS_WITH).



On Thu, May 29, 2014 at 1:48 PM, Jorge Gallardo <[hidden email]> wrote:
Hello all,

Im trying to serve static content from the filesystem but I cant. I was able to do it with a plain and simple Reslet application but not using SpringBeanRouter. 
Here is the code:


@Component("/v1/images/{imageId}")

@Scope("prototype")

public class ImagesResource extends DirectoryServerResource {

private String imageId;

@Override

public void doInit() throws ResourceException {

Directory directory = new Directory(getContext(),

"file:///path/images");

getRequestAttributes().put("org.restlet.directory", directory);

super.doInit();

}

}

And here the xml mapping:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>

<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<property name="name" value="REST API" />

<property name="inboundRoot" ref="ServerHeaderFilter" />

<property name="encoderService.enabled" value="true" />

</bean>

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


Im facing two issues:

1- the URL mapping. If i use "/v1/images", SpringBeanRouter does not match it, so I went for "/v1/images/{imageId}"

2- If i dont put the Directory in the map, I get a NullPointerException. It looks odd, is there any other/better way of doing it?

Thanks!

--
Jorge Gallardo
----------------------------------------
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Serve static content using SpringBeanRouter?

Jorge Gallardo
Thanks Jerome for the prompt reply.

So after I got some rest I could address this with a clearer mind. I resolved it this way:


spring xml:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<!-- the defaultTarget for this component is our Restlet Application -->

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>


<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<!-- all requests to this Application will be sent to myPath2BeanRouter -->

<property name="name" value="REST API" />

<!--property name="inboundRoot" ref="authenticator" / -->

<property name="inboundRoot" ref="springRouter" />

<property name="encoderService.enabled" value="true" />

</bean>


<!-- Reslet component's Context bean -->

<bean id="restletComponent.context"

class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />


<bean id="restletContext" factory-bean="restletComponent.context"

factory-method="createChildContext" scope="prototype" />


<bean name="/images" autowire="byName" scope="prototype"

class="com.zebra.rest.directory.ImagesDirectory">

<constructor-arg ref="restletContext" />

</bean>


<!-- Singleton instance of this class -->

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


and then I created a simple Java class extending from Directory

import org.restlet.Context;

import org.restlet.resource.Directory;


public class ImagesDirectory extends Directory {


public ImagesDirectory(Context context) {

super(context, "clap://class/images");

}

}


And Voilá! it worked. 

The only funny thing I see is double logging:

May 30, 2014 10:59:46 AM org.restlet.engine.log.LogFilter afterHandle

INFO: 2014-05-30 10:59:46 0:0:0:0:0:0:0:1 - 0:0:0:0:0:0:0:1 9091 GET /images/zebra_logo.png - 200 - 0 15 http://localhost:9091 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 -

May 30, 2014 10:59:46 AM org.restlet.engine.log.LogFilter afterHandle

INFO: 2014-05-30 10:59:46 0:0:0:0:0:0:0:1 - 0:0:0:0:0:0:0:1 9091 GET /images/zebra_logo.png - 200 - 0 1 http://localhost:9091 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 -

Could be related to the clap protocol? Other than that, I debugged it and looks it is working as expected, nothing strange under the sun.


Thanks!

-JG




On Thu, May 29, 2014 at 4:15 PM, Jerome Louvel <[hidden email]> wrote:
Hi Jorge,

Normally, you shouldn't embed a Directory inside a ServerResource but directly attach it to your SpringBeanRouter.
To not have to use the {imageId} path variable (for your file names I guess), you can set the route's matchingMode to Template.STARTS_WITH. I'm not sure exactly how to do it with SpringBeanRouter, but with a regular router you would do this:

    myRouter.attach("/v1/images/", myDirectory).setMatchingMode(Template.STARTS_WITH).



On Thu, May 29, 2014 at 1:48 PM, Jorge Gallardo <[hidden email]> wrote:
Hello all,

Im trying to serve static content from the filesystem but I cant. I was able to do it with a plain and simple Reslet application but not using SpringBeanRouter. 
Here is the code:


@Component("/v1/images/{imageId}")

@Scope("prototype")

public class ImagesResource extends DirectoryServerResource {

private String imageId;

@Override

public void doInit() throws ResourceException {

Directory directory = new Directory(getContext(),

"file:///path/images");

getRequestAttributes().put("org.restlet.directory", directory);

super.doInit();

}

}

And here the xml mapping:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>

<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<property name="name" value="REST API" />

<property name="inboundRoot" ref="ServerHeaderFilter" />

<property name="encoderService.enabled" value="true" />

</bean>

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


Im facing two issues:

1- the URL mapping. If i use "/v1/images", SpringBeanRouter does not match it, so I went for "/v1/images/{imageId}"

2- If i dont put the Directory in the map, I get a NullPointerException. It looks odd, is there any other/better way of doing it?

Thanks!

--
Jorge Gallardo
----------------------------------------
[hidden email]




--
Jorge Gallardo
----------------------------------------
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Serve static content using SpringBeanRouter?

Jerome Louvel-3
Thanks Jorge for the follow-up with your solution!
I'm not sure why you get two GET calls. Can you try with a regular HTTP client such as POSTMAN?

Jérôme



On Fri, May 30, 2014 at 9:00 AM, Jorge Gallardo <[hidden email]> wrote:
Thanks Jerome for the prompt reply.

So after I got some rest I could address this with a clearer mind. I resolved it this way:


spring xml:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<!-- the defaultTarget for this component is our Restlet Application -->

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>


<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<!-- all requests to this Application will be sent to myPath2BeanRouter -->

<property name="name" value="REST API" />

<!--property name="inboundRoot" ref="authenticator" / -->

<property name="inboundRoot" ref="springRouter" />

<property name="encoderService.enabled" value="true" />

</bean>


<!-- Reslet component's Context bean -->

<bean id="restletComponent.context"

class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />


<bean id="restletContext" factory-bean="restletComponent.context"

factory-method="createChildContext" scope="prototype" />


<bean name="/images" autowire="byName" scope="prototype"

class="com.zebra.rest.directory.ImagesDirectory">

<constructor-arg ref="restletContext" />

</bean>


<!-- Singleton instance of this class -->

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


and then I created a simple Java class extending from Directory

import org.restlet.Context;

import org.restlet.resource.Directory;


public class ImagesDirectory extends Directory {


public ImagesDirectory(Context context) {

super(context, "clap://class/images");

}

}


And Voilá! it worked. 

The only funny thing I see is double logging:

May 30, 2014 10:59:46 AM org.restlet.engine.log.LogFilter afterHandle

INFO: 2014-05-30 10:59:46 0:0:0:0:0:0:0:1 - 0:0:0:0:0:0:0:1 9091 GET /images/zebra_logo.png - 200 - 0 15 http://localhost:9091 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 -

May 30, 2014 10:59:46 AM org.restlet.engine.log.LogFilter afterHandle

INFO: 2014-05-30 10:59:46 0:0:0:0:0:0:0:1 - 0:0:0:0:0:0:0:1 9091 GET /images/zebra_logo.png - 200 - 0 1 http://localhost:9091 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 -

Could be related to the clap protocol? Other than that, I debugged it and looks it is working as expected, nothing strange under the sun.


Thanks!

-JG




On Thu, May 29, 2014 at 4:15 PM, Jerome Louvel <[hidden email]> wrote:
Hi Jorge,

Normally, you shouldn't embed a Directory inside a ServerResource but directly attach it to your SpringBeanRouter.
To not have to use the {imageId} path variable (for your file names I guess), you can set the route's matchingMode to Template.STARTS_WITH. I'm not sure exactly how to do it with SpringBeanRouter, but with a regular router you would do this:

    myRouter.attach("/v1/images/", myDirectory).setMatchingMode(Template.STARTS_WITH).



On Thu, May 29, 2014 at 1:48 PM, Jorge Gallardo <[hidden email]> wrote:
Hello all,

Im trying to serve static content from the filesystem but I cant. I was able to do it with a plain and simple Reslet application but not using SpringBeanRouter. 
Here is the code:


@Component("/v1/images/{imageId}")

@Scope("prototype")

public class ImagesResource extends DirectoryServerResource {

private String imageId;

@Override

public void doInit() throws ResourceException {

Directory directory = new Directory(getContext(),

"file:///path/images");

getRequestAttributes().put("org.restlet.directory", directory);

super.doInit();

}

}

And here the xml mapping:

<bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent">

<property name="defaultTarget" ref="restletApplication" />

<property name="clientsList">

<list>

<value>CLAP</value>

<value>FILE</value>

</list>

</property>

</bean>

<bean id="restletApplication" class="org.restlet.ext.wadl.WadlApplication"

scope="singleton">

<property name="name" value="REST API" />

<property name="inboundRoot" ref="ServerHeaderFilter" />

<property name="encoderService.enabled" value="true" />

</bean>

<bean name="springRouter" class="org.restlet.ext.spring.SpringBeanRouter" />


Im facing two issues:

1- the URL mapping. If i use "/v1/images", SpringBeanRouter does not match it, so I went for "/v1/images/{imageId}"

2- If i dont put the Directory in the map, I get a NullPointerException. It looks odd, is there any other/better way of doing it?

Thanks!

--
Jorge Gallardo
----------------------------------------
[hidden email]




--
Jorge Gallardo
----------------------------------------
[hidden email]