1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support.policy;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import javax.wsdl.Definition;
20 import javax.wsdl.extensions.ElementExtensible;
21 import javax.xml.namespace.QName;
22
23 import org.apache.xmlbeans.XmlObject;
24 import org.apache.xmlbeans.XmlOptions;
25 import org.w3.x2007.x05.addressing.metadata.AddressingDocument.Addressing;
26 import org.w3c.dom.Element;
27 import org.xmlsoap.schemas.ws.x2004.x09.policy.OptionalType;
28 import org.xmlsoap.schemas.ws.x2004.x09.policy.Policy;
29 import org.xmlsoap.schemas.ws.x2004.x09.policy.PolicyDocument;
30
31 import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
32 import com.eviware.soapui.impl.wsdl.support.wsa.WsaUtils;
33 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
34 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils;
35 import com.eviware.soapui.support.StringUtils;
36 import com.eviware.soapui.support.xml.XmlUtils;
37
38 public class PolicyUtils
39 {
40 public final static String WS_XMLSOAP_POLICY_NAMESPACE = "http://schemas.xmlsoap.org/ws/2004/09/policy";
41 public final static String WS_W3_POLICY_NAMESPACE = "http://www.w3.org/ns/ws-policy";
42 public final static String WS_SECURITY_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
43
44 public static List<Policy> getPolicies( WsdlContext wsdlContext )
45 {
46
47 List<Policy> policies = new ArrayList<Policy>();
48 try
49 {
50 List<InterfaceDefinitionPart> parts = wsdlContext.getDefinitionCache().getDefinitionParts();
51 for( int i = 0; i < parts.size(); i++ )
52 {
53 InterfaceDefinitionPart part = parts.get( i );
54 String content = part.getContent();
55 XmlObject xml = XmlObject.Factory.parse( content );
56
57 XmlObject[] paths = xml.selectPath( "declare namespace wsp='" + WS_W3_POLICY_NAMESPACE + "';"
58 + "//wsp:Policy" );
59 List<XmlObject> listOfXmlObjcts = Arrays.asList( paths );
60
61 XmlObject[] paths1 = xml.selectPath( "declare namespace wsp='" + WS_XMLSOAP_POLICY_NAMESPACE + "';"
62 + "//wsp:Policy" );
63 listOfXmlObjcts.addAll( Arrays.asList( paths1 ) );
64 paths = ( XmlObject[] )listOfXmlObjcts.toArray();
65
66 for( XmlObject obj : paths )
67 {
68 String xx = obj.xmlText( new XmlOptions().setSaveOuter() );
69 PolicyDocument policyDocument = PolicyDocument.Factory.parse( xx );
70 org.xmlsoap.schemas.ws.x2004.x09.policy.Policy polc = ( org.xmlsoap.schemas.ws.x2004.x09.policy.Policy )policyDocument
71 .getPolicy();
72 policies.add( polc );
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 }
88
89 }
90 }
91 catch( Exception e )
92 {
93
94 e.printStackTrace();
95 }
96
97 return null;
98 }
99
100 public static boolean isAddressing( Policy policy )
101 {
102
103 if( policy.getAddressingList().size() > 0 )
104 {
105 return true;
106 }
107
108 return false;
109 }
110
111 public static List<Policy> getAddressingPolicies( WsdlContext wsdlContext )
112 {
113 List<Policy> addressingPolicies = new ArrayList<Policy>();
114 List<Policy> policies = getPolicies( wsdlContext );
115 for( Policy policy : policies )
116 {
117 if( isAddressing( policy ) )
118 {
119 addressingPolicies.add( policy );
120 }
121 }
122 return addressingPolicies;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public static Policy getAttachedPolicy( ElementExtensible item, Definition def )
223 {
224
225 Policy rtnPolicy = null;
226 String usedPolicyNamespace = PolicyUtils.WS_W3_POLICY_NAMESPACE;
227 Element[] policyReferences = WsdlUtils.getExentsibilityElements( item, new QName(
228 PolicyUtils.WS_W3_POLICY_NAMESPACE, "PolicyReference" ) );
229 if( policyReferences.length <= 0 )
230 {
231 policyReferences = WsdlUtils.getExentsibilityElements( item, new QName(
232 PolicyUtils.WS_XMLSOAP_POLICY_NAMESPACE, "PolicyReference" ) );
233 usedPolicyNamespace = PolicyUtils.WS_XMLSOAP_POLICY_NAMESPACE;
234 }
235 if( policyReferences.length > 0 )
236 {
237 String policyId = policyReferences[0].getAttribute( "URI" );
238 if( !StringUtils.isNullOrEmpty( policyId ) )
239 {
240 Element[] policies = WsdlUtils.getExentsibilityElements( def, new QName( usedPolicyNamespace, "Policy" ) );
241 Element policy = null;
242 for( int i = 0; i < policies.length; i++ )
243 {
244 policy = policies[i];
245 String policyIdx = policy.getAttributeNS( WS_SECURITY_NAMESPACE, "Id" );
246 if( policyId.equals( "#" + policyIdx ) )
247 {
248 rtnPolicy = getPolicy( policy, usedPolicyNamespace );
249 continue;
250 }
251
252 }
253 }
254 }
255 else
256 {
257
258 Element[] itemPolicies = WsdlUtils.getExentsibilityElements( item, new QName( usedPolicyNamespace, "Policy" ) );
259 if( itemPolicies.length > 0 )
260 {
261 for( int i = 0; i < itemPolicies.length; i++ )
262 {
263 Element policy = itemPolicies[i];
264 rtnPolicy = getPolicy( policy, usedPolicyNamespace );
265
266 }
267 }
268 }
269 return rtnPolicy;
270 }
271
272 public static Policy getPolicy( Element policy, String usedPolicyNamespace )
273 {
274
275
276 Policy newPolicy = null;
277
278
279
280 Element exactlyOne = XmlUtils.getFirstChildElementNS( policy, usedPolicyNamespace, "ExactlyOne" );
281 if( exactlyOne != null )
282 {
283 Element all = XmlUtils.getFirstChildElementNS( exactlyOne, usedPolicyNamespace, "All" );
284 if( all != null )
285 {
286 newPolicy = getAddressingPolicy( all, usedPolicyNamespace );
287 }
288 }
289 else
290 {
291 newPolicy = getAddressingPolicy( policy, usedPolicyNamespace );
292 }
293 return newPolicy;
294 }
295
296 private static Policy getAddressingPolicy( Element wsamAddressingElm, String usedPolicyNamespace )
297 {
298
299 Element wsAddressing = XmlUtils.getFirstChildElementNS( wsamAddressingElm, WsaUtils.WS_A_NAMESPACE_200705,
300 "Addressing" );
301 Element addressingPolicy = null;
302 Policy newPolicy = PolicyDocument.Factory.newInstance().addNewPolicy();
303 Addressing newAddressing = null;
304 if( wsAddressing != null )
305 {
306 newAddressing = newPolicy.addNewAddressing();
307 String optional = wsAddressing.getAttributeNS( usedPolicyNamespace, "Optional" );
308 if( !StringUtils.isNullOrEmpty( optional ) && optional.equals( OptionalType.TRUE.toString() ) )
309 {
310 newAddressing.setOptional( OptionalType.TRUE );
311 }
312 else
313 {
314 newAddressing.setOptional( OptionalType.FALSE );
315 }
316 addressingPolicy = XmlUtils.getFirstChildElementNS( wsAddressing, usedPolicyNamespace, "Policy" );
317 if( addressingPolicy != null )
318 {
319 Element exactlyOne = XmlUtils.getFirstChildElementNS( addressingPolicy, usedPolicyNamespace, "ExactlyOne" );
320 if( exactlyOne != null )
321 {
322 Element all = XmlUtils.getFirstChildElementNS( exactlyOne, usedPolicyNamespace, "All" );
323 if( all != null )
324 {
325 getAddressingAnonymous( all, newAddressing );
326 }
327 }
328 else
329 {
330 getAddressingAnonymous( addressingPolicy, newAddressing );
331 }
332
333 }
334 }
335 Element usingAddressing = XmlUtils.getFirstChildElementNS( wsamAddressingElm, WsaUtils.WS_A_NAMESPACE_200605,
336 "UsingAddressing" );
337 if( usingAddressing != null )
338 {
339
340 newPolicy.addNewUsingAddressing();
341 }
342 return newPolicy;
343 }
344
345 private static void getAddressingAnonymous( Element addressingPolicy, Addressing newAddressing )
346 {
347 Policy innerPolicy = newAddressing.addNewPolicy();
348
349 Element anonymousElm = XmlUtils.getFirstChildElementNS( addressingPolicy, new QName(
350 WsaUtils.WS_A_NAMESPACE_200705, "AnonymousResponses" ) );
351 if( anonymousElm != null )
352 {
353 innerPolicy.addNewAnonymousResponses();
354 }
355 else
356 {
357 Element nonAnonymousElement = XmlUtils.getFirstChildElementNS( addressingPolicy, new QName(
358 WsaUtils.WS_A_NAMESPACE_200705, "NonAnonymousResponses" ) );
359 if( nonAnonymousElement != null )
360 {
361 innerPolicy.addNewNonAnonymousResponses();
362 }
363 }
364 }
365 }