我们正在努力解决从文件系统某处但在Web应用程序之外提供静态文件的简单问题,但我们无法让它运行。
有关于如何执行此操作的相关示例,但它们似乎都没有工作,到目前为止我们无法找到某人确认其确实有效的确认。
在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,538 |信息| pool-7-thread-2 | ContextHandler中
| 113 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 |停止了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 loggin。
2015-06-03 15:33:25,492 |调试| 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 |调试| 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} .setHandler(org.eclipse.jetty.server.handler.ResourceHandler@3855ace4) 2015-06-03 15:33:25,529 |调试| pool-6-thread-1 |集装箱
| 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作为处理程序 2015-06-03 15:33:25,529 |调试| pool-6-thread-1 |集装箱
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 |容器 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 |调试| pool-6-thread-1 | AbstractLifeCycle
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 |启动o.e.j.s.h.ContextHandler {/ static-content,null} 2015-06-03 15:33:25,542 |调试| pool-6-thread-1 | AbstractHandler
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 |启动o.e.j.s.h.ContextHandler {/ static-content,null} 2015-06-03 15:33:25,543 |调试| 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 |调试| / static-content |服务器
| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 |请求/静态内容 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 |调试| / static-content | ServerModel
| 78 - org.ops4j.pax.web.pax-web-spi - 3.1.4 |匹配 [/ static-content] ... 2015-06-03 15:34:27,975 |调试| / static-content | ServerModel | 78 - org.ops4j.pax.web.pax-web-spi - 3.1.4 |路径[/ static-content]可以 不匹配任何背景2015-06-03 15:34:27,975 |调试| / static-content |服务器| 71 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | RESPONSE / static-content 200 handling = false
在这里,我注意到Get Version(不工作)和Set版本(工作)之间存在差异。
Set设置类org.eclipse.jetty.server.handler.HandlerList
获取并添加到类org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection
,其描述为
Jetty Handler集合,只调用处理程序(= context) 在执行基于子串的匹配后匹配请求路径 请求路径到已注册的别名
有关别名的问题吗?
编辑2:
我试着深入研究这个,但我真的无法让它发挥作用。我不知道集成测试和常规karaf之间的区别,但必定存在问题。要重现此问题,只需使用一个新的karaf(3.0.3)容器,执行feature:install war
并将代码段添加到etc / jetty.xml中,使其看起来像这样并编辑resourceBase的路径,使其匹配a本地路径。
<?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的多个系统上尝试过这个。
答案 0 :(得分:1)
问题可能与pax-web版本有关。在Karaf 3.0.5中它使用的版本Pax-web 3.2.6我已阅读(抱歉,我找不到链接)有一个与提供静态内容相关的错误。
我已经在Karaf 4.0.3(Pax-web 4.2.3)中测试了@ Achim的方法,它就像魅力一样。
答案 1 :(得分:0)
实际上你已经在样本中得到了它,这就是最好的方式。 从这个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>
答案 2 :(得分: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
好。还没有码头。
安装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>
...
好。码头还活着。
将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>
...
不确定我需要重新启动任何内容。无论如何,我重启了整个卡拉夫。
两个卷发都给出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
然后在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一样。
缺少什么?