欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAX-WS:客户端中的SOAP处理程序

程序员文章站 2022-05-18 15:05:57
...

这是JAX-WS SOAP处理程序的第2部分。 在上一篇文章– JAX-WS:服务器端的SOAP处理程序中 ,您为每个传入的SOAP消息创建了一个Web服务并附加了一个处理程序以在标头块中检索客户端MAC地址。

客户端中的SOAP处理程序

在本文中,您将开发一个Web服务客户端,以访问上一篇文章中发布的服务,并为每个客户端发送的每个传出SOAP消息附加一个处理程序,以将客户端的MAC地址注入到标头块中。

本示例的目录结构

JAX-WS:客户端中的SOAP处理程序

1. Web服务客户端

使用wsimport命令分析已发布的服务WSDL文件( http:// localhost:8888 / ws / server?wsdl ),并生成访问服务所需的所有文件。

C:\>wsimport -keep -verbose http://localhost:8888/ws/server?wsdl
parsing WSDL...

generating code...
com\mkyong\ws\GetServerName.java
com\mkyong\ws\GetServerNameResponse.java
com\mkyong\ws\ObjectFactory.java
com\mkyong\ws\ServerInfo.java
com\mkyong\ws\ServerInfoService.java
com\mkyong\ws\package-info.java

自动生成六个文件,您可能只需要关注ServerInfoService.java

文件:ServerInfoService.java

@WebServiceClient(name = "ServerInfoService", 
	targetNamespace = "http://ws.mkyong.com/", 
	wsdlLocation = "http://localhost:8888/ws/server?wsdl")
public class ServerInfoService extends Service
{
	//......
}

客户端访问发布的服务。
文件:WsClient.java

package com.mkyong.client;

import com.mkyong.ws.ServerInfo;
import com.mkyong.ws.ServerInfoService;

public class WsClient{
	
	public static void main(String[] args) throws Exception {
	   
		ServerInfoService sis = new ServerInfoService();
		ServerInfo si = sis.getServerInfoPort();

		System.out.println(si.getServerName());
       
    }

}

2. SOAP处理程序

创建一个SOAP处理程序,以针对每个传出的SOAP消息将客户端的MAC地址注入SOAP标头块中。 有关代码说明,请参见注释。

文件:MacAddressInjectHandler.java

package com.mkyong.handler;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
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;

public class MacAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{

   @Override
   public boolean handleMessage(SOAPMessageContext context) {

	System.out.println("Client : handleMessage()......");
		
	Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

	//if this is a request, true for outbound messages, false for inbound
	if(isRequest){
			
	try{
	    SOAPMessage soapMsg = context.getMessage();
            SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
            SOAPHeader soapHeader = soapEnv.getHeader();
	            
            //if no header, add one
            if (soapHeader == null){
            	soapHeader = soapEnv.addHeader();
            }

            //get mac address
            String mac = getMACAddress();
	            
            //add a soap header, name as "mac address"
            QName qname = new QName("http://ws.mkyong.com/", "macAddress");
            SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);

            soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
            soapHeaderElement.addTextNode(mac);
            soapMsg.saveChanges();

            //tracking
            soapMsg.writeTo(System.out);

	             
	   }catch(SOAPException e){
		System.err.println(e);
	   }catch(IOException e){
		System.err.println(e);
	   }
            
         }

	   //continue other handler chain
	   return true;
   }

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		System.out.println("Client : handleFault()......");
		return true;
	}

	@Override
	public void close(MessageContext context) {
		System.out.println("Client : close()......");
	}

	@Override
	public Set<QName> getHeaders() {
		System.out.println("Client : getHeaders()......");
		return null;
	}
	
   //return current client mac address
   private String getMACAddress(){
		
	InetAddress ip;
	StringBuilder sb = new StringBuilder();
		
	try {
			
		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());
		
		NetworkInterface network = NetworkInterface.getByInetAddress(ip);

		byte[] mac = network.getHardwareAddress();
			
		System.out.print("Current MAC address : ");
			
		for (int i = 0; i < mac.length; i++) {
				
			sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
				
		}
		System.out.println(sb.toString());
			
	} catch (UnknownHostException e) {
		
		e.printStackTrace();
		
	} catch (SocketException e){
			
		e.printStackTrace();
			
	}
		
	return sb.toString();
   }
	
}

3. SOAP处理程序XML文件

创建一个SOAP处理程序XML文件,然后放入SOAP处理程序声明。

文件:handler-chain.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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.mkyong.handler.MacAddressInjectHandler</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains>

4.附加SOAP处理程序–> Web服务客户端

要将上述SOAP处理程序附加到Web服务客户端,请编辑ServerInfoService.java文件(通过wsimport生成),并使用@HandlerChain进行注释,并在其中指定SOAP处理程序文件名。

文件:ServerInfoService.java

@WebServiceClient(name = "ServerInfoService", 
	targetNamespace = "http://ws.mkyong.com/", 
	wsdlLocation = "http://localhost:8888/ws/server?wsdl")
@HandlerChain(file="handler-chain.xml")
public class ServerInfoService extends Service
{
	//......
}

完成后,请继续下一篇文章– 第3部分:JAX-WS –用于客户端和服务器端的SOAP处理程序测试

下载源代码

下载它– JAX-WS-Handler-Example.zip (21KB)

From: https://mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/