jax-ws soap Handler

服务器端
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>org.soap.service.handler.LicenseHandler</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains>


public class LicenseHandler implements SOAPHandler<SOAPMessageContext> {

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		try {
			Boolean out = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
			if(!out) {
				SOAPMessage message = context.getMessage();
				SOAPEnvelope enve = message.getSOAPPart().getEnvelope();
				SOAPHeader header = enve.getHeader();
				SOAPBody body = enve.getBody();
				Node bn = body.getChildNodes().item(0);
				String partname = bn.getLocalName();
				if("list".equals(partname)||"addUser".equals(partname)) {
					if(header==null) {
						//添加一个错误信息
						System.out.println("ttt");
						SOAPFault fault = body.addFault();
						fault.setFaultString("头部信息不能为空!");
						throw new SOAPFaultException(fault);
					}
					Iterator<SOAPHeaderElement> iterator = header.extractAllHeaderElements();
					if(!iterator.hasNext()) {
						//添加一个错误信息
						System.out.println("ttt");
						SOAPFault fault = body.addFault();
						fault.setFaultString("头部信息不正确!");
						throw new SOAPFaultException(fault);
					}
					while(iterator.hasNext()) {
						SOAPHeaderElement ele = iterator.next();
						System.out.println(ele.getTextContent());
					}
				}
			}
		} catch (SOAPException e) {
			e.printStackTrace();
		}
		return true;
	}

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

	@Override
	public void close(MessageContext context) {
		
	}

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

}




客户端

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>org.soap.service.handler.HeaderHandler</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains>




public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		try {
			String ns = "http://service.soap.org/";
			//发送信息时MESSAGE_OUTBOUND_PROPERTY
			//接收信息时INBOUND_MESSAGE_ATTACHMENTS
			Boolean out = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
			if(out) {
				SOAPMessage message = context.getMessage();
				//1、判断message中是否存在header
				SOAPEnvelope enve = message.getSOAPPart().getEnvelope();
				
				SOAPBody body = enve.getBody();
				Node node = body.getChildNodes().item(0);
				String pn = node.getLocalName();
				if("list".equals(pn)||"addUser".equals(pn)) {
					SOAPHeader header = enve.getHeader();
					if(header==null) header = enve.addHeader();
					
					QName qname = new QName(ns, "licenseInfo","ns");
					header.addHeaderElement(qname).setValue("123123");
					message.writeTo(System.out);
				}
			}
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		System.out.println("error");
		return false;
	}

	@Override
	public void close(MessageContext context) {
		
	}

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

}

你可能感兴趣的:(handler)