init ()
destroy()
getHeaders()
handleRequest()
handleResponse()
handleFault()
假如使用weblogic.webservice.GenericHandler 类,只需要重写必须的方法,
在这个例子中验证处理程序重写了handleRequest() 和handleResponse() 方法。
假如handleRequest()方法返回true,将会调用处理链中的下一个处理程序,
假如这个处理程序是处理链中最后一个元素,将会调用后台的WEB服务程序。
假如 handleRequest()方法返回false,对应的handleResponse()方法将会被调用。
验证处理程序中handleRequest() 将从SOAP的头部分中重新获得帐号和个人识别码,
并且验证后要返回true 或false结果。
AuthenticationHandler.java
import java.util.Map;
import java.util.Iterator;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import weblogic.webservice.GenericHandler;
public class AuthenticationHandler extends GenericHandler
{
PRivate int me = System.identityHashCode(this);
private HandlerInfo handlerInfo = null;
String accountNo = null;
String pin = null;
public void init(HandlerInfo handlerInfo)
{
this.handlerInfo = handlerInfo;
}
public boolean handleRequest(MessageContext messageContext)
{
System.err.println("** handleRequest called in: "+me);
try
{
SOAPMessageContext sctx = (SOAPMessageContext)messageContext;
SOAPMessage message = sctx.getMessage();
SOAPPart sp = message.getSOAPPart();
SOAPEnvelope senv = sp.getEnvelope();
SOAPHeader sh = senv.getHeader();
Iterator iter = sh.getChildElements();
if(iter.hasNext())
{
iter.next(); //skip text
Object obj = iter.next(); //account details
SOAPElement elt = (SOAPElement)obj;
iter = elt.getChildElements();
iter.next(); //skip text node
obj = iter.next();
elt = (SOAPElement)obj;
accountNo = elt.getValue();
iter.next(); //skip text node
obj = iter.next();
elt = (SOAPElement)obj;
pin = elt.getValue();
}
}
catch (Exception e)
{
e.printStackTrace();
throw new JAXRPCException(e);
}
if(accountNo.equals("12345") && pin.equals("6789"))
return true;
else
return false;
}
public boolean handleResponse(MessageContext messageContext)
{
System.out.println("Inside HandleResponse");
return true;
}
public QName[] getHeaders()
{
return handlerInfo.getHeaders();
}
}
public class BalanceEnquiryService
{
public float getBalance(String accountNo)
{
if(accountNo.equals("12345"))
return 5000f;
else
return 100f;
}
}
<OperaTION name="chainService"
handler-chain="myChain" />
import org.apache.axis.client.Call;
import org.apache.axis.client.ServiceFactory;
import org.apache.axis.client.Service;
import org.apache.axis.MessageContext;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.message.SOAPEnvelope;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import java.util.Iterator;
public class Client
{
private static String TARGET_NAMESPACE = "http://www.bea.com";
private static QName xsdFloat = new
QName("http://www.w3.org/2001/XMLSchema", "float");
public static org.apache.axis.message.SOAPEnvelope env = null;
public static SOAPMessage message = null;
public static void main(String[] argv) throws Exception
{
Client client = new Client();
env = client.constrUCtSOAPEnvelope();
client.constructHeader(env);
client.constructBody(env);
System.setProperty( "javax.xml.rpc.ServiceFactory",
"org.apache.axis.client.ServiceFactory" );
String url =
"http://localhost:7001/BalanceEnquiry/BalanceEnquiryService";
ServiceFactory factory =
(org.apache.axis.client.ServiceFactory)ServiceFactory.
newInstance();
QName serviceName = new QName(TARGET_NAMESPACE,
"BalanceEnquiryService");
Service service =
(org.apache.axis.client.Service)factory.
createService(serviceName);
Call call = (org.apache.axis.client.Call)service.createCall();
call.setTargetEndpointAddress(url);
SOAPEnvelope result = call.invoke(env);
System.out.println(result);
}
public SOAPEnvelope constructSOAPEnvelope() throws Exception
{
org.apache.axis.message.SOAPEnvelope env = new
org.apache.axis.message.SOAPEnvelope();
return env;
}
public void constructHeader(SOAPEnvelope envelope) throws Exception
{
SOAPHeader header = envelope.getHeader();
Name headerElementName =
envelope.createName("AccountDetails","",
"http://schemas.xmlsoap.org/soap/
envelope/");
SOAPHeaderElement headerElement =
header.addHeaderElement(headerElementName);
headerElement.setMustUnderstand(false);
headerElement.addNamespaceDeclaration("soap",
"http://schemas.xmlsoap.org/soap/envelope/");
SOAPElement accNo = headerElement.addChildElement("accountNo");
accNo.addTextNode("12345");
SOAPElement pinNo = headerElement.addChildElement("pin");
pinNo.addTextNode("6789");
}
public void constructBody(SOAPEnvelope envelope) throws Exception
{
SOAPBody body = envelope.getBody();
Name bodyRootElementName =
envelope.createName("getBalance","",
"http://schemas.xmlsoap.org/soap/
encoding/");
SOAPBodyElement bodyRootElement =
body.addBodyElement(bodyRootElementName);
SOAPElement bodyElement =
bodyRootElement.addChildElement("param0");
bodyElement.addAttribute(envelope.createName("xsi:type"),
"xsd:string");
bodyElement.addTextNode("12");
}
}
QName portName = new QName( "http://bea.com/",
"HelloWorldServicePort");
HandlerRegistry registry = service.getHandlerRegistry();
List handlerList = new ArrayList();
handlerList.add( new HandlerInfo( ClientHandler.class, null,null ) );
registry.setHandlerChain( portName, handlerList );
结论:
我们希望能让大家更多的了解关于处理链机制并且应用到WEB服务中。
新闻热点
疑难解答