【问题标题】:CXFNonSpringServlet in camel-cxfcamel-cxf 中的 CXFNonSpringServlet
【发布时间】:2015-09-22 14:52:23
【问题描述】:

我需要在 camel-cxf 中设置 use-x-forwarded-headers Http 标头,如下所示,并将其作为 OSGi 包部署在 Karaf 中。

<servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <init-param>         
          <param-name>use-x-forwarded-headers</param-name>
          <param-value>true</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

我不想把它打包成一个 WAR,而是把它做成一个 OSGi 包。

因此,我创建了一个扩展 CXFNonSpringServlet 的类,如下面的代码并设置了 init-params,

public class XForwadedServlet extends CXFNonSpringServlet {

@Override
    protected void loadBus(ServletConfig sc) {
        sc.getServletContext().setInitParameter("use-x-forwarded-headers",
                "true");
        super.loadBus(sc);
        Bus bus = getBus();
        BusFactory.setDefaultBus(bus);
        // createFactoryBean();
    }


    @Override
    public void init(ServletConfig sc) throws ServletException {
        sc.getServletContext().setInitParameter("use-x-forwarded-headers",
                "true");
        super.init(sc);
    }

这是我的骆驼路线,

<cxf:cxfEndpoint id="serviceEndpoint"
        address="http://localhost:8123/cxf/testCxfRedirect"
        loggingFeatureEnabled="true" serviceClass="com.redhat.HelloServiceImpl">
    </cxf:cxfEndpoint>
    <camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="cxf:bean:serviceEndpoint"/>
    <log message="${body}" loggingLevel="INFO"/>
    <bean ref="clientAddress" method="getClientAddress"/>
  </route>
</camelContext>

    <bean id="destinationRegistry" class="org.apache.cxf.transport.http.DestinationRegistryImpl">
    </bean>
    <bean id="osgiServlet" class="com.redhat.XForwadedServlet">
        <constructor-arg ref="destinationRegistry"></constructor-arg>
        <constructor-arg value="true"></constructor-arg>
    </bean>

通过soapui,我将X-Forwarded-For标头设置如下,

Address: http://localhost:8123/cxf/testCxfRedirect
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""], X-Forwarded-For=[http://google.com]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHello xmlns:ns1="http://redhat.com/"><arg0 xmlns="http://redhat.com/">Test</arg0></ns1:sayHello></soap:Body></soap:Envelope>
--------------------------------------

如何通过设置 init-params 在 OSGi Blueprint 容器中注册 CXFNonSpringServlet ? 有没有办法在 OSGi 中做到这一点?

【问题讨论】:

    标签: apache-camel cxf jbossfuse karaf


    【解决方案1】:

    这可以通过使用 Fuse、Camel 中的 pax-web-extender 支持来实现。 但是,包装将需要是 WAR。

    这是来自 Karaf 的参考资料: https://ops4j1.jira.com/wiki/display/ops4j/Pax+Web+Extender+-+War+-+Examples

    这是来自 Camel Committers 的参考实现: https://github.com/apache/camel/tree/master/examples/camel-example-reportincident

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多