基于Jax--ws的webservice
在开发webservice的时候,我们需要加入自己的控制逻辑,自定义过滤器,对soap消息进行处理,或者是预先进行一些设置,等等。首先介绍一下如何自定义处理类逻辑。
基于JAS-WS有一个重要的文件,sun.jaxws.xml,这个文件是用来定义服务的处理类的,就好比web.xml一样,我们访问某个业务处理,是由哪一个servlet处理一样。
OK,先看文件。
<?xml version="1.0" encoding="UTF-8"?><endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'><endpoint name="hotel" interface="com.xmlapi.shop.hotel.v1.HotelShopPortType" implementation="com.shopsvc.webservices.hotel.HotelShopSoapServiceImpl" binding="http://www.w3.org/2003/05/soap/bindings/HTTP/" url-pattern="/soap/hotel/v1/*" service="{urn:com:xmlapi:shop:hotel:v1}HotelShopService" port="{urn:com:xmlapi:shop:hotel:v1}HotelShopPort" wsdl="WEB-INF/wsdl/com.xmlapi.shop.hotel.v1.wsdl"><javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"><javaee:handler-chain><javaee:handler><javaee:handler-class>com.shopsvc.soap.InboundHandler</javaee:handler-class></javaee:handler><javaee:handler><javaee:handler-class>com.shopsvc.soap.OutboundHandler</javaee:handler-class></javaee:handler></javaee:handler-chain></javaee:handler-chains></endpoint></endpoints>
public abstract class BaseProtocolHandler implements SOAPHandler<SOAPMessageContext>{/** * * @param smc context of SOAP request message */abstract void handleRequest(SOAPMessageContext smc);/** * * @param smc context of SOAP response message */abstract void handleResponse(SOAPMessageContext smc);/** * Passes message handling down to the EWS filter manager. */public boolean handleMessage(SOAPMessageContext smc) {if (smc == null){ SoapUtils.createInternalErrorException();}Object objectOutbound = smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);if (objectOutbound == null || objectOutbound.getClass() != Boolean.class){ SoapUtils.createInternalErrorException();}boolean direction = (Boolean)objectOutbound;if (direction == FilterChain.INBOUND_DIRECTION){this.handleRequest(smc);}else{this.handleResponse(smc);}// Returning true tells JAX-WS that it should not stop calling other handlers in// the chain. Returning false would stop calling the remaining handlers if any. return true;}/** * Passes fault handling down to the filter manager. */public boolean handleFault(SOAPMessageContext smc){smc.put(EwsSoapUtils.IS_FAULT_RESPONSE_KEY, true);this.handleResponse(smc);return true;}/** * Informs JAX-WS what headers will understand. * * This is necessary to be able to process Security header that comes * with mustUnderstand="true" attribute. * * @see javax.xml.ws.handler.soap.SOAPHandler#getHeaders() */public Set<QName> getHeaders(){HashSet<QName> headers = new HashSet<QName>();headers.add(new QName("http://schemas.xmlsoap.org/ws/2002/12/secext", "Security", ""));// This is to support .NET WSE 3.0 security. headers.add(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", ""));return headers;}