在karaf中使用jetty提供静态文件服务(不在bundle内)

3
我们正努力解决一个简单的问题:如何从文件系统中的某个位置提供静态文件,但我们无法让其运行。有一些关于如何做到这点的示例,但似乎都不起作用,迄今为止我们也找不到有人证实它确实可行的消息。在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中运行的其他Web应用程序。例如,我们正在使用Camel Servlet组件。

那么,有人有通过karaf在jetty实例中提供静态文件的工作配置或知道如何做到这一点吗?

非常感谢任何帮助!

顺便说一句:使用Karaf 3.0.3

编辑:

我使用Achim给出的代码片段重新运行了测试,并启用了DEBUG日志记录。

2015年06月03日15时33分25秒,调试信息显示XML o.e.j.s.h.ContextHandler{/,null}.setContextPath(/static-content),XML o.e.j.s.h.ContextHandler{/static-content,null}.setHandler(org.eclipse.jetty.server.handler.ResourceHandler@3855ace4),容器o.e.j.s.h.ContextHandler{/static-content,null} + org.eclipse.jetty.server.handler.ResourceHandler@3855ace4作为处理程序,容器org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection@6665534e + o.e.j.s.h.ContextHandler{/static-content,null}作为处理程序,启动o.e.j.s.h.ContextHandler{/static-content,null},匹配[/static-content]...,路径[/static-content]不匹配任何上下文,响应/static-content 200 handled=false。

我注意到Get Version(不起作用)和Set Version(起作用)之间存在差异。

Set设置了类org.eclipse.jetty.server.handler.HandlerList, Get获取并添加到类org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection,该类被描述为

Jetty处理程序集合,仅调用匹配请求路径的处理程序(=上下文),在执行基于子字符串的请求路径与注册别名匹配后

可能与别名有关的问题吗?

编辑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的多个系统上尝试过此操作。
3个回答

1
这个问题可能与Pax-web版本有关。在Karaf 3.0.5中,它使用的是Pax-web 3.2.6版本,我读过(抱歉我找不到链接)一个与提供静态内容相关的错误。我已经在Karaf 4.0.3(Pax-web 4.2.3)中测试了@Achim的方法,它非常有效。

0

实际上,您已经在示例中得到了它,这是最好的方法。正如可以从此集成测试看出,它正在工作。

因此,请确保您按照此方式进行:

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

我仔细检查了并在上面的问题中添加了更多细节。感谢您对此进行了解。 - Florian
Achim,在使用Karaf 3.0.5时,仅仅将这个片段添加到jetty.xml中对我来说是不起作用的。我刚刚在回答这个问题时发布了我所采取的确切步骤(请参见上文)。 - Max Spring

0

我也无法让它工作。 使用最新下载的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

好的。还没有Jetty。

正在安装pax web。

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。

两个curl都返回404:

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

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

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

然后按照高级Jetty配置中的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基本相同。

还缺少什么?


2
这并没有真正回答问题。如果你有不同的问题,可以通过点击提问来提出。你也可以添加赏金来吸引更多人关注这个问题。- 来自审核 - theB
1
正确,我的“答案”实际上没有回答问题。 我将其作为对Achim评论的跟进,并提供了更多有关如何重现问题的详细信息。 - Max Spring

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接