首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

动态的替web service soap消息增加验证规则

2012-11-12 
动态的为web service soap消息增加验证规则在我们当前的SaaS系统中request和response的验证包括两部分,一

动态的为web service soap消息增加验证规则

在我们当前的SaaS系统中request和response的验证包括两部分,一部分为jaxb的schema验证,一部分为局部业务规则约束的验证(通过在service实现中侵入代码进行验证)。

package com.hp.test.cxf.handlers;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;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;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathExpression;import javax.xml.xpath.XPathFactory;import org.xml.sax.InputSource;public class ValidationHandler implements SOAPHandler<SOAPMessageContext> {public Set<QName> getHeaders() {// TODO Auto-generated method stubreturn null;}public void close(MessageContext arg0) {// TODO Auto-generated method stub}public boolean handleFault(SOAPMessageContext arg0) {// TODO Auto-generated method stubreturn false;}public boolean handleMessage(SOAPMessageContext smc) {Boolean outboundProperty = (Boolean)smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);if (outboundProperty.booleanValue()) {//System.out.println("Out:"+smc.toString()); return true; } else { try{ //从缓存中获取此URL(operation)对应的所有验证规则 //循环验证key-rule SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.setNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); // instead of "SOAP-ENV" nsContext.setNamespace("cxf", "http://cxf.test.hp.com/"); String name = getDataXpath(smc.getMessage(), "/soapenv:Envelope/soapenv:Body/cxf:Person/name/child::text()", nsContext); //if(!name.matchs(rule)){throw new RuntimeException("name is invalid!");} //循环结束 }catch(Exception e){e.printStackTrace();} return true; }}public String getDataXpath( SOAPMessage message, String expression, NamespaceContext nsContext ) throws java.io.IOException, javax.xml.soap.SOAPException, javax.xml.xpath.XPathExpressionException { ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); InputSource inputSource = new InputSource( new ByteArrayInputStream(out.toByteArray()) ); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); if (nsContext != null) xPath.setNamespaceContext( nsContext ); XPathExpression xpathExpression = xPath.compile( expression ); return xpathExpression.evaluate( inputSource ); } }class SimpleNamespaceContext implements NamespaceContext { private Map<String,String> map; public SimpleNamespaceContext() { map = new HashMap<String,String>(); } public void setNamespace( String prefix, String namespaceURI ){ map.put(prefix, namespaceURI); } // !!! doesn't fully implement getNamespaceURI API spec !!! public String getNamespaceURI( String prefix ){ return map.get(prefix); } // !!! doesn't fully implement getPrefix API spec !!! public String getPrefix( String namespaceURI ){ SinglePrefixCollector collector = new SinglePrefixCollector(); collectPrefixes( namespaceURI, collector ); return collector.getResult(); } // !!! doesn't fully implement getPrefixes API spec !!! public Iterator getPrefixes( String namespaceURI ){ MultiPrefixCollector collector = new MultiPrefixCollector(); collectPrefixes( namespaceURI, collector ); return collector.getResult(); } protected void collectPrefixes( String namespaceURI, PrefixCollector collector ) { Iterator<String> iterator = map.keySet().iterator(); while( iterator.hasNext() ) { String prefix = iterator.next(); if ( getNamespaceURI(prefix).equals(namespaceURI) ) { boolean addMore = collector.addPrefix(prefix); if(! addMore ) break; } } } protected interface PrefixCollector { // template method for subclasses // return: true - continue collecting; false - stop collecting. boolean addPrefix(String prefix); } static protected class SinglePrefixCollector implements PrefixCollector { private String prefix = null; // returns false to stop further additions as it can only hold one prefix public boolean addPrefix( String prefix ) { this.prefix = prefix; return false; } public String getResult(){ return prefix; } } static protected class MultiPrefixCollector implements PrefixCollector { private List<String> prefixes = new ArrayList<String>(); // returns true as it can hold more than one prefix public boolean addPrefix( String prefix ) { prefixes.add(prefix); return true; } public Iterator getResult() { return prefixes.iterator(); } } } ?

?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cxf="http://cxf.test.hp.com/"> <soapenv:Header/> <soapenv:Body> <cxf:Person> <name>1</name> <address>2</address> <phone>3</phone> <email>eddis</email> <addresslist> <address> <address>8</address> <test>9</test> </address> </addresslist> </cxf:Person> </soapenv:Body></soapenv:Envelope>?

?

?

热点排行