-1

I am trying to publish the URL of a CXF service using the CXF WS-Discovery plugins.

The tutorial https://cxf.apache.org/docs/writing-a-service-with-spring.html helps me to build an helloworld service. The CXF documentation of WS-Discovery https://cxf.apache.org/docs/ws-discovery.html, explain that adding cxf-services-ws-discovery-service.jar allow to publish it.

Great, but the published url is relative to the servlet and then not reachable from client that send the WS-Discovery Probe.

I found an interesting approach http://osdir.com/ml/users-cxf-apache/2012-05/msg00524.html that suggests to use the following web.xml and cxf-servlet.xml files :

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app.xsd">
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webservices/*</url-pattern>
    </servlet-mapping>  
</web-app>

cxf-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="localhost" class="java.net.InetAddress" factory-method="getLocalHost" />
    <bean id="publishedWebServiceUrl" class="java.lang.String">
        <constructor-arg value="#{'http://' + localhost.hostAddress + ':8080' + servletContext.contextPath + '/webservices/hello_world'}"/>
    </bean>

    <jaxws:endpoint id="hello_world" implementor="HelloWorldImpl"  address="/hello_world">
                <jaxws:properties>
                        <entry key="publishedEndpointUrl" ><ref bean="publishedWebServiceUrl" /></entry>
                </jaxws:properties>
    </jaxws:endpoint>
</beans>

This works fine if the servlet container use the port 8080.

I tried to use servletContext.getRealPath('/webservices') but this give the filesystem path and not the http address.

Is there a way to get the servlet container port (compliant with tomcat, jetty, ...) ? or an other way to publish an exportable URL ?

mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

0

I'm afraid you need configure the address from outside, I don't think CXF can tell the port that the servlet container is using.

Willem Jiang
  • 3,291
  • 1
  • 14
  • 14
  • Each servlet container use a different way to set its port. I will try to see how to use the same property to configure CXF and the servlet container. I was expecting that in some way, it will be possible to get it from the applicationContext... – mpromonet Mar 20 '14 at 13:14