cxf 配置客户端

springcxf.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:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.1.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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	     
	<jaxws:client id="AvailabilityClient" serviceClass="com.evertrip.ws.bean.AvailabilitySoap" address="https://xml.hotelclub.net/XMLWS_V2/XmlWsdl/V2.00/Availability.asmx">

		<jaxws:outInterceptors>      
			<ref bean="WSInterceptor" />  
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
		</jaxws:inInterceptors>	
	</jaxws:client>     
    
     
</beans>


使用拦截器向请求加入头信息


package com.evertrip.ws.util;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * WebService全局拦截器
 * 
 * @author lcy
 * 
 */

@Component("WSInterceptor")
public class WSInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	public WSInterceptor() {
		// 指定该拦截器在哪个阶段被激发
		super(Phase.PREPARE_SEND);

	}

	@Override
	public void handleMessage(SoapMessage soapmessage) throws Fault {
		/*
		 * //参考cxf源码-MessageImpl.java的getContent(Class<T> format)方法 final
		 * MessageContentsList mcl = (MessageContentsList)
		 * soapmessage.getContent(List.class);
		 */

		final Document document = DOMUtils.createDocument();
		final Element spId = document.createElement("AffiliateID");
		final Element spPass = document.createElement("Password");
		spId.setTextContent("xxxx");
		spPass.setTextContent("xxxxxx");

		final Element root = document.createElementNS(
				"https://xml.hotelclub.net/xmlws/services/v2/",
				"AuthenticationInfo");

		root.appendChild(spId);
		root.appendChild(spPass);

		final Element spip = document.createElement("ClientIP");
		spip.setTextContent(fetchRandomIP());
		root.appendChild(spip);

		soapmessage.getHeaders().add(new SoapHeader(new QName(""), root));
	}

	/**
	 * 获取随机外网ip
	 * 
	 * @author lcy
	 * @return
	 */

	static int i;

	public static String fetchRandomIP() {

		// 非127,192开头
		final StringBuilder sb = new StringBuilder();
		int firstpart = 0;
		while (true) {
			firstpart = randomnumber(1, 254);
			if (firstpart < 224 && firstpart != 127 && firstpart != 192
					&& firstpart != 10 && firstpart != 0)
				break;
		}
		sb.append(firstpart).append('.').append(randomnumber(1, 254))
				.append('.').append(randomnumber(1, 254)).append('.')
				.append(randomnumber(1, 254));

		 return sb.toString(); 
	}

	private static int randomnumber(int min, int max) {
		return (int) Math.abs(Math.round(Math.random() * (min - max) + min));
	}

	@Override
	public void handleFault(SoapMessage arg0) {
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值