users@grizzly.java.net

Re: Serving static files with CLStaticHttpHandler

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Wed, 12 Jun 2013 07:51:10 -0700

Hi Christian,

can you pls. file the JIRA issue here:
https://java.net/jira/browse/GRIZZLY

I'll add this functionality asap.

Thanks.

WBR,
Alexey.

On 12.06.13 06:07, Christian Helmbold wrote:
> Hi,
>
> I want to serve static files which are under target/classes/templates in my maven project structure or in the corresponding .jar file. I need both options, because I want to serve the files during development (where I don't package .jars) and during production.
>
> I tried it like this:
>
> server.getServerConfiguration().addHttpHandler(
> new CLStaticHttpHandler(Main.class.getClassLoader()), "/static");
>
> But if I look at the source code I'm missing an option to set a base path. The request URI is used directly to look up the resource starting in the root directory of the class loader: https://github.com/GrizzlyNIO/grizzly-mirror/blob/2.3.x/modules/http-server/src/main/java/org/glassfish/grizzly/http/server/CLStaticHttpHandler.java#L106
>
> If I use the StaticHttpHandler like this:
>
> server.getServerConfiguration().addHttpHandler(
>
> new StaticHttpHandler(getTemplatePath()), "/static");
>
> // ...
>
> private static String getTemplatePath() throws URISyntaxException {
> return Main.class.getClassLoader().getResource("templates/static").getPath();
> }
>
> everything works as expected, as long as I'm not running the application as a jar.
>
> How can I serve static files from the file system and from within a .jar with Grizzly?
>
> Thanks,
> Christian