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
46
47
48
49
50
51
52
53
54
55
56
57
58 }
59
60 }
61 }
62 catch (Exception e)
63 {
64
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
94
95
96
97
98
99
100
101 List<OperatorContentType> eoList = policy.getExactlyOneList();
102 ExactlyOneDocument.Factory.newInstance();
103
104
105
106
107
108
109
110
111
112
113
114
115 return policy;
116 }
117 public static Element normalize(Element policy)
118 {
119
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 }