How to get Jetty in Karaf to serve static content from file system?

5.1k views Asked by At

I need to serve static content from outside a bundle in Karaf. Since it already has Pax Web and Jetty built in, I've thought it wouldn't be a problem, but no success so far :(

I have jetty, http, http-whiteboard and war features installed. Following http://team.ops4j.org/wiki/display/paxweb/Advanced+Jetty+Configuration, I've added this to etc/jetty.xml:

<Get name="handler">
    <Call name="addHandler">
        <Arg>
            <New class="org.eclipse.jetty.servlet.ServletContextHandler">
                <Set name="contextPath">/app</Set>
                <Set name="resourceBase">/home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.poim.resources-rodniki/
                </Set>
                <Call name="addServlet">
                    <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                    <Arg>/</Arg>
                </Call>
            </New>
        </Arg>
    </Call>
</Get>

The complete file is:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//
DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Set connectors -->
    <!-- =========================================================== -->
    <!-- One of each type! -->
    <!-- =========================================================== -->

    <!-- Use this connector for many frequently idle connections and for threadless 
        continuations. -->
    <Call name="addConnector">
        <Arg>
            <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <Set name="host">
                    <Property name="jetty.host" default="0.0.0.0" />
                </Set>
                <Set name="port">
                    <Property name="jetty.port" default="8282" />
                </Set>
                <Set name="maxIdleTime">300000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8443</Set>
                <Set name="lowResourcesConnections">20000</Set>
                <Set name="lowResourcesMaxIdleTime">5000</Set>
            </New>
        </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Configure Authentication Realms -->
    <!-- Realms may be configured for the entire server here, or -->
    <!-- they can be configured for a specific web app in a context -->
    <!-- configuration (see $(jetty.home)/contexts/test.xml for an -->
    <!-- example). -->
    <!-- =========================================================== -->
    <Call name="addBean">
        <Arg>
            <New class="org.eclipse.jetty.plus.jaas.JAASLoginService">
                <Set name="name">karaf</Set>
                <Set name="loginModuleName">karaf</Set>
                <Set name="roleClassNames">
                    <Array type="java.lang.String">
                        <Item>org.apache.karaf.jaas.modules.RolePrincipal</Item>
                    </Array>
                </Set>
            </New>
        </Arg>
    </Call>
    <Call name="addBean">
        <Arg>
            <New class="org.eclipse.jetty.plus.jaas.JAASLoginService">
                <Set name="name">default</Set>
                <Set name="loginModuleName">karaf</Set>
                <Set name="roleClassNames">
                    <Array type="java.lang.String">
                        <Item>org.apache.karaf.jaas.modules.RolePrincipal</Item>
                    </Array>
                </Set>
            </New>
        </Arg>
    </Call>


    <Get name="handler">
        <Call name="addHandler">
            <Arg>
                <New class="org.eclipse.jetty.servlet.ServletContextHandler">
                    <Set name="contextPath">/app</Set>
                    <Set name="resourceBase">/home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.poim.resources-rodniki
                    </Set>
                    <Call name="addServlet">
                        <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                        <Arg>/</Arg>
                    </Call>
                </New>
            </Arg>
        </Call>
    </Get>

</Configure>

And created the file org.ops4j.pax.web.cfg with this content:

org.ops4j.pax.web.config.file=/home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.karaf.launcher/etc/jetty.xml

In the log I see

2012-06-02 12:03:52,800 | INFO  | g.ops4j.pax.web) | Server                           |  -  -  | jetty-7.5.4.v20111024
2012-06-02 12:03:52,848 | INFO  | g.ops4j.pax.web) | ContextHandler                   |  -  -  | started o.e.j.s.ServletContextHandler{/app,file:/home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.poim.resources-rodniki/}
2012-06-02 12:03:52,907 | INFO  | g.ops4j.pax.web) | AbstractConnector                |  -  -  | Started [email protected]:8282 STARTING
2012-06-02 12:03:52,914 | INFO  | g.ops4j.pax.web) | JettyServerImpl                  | 124 - org.ops4j.pax.web.pax-web-jetty - 1.0.9 | Pax Web available at [0.0.0.0]:[8080]
2012-06-02 12:03:52,927 | INFO  | g.ops4j.pax.web) | AbstractConnector                |  -  -  | Started [email protected]:8080 STARTING

But the files aren't visible. E.g., I have a file /home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.poim.resources-rodniki/.style, but going to either http://192.168.1.9:8080/app/.style or http://192.168.1.9:8282/app/.style gives 404 error.

1

There are 1 answers

2
Brimstedt On

A bit late, perhaps, but..

Doing the same thing worked well for me, i.e. i copied the code snippet from http://team.ops4j.org/wiki/display/paxweb/Advanced+Jetty+Configuration and changed the two paths. I restarted karaf and my files were served.

Are you running karaf as a user that has access to /home/aromanov?