users@jersey.java.net

Re: [Jersey] serving static content from a resource

From: Robert Koberg <rob_at_koberg.com>
Date: Fri, 12 Feb 2010 07:02:13 -0800

On Feb 12, 2010, at 6:32 AM, Paul Sandoz wrote:

>
> On Feb 11, 2010, at 8:18 PM, Arul Dhesiaseelan wrote:
>
>> I am using velocity. I would like to get rid of the Velocity servlet and use the template processor instead.
>>
>> I hope this is possible.
>>
>
> Yes, it is possible.


You can also send a file:

File file = new File(base, path);
if (!file.exists()) {
  return some Response or create new file;
}
return Response.ok(file).build();

I am wondering if there are issues with this approach since Paul posts the class below.

-Rob


> You can easily write a template processor that simply spits out the contents of a resource:
>
> public class HTMLViewProcessor implements ViewProcessor<URL> {
>
> public URL resolve(String path) {
> Classloader c = .... // use thread context class loader
> // Perhaps check if path is an HTML file
> return c.getResource(path);
>
> // Or if you want things picked up from the Web area inject ServletContext
> // ServletContext sc = ...
> // return sc.getResource(path); // catch MalformedURLException
> }
>
> public void writeTo(URL resource, Viewable viewable, OutputStream out) throws IOException {
> ReaderWriter.writeTo(resource.openStream(), out);
> }
> }
>
> This has the advantage that it will also work with templates located according to the rules of Jersey MVC.
>
> Note that ViewProcessor was introduced as the replacement for TemplateProcessor in Jersey 1.1.5.
>
> I think i will change Viewable such that you can do:
>
> public Viewable getHomePage() {
> return new Viewable("/home.html");
> }
>
>
> However, as Jan says you can also place stuff directly to be served by the servlet container if you so wish.
>
> Paul.
>
>> On Feb 11, 2010, at 12:05 PM, Jan Algermissen wrote:
>>
>>>
>>> On Feb 11, 2010, at 7:54 PM, Arul Dhesiaseelan wrote:
>>>
>>>> Hi,
>>>>
>>>> Is it possible to serve static content from a resource?
>>>>
>>>> I would like to render HTML from a resource method using explicit MVC, something like:
>>>>
>>>> @Path("/")
>>>> public class RootResource {
>>>>
>>>> @Produces(MediaType.TEXT_HTML)
>>>> @GET
>>>> public Viewable getHomePage() {
>>>> return new Viewable("/home.html", null);
>>>
>>>
>>> Why don't you put that HTML file in a static loocation of your WAR?
>>>
>>> Jan
>>>
>>>
>>>> }
>>>> }
>>>>
>>>> Would this work?
>>>>
>>>> Thank you,
>>>> Arul
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>
>>> -----------------------------------
>>> Jan Algermissen, Consultant
>>> NORD Software Consulting
>>>
>>> Mail: algermissen_at_acm.org
>>> Blog: http://www.nordsc.com/blog/
>>> Work: http://www.nordsc.com/
>>> -----------------------------------
>>>
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>