Getting the CXF Endpoint URL

Situation

I am deploying web services with Apache CXF 2.6.2 on a Tomcat server. I export services using CXFServlet and the following Spring 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 deployment example, the CXFServlet uses the relative path / service, and for example, the web service implemented by the TestImpl class is available as http://domain.com/tomcat-context/services/test The TestBean class has an installer for the endpoint and is installed by Spring .

goal

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

What i 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 a way without creating it myself?

+4
source share
3 answers

Have you tried scrolling through the CXF message to see if it is in one of the properties? I use Camel with CXF and get the actual CXF message as follows:

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

You should be able to receive a CXF message in plain CXF, for example:

 PhaseInterceptorChain.getCurrentMessage() 

See this URL: Is there a way to access CXF messaging from a JAX-RS REST resource in CXF?

From there you can get properties such as:

 org.apache.cxf.request.url=someDomain/myURL 
+1
source

I had the same requirements. However, I think it is not possible to get the host and port only from the endpoint definition. And, as you already mentioned, endpoint.getAddress() just gives the service name instead of the whole URL. Here is my reason:

See the expected endpoint address: http://domain.com/tomcat-context/CXFServlet-pattern/test

CXF runtime runs in the servlet container. The middle two parts ( tomcat-context/CXFServlet-pattern ) are actually processed by the servlet container and can be obtained from ServletContext . You can implement org.springframework.web.context.ServletContextAware in Spring. The last part ( test , which is the name of the service) is processed by CXF and can be obtained using endpoint.getAddress() . But the first part, which is schema://host:port , goes beyond any of them and is controlled by the host configuration. For example, your service may receive a request to both http://domain.com and https://doman.com , and the CXF runtime never knows it when deploying services. However, when the request has arrived, it can be obtained from the request or message, as indicated in other messages.

NTN

+1
source

You can create url with the following code. And you can edit depending on 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; } 
0
source

Source: https://habr.com/ru/post/1437440/


All Articles