Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] How to anable symlink / aliases for embedded jetty resources

also, it was a bit surprising, that to make aliases work, I had to call
setAliases(true) not on ResourceHandler, but on ContextHandler
which was wrapped around it.

On 22/09/12 00:13, Olek Swirski wrote:
Joakim,
thx for pointing me to the examples. I managed to do what I wanted,
by using ResourceHandler together with WebAppContext. while working on
it, it turned out, that I don't even need to enable aliases, as I can just choose
resourceBase pointing to my target dir, for the new ResourceHandler. And
I attached it at the context, where symlink was supposed to be - so it works
like alias. I left alias setting enabled in code below, just for reference if someone else
would be looking for it. Also I noticed that for WebAppContext setAliases(true) didn't
really make symlinks work, and this is primary why I got confused. But it did
work with ResourceHandler. So what's the meaning of this setting for a
WebAppContext, why is it different?

Anyway here is what I ended up with:

val port = portFromCommandLine.getOrElse(Props.getInt("jetty.emb.port", 9090))
    val connector = new SelectChannelConnector()
    connector.setPort(port)
    println("USING PORT: " + port)
    val server = new Server()
    server.addConnector(connector)

    val webctx = new WebAppContext
    /* use embedded webapp dir as source of the web content
     * web.xml is in default location, of embedded webapp dir,
     * so don't need to adjust that */
    val webappDirInsideJar = webctx.getClass.getClassLoader.getResource("webapp").toExternalForm
    webctx.setWar(webappDirInsideJar)

    /* might use use external pre-existing webapp dir instead of referencing
     * the embedded webapp dir but it's not very useful. why would we put
     * webapp inside jar if we end up using some other external webapp dir.
     * I put it for reference, as it may make sense under some circumstances.
     * webctx.setResourceBase("/path/to/existing/webapp-dir") */

    webctx.setContextPath("/")
    /* optionally extract embedded webapp to specific temporary location and serve
     * from there. Often /tmp is used, but it is not always advisable, because it
     * gets cleaned-up from time to time, so you need to use other location such as
     * /var/www/sqrlrcrd.com for anything that should last */
    Props.get("jetty.emb.tmpdir").foreach(dir => webctx.setTempDirectory(new File(dir)))


    val albumResHndlr = new ResourceHandler()
    albumResHndlr.setResourceBase(Props.get("album.dir","/tmp/album"))
    // albumResHndlr.setDirectoriesListed(true)
    val albumCtx = new ContextHandler()
    albumCtx.setContextPath("/album")
   /* didn't really need line below -aliases - after all, as setResourceBase
    pointed to the external dir I wanted */
    albumCtx.setAliases(true)
    albumCtx.setHandler(albumResHndlr)

    val handlerList = new HandlerList()
    handlerList.setHandlers(Array(albumCtx, webctx))

    server.setHandler(handlerList)
    server.start
    server.join

On 21/09/12 16:44, Joakim Erdfelt wrote:
Since you are doing embedded work, you might want to take a look at our embedded jetty examples.

Of particular interest might be FileServer.java
Which uses ResourceHandler to serve files.
Incidentally, we serve javadoc in the distribution with ResourceHandler -- ${jetty.home}/contexts/javadoc.xml

Also note that the WebAppContext.setDefaultsDescriptor() is how you can configure to use the default servlet from a WebAppContext.

For an example of this usage, see the distribution again -- ${jetty.home}/contexts/test.xml


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Developer advice, services and support
from the Jetty & CometD experts.



On Fri, Sep 21, 2012 at 7:22 AM, Olek Swirski <olekswirski@xxxxxxxxx> wrote:
Hi,
I'm using embedded jetty instance to serve my Lift application (Scala based web framework).
I use a runnable Start object with main method, which configures embedded jetty and starts
the server. So the jar is executed like so: java -jar my-webapp.jar
My application uses a directory, where images are stored. It can grow quite big and because
of that I don't want to include it and deploy together with my runnable jar. So the ideal
thing to do for me is to use a symlink to some external dir holding these pictures. However,
I have problem setting this up. When I was using standalone jetty entry like this:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>aliases</param-name>
<param-value>true</param-value>
</init-param>
</servlet>

in etc/webdefault.xml was working fine. But in my embedded jetty i'm not
using xml file config. Instead my setup is done in the main method of executable
class, that runs the jar (please take a look below the messag to see the code -
(it's in Scala but should be very easy to understand for java devs). So, the thing is
I tried:
webctx.setAttribute("aliases", "true")
and also:
webctx.setInitParameter("aliases", "true")
but then, when restarted jetty, nothing really changed, and aliases / symlinks
were not traversed by jetty. Instead I got 404s.
I was trying to set up a separate handler, and call .setAliases(true) on it, but
then when I did:
server.setHandler(webctx)
server.setHandler(aliasedHandler)
server.start
server.join
the app wouldn't work even for non-aliased files. I understand, probably this
was because of both handlers overlapping. But I'm not very experienced with
jetty, so please let me know, how to make it work.  So what should I do to have
it work same way as with xml based
<param-name>aliases</param-name>
<param-value>true</param-value> ?

I did search for quite long time to find an answer for this, but didn't find
anything that actually worked. Please share your knowledge on this subject,
any hint will be greatly appreciated :) Here is my current Scala code
which unfortunately doesn't work with aliases / symlinks:

val port = portFromCommandLine.getOrElse(Props.getInt("jetty.emb.port", 9090))
    println("USING PORT: " + port)
    val server = new Server(port)
    val webctx = new WebAppContext
    /* use embedded webapp dir as source of the web content
     * web.xml is in default location, of embedded webapp dir,
     * so don't need to adjust that */
    val webappDirInsideJar = webctx.getClass.getClassLoader.getResource("webapp").toExternalForm
    webctx.setWar(webappDirInsideJar)
    /* might use use external pre-existing webapp dir instead of referencing
     * the embedded webapp dir but it's not very useful. why would we put
     * webapp inside jar if we end up using some other external webapp dir.
     * I put it for reference, as it may make sense under some circumstances.
     * webctx.setResourceBase("/path/to/existing/webapp-dir") */
    webctx.setContextPath("/")
    webctx.setAttribute("aliases", "true")

    /* optionally extract embedded webapp to specific temporary location and serve
     * from there. In fact /tmp is not a good place, because it gets cleaned up from
     * time to time so you need to specify some location such as /var/www/sqrlrcrd.com
     * for anything that should last */
    val shouldExtract = Props.getBool("jetty.emb.extract", false)
    if (shouldExtract) {
      val webtmpdir = Props.get("jetty.emb.tmpdir", "/tmp")
      webctx.setTempDirectory(new File(webtmpdir))
    }

    server.setHandler(webctx)
    server.start
    server.join

Here is this file at github, just I didn't put alias-enabling code there, because
couldn't make it work so far:  https://github.com/oolekk/sqrlrcrd.com/blob/master/src/main/scala/bootstrap/liftweb/Start.scala

Also here is a thread on Lift mailing list, about packaging jetty 8 together with
lift web application to create runnable, self serving jar:
https://groups.google.com/forum/?fromgroups=#!topic/liftweb/uwwIA8FwmU8

Thank you in advance for your help.

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users

Back to the top