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

Since you are doing embedded work, you might want to take a look at our embedded jetty examples.

Of particular interest might be
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:
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:

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:
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-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
    /* 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.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/
     * 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))


Here is this file at github, just I didn't put alias-enabling code there, because
couldn't make it work so far:

Also here is a thread on Lift mailing list, about packaging jetty 8 together with
lift web application to create runnable, self serving jar:!topic/liftweb/uwwIA8FwmU8

Thank you in advance for your help.

jetty-users mailing list

Back to the top