xfire

webservice 调用前需要验证头信息的简单实现:

 

xfire服务端目录结构:


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客户端目录结构:


xfire
 

客户端接口类:

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

 以上为全部代码,代码也一并上传了。

 

你可能感兴趣的:(xfire)