users@grizzly.java.net

Re: Confusion around GrizzlyWebServer, GrizzlyAdapter, and serving static resources

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Wed, 11 Nov 2009 12:27:51 +0100

Hi Jon.

if you want to use dynamic Adapter, then static resource processing
will not work.
You'll need to add static resource adapter explicitly like [1].

Hope this helps.

WBR,
Alexey.

[1]

         test.addGrizzlyAdapter(...);
         test.addGrizzlyAdapter(...);
         test.addGrizzlyAdapter(...);
         test.addGrizzlyAdapter(...);
// Here add adapter for static resources:

         test.addGrizzlyAdapter(new StaticGrizzlyAdapter("/Users/
me/"), new String[]{"/"});

     static class StaticGrizzlyAdapter extends GrizzlyAdapter {

         public StaticGrizzlyAdapter(String publicDirectory) {
             super(publicDirectory);
             setHandleStaticResources(true);
         }

         public void service(GrizzlyRequest grizzlyRequest,
GrizzlyResponse grizzlyResponse) {
             try {
                 grizzlyResponse.setStatus(404);
                 grizzlyResponse.getWriter().print("Resoure can not be
found");
             } catch (IOException e) {
             }
         }
     }


On Nov 10, 2009, at 18:32 , Jonathan Gold wrote:

> I'm trying to use GrizzlyWebServer to serve dynamic requests
> (serviced by my
> sublcass of GrizzlyAdapter) from uri path '/dynamic' and static
> resources from
> '/www'. I've tried various combinations of things as illustrated in
> the
> tutorials and in the javadocs online, but am not having success.
>
> What I'm finding:
>
> - If I do this:
>
> GrizzlyWebServer gws = new GrizzlyWebServer( port, docRoot );
>
> then I get a webserver that will serve the static files, but of
> course no
> dynamic ones since I haven't added the adapter.
>
> - If I do this:
>
> GrizzlyWebServer gws = new GrizzlyWebServer( port, docRoot );
> gws.addGrizzlyAdapter( adapter, new String[] { "/dynamic" } );
>
> then I can handle dynamic requests but static resources come
> back as not
> found (nor could I have their uri root be /www)
>
> - If I do this:
>
> GrizzlyWebServer gws = new GrizzlyWebServer( port );
> adapter.setResourcesContextPath( "/www" );
> adapter.setRootFolder( docRoot );
> gws.addGrizzlyAdapter( adapter, new String[] { "/dynamic" } );
>
> then I can serve dynamic requests but get a 404 for static files.
> Additionally, javac warns that setRootFolder() is deprecated,
> though the
> javadocs don't say so nor do they mention what I should use
> instead. I
> looked in the source and saw this:
>
> /**
> * Set the directory from where files will be serviced.
> *
> * NOTE: For backward compatibility, invoking that method will
> * clear all previous values added using {_at_link
> #addRootFolder}.
> *
> * @param rootFolder the directory from where files will be
> serviced.
> *
> * @deprecated - use {_at_link #addRootFolders}
> */
> public void setRootFolder(String rootFolder) {
> rootFolders.clear();
> addRootFolder(rootFolder);
> }
>
> It looks like the target of the @link tag is a typo. There is a
> method
> addRootFolder (singular), but for reasons I don't understand,
> it does not
> show up in the javadocs online.
>
> Can someone can help me understand what's happening here? I'm using
> grizzly-1.9.18e which I got from the dev site as the latest stable
> release.
>
> jon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>