View Javadoc

1   package com.eviware.soapui.impl.wsdl.support.policy;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.xmlbeans.XmlObject;
7   import org.apache.xmlbeans.XmlOptions;
8   import org.w3c.dom.Element;
9   import org.w3c.dom.Node;
10  import org.w3c.dom.NodeList;
11  import org.xmlsoap.schemas.ws.x2004.x09.policy.ExactlyOneDocument;
12  import org.xmlsoap.schemas.ws.x2004.x09.policy.OperatorContentType;
13  import org.xmlsoap.schemas.ws.x2004.x09.policy.Policy;
14  import org.xmlsoap.schemas.ws.x2004.x09.policy.PolicyDocument;
15  
16  import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
17  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
18  import com.eviware.soapui.support.StringUtils;
19  
20  public class PolicyUtils
21  {
22  	public static final String WSAM_NAMESPACE = "http://www.w3.org/2007/05/addressing/metadata";
23  	public final static String WS_POLICY_NAMESPACE = "http://schemas.xmlsoap.org/ws/2004/09/policy";
24  	public final static String WS_SECURITY_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
25  
26  	public static List<Policy> getPolicies(WsdlContext wsdlContext) {
27        
28  		List<Policy> policies = new ArrayList<Policy>();
29  		try
30  		{
31  			List<InterfaceDefinitionPart> parts = wsdlContext.getDefinitionCache().getDefinitionParts();
32  			for (int i = 0; i < parts.size(); i++)
33  			{
34  				InterfaceDefinitionPart part = parts.get(i);
35  				String content = part.getContent();
36  				XmlObject xml = XmlObject.Factory.parse(content);
37  				XmlObject[] paths = xml.selectPath( "declare namespace wsp='" + WS_POLICY_NAMESPACE + "';" + 
38  				         "//wsp:Policy");	
39  				
40  				for( XmlObject obj : paths )
41  				{
42  					String xx = obj.xmlText(new XmlOptions().setSaveOuter());
43  					PolicyDocument policyDocument = PolicyDocument.Factory.parse( xx );
44  					org.xmlsoap.schemas.ws.x2004.x09.policy.Policy polc = (org.xmlsoap.schemas.ws.x2004.x09.policy.Policy)policyDocument.getPolicy();
45  					policies.add(polc);
46  //					List<Addressing> addressingList = polc.getAddressingList();
47  //					Addressing a = null;
48  //					if (addressingList.size() > 0 )
49  //					{
50  //						a = addressingList.get(0);
51  //					}
52  //					AnonymousResponses ar = null;
53  //					List<AnonymousResponses> anList = polc.getAnonymousResponsesList();
54  //					if (anList.size() > 0)
55  //					{
56  //						ar = anList.get(0);
57  //					}
58  				
59  				}
60  				
61  			}
62  		}
63  		catch (Exception e)
64  		{
65  			// TODO Auto-generated catch block
66  			e.printStackTrace();
67  		}
68        
69  		return null;
70  	}
71  	public static boolean isAddressing(Policy policy) {
72  		
73  		if (policy.getAddressingList().size() > 0 )
74  		{
75  			return true;
76  		}
77  		
78  		return false;
79  	}
80  	public static List<Policy> getAddressingPolicies(WsdlContext wsdlContext) {
81  		List<Policy> addressingPolicies = new ArrayList<Policy>();
82  		List<Policy> policies = getPolicies(wsdlContext);
83  		for (Policy policy : policies)
84  		{
85  			if (isAddressing(policy))
86  			{
87  				addressingPolicies.add(policy);
88  			}
89  		}
90  		return addressingPolicies;
91  	}
92  	public static Policy normalize(Policy policy) {
93  		
94  //		1.Start with the Element Information Item E (as defined in the XML Information Set [XML Information Set]) of the policy expression. 
95  //		The [namespace name] of E is always "http://www.w3.org/ns/ws-policy". In the base case, the [local name] property of E is "Policy"; 
96  //		in the recursive case, the [local name] property of E is "Policy", "ExactlyOne", or "All".
97  //
98  //		2.Expand Element Information Items (as defined in the XML Information Set [XML Information Set]) in the [children] property of E that 
99  //		are policy references per Section 4.3.5 Policy Inclusion.
100 //
101 //		3.Convert each Element Information Item C in the [children] property of E into normal form.
102 		List<OperatorContentType> eoList = policy.getExactlyOneList();
103 		ExactlyOneDocument.Factory.newInstance();
104 //
105 //		3.1 If the [namespace name] property of C is "http://www.w3.org/ns/ws-policy" and the [local name] property of C is "Policy", 
106 //		"ExactlyOne", or "All", C is an expression of a policy operator; normalize C by recursively applying this procedure.
107 //
108 //		3.2 Otherwise the Element Information Item C is an assertion; normalize C per Sections 4.3.1 Optional Policy Assertions and 4.3.2 
109 //		Policy Assertion Nesting.
110 //
111 //		4.Apply the policy operator indicated by E to the normalized Element Information Items in its [children] property and co1.nstruct a 
112 //		normal form per Section 4.3.3 Policy Operators and 4.1 Normal Form Policy Expression.
113 //
114 
115 	
116 		return policy;
117 	}
118 	public static Element normalize(Element policy)
119 	{
120 //		if (!StringUtils.isNullOrEmpty(nameSpace) && !StringUtils.isNullOrEmpty(localName))
121 //		{
122 		NodeList nl = policy.getChildNodes();
123       List<Element> listElms = new ArrayList<Element>();
124       for( int c = 0; c < nl.getLength(); c++ )
125       {
126          Node item = nl.item( c );
127          if( item.getParentNode() == policy && item.getNodeType() == Node.ELEMENT_NODE )
128             listElms.add( (Element) item );
129       }
130       
131 		for (int i = 0; i < listElms.size(); i++)
132 		{
133 			Element elm = listElms.get(i);
134 			Element newElm = null;
135 			String nameSpace = elm.getNamespaceURI();
136 			String localName = elm.getLocalName();
137 			if (nameSpace.equals(WS_POLICY_NAMESPACE)
138 					&& (localName.equals("Policy") || localName.equals("All") || localName.equals("ExactlyOne")))
139 			{
140 					newElm = normalize(elm);
141 
142 			} else {
143 				
144 				Element allElm = elm.getOwnerDocument().createElementNS(WS_POLICY_NAMESPACE, "All");
145 				allElm.appendChild(elm);
146 
147 				Element exactlyOneElm = elm.getOwnerDocument().createElementNS(WS_POLICY_NAMESPACE, "ExactlyOne");
148 				exactlyOneElm.appendChild(allElm);
149 				
150 				String optional = elm.getAttributeNS(WS_POLICY_NAMESPACE, "Optional");
151 				if (!StringUtils.isNullOrEmpty(optional) &&  optional.equals("true"))
152 				{
153 					Element allElmEmpty = elm.getOwnerDocument().createElementNS(WS_POLICY_NAMESPACE, "All");
154 					exactlyOneElm.appendChild(allElmEmpty);
155 				}
156 				
157 				newElm = exactlyOneElm;
158 			}
159 			elm.getParentNode().replaceChild(elm, newElm);
160 		}
161 //		}
162 
163 		return policy;
164 	}
165 }