JAX-WS HandlerChain使用詳解

JAX-WS的Handler和Servlet的Filter相似,可以對所有WebServicer進行攔截,在Handler中可以記錄日誌、權限控制、對請求的SOAP消息進行加密,解密等。JAX-WS提供兩個Handler接口,LogicalHandler和SOAPHandler。LogicalHandler處理的是Message Payload,只能夠訪問消息單元中的SOAP消息體。SOAPHandler處理的是整個SOAP消息(包含SOAP header和SOAP body),可以訪問整個SOAP消息。

註冊Handler的方式有下面幾種:

使用HandlerResolver(客戶端比較方便)

使用HandlerChain註解和配置文件

從WSDL生成

使用Custom Binding聲明HandlerChain


實例代碼http://download.csdn.net/detail/accountwcx/8922191


JAX-WS中WebService執行順序如圖所示



下面用SOAPHandler實現在WebService服務端記錄請求內容和響應內容。

import java.io.IOException;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

/**
 * 記錄SOAP請求及響應
 * @author [email protected]
 *
 */
public class LoggerHandler implements SOAPHandler<SOAPMessageContext> {

	@Override
	public void close(MessageContext context) {
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return true;
	}

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		// 判斷消息是輸入還是輸出
		Boolean output = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		System.out.println(output ? "響應SOAP:" : "請求SOAP:");
		
		SOAPMessage message = context.getMessage();
		
		try {
			message.writeTo(System.out);
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("");

		return true;
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}

在classpath下建handler-chain.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<javaee:handler-chain>
		<javaee:handler>
			<javaee:handler-class>com.rvho.server.ws.handler.LoggerHandler</javaee:handler-class>
		</javaee:handler>
	</javaee:handler-chain>
</javaee:handler-chains>

在服務實現類上添加HandlerChain配置

package com.rvho.server.ws.impl;

import java.util.Date;

import javax.jws.HandlerChain;
import javax.jws.WebService;

import com.rvho.server.ws.HelloWService;

@WebService(
	endpointInterface = "com.rvho.server.ws.HelloWService",
	portName = "HelloWSPort",
	serviceName = "HelloWSService",
	targetNamespace = "http://www.tmp.com/ws/hello"
)
@HandlerChain(file="handler-chain.xml") //添加Handler配置文件
public class HelloWServiceImpl implements HelloWService {
	public String index() {
		return "hello";
	}

	public Integer add(Integer x, Integer y) {
		return x + y;
	}

	public Date now() {
		return new Date();
	}
}

服務實現接口

package com.rvho.server.ws;

import java.util.Date;

import javax.jws.WebService;

/**
 * WebService接口
 */
@WebService(
	name = "HelloWS",
	targetNamespace = "http://www.tmp.com/ws/hello"
)
public interface HelloWService {
	/**
	 * 返回字符串
	 * 
	 * @return
	 */
	String index();

	/**
	 * 兩個整數相加
	 * 
	 * @param x
	 * @param y
	 * @return 相加後的值
	 */
	Integer add(Integer x, Integer y);

	/**
	 * 返回當前時間
	 * 
	 * @return
	 */
	Date now();
}

客戶端發起index請求,服務端的記錄

請求SOAP:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
			xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Header/>
	<S:Body>
		<ns2:index xmlns:ns2="http://www.tmp.com/ws/hello" />
	</S:Body>
</S:Envelope>

響應SOAP:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
			xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Header/>
	<S:Body>
		<ns2:indexResponse xmlns:ns2="http://www.tmp.com/ws/hello">
			<return>hello</return>
		</ns2:indexResponse>
	</S:Body>
</S:Envelope>


發佈了91 篇原創文章 · 獲贊 40 · 訪問量 86萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章