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