2012-10-02 47 views
4

情境

我正在将Apache CXF 2.6.2的Web服务部署到Tomcat服务器。我出口使用CXFServlet和下面的Spring基于配置的服务:获取CXF端点的URL

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

在我的例子部署CXFServlet使用相对路径/服务,例如通过TestImpl类实现的Web服务可作为http://domain.com/tomcat-context/services/test TestBean类有一个端点设置器,它由Spring设置。

目标

我想确定地址(URL),这是testBean就采用端点领域中的类由端点test_endpoint提供。结果应该是“http://domain.com/tomcat-context/services/test”。

我已经试过

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()); 

,但结果却

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

我怎样才能得到完整的网址?有没有办法独自建造它?

回答

0

您是否尝试过绕过CXF消息来查看它是否在某个属性中?我用骆驼与CXF并获得实际的CXF的消息是这样的:

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

你应该能够得到CXF邮件中只是普通的CXF像这样:

PhaseInterceptorChain.getCurrentMessage() 

看到这个网址:Is there a way to access the CXF message exchange from a JAX-RS REST Resource within CXF?

从那里,你可以得到的属性,如:

org.apache.cxf.request.url=someDomain/myURL 
0

您可以构建具有以下代码的网址。并且您可以根据您的环境进行适当的编辑。

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; 
     } 
1

我有同样的要求。但是,我认为从端点定义中检索主机和端口是不可能的。正如你所提到的,endpoint.getAddress()只是提供服务名称而不是整个网址。这里是我的理由:

让我们来看看预期端点地址:http://domain.com/tomcat-context/CXFServlet-pattern/test

CXF运行在servlet容器中运行。中间两部分(tomcat-context/CXFServlet-pattern)实际上是由servlet容器处理的,可以从ServletContext中检索。你可以在Spring中实现org.springframework.web.context.ServletContextAware。最后一部分(服务名称为test)由CXF处理,可通过endpoint.getAddress()进行检索。但第一部分为schema://host:port超出此范围,且受控于主机配置。例如,您的服务可能同时收到http://domain.comhttps://doman.com的请求,并且在部署服务时CXF运行时永远不会知道它。但是,当请求到达时,可以从请求或消息中检索,如其他帖子中提到的。

HTH