在 karaf 中使用 jetty 服务静态文件(在 bundle 之外)

Serving static files with jetty in karaf (outside of bundle)

我们正在努力解决从文件系统中某处但在 Web 应用程序外部提供静态文件的简单问题,但我们无法获得它 运行。

有很多关于如何执行此操作的示例,但其中 none 似乎有效,但到目前为止我们无法找到任何人确认它确实有效。

在etc目录中找到的jetty.xml已按照说明进行了编辑 这里 https://ops4j1.jira.com/wiki/display/paxweb/Advanced+Jetty+Configuration 或者在这里 ops4j GitHub sample

所以添加到jetty.xml这个:

<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>

或者这个:

<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>

两个版本的 jetty/karaf 都可以正常启动,当 karaf 关闭时我可以看到

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/}

但文件不在 localhost:8181/fileserver

下提供

它工作的唯一方法(在新安装的 karaf 容器中)是使用

<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>

但是这样做会破坏 karaf 中的其他网络应用程序 运行。例如,我们正在使用 Camel Servlet 组件。

有人有通过 karaf 中的 jetty 实例提供静态文件的工作配置吗?或者现在该怎么做?

感谢任何帮助。提前致谢!

顺便说一句:使用 Karaf 3.0.3

编辑:

我使用 Achim 提供的代码片段重新运行测试并启用 DEBUG 登录。

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

在这里我注意到获取版本(不工作)和设置版本(工作)之间的区别。

集集 class org.eclipse.jetty.server.handler.HandlerList Get获取并添加到classorg.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection,描述为

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

别名会不会有问题?

编辑 2:

我试着深入研究这个,但我真的无法让它工作。我不知道集成测试和常规 karaf 之间的区别,但肯定有问题。要重现该问题,只需使用一个新的 karaf (3.0.3) 容器,执行 feature:install war 并将代码片段添加到 etc/jetty.xml 使其看起来像这样并编辑路径resourceBase 以便它匹配本地路径。

<?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>

尝试使用 localhost:8181/static-content 通过浏览器访问上下文。

结果始终为 404 - 未找到。

我们已经在多个系统上尝试过 运行 linux 和 windows.

实际上你已经在你的样本中得到了它,这是最好的方式。 从这个 integration test 可以看出它正在工作。

所以请确保你有这个:

<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">target/logs</Set>
                        <Set name="directoriesListed">true</Set>
                    </New>
                </Set>
            </New>
        </Arg>
    </Call>
</Get>

我也无法让它工作。 使用新下载的 Karaf 3.0.5:

$ cd /tmp
$ tar xzf ~/Downloads/apache-karaf-3.0.5.tar.gz 
$ ./apache-karaf-3.0.5/bin/karaf 
        __ __                  ____      
       / //_/____ __________ _/ __/      
      / ,<  / __ `/ ___/ __ `/ /_        
     / /| |/ /_/ / /  / /_/ / __/        
    /_/ |_|\__,_/_/   \__,_/_/         

  Apache Karaf (3.0.5)

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown Karaf.

karaf@root()> 

正在检查是否有任何东西:

$ curl http://localhost:8181/
curl: (7) Failed to connect to localhost port 8181: Connection refused

很好。还没有码头。

正在安装 pax 网络

karaf@root()> feature:repo-add mvn:org.ops4j.pax.web/pax-web-features/2.1.0/xml/features
Adding feature url mvn:org.ops4j.pax.web/pax-web-features/2.1.0/xml/features
karaf@root()> feature:install pax-jetty
karaf@root()> feature:install http

再次使用 curl 检查

$ curl http://localhost:8181/
...
<h2>HTTP ERROR: 404</h2>
<hr /><i><small>Powered by Jetty://</small></i>
...

很好。 Jetty 还活着。

将 ResourceHandler 添加到 apache-karaf-3.0.5/etc/jetty.xml

<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">/tmp</Set>
                        <Set name="directoriesListed">true</Set>
                    </New>
                </Set>
            </New>
        </Arg>
    </Call>
</Get>

curl 仍然看到 404:

$ curl http://localhost:8181/
...
<h2>HTTP ERROR: 404</h2>
...

不确定我是否需要重新启动任何东西。无论如何,我重新启动了整个 Karaf。

两个卷发都给出 404:

$ curl http://localhost:8181/
$ curl http://localhost:8181/static-content

在 apache-karaf-3.0.5/etc/org 中启用 DEBUG 日志记录。ops4j.pax.logging.cfg

log4j.rootLogger=DEBUG, out, osgi:*

curl 对于 http://localhost:8181/static-content 仍然得到 404,而 karaf.log 表示:

2015-11-14 12:57:00,938 | DEBUG | 673-63 Selector0 | nio                              | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | created SCEP@5b87b8b7{l(/127.0.0.1:46304)<->r(/127.0.0.1:8181),s=0,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}
2015-11-14 12:57:00,939 | DEBUG | qtp425678673-70  | HttpParser                       | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | filled 92/92
2015-11-14 12:57:00,939 | DEBUG |  /static-content | Server                           | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | REQUEST /static-content on AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=3,c=0},r=1
2015-11-14 12:57:00,940 | DEBUG |  /static-content | ServerModel                      | 77 - org.ops4j.pax.web.pax-web-spi - 3.2.6 | Matching [/static-content]...
2015-11-14 12:57:00,940 | DEBUG |  /static-content | ServerModel                      | 77 - org.ops4j.pax.web.pax-web-spi - 3.2.6 | Path [/static-content] does not match any context
2015-11-14 12:57:00,940 | DEBUG |  /static-content | Server                           | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | RESPONSE /static-content  200 handled=false
2015-11-14 12:57:00,940 | DEBUG | qtp425678673-70  | AsyncHttpConnection              | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | Enabled read interest SCEP@5b87b8b7{l(/127.0.0.1:46304)<->r(/127.0.0.1:8181),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0r}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=4,h=0,b=0,c=-1},p=HttpParser{s=0,l=3,c=0},r=1}
2015-11-14 12:57:00,941 | DEBUG | qtp425678673-70  | ChannelEndPoint                  | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | ishut SCEP@5b87b8b7{l(/127.0.0.1:46304)<->r(/127.0.0.1:8181),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0r}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=-3},r=1}
2015-11-14 12:57:00,941 | DEBUG | qtp425678673-70  | HttpParser                       | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | filled -1/0
2015-11-14 12:57:00,941 | DEBUG | qtp425678673-70  | AsyncHttpConnection              | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | Disabled read interest while writing response SCEP@5b87b8b7{l(/127.0.0.1:46304)<->r(/127.0.0.1:8181),s=1,open=true,ishut=true,oshut=false,rb=false,wb=false,w=true,i=0r}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
2015-11-14 12:57:00,942 | DEBUG | qtp425678673-70  | ChannelEndPoint                  | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | close SCEP@5b87b8b7{l(/127.0.0.1:46304)<->r(/127.0.0.1:8181),s=1,open=true,ishut=true,oshut=false,rb=false,wb=false,w=true,i=0r}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
2015-11-14 12:57:00,942 | DEBUG | 673-63 Selector0 | nio                              | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | destroyEndPoint SCEP@5b87b8b7{l(null)<->r(0.0.0.0/0.0.0.0:8181),s=0,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=0!}-{AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
2015-11-14 12:57:00,943 | DEBUG | 673-63 Selector0 | AbstractHttpConnection           | 70 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.17.v20150415 | closed AsyncHttpConnection@21d1b60b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1

然后尝试 Advanced Jetty Configuration 中的 DefaultServlet, 将我之前在 jetty.xml 中添加的内容替换为

<Get name="handler">
    <Call name="addHandler">
      <Arg>
    <New class="org.eclipse.jetty.servlet.ServletContextHandler">
          <Set name="contextPath">/static-content</Set>
          <Set name="resourceBase">/tmp</Set>
          <Call name="addServlet">
            <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
            <Arg>/</Arg>
          </Call>
        </New>
      </Arg>
    </Call>
</Get>

重新启动 Karaf,curl http://localhost:8181/static-content 仍然给出 404 和 Karaf 日志说基本上与 ResourceHandler 相同。

缺少什么?

问题可能与pax-web版本有关。在 Karaf 3.0.5 中,它使用我读过的版本 Pax-web 3.2.6(抱歉,我找不到 link)有一个与服务静态内容相关的错误。

我已经在 Karaf 4.0.3 (Pax-web 4.2.3) 中测试了@Achim 的方法,它非常有效。