0

I need to set the use-x-forwarded-headers Http header in camel-cxf like the below and make it as an OSGi bundle to be deployed in 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>

I don't want to package this as a WAR, instead i want to make it an OSGi bundle.

Hence, i created a class which extends CXFNonSpringServlet as the code below and set the 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);
    }

And here's my camel route,

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

Through soapui, i set the X-Forwarded-For header as below,

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

How can i register the CXFNonSpringServlet in OSGi Blueprint container with setting that init-params ? Is there a way to do this in OSGi ?

Viral Gohel
  • 316
  • 1
  • 7

1 Answers1

0

This can be achieved by using the pax-web-extender support in Fuse, Camel. However the packaging will need to be WAR.

Here's the reference from Karaf: https://ops4j1.jira.com/wiki/display/ops4j/Pax+Web+Extender+-+War+-+Examples

Here's a reference implementation from the Camel Committers: https://github.com/apache/camel/tree/master/examples/camel-example-reportincident

Hope it helps.

Ashoka
  • 935
  • 7
  • 20