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