we are struggling with the simple issue of serving static files from somewhere in the filesystem but outside of the web application, but we can't get it running.
There are sever examples on how to do this, but none of them seem to work and so far we couldn't find a confirmation from someone that it actually does work.
The jetty.xml found in the etc directory has been edited as it is stated here https://ops4j1.jira.com/wiki/display/paxweb/Advanced+Jetty+Configuration or here ops4j GitHub sample
So added to jetty.xml this:
<Get name="handler">
<Call name="addHandler">
<Arg>
<New class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/fileserver</Set>
<Set name="resourceBase">/Users/Shared/testenv</Set>
<Call name="addServlet">
<Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
<Arg>/</Arg>
</Call>
</New>
</Arg>
</Call>
</Get>
or this:
<Get name="handler">
<Call name="addHandler">
<Arg>
<New class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/fileserver</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/Users/Shared/testenv</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
</New>
</Arg>
</Call>
</Get>
With both versions jetty / karaf starts up fine and when karaf is shut down I can see
2015-06-02 12:02:57,838 | INFO | pool-7-thread-2 | ContextHandler
| 113 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | stopped o.e.j.s.ServletContextHandler{/fileserver,file:/Users/Shared/testenv/}
But the files are not served under localhost:8181/fileserver
The only way it is working (in a freshly installed karaf container) is to use
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerList">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/fileserver</Set>
<Set name="resourceBase">/Users/Shared/testenv</Set>
<Call name="addServlet">
<Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
<Arg>/</Arg>
</Call>
</New>
</Item>
</Array>
</Set>
</New>
</Set>
But doing this, will break other web applications running in karaf. For example we are using the Camel Servlet component.
So does someone has a working configuration of serving static files through jetty instance in karaf or now how to do this?
Any help appreciated. Thank in advance!
By the way: Using Karaf 3.0.3
EDIT:
I rerun the test with the snippet given by Achim and enabled DEBUG loggin.
2015-06-03 15:33:25,492 | DEBUG | pool-6-thread-1 | XmlConfiguration | 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | XML o.e.j.s.h.ContextHandler{/,null}.setContextPath(/static-content) 2015-06-03 15:33:25,527 | DEBUG | pool-6-thread-1 | XmlConfiguration | 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | XML o.e.j.s.h.ContextHandler{/static-content,null}.setHandler(org.eclipse.jetty.server.handler.ResourceHandler@3855ace4) 2015-06-03 15:33:25,529 | DEBUG | pool-6-thread-1 | Container
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | Container o.e.j.s.h.ContextHandler{/static-content,null} + org.eclipse.jetty.server.handler.ResourceHandler@3855ace4 as handler 2015-06-03 15:33:25,529 | DEBUG | pool-6-thread-1 | Container
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | Container org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection@6665534e + o.e.j.s.h.ContextHandler{/static-content,null} as handler 2015-06-03 15:33:25,542 | DEBUG | pool-6-thread-1 | AbstractLifeCycle
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | starting o.e.j.s.h.ContextHandler{/static-content,null} 2015-06-03 15:33:25,542 | DEBUG | pool-6-thread-1 | AbstractHandler
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | starting o.e.j.s.h.ContextHandler{/static-content,null} 2015-06-03 15:33:25,543 | DEBUG | pool-6-thread-1 | AbstractLifeCycle
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | STARTED o.e.j.s.h.ContextHandler{/static-content,null} 2015-06-03 15:34:27,974 | DEBUG | /static-content | Server
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | REQUEST /static-content on AsyncHttpConnection@638f2d20,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=10,c=0},r=1 2015-06-03 15:34:27,974 | DEBUG | /static-content | ServerModel
| 78 - org.ops4j.pax.web.pax-web-spi - 3.1.4 | Matching [/static-content]... 2015-06-03 15:34:27,975 | DEBUG | /static-content | ServerModel | 78 - org.ops4j.pax.web.pax-web-spi - 3.1.4 | Path [/static-content] does not match any context 2015-06-03 15:34:27,975 | DEBUG | /static-content | Server | 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | RESPONSE /static-content 200 handled=false
Here I noticed a difference between the Get Version (not working) and the Set version (working).
Set sets the class org.eclipse.jetty.server.handler.HandlerList
Get gets and add to the class org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection
, which is described as
Jetty Handler collection that calls only the handler (=context) that matches the request path after performing the substring based matching of requests path to registered aliases
Could there be a problem regarding the aliases?
EDIT 2:
I tried digging into this, but I really can't get this to work. I do not know about the differences between the integration test and the regular karaf, but there must be an issue. To reproduce the issue, just take a fresh karaf (3.0.3) container, do feature:install war
and add the snippet to the etc/jetty.xml so it looks like this and edit the path of the resourceBase so it matches a local path.
<?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" />
</Set>
<Set name="port">
<Property name="jetty.port" default="8181" />
</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.boot.principal.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.boot.principal.RolePrincipal
</Item>
</Array>
</Set>
</New>
</Arg>
</Call>
<Get name="handler">
<Call name="addHandler">
<Arg>
<New class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/static-content</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/Users/Shared/testenv/in</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
</New>
</Arg>
</Call>
</Get>
</Configure>
Try to access the context via browser using localhost:8181/static-content.
Result is always 404 - Not found.
We have tried this on multiple systems running linux and windows.
The problem maybe related with pax-web version. In Karaf 3.0.5 it uses version Pax-web 3.2.6 which I have read (sorry I cannot find the link) had a bug related with serving static content.
I have tested @Achim's approach in Karaf 4.0.3 (Pax-web 4.2.3) and it works like charm.