Serving static files with CLStaticHttpHandler

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

Serving static files with CLStaticHttpHandler

Christian Helmbold
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
Reply | Threaded
Open this post in threaded view
|

Re: Serving static files with CLStaticHttpHandler

oleksiys
Administrator
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

Reply | Threaded
Open this post in threaded view
|

Re: Serving static files with CLStaticHttpHandler

Christian Helmbold
Hi Alexey,

thanks for your quick reply. I've filed the issue: https://java.net/jira/browse/GRIZZLY-1525

I'm looking forward to your implementation.

Thanks.

Christian
Reply | Threaded
Open this post in threaded view
|

Re: Serving static files with CLStaticHttpHandler

oleksiys
Administrator
Hi Christian,

can you pls. check out the latest Grizzly 2.3.4-SNAPSHOT from this
repository?
You have to be able to set doc root either via constructor or using
CLStaticHttpHandler.addDocRoot(String) method.

Pls. let me know if it works for you.

Thanks.

WBR,
Alexey.

[1] https://maven.java.net/content/repositories/snapshots/

On 12.06.13 12:59, Christian Helmbold wrote:
> Hi Alexey,
>
> thanks for your quick reply. I've filed the issue: https://java.net/jira/browse/GRIZZLY-1525
>
> I'm looking forward to your implementation.
>
> Thanks.
>
> Christian

Reply | Threaded
Open this post in threaded view
|

Re: Serving static files with CLStaticHttpHandler

Christian Helmbold
Hi Alexey,

I'm excited about your quick improvement. It works like a charm!

Thank you very much.

Christian