如何在Karaf中使用Jetty来提供文件系统中的静态内容?

时间:2012-06-02 08:11:16

标签: jetty apache-karaf pax-web

我需要在Karaf的服务外部提供静态内容。由于它已经内置了Pax Web和Jetty,我认为这不会有问题,但到目前为止还没有成功:(

我已安装jettyhttphttp-whiteboardwar个功能。在http://team.ops4j.org/wiki/display/paxweb/Advanced+Jetty+Configuration之后,我已将其添加到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>

完整的文件是:

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

使用以下内容创建文件org.ops4j.pax.web.cfg

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

在日志中我看到了

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 SelectChannelConnector@0.0.0.0: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 NIOSocketConnectorWrapper@0.0.0.0:8080 STARTING

但文件不可见。例如,我有一个文件/home/aromanov/workspaces/odp-server/ru.focusmedia.odp.server.poim.resources-rodniki/.style,但转到http://192.168.1.9:8080/app/.stylehttp://192.168.1.9:8282/app/.style会出现404错误。

1 个答案:

答案 0 :(得分:1)

可能有点晚了,但是......

同样的事情对我来说很有效,即我从http://team.ops4j.org/wiki/display/paxweb/Advanced+Jetty+Configuration复制了代码片段并更改了两条路径。我重新开始使用karaf并提供了我的文件。

您是否以有权访问/ home / aromanov的用户身份运行karaf?