RE: Re: how to Router.attach() a resource object

2009-09-23 Thread sam l
ah, thank you very much.
indeed, template names are directly inferred from the request path. 
So, I just modified DirectToTemplateResource.

Thank you again for prompt response.
It could have taken me a long time.

Thanks.
sam

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2398639


how to Router.attach() a resource object

2009-09-22 Thread webpost
hi,

I am trying to do:
{{{

final Router router = new Router();
router.attach(/about, new DirectToTemplateResource(about.xml).getClass());
router.attach(/contact, new DirectToTemplateResource(contact.xml));

}}}

DirectToTemplateResource will return StringRepresentation of HTML according to 
some template passed in the constructor.

But since Router tries to instantiate DirectToTemplateResource() using default 
constructor (which does not exist), I can't make it happen.

I am sure I'm not using Restlet properly.
What am I misunderstanding?

As a way around, I could do:
{{{
router.attach(/about, AboutResource.class);
router.attach(/contact, ContactResource.class);
}}}

But I need to create many similar classes.

I also tried to extend Restlet (or Application) because router.attach takes 
Restlet instance.

Is it safe for me to override Restlet.handle() method? So, my code would look 
like:
{{{
router.attach(/about, new RenderTemplateRestlet(about.xml));
router.attach(/contact, new RenderTemplateRestlet(contact.xml));
}}}

where RenderTemplateRestlet will have handle() method overloaded properly.

What's a best way to do what I'm trying to do?

Thanks.

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2397585


Re: how to Router.attach() a resource object

2009-09-22 Thread Rhett Sutphin
Hi Anonymous,

On Sep 21, 2009, at 4:03 PM, webp...@tigris.org wrote:

 hi,

 I am trying to do:
 {{{

 final Router router = new Router();
 router.attach(/about, new DirectToTemplateResource 
 (about.xml).getClass());
 router.attach(/contact, new DirectToTemplateResource 
 (contact.xml));

 }}}

 DirectToTemplateResource will return StringRepresentation of HTML  
 according to some template passed in the constructor.

 But since Router tries to instantiate DirectToTemplateResource()  
 using default constructor (which does not exist), I can't make it  
 happen.

 I am sure I'm not using Restlet properly.
 What am I misunderstanding?

Restlet resources are not singletons -- a new instance is created to  
handle each request.


 As a way around, I could do:
 {{{
 router.attach(/about, AboutResource.class);
 router.attach(/contact, ContactResource.class);
 }}}

 But I need to create many similar classes.

 I also tried to extend Restlet (or Application) because  
 router.attach takes Restlet instance.

 Is it safe for me to override Restlet.handle() method? So, my code  
 would look like:
 {{{
 router.attach(/about, new RenderTemplateRestlet(about.xml));
 router.attach(/contact, new RenderTemplateRestlet(contact.xml));
 }}}

 where RenderTemplateRestlet will have handle() method overloaded  
 properly.

This is one solution, but you lose out on a lot of Restlet's  
usefulness if you go this route.

 What's a best way to do what I'm trying to do?

Two options:

1) Since in your example the template filename seems to be based on  
the URL, you could modify DirectToTemplateResource to infer the  
template to use based on the request information.

2) If you do need to be explicit about the template filename (i.e., if  
the template filename isn't always directly related to the URL), you  
can extend org.restlet.Finder.  Depending on which version of restlet  
you're using, you'll need to either override #createTarget (1.1) or  
#create (2.0) to create instances of your resource.  Then you'll wire  
up instances of your new finder to the router.  See the Finder javadoc  
for more info.

Rhett

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2397825