CXF添加Header

Posted by Vincent on May 10, 2015

CXF的header处理了半天。。。

添加拦截器

    
public class HotelHeaderInterceptor extends AbstractSoapInterceptor {
public void handleMessage(SoapMessage message) throws Fault {
		    
		    List<Header> headers = message.getHeaders(); 
		 
		    AuthenticationHeader aheader = new AuthenticationHeader();
		    aheader.setCulture(com.tourico.schemas.webservices.authentication.Culture.EN_US);
		    aheader.setLoginName(username);
		    aheader.setPassword(password);
		    aheader.setVersion(new JAXBElement<String>(new QName("", "version"), String.class, version));
		    
		    JAXBElement<AuthenticationHeader> authHeaders = new ObjectFactory().createAuthenticationHeader(aheader);
		    
		    Header header;
			try {
				header = new Header(authHeaders.getName(), aheader,new JAXBDataBinding(AuthenticationHeader.class));
				headers.add(header);
			    message.put(Header.HEADER_LIST, headers);
			} catch (JAXBException e) {
				
				e.printStackTrace();
			}
	}

调用服务之前处理

    IDestinationContracts  port = service.getIISDestinationHosting() ;
			Client cxfClient = ClientProxy.getClient(port);
			
			cxfClient.getOutInterceptors().add(new HeaderInterceptor(Phase.WRITE));