基于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>


从这个文件我们自定义了2个handle来处理我们的请求和响应。包括当前提供的服务,处理那些url。
接下来我们看看handle类的结构。

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




这是大体的逻辑框架,当服务进入的时候,首先会进入我们的handleMessage,然后去调用handleRequest or response.
这只是父类的实现,接下来我们继续讲子类的实现,以及自定义过滤器的运用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值