views:

557

answers:

3

Is there an easy way to map a directory in the web.xml or other deployment descriptor (jetty.xml, etc) files?

For example, if I have a directory /opt/files/ is there a way that I can access its files and sub-directories by visiting http://localhost/some-mapping/? It strikes me that there should be some simple way of doing this, but I haven't been able to find out how (via google, stackoverflow, etc). All I've found are servlets that mimic fileservers, which is not what I would like.

For reference I am using jetty on an AIX box.

A: 

I don't know that you can do that as a URL mapping, however files and folders in the same directory that your web.xml file is in will be accessible in the way you describe, although you can't control the mapping under your webapp.

Does that suit your needs?

Brabster
Thanks, but I know this. I'm looking for a way to map a directory that is external to the web container. I know that this is probably not the best practice, but this is for a server which will remain isolated from the internet and only accessible from within an intranet.
prometheus
A shortcut (UNIX softlink or windows shortcut) in that root directory?
Brabster
A UNIX softlink might work, but a windows shortcut doesn't (I just tested it). I think I've found an answer to my own question (after some heavy google-fooing). See my answer below.
prometheus
+1  A: 

After some more fiddling around, the best way to do this (for jetty) is to deploy a descriptor in the context directory that looks like the following...

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"&gt;

<!-- Configuration of a custom context. -->
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
    <Call class="org.eclipse.jetty.util.log.Log" name="debug">
        <!-- The default message to show if our context breaks. -->
        <Arg>Configure context.xml</Arg>
    </Call>
    <!--
        The context path is the web location of the context in relation to the
        server address.
    -->
    <Set name="contextPath">/context</Set>
    <!--
        The resource base is the server directory to use for fetching files.
    -->
    <Set name="resourceBase">/path/to/files/on/server</Set>
    <Set name="handler">
        <New class="org.eclipse.jetty.server.handler.ResourceHandler">
            <Set name="directoriesListed">true</Set>
            <!-- For now we don't need any welcome files -->
            <!--
                <Set name="welcomeFiles"> <Array type="String">
                <Item>index.html</Item> </Array> </Set>
            -->
            <!--
                The cache time limit in seconds (ie max-age=3600 means that if the
                document is older than 1 hour a fresh copy will be fetched).
            -->
            <Set name="cacheControl">max-age=3600,public</Set>
        </New>
    </Set>
</Configure>

I hope that this helps someone else!

prometheus
+1  A: 

No idea how to do it with Jetty, but in Tomcat you can just add a new <Context> to server.xml:

<Context docBase="/opt/files" path="/files" />

This way it's accessible by http://example.com/files/.... See if something similar exist for Jetty.

Update: after Googling, the "normal Java code" equivalent would be something like:

WebAppContext files = new WebAppContext("/opt/files", "/files");
Server server  = new Server(8080);
server.setHandler(files);
server.start(); 

Now yet to translate that into jetty.xml flavor. I am a bit guessing based on the documentation and examples found on the web, so don't pin me on it:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="webApp">/opt/files</Set>
    <Set name="contextPath">/files</Set>
</Configure>

Another possibility may be this:

<Configure class="org.mortbay.jetty.Server">
    <Call name="addHandler">
        <Arg>
            <New class="org.mortbay.jetty.webapp.WebAppContext">
                <Arg name="webApp">/opt/files</Arg>
                <Arg name="contextPath">/files</Arg>
            </New>
        </Arg>
    </Call>
</Configure>
BalusC
Thanks, this is definitely the way to do it on Tomcat (which I am more familiar with). I think the corresponding jetty file is called jetty.xml. The xml language for mapping the directory on jetty appears to be quite different from tomcat.
prometheus
Thanks for the update. Unfortunately, I am using the eclipse jetty api, which is a bit different. The first suggestion does not work at all, the second doesn't work because there isn't an "addHandler" method in eclipse jetty http://download.eclipse.org/jetty/stable-7/apidocs/.
prometheus
I see. Does this FileServer example help? http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#File_Server
BalusC
Yeah, if you download a copy of jetty, the etc/jetty-fileserver.xml file contains a copy of this. Thanks for all of your help!
prometheus