java使用Message Handler修改WebService客户端的SOAP头

2015-02-19 06:30:01 阅读( 916 )

2984人 收藏本页

标签:Java面试题

在调用webService时,有时候需要在SOAP头中插入信息,比如鉴权信息。

下面的例子演示如何设置给WebService设置授权信息。

首先我们需要实现SOAPHandler接口的类,这个类决定了要调用那些Handler,以什么顺序调用。

最后我们需要给WebService的客户端添加HandlerResolver类实例.

默认情况下SOAP头是空的:

<S:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>TestUser</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">TestPassword</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</S:Header>

如下是SOAPHandler<SOAPMessageContext>的实现,这里有好多方法可以实现,但在这里我们仅需要实现handleMessage()方法。

package cn.outofmemory.ws.example;

import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
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;

/**
*
* @author outofmemory.cn
*/
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

public boolean handleMessage(SOAPMessageContext smc) {

Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if (outboundProperty.booleanValue()) {

SOAPMessage message = smc.getMessage();

try {

SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();

SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

SOAPElement usernameToken =
security.addChildElement("UsernameToken", "wsse");
usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

SOAPElement username =
usernameToken.addChildElement("Username", "wsse");
username.addTextNode("TestUser");

SOAPElement password =
usernameToken.addChildElement("Password", "wsse");
password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
password.addTextNode("TestPassword");

//Print out the outbound SOAP message to System.out
message.writeTo(System.out);
System.out.println("");

} catch (Exception e) {
e.printStackTrace();
}

} else {
try {

//This handler does nothing with the response from the Web Service so
//we just print out the SOAP message.
SOAPMessage message = smc.getMessage();
message.writeTo(System.out);
System.out.println("");

} catch (Exception ex) {
ex.printStackTrace();
}
}

return outboundProperty;

}

public Set getHeaders() {
//throw new UnsupportedOperationException("Not supported yet.");
return null;
}

public boolean handleFault(SOAPMessageContext context) {
//throw new UnsupportedOperationException("Not supported yet.");
return true;
}

public void close(MessageContext context) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}

如下是HandlerResolver的实现类定义:

package cn.outofmemory.ws.example;

import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;

/**
*
* @author outofmemory.cn
*/
public class HeaderHandlerResolver implements HandlerResolver {

public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();

HeaderHandler hh = new HeaderHandler();

handlerChain.add(hh);

return handlerChain;
}
}

下面是调用webService的代码:

JavadbWebServiceService service = new JavadbWebServiceService();

HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
service.setHandlerResolver(handlerResolver);

JavadbWebService port = service.getJavadbWebServicePort();

//调用web service
String currentTime = port.getTime();

System.out.println("Current time is: " + currentTime);

下一篇:java vector 使用示例

上一篇:写出java解压缩zip文件代码

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息: 面试题