Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting URL of an CXF endpoint

Situation

I'm deploying web services with Apache CXF 2.6.2 to a Tomcat server. I'm exporting the services using the CXFServlet and the following Spring based configuration:

<?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.xsd
         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint id="test_endpoint"
                    implementor="org.xyz.TestImpl"
                    address="/test"/>

    <bean id="testBean" class="org.xyz.TestBean">
        <property name="endpoint" ref="test_endpoint" />
    </bean>
</beans>

In my example deployment the CXFServlet is using the relative path /service and for example the web service implemented by TestImpl class is available as http://domain.com/tomcat-context/services/test The TestBean class has an setter for endpoint and it is set by Spring.

Goal

I want to determine the address (URL) which is provided by the endpoint test_endpoint in the class TestBean using the endpoint field. The result should be excactly "http://domain.com/tomcat-context/services/test".

What I've tried

log.info("Endpoint set to " + endpoint);
log.info("Address: " + endpoint.getAddress());
org.apache.cxf.jaxws.EndpointImpl ep = (org.apache.cxf.jaxws.EndpointImpl) endpoint;
log.info("Other Address: " + ep.getBindingUri());
log.info("Props: " + ep.getProperties());

but the result is just

Address: /Sachbearbeiter
Other Address: null
Props: {}

How can I get the full URL? Is there is a way without building it on my own?

like image 277
MrBurns977 Avatar asked Oct 02 '12 18:10

MrBurns977


2 Answers

Have you tried poking around the CXF message to see if it is in one of the properties? I use Camel with CXF and obtain the actual CXF message like this:

Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class);

You should be able to get the CXF Message in just plain CXF like so:

PhaseInterceptorChain.getCurrentMessage()

See this URL: Is there a way to access the CXF message exchange from a JAX-RS REST Resource within CXF?

From there, you can get properties such as:

org.apache.cxf.request.url=someDomain/myURL
like image 168
Yogesh Chawla Avatar answered Sep 27 '22 20:09

Yogesh Chawla


You can construct the url with following code. And you can edit as appropriate to your environment.

String requestURI = (String) message.get(Message.class.getName() + ".REQUEST_URI");
Map<String, List<String>> headers = CastUtils.cast((Map) message.get(Message.PROTOCOL_HEADERS));
List sa = null;
String hostName=null;
    if (headers != null) {
            sa = headers.get("host");
        }

        if (sa != null && sa.size() == 1) {
            hostName = "http://"+ sa.get(0).toString()+requestURI;
        }
like image 24
sanjeevasrinath Avatar answered Sep 27 '22 21:09

sanjeevasrinath