xfire

webservice 調用前需要驗證頭信息的簡單實現:

 

xfire服務端目錄結構:



 服務端接口代碼:

package com.demo.xfire.service;

public interface FriendService {
	String findFriendGroups(String personId);
}

 服務端接口實現代碼:

package com.demo.xfire.service.impl;

import com.demo.xfire.service.FriendService;

public class FriendServiceImpl implements FriendService {

	public String  findFriendGroups(String personId) {
		if (!personId.equals("")){
			return personId;
		}
		throw new RuntimeException("Can't find person");
	}

}

 服務端校驗頭信息的實現類:

package com.demo.xfire.service;

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.XFireRuntimeException;
import org.jdom.Element;
import org.jdom.Namespace;

public class XfireAuthenticationHandler extends
		org.codehaus.xfire.handler.AbstractHandler {
	private static final String USERNAME = "libiao";
	private static final String PASSWORD = "neusoft";
	private static final String NS = "http://trends.bfsu.edu.cn/friendService";

	public void invoke(MessageContext ctx) throws Exception {
		// Check if header exists

		Element header = ctx.getInMessage().getHeader();
		if (header == null) {
			throw new XFireRuntimeException("Missing SOAP Header");
		}
		// Does it have version tag
		Element name = header.getChild("USERNAME", Namespace.getNamespace(NS));
		Element pass = header.getChild("PASSWORD", Namespace.getNamespace(NS));
		if (name.getValue().equals(USERNAME) && pass.getValue().equals(PASSWORD)) {
			System.out.println("驗證通過");
		} else {
			System.out.println("驗證未通過");
			throw new XFireRuntimeException("Authentication Failure");
		}
		ctx.setProperty("USERNAME", USERNAME);
		ctx.setProperty("PASSWORD", PASSWORD);
	}
	
}

 服務端創建services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<service xmlns="http://xfire.codehaus.org/config/1.0">
		<name>FriendService</name>
		<namespace>http://localhost:8881/dcp/webservice/FriendService
		</namespace>
		<serviceClass>com.demo.xfire.service.FriendService
		</serviceClass>
		<implementationClass>com.demo.xfire.service.impl.FriendServiceImpl
		</implementationClass>
		<inHandlers>
			<handler handlerClass="com.demo.xfire.service.XfireAuthenticationHandler"></handler>
		</inHandlers>
	</service>
</beans>

 修正web.xml文件:

<!--  提供webservice 服務 -->
	<servlet>  
        <servlet-name>xfire</servlet-name>  
        <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>xfire</servlet-name>  
        <url-pattern>/webservice/*</url-pattern>  
    </servlet-mapping> 

 

以上爲服務器端代碼,下面爲客戶端代碼。

xifire客戶端目錄結構:



 

客戶端接口類:

package com.demo.xfire.client;
public interface FriendService {
	String findFriendGroups(String personId);
}

 客戶端調用webservice類:

package com.demo.xfire.client;

import java.lang.reflect.Proxy;

import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FriendServieTest {

	public static void main(String[] args) {
		try {
			//方式一
			org.codehaus.xfire.service.Service service = new ObjectServiceFactory().create(FriendService.class);
			FriendService friend = (FriendService) new XFireProxyFactory().create(service, "http://localhost:8881/xfire/webservice/FriendService");
			XFireProxy proxy = (XFireProxy) Proxy.getInvocationHandler(friend);
			Client client = proxy.getClient();
	        client.addOutHandler(new ClientHeaderHandler());
	        String str = friend.findFriendGroups("2011001");
	        System.out.println(str);
	        client.close();
			
	        //方式二
	        ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{});
//			org.springframework.core.io.Resource resource = new ClassPathResource("/wsdl.xml");
	        org.springframework.core.io.Resource resource = appContext.getResource("url:http://localhost:8881/xfire/webservice/FriendService?wsdl");
			Client client1 = new Client(resource.getInputStream(), null); //根據WSDL創建客戶實例  
			client1.addOutHandler(new ClientHeaderHandler());  
			Object[] objArray = new Object[1];  
			objArray[0] = "123456";  
			//調用特定的Web Service方法  
			Object[] results = client1.invoke("findFriendGroups", objArray); 
			System.out.println(results[0]);
			client1.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

 在發送請求中添加頭信息的類:

package com.demo.xfire.client;

import org.jdom.Element;

public class ClientHeaderHandler extends org.codehaus.xfire.handler.AbstractHandler{

	private static final String USERNAME = "libiao";
	private static final String PASSWORD = "neusoft";
	private static final String NS = "http://trends.bfsu.edu.cn/friendService";

	public void invoke(org.codehaus.xfire.MessageContext ctx) throws Exception {
		Element header = ctx.getOutMessage().getOrCreateHeader();
		header.addContent(new Element("USERNAME", NS).addContent(USERNAME));
		header.addContent(new Element("PASSWORD", NS).addContent(PASSWORD));
	}
	
}

 以上爲全部代碼,代碼也一併上傳了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章