1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support.wsdl;
14
15 import java.io.StringWriter;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URLDecoder;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.wsdl.Binding;
24 import javax.wsdl.BindingFault;
25 import javax.wsdl.BindingInput;
26 import javax.wsdl.BindingOperation;
27 import javax.wsdl.BindingOutput;
28 import javax.wsdl.Definition;
29 import javax.wsdl.Fault;
30 import javax.wsdl.Input;
31 import javax.wsdl.Message;
32 import javax.wsdl.Operation;
33 import javax.wsdl.Output;
34 import javax.wsdl.Part;
35 import javax.wsdl.Port;
36 import javax.wsdl.Service;
37 import javax.wsdl.WSDLException;
38 import javax.wsdl.extensions.AttributeExtensible;
39 import javax.wsdl.extensions.ElementExtensible;
40 import javax.wsdl.extensions.ExtensibilityElement;
41 import javax.wsdl.extensions.ExtensionDeserializer;
42 import javax.wsdl.extensions.ExtensionRegistry;
43 import javax.wsdl.extensions.UnknownExtensibilityElement;
44 import javax.wsdl.extensions.mime.MIMEContent;
45 import javax.wsdl.extensions.mime.MIMEMultipartRelated;
46 import javax.wsdl.extensions.mime.MIMEPart;
47 import javax.wsdl.extensions.soap.SOAPAddress;
48 import javax.wsdl.extensions.soap.SOAPBinding;
49 import javax.wsdl.extensions.soap.SOAPBody;
50 import javax.wsdl.extensions.soap.SOAPFault;
51 import javax.wsdl.extensions.soap.SOAPHeader;
52 import javax.wsdl.extensions.soap.SOAPOperation;
53 import javax.wsdl.extensions.soap12.SOAP12Address;
54 import javax.wsdl.extensions.soap12.SOAP12Binding;
55 import javax.wsdl.extensions.soap12.SOAP12Body;
56 import javax.wsdl.extensions.soap12.SOAP12Fault;
57 import javax.wsdl.extensions.soap12.SOAP12Header;
58 import javax.wsdl.extensions.soap12.SOAP12Operation;
59 import javax.wsdl.factory.WSDLFactory;
60 import javax.wsdl.xml.WSDLReader;
61 import javax.xml.namespace.QName;
62
63 import org.apache.log4j.Logger;
64 import org.apache.xmlbeans.SchemaGlobalElement;
65 import org.apache.xmlbeans.SchemaType;
66 import org.apache.xmlbeans.XmlObject;
67 import org.w3.x2007.x05.addressing.metadata.AddressingDocument.Addressing;
68 import org.w3.x2007.x05.addressing.metadata.AnonymousResponsesDocument.AnonymousResponses;
69 import org.w3c.dom.Document;
70 import org.w3c.dom.DocumentFragment;
71 import org.w3c.dom.Element;
72 import org.w3c.dom.Node;
73 import org.xml.sax.InputSource;
74 import org.xmlsoap.schemas.ws.x2004.x09.policy.OptionalType;
75 import org.xmlsoap.schemas.ws.x2004.x09.policy.Policy;
76 import org.xmlsoap.schemas.ws.x2004.x09.policy.PolicyDocument;
77
78 import com.eviware.soapui.SoapUI;
79 import com.eviware.soapui.config.AnonymousTypeConfig;
80 import com.eviware.soapui.config.DefinitionCacheConfig;
81 import com.eviware.soapui.config.DefinitionCacheTypeConfig;
82 import com.eviware.soapui.config.DefintionPartConfig;
83 import com.eviware.soapui.config.WsaVersionTypeConfig;
84 import com.eviware.soapui.impl.support.definition.DefinitionLoader;
85 import com.eviware.soapui.impl.wsdl.WsdlInterface;
86 import com.eviware.soapui.impl.wsdl.WsdlOperation;
87 import com.eviware.soapui.impl.wsdl.WsdlRequest;
88 import com.eviware.soapui.impl.wsdl.support.Constants;
89 import com.eviware.soapui.impl.wsdl.support.policy.PolicyUtils;
90 import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
91 import com.eviware.soapui.impl.wsdl.support.wsa.WsaConfig;
92 import com.eviware.soapui.impl.wsdl.support.wsa.WsaUtils;
93 import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
94 import com.eviware.soapui.settings.WsaSettings;
95 import com.eviware.soapui.support.StringUtils;
96 import com.eviware.soapui.support.types.StringList;
97 import com.eviware.soapui.support.xml.XmlUtils;
98 import com.ibm.wsdl.util.xml.QNameUtils;
99 import com.ibm.wsdl.xml.WSDLReaderImpl;
100 import com.ibm.wsdl.xml.WSDLWriterImpl;
101
102 /***
103 * Wsdl-related tools
104 *
105 * @author Ole.Matzura
106 */
107
108 public class WsdlUtils
109 {
110 private final static Logger log = Logger.getLogger(WsdlUtils.class);
111 private static WSDLReader wsdlReader;
112 private final static String WSDL_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
113 private final static String WSU_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
114
115 public static <T extends ExtensibilityElement> T getExtensiblityElement(List<?> list, Class<T> clazz)
116 {
117 List<T> elements = getExtensiblityElements(list, clazz);
118 return elements.isEmpty() ? null : elements.get(0);
119 }
120
121 public static <T extends ExtensibilityElement> List<T> getExtensiblityElements(List list, Class<T> clazz)
122 {
123 List<T> result = new ArrayList<T>();
124
125 for (Iterator<T> i = list.iterator(); i.hasNext();)
126 {
127 T elm = i.next();
128 if (clazz.isAssignableFrom(elm.getClass()))
129 {
130 result.add(elm);
131 }
132 }
133
134 return result;
135 }
136
137 public static Element[] getExentsibilityElements(ElementExtensible item, QName qname)
138 {
139 List<Element> result = new ArrayList<Element>();
140
141 List list = item.getExtensibilityElements();
142 for (Iterator<?> i = list.iterator(); i.hasNext();)
143 {
144 ExtensibilityElement elm = (ExtensibilityElement) i.next();
145 if (elm.getElementType().equals(qname) && elm instanceof UnknownExtensibilityElement)
146 {
147 result.add(((UnknownExtensibilityElement) elm).getElement());
148 }
149 }
150
151 return result.toArray(new Element[result.size()]);
152 }
153
154 public static String[] getExentsibilityAttributes(AttributeExtensible item, QName qname)
155 {
156 StringList result = new StringList();
157
158 Map map = item.getExtensionAttributes();
159
160 for (Iterator<?> i = map.keySet().iterator(); i.hasNext();)
161 {
162 QName name = (QName) i.next();
163 if (name.equals(qname))
164 {
165 result.add(map.get(name).toString());
166 }
167 }
168
169 return result.toStringArray();
170 }
171
172 public static String getSoapAction(BindingOperation operation)
173 {
174 List list = operation.getExtensibilityElements();
175 SOAPOperation soapOperation = WsdlUtils.getExtensiblityElement(list, SOAPOperation.class);
176 if (soapOperation != null)
177 return soapOperation.getSoapActionURI();
178
179 SOAP12Operation soap12Operation = WsdlUtils.getExtensiblityElement(list, SOAP12Operation.class);
180 if (soap12Operation != null)
181 return soap12Operation.getSoapActionURI();
182
183 return null;
184 }
185
186 public static String[] getEndpointsForBinding(Definition definition, Binding binding)
187 {
188 List<String> result = new ArrayList<String>();
189 Map map = definition.getAllServices();
190 for (Iterator i = map.values().iterator(); i.hasNext();)
191 {
192 Service service = (Service) i.next();
193 Map portMap = service.getPorts();
194 for (Iterator i2 = portMap.values().iterator(); i2.hasNext();)
195 {
196 Port port = (Port) i2.next();
197 if (port.getBinding() == binding)
198 {
199 String endpoint = WsdlUtils.getSoapEndpoint(port);
200 if (endpoint != null)
201 result.add(endpoint);
202 }
203 }
204 }
205
206 return result.toArray(new String[result.size()]);
207 }
208
209 public static Binding findBindingForOperation(Definition definition, BindingOperation bindingOperation)
210 {
211 Map services = definition.getAllServices();
212 Iterator<Service> s = services.values().iterator();
213
214 while (s.hasNext())
215 {
216 Map ports = s.next().getPorts();
217 Iterator<Port> p = ports.values().iterator();
218 while (p.hasNext())
219 {
220 Binding binding = p.next().getBinding();
221 List bindingOperations = binding.getBindingOperations();
222 for (Iterator iter = bindingOperations.iterator(); iter.hasNext();)
223 {
224 BindingOperation op = (BindingOperation) iter.next();
225 if (op.getName().equals(bindingOperation.getName()))
226 return binding;
227 }
228 }
229 }
230
231 Map bindings = definition.getAllBindings();
232 Iterator<QName> names = bindings.keySet().iterator();
233 while (names.hasNext())
234 {
235 Binding binding = definition.getBinding(names.next());
236 List bindingOperations = binding.getBindingOperations();
237 for (Iterator iter = bindingOperations.iterator(); iter.hasNext();)
238 {
239 BindingOperation op = (BindingOperation) iter.next();
240 if (op.getName().equals(bindingOperation.getName()))
241 return binding;
242 }
243 }
244
245 return null;
246 }
247
248 public static BindingOperation findBindingOperation(Definition definition, String operationName)
249 {
250 Map services = definition.getAllServices();
251 for (Iterator i = services.keySet().iterator(); i.hasNext();)
252 {
253 QName qName = (QName) i.next();
254 Service service = definition.getService(qName);
255 Map ports = service.getPorts();
256
257 for (Iterator iterator = ports.keySet().iterator(); iterator.hasNext();)
258 {
259 String key = (String) iterator.next();
260 Port port = service.getPort(key);
261 BindingOperation bindingOperation = port.getBinding().getBindingOperation(operationName, null, null);
262 if (bindingOperation != null)
263 return bindingOperation;
264 }
265 }
266
267 Map bindings = definition.getAllBindings();
268 for (Iterator i = bindings.keySet().iterator(); i.hasNext();)
269 {
270 Object key = i.next();
271 Binding binding = (Binding) bindings.get(key);
272 BindingOperation bindingOperation = binding.getBindingOperation(operationName, null, null);
273 if (bindingOperation != null)
274 return bindingOperation;
275 }
276
277 return null;
278 }
279
280 public static boolean isInputSoapEncoded(BindingOperation bindingOperation)
281 {
282 if (bindingOperation == null)
283 return false;
284
285 BindingInput bindingInput = bindingOperation.getBindingInput();
286 if (bindingInput == null)
287 return false;
288
289 SOAPBody soapBody = WsdlUtils.getExtensiblityElement(bindingInput.getExtensibilityElements(), SOAPBody.class);
290
291 if (soapBody != null)
292 {
293 return soapBody.getUse() != null
294 && soapBody.getUse().equalsIgnoreCase("encoded")
295 && (soapBody.getEncodingStyles() == null || soapBody.getEncodingStyles().contains(
296 "http://schemas.xmlsoap.org/soap/encoding/"));
297 }
298
299 SOAP12Body soap12Body = WsdlUtils.getExtensiblityElement(bindingInput.getExtensibilityElements(),
300 SOAP12Body.class);
301
302 if (soap12Body != null)
303 {
304 return soap12Body.getUse() != null
305 && soap12Body.getUse().equalsIgnoreCase("encoded")
306 && (soap12Body.getEncodingStyle() == null || soap12Body.getEncodingStyle().equals(
307 "http://schemas.xmlsoap.org/soap/encoding/"));
308 }
309
310 return false;
311 }
312
313 public static boolean isOutputSoapEncoded(BindingOperation bindingOperation)
314 {
315 if (bindingOperation == null)
316 return false;
317
318 BindingOutput bindingOutput = bindingOperation.getBindingOutput();
319 if (bindingOutput == null)
320 return false;
321
322 SOAPBody soapBody = WsdlUtils.getExtensiblityElement(bindingOutput.getExtensibilityElements(), SOAPBody.class);
323
324 if (soapBody != null)
325 {
326 return soapBody.getUse() != null
327 && soapBody.getUse().equalsIgnoreCase("encoded")
328 && (soapBody.getEncodingStyles() == null || soapBody.getEncodingStyles().contains(
329 "http://schemas.xmlsoap.org/soap/encoding/"));
330 }
331
332 SOAP12Body soap12Body = WsdlUtils.getExtensiblityElement(bindingOutput.getExtensibilityElements(),
333 SOAP12Body.class);
334
335 if (soap12Body != null)
336 {
337 return soap12Body.getUse() != null
338 && soap12Body.getUse().equalsIgnoreCase("encoded")
339 && (soap12Body.getEncodingStyle() == null || soap12Body.getEncodingStyle().equals(
340 "http://schemas.xmlsoap.org/soap/encoding/"));
341 }
342
343 return false;
344 }
345
346 public static boolean isRpc(Definition definition, BindingOperation bindingOperation)
347 {
348 SOAPOperation soapOperation = WsdlUtils.getExtensiblityElement(bindingOperation.getExtensibilityElements(),
349 SOAPOperation.class);
350
351 if (soapOperation != null && soapOperation.getStyle() != null)
352 return soapOperation.getStyle().equalsIgnoreCase("rpc");
353
354 SOAP12Operation soap12Operation = WsdlUtils.getExtensiblityElement(bindingOperation.getExtensibilityElements(),
355 SOAP12Operation.class);
356
357 if (soap12Operation != null && soap12Operation.getStyle() != null)
358 return soap12Operation.getStyle().equalsIgnoreCase("rpc");
359
360 Binding binding = findBindingForOperation(definition, bindingOperation);
361 if (binding == null)
362 {
363 log.error("Failed to find binding for operation [" + bindingOperation.getName() + "] in definition ["
364 + definition.getDocumentBaseURI() + "]");
365 return false;
366 }
367
368 return isRpc(binding);
369 }
370
371 public static boolean isRpc(Binding binding)
372 {
373 SOAPBinding soapBinding = WsdlUtils.getExtensiblityElement(binding.getExtensibilityElements(), SOAPBinding.class);
374
375 if (soapBinding != null)
376 return "rpc".equalsIgnoreCase(soapBinding.getStyle());
377
378 SOAP12Binding soap12Binding = WsdlUtils.getExtensiblityElement(binding.getExtensibilityElements(),
379 SOAP12Binding.class);
380
381 if (soap12Binding != null)
382 return "rpc".equalsIgnoreCase(soap12Binding.getStyle());
383
384 return false;
385 }
386
387 /***
388 * Returns a list of parts for the specifed operation, either as specified in
389 * body or all
390 */
391
392 public static Part[] getInputParts(BindingOperation operation)
393 {
394 List<Part> result = new ArrayList<Part>();
395 Input input = operation.getOperation().getInput();
396 if (input == null)
397 return new Part[0];
398
399 Message msg = input.getMessage();
400
401 if (msg != null)
402 {
403 SOAPBody soapBody = WsdlUtils.getExtensiblityElement(operation.getBindingInput().getExtensibilityElements(),
404 SOAPBody.class);
405
406 if (soapBody == null || soapBody.getParts() == null)
407 {
408 SOAP12Body soap12Body = WsdlUtils.getExtensiblityElement(operation.getBindingInput()
409 .getExtensibilityElements(), SOAP12Body.class);
410
411 if (soap12Body == null || soap12Body.getParts() == null)
412 {
413 if (msg != null)
414 result.addAll(msg.getOrderedParts(null));
415 }
416 else
417 {
418 Iterator i = soap12Body.getParts().iterator();
419 while (i.hasNext())
420 {
421 String partName = (String) i.next();
422 Part part = msg.getPart(partName);
423
424 result.add(part);
425 }
426 }
427 }
428 else
429 {
430 Iterator i = soapBody.getParts().iterator();
431 while (i.hasNext())
432 {
433 String partName = (String) i.next();
434 Part part = msg.getPart(partName);
435
436 result.add(part);
437 }
438 }
439 }
440 else
441 {
442 }
443
444 return result.toArray(new Part[result.size()]);
445 }
446
447 public static boolean isAttachmentInputPart(Part part, BindingOperation operation)
448 {
449 return getInputMultipartContent(part, operation).length > 0;
450 }
451
452 public static boolean isAttachmentOutputPart(Part part, BindingOperation operation)
453 {
454 return getOutputMultipartContent(part, operation).length > 0;
455 }
456
457 public static MIMEContent[] getOutputMultipartContent(Part part, BindingOperation operation)
458 {
459 MIMEMultipartRelated multipartOutput = WsdlUtils.getExtensiblityElement(operation.getBindingOutput()
460 .getExtensibilityElements(), MIMEMultipartRelated.class);
461
462 return getContentParts(part, multipartOutput);
463 }
464
465 public static MIMEContent[] getInputMultipartContent(Part part, BindingOperation operation)
466 {
467 MIMEMultipartRelated multipartInput = WsdlUtils.getExtensiblityElement(operation.getBindingInput()
468 .getExtensibilityElements(), MIMEMultipartRelated.class);
469
470 return getContentParts(part, multipartInput);
471 }
472
473 public static MIMEContent[] getContentParts(Part part, MIMEMultipartRelated multipart)
474 {
475 List<MIMEContent> result = new ArrayList<MIMEContent>();
476
477 if (multipart != null)
478 {
479 List<MIMEPart> parts = multipart.getMIMEParts();
480
481 for (int c = 0; c < parts.size(); c++)
482 {
483 List<MIMEContent> contentParts = WsdlUtils.getExtensiblityElements(parts.get(c).getExtensibilityElements(),
484 MIMEContent.class);
485
486 for (MIMEContent content : contentParts)
487 {
488 if (content.getPart().equals(part.getName()))
489 result.add(content);
490 }
491 }
492 }
493
494 return result.toArray(new MIMEContent[result.size()]);
495 }
496
497 public static Part[] getFaultParts(BindingOperation bindingOperation, String faultName) throws Exception
498 {
499 List<Part> result = new ArrayList<Part>();
500
501 BindingFault bindingFault = bindingOperation.getBindingFault(faultName);
502 SOAPFault soapFault = WsdlUtils.getExtensiblityElement(bindingFault.getExtensibilityElements(), SOAPFault.class);
503
504 Operation operation = bindingOperation.getOperation();
505 if (soapFault != null && soapFault.getName() != null)
506 {
507 Fault fault = operation.getFault(soapFault.getName());
508 if (fault == null)
509 throw new Exception("Missing Fault [" + soapFault.getName() + "] in operation [" + operation.getName()
510 + "]");
511 result.addAll(fault.getMessage().getOrderedParts(null));
512 }
513 else
514 {
515 SOAP12Fault soap12Fault = WsdlUtils.getExtensiblityElement(bindingFault.getExtensibilityElements(),
516 SOAP12Fault.class);
517
518 if (soap12Fault != null && soap12Fault.getName() != null)
519 {
520 result.addAll(operation.getFault(soap12Fault.getName()).getMessage().getOrderedParts(null));
521 }
522 else
523 {
524 result.addAll(operation.getFault(faultName).getMessage().getOrderedParts(null));
525 }
526 }
527
528 return result.toArray(new Part[result.size()]);
529 }
530
531 public static String findSoapFaultPartName(SoapVersion soapVersion, BindingOperation bindingOperation, String message)
532 throws Exception
533 {
534 if (WsdlUtils.isOutputSoapEncoded(bindingOperation))
535 throw new Exception("SOAP-Encoded messages not supported");
536
537 XmlObject xml = XmlObject.Factory.parse(message);
538 XmlObject[] msgPaths = xml.selectPath("declare namespace env='" + soapVersion.getEnvelopeNamespace() + "';"
539 + "$this/env:Envelope/env:Body/env:Fault");
540 if (msgPaths.length == 0)
541 return null;
542
543 XmlObject msgXml = msgPaths[0];
544
545 Map faults = bindingOperation.getBindingFaults();
546 for (Iterator<BindingFault> i = faults.values().iterator(); i.hasNext();)
547 {
548 BindingFault bindingFault = i.next();
549 String faultName = bindingFault.getName();
550 Part[] faultParts = WsdlUtils.getFaultParts(bindingOperation, faultName);
551 Part part = faultParts[0];
552
553 QName elementName = part.getElementName();
554 if (elementName != null)
555 {
556 XmlObject[] faultPaths = msgXml.selectPath("declare namespace env='" + soapVersion.getEnvelopeNamespace()
557 + "';" + "declare namespace ns='" + elementName.getNamespaceURI() + "';" + "//env:Fault/detail/ns:"
558 + elementName.getLocalPart());
559
560 if (faultPaths.length == 1)
561 return faultName;
562 }
563
564 else if (part.getTypeName() != null)
565 {
566 QName typeName = part.getTypeName();
567 XmlObject[] faultPaths = msgXml.selectPath("declare namespace env='" + soapVersion.getEnvelopeNamespace()
568 + "';" + "declare namespace ns='" + typeName.getNamespaceURI() + "';" + "//env:Fault/detail/ns:"
569 + part.getName());
570
571 if (faultPaths.length == 1)
572 return faultName;
573 }
574 }
575
576 return null;
577 }
578
579 public static Part[] getOutputParts(BindingOperation operation)
580 {
581 BindingOutput bindingOutput = operation.getBindingOutput();
582 if (bindingOutput == null)
583 return new Part[0];
584
585 List<Part> result = new ArrayList<Part>();
586 Output output = operation.getOperation().getOutput();
587 if (output == null)
588 return new Part[0];
589
590 Message msg = output.getMessage();
591 if (msg != null)
592 {
593 SOAPBody soapBody = WsdlUtils.getExtensiblityElement(bindingOutput.getExtensibilityElements(), SOAPBody.class);
594
595 if (soapBody == null || soapBody.getParts() == null)
596 {
597 SOAP12Body soap12Body = WsdlUtils.getExtensiblityElement(bindingOutput.getExtensibilityElements(),
598 SOAP12Body.class);
599
600 if (soap12Body == null || soap12Body.getParts() == null)
601 {
602 result.addAll(msg.getOrderedParts(null));
603 }
604 else
605 {
606 Iterator i = soap12Body.getParts().iterator();
607 while (i.hasNext())
608 {
609 String partName = (String) i.next();
610 Part part = msg.getPart(partName);
611
612 result.add(part);
613 }
614 }
615 }
616 else
617 {
618 Iterator i = soapBody.getParts().iterator();
619 while (i.hasNext())
620 {
621 String partName = (String) i.next();
622 Part part = msg.getPart(partName);
623
624 result.add(part);
625 }
626 }
627 }
628 else
629 {
630 log.warn("Missing output message for binding operation [" + operation.getName() + "]");
631 }
632
633 return result.toArray(new Part[result.size()]);
634 }
635
636 public static boolean isMultipartRequest(Definition definition, BindingOperation bindingOperation)
637 {
638 return WsdlUtils.getExtensiblityElement(bindingOperation.getBindingInput().getExtensibilityElements(),
639 MIMEMultipartRelated.class) != null;
640 }
641
642 public static String getUsingAddressing(ElementExtensible item, Definition def)
643 {
644 String version = WsaVersionTypeConfig.NONE.toString();
645
646 Element[] usingAddressingElements = WsdlUtils.getExentsibilityElements(item, new QName(
647 "http://www.w3.org/2006/05/addressing/wsdl", "UsingAddressing"));
648 if (usingAddressingElements.length == 0)
649 {
650 usingAddressingElements = WsdlUtils.getExentsibilityElements(item, new QName(
651 "http://www.w3.org/2006/02/addressing/wsdl", "UsingAddressing"));
652 }
653
654 if (usingAddressingElements.length != 0)
655 {
656
657
658 String addressingVersion = usingAddressingElements[0].getAttributeNS(WSDL_NAMESPACE, "required");
659 if (addressingVersion != null && addressingVersion.equals("true"))
660 {
661 version = WsaVersionTypeConfig.X_200508.toString();
662 }
663
664 }
665 return version;
666 }
667 public static Policy getAttachedPolicy(ElementExtensible item, Definition def) {
668
669 Policy rtnPolicy = null;
670 Element[] policyReferences = WsdlUtils
671 .getExentsibilityElements(item, new QName(PolicyUtils.WS_POLICY_NAMESPACE, "PolicyReference"));
672 if (policyReferences.length > 0)
673 {
674 String policyId = policyReferences[0].getAttribute("URI");
675 if (!StringUtils.isNullOrEmpty(policyId))
676 {
677 Element[] policies = WsdlUtils.getExentsibilityElements(def, new QName(PolicyUtils.WS_POLICY_NAMESPACE, "Policy"));
678 Element policy = null;
679 for (int i = 0; i < policies.length; i++)
680 {
681 policy = policies[i];
682 String policyIdx = policy.getAttributeNS(WSU_NAMESPACE, "Id");
683 if (policyId.equals("#" + policyIdx))
684 {
685 rtnPolicy = getPolicy(policy);
686 continue;
687 }
688
689 }
690 }
691 }
692 else
693 {
694
695 Element[] itemPolicies = WsdlUtils.getExentsibilityElements(item, new QName(PolicyUtils.WS_POLICY_NAMESPACE, "Policy"));
696 if (itemPolicies.length > 0)
697 {
698 for (int i = 0; i < itemPolicies.length; i++)
699 {
700 Element policy = itemPolicies[i];
701 rtnPolicy = getPolicy(policy);
702
703 }
704 }
705 }
706 return rtnPolicy;
707 }
708
709 public static Policy getPolicy(Element policy)
710 {
711
712
713
714
715 Element wsAddressing = XmlUtils.getFirstChildElementNS(policy, WsaUtils.WSAM_NAMESPACE, "Addressing");
716 Element addressingPolicy = null;
717 Policy newPolicy = PolicyDocument.Factory.newInstance().addNewPolicy();
718 Addressing newAddressing = null;
719 if (wsAddressing != null)
720 {
721 addressingPolicy = XmlUtils.getFirstChildElementNS(wsAddressing, PolicyUtils.WS_POLICY_NAMESPACE, "Policy");
722 if (addressingPolicy != null)
723 {
724 newAddressing = newPolicy.addNewAddressing();
725 String optional = wsAddressing.getAttributeNS(PolicyUtils.WS_POLICY_NAMESPACE, "Optional");
726 if (!StringUtils.isNullOrEmpty(optional) && optional.equals(OptionalType.TRUE.toString()))
727 {
728 newAddressing.setOptional(OptionalType.TRUE);
729 } else {
730 newAddressing.setOptional(OptionalType.FALSE);
731 }
732 Policy innerPolicy = newAddressing.addNewPolicy();
733
734 Element anonymousElm = XmlUtils.getFirstChildElementNS(addressingPolicy, new QName(PolicyUtils.WSAM_NAMESPACE,"AnonymousResponses"));
735 if (anonymousElm != null)
736 {
737 AnonymousResponses anr = innerPolicy.addNewAnonymousResponses();
738 } else {
739 Element nonAnonymousElement = XmlUtils.getFirstChildElementNS(addressingPolicy, new QName(PolicyUtils.WSAM_NAMESPACE,"NonAnonymousResponses"));
740 if (nonAnonymousElement != null)
741 {
742 innerPolicy.addNewNonAnonymousResponses();
743 }
744 }
745 }
746 }
747 return newPolicy;
748 }
749 private static String checkIfWsaPolicy(String version, Element policy)
750 {
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783 return version;
784 }
785
786 public static String getWsaPolicyAnonymous(Element policy)
787 {
788 String version = WsaVersionTypeConfig.NONE.toString();
789 String anonymous = AnonymousTypeConfig.OPTIONAL.toString();
790
791 Element wsAddressing = XmlUtils.getFirstChildElementNS(policy, WsaUtils.WSAM_NAMESPACE, "Addressing");
792 Element addressingPolicy = null;
793 if (wsAddressing != null)
794 {
795 String optional = wsAddressing.getAttributeNS(PolicyUtils.WS_POLICY_NAMESPACE, "Optional");
796 addressingPolicy = XmlUtils.getFirstChildElementNS(wsAddressing, PolicyUtils.WS_POLICY_NAMESPACE, "Policy");
797 if (addressingPolicy != null)
798 {
799 if (StringUtils.isNullOrEmpty(optional) || optional.equals("false") ||
800 (optional.equals("true") && SoapUI.getSettings().getBoolean(WsaSettings.ENABLE_FOR_OPTIONAL)) )
801 {
802 version = WsaVersionTypeConfig.X_200508.toString();
803 }
804
805 Element anonymousElm = XmlUtils.getFirstChildElementNS(addressingPolicy, new QName(PolicyUtils.WSAM_NAMESPACE,"AnonymousResponses"));
806 if (anonymousElm != null)
807 {
808 anonymous = AnonymousTypeConfig.REQUIRED.toString();
809 } else {
810 Element nonAnonymousElement = XmlUtils.getFirstChildElementNS(addressingPolicy, new QName(PolicyUtils.WSAM_NAMESPACE,"NonAnonymousResponses"));
811 if (nonAnonymousElement != null)
812 {
813 anonymous = AnonymousTypeConfig.PROHIBITED.toString();
814 }
815 }
816 }
817 }
818 return anonymous;
819 }
820 public static String getSoapEndpoint(Port port)
821 {
822 SOAPAddress soapAddress = WsdlUtils.getExtensiblityElement(port.getExtensibilityElements(), SOAPAddress.class);
823 if (soapAddress != null)
824 try
825 {
826 return URLDecoder.decode(soapAddress.getLocationURI(), "UTF-8");
827 }
828 catch (UnsupportedEncodingException e)
829 {
830 e.printStackTrace();
831 return soapAddress.getLocationURI();
832 }
833
834 SOAP12Address soap12Address = WsdlUtils.getExtensiblityElement(port.getExtensibilityElements(),
835 SOAP12Address.class);
836 if (soap12Address != null)
837 try
838 {
839 return URLDecoder.decode(soap12Address.getLocationURI(), "UTF-8");
840 }
841 catch (UnsupportedEncodingException e)
842 {
843 e.printStackTrace();
844 return soap12Address.getLocationURI();
845 }
846
847 return null;
848 }
849
850 public static boolean replaceSoapEndpoint(Port port, String endpoint)
851 {
852 SOAPAddress soapAddress = WsdlUtils.getExtensiblityElement(port.getExtensibilityElements(), SOAPAddress.class);
853 if (soapAddress != null)
854 {
855 soapAddress.setLocationURI(endpoint);
856 return true;
857 }
858
859 SOAP12Address soap12Address = WsdlUtils.getExtensiblityElement(port.getExtensibilityElements(),
860 SOAP12Address.class);
861 if (soap12Address != null)
862 {
863 soap12Address.setLocationURI(endpoint);
864 return true;
865 }
866
867 return false;
868 }
869
870 public static String getSoapBodyNamespace(List list)
871 {
872 SOAPBody soapBody = WsdlUtils.getExtensiblityElement(list, SOAPBody.class);
873 if (soapBody != null)
874 return soapBody.getNamespaceURI();
875
876 SOAP12Body soap12Body = WsdlUtils.getExtensiblityElement(list, SOAP12Body.class);
877 if (soap12Body != null)
878 return soap12Body.getNamespaceURI();
879
880 return null;
881 }
882
883 public static final class NonSchemaImportingWsdlReaderImpl extends WSDLReaderImpl
884 {
885 @SuppressWarnings("unchecked")
886 @Override
887 protected ExtensibilityElement parseSchema(Class parentType, Element el, Definition def, ExtensionRegistry extReg)
888 throws WSDLException
889 {
890 QName elementType = QNameUtils.newQName(el);
891
892 ExtensionDeserializer exDS = extReg.queryDeserializer(parentType, elementType);
893
894
895 ExtensibilityElement ee = exDS.unmarshall(parentType, elementType, el, def, extReg);
896
897 return ee;
898 }
899
900 }
901
902 /***
903 * A SOAP-Header wrapper
904 *
905 * @author ole.matzura
906 */
907
908 public interface SoapHeader
909 {
910 public QName getMessage();
911
912 public String getPart();
913 }
914
915 /***
916 * SOAP 1.1 Header implementation
917 *
918 * @author ole.matzura
919 */
920
921 public static class Soap11Header implements SoapHeader
922 {
923 private final SOAPHeader soapHeader;
924
925 public Soap11Header(SOAPHeader soapHeader)
926 {
927 this.soapHeader = soapHeader;
928 }
929
930 public QName getMessage()
931 {
932 return soapHeader.getMessage();
933 }
934
935 public String getPart()
936 {
937 return soapHeader.getPart();
938 }
939 }
940
941 /***
942 * SOAP 1.2 Header implementation
943 *
944 * @author ole.matzura
945 */
946
947 public static class Soap12Header implements SoapHeader
948 {
949 private final SOAP12Header soapHeader;
950
951 public Soap12Header(SOAP12Header soapHeader)
952 {
953 this.soapHeader = soapHeader;
954 }
955
956 public QName getMessage()
957 {
958 return soapHeader.getMessage();
959 }
960
961 public String getPart()
962 {
963 return soapHeader.getPart();
964 }
965 }
966
967 public static List<SoapHeader> getSoapHeaders(List list)
968 {
969 List<SoapHeader> result = new ArrayList<SoapHeader>();
970
971 List<SOAPHeader> soapHeaders = WsdlUtils.getExtensiblityElements(list, SOAPHeader.class);
972 if (soapHeaders != null && !soapHeaders.isEmpty())
973 {
974 for (SOAPHeader header : soapHeaders)
975 result.add(new Soap11Header(header));
976 }
977 else
978 {
979 List<SOAP12Header> soap12Headers = WsdlUtils.getExtensiblityElements(list, SOAP12Header.class);
980 if (soap12Headers != null && !soap12Headers.isEmpty())
981 {
982 for (SOAP12Header header : soap12Headers)
983 result.add(new Soap12Header(header));
984 }
985 }
986
987 return result;
988 }
989
990 public static synchronized Definition readDefinition(String wsdlUrl) throws Exception
991 {
992 if (wsdlReader == null)
993 {
994 WSDLFactory factory = WSDLFactory.newInstance();
995 wsdlReader = factory.newWSDLReader();
996 wsdlReader.setFeature("javax.wsdl.verbose", true);
997 wsdlReader.setFeature("javax.wsdl.importDocuments", true);
998 }
999
1000 return wsdlReader.readWSDL(new UrlWsdlLoader(wsdlUrl));
1001 }
1002
1003 public static SchemaType getSchemaTypeForPart(WsdlContext wsdlContext, javax.wsdl.Part part) throws Exception
1004 {
1005 SchemaType schemaType = null;
1006 QName elementName = part.getElementName();
1007
1008 if (elementName != null)
1009 {
1010 SchemaGlobalElement elm = wsdlContext.getSchemaTypeLoader().findElement(elementName);
1011 if (elm != null)
1012 {
1013 schemaType = elm.getType();
1014 }
1015 else
1016 WsdlRequest.log.error("Could not find element [" + elementName + "] specified in part [" + part.getName()
1017 + "]");
1018 }
1019 else
1020 {
1021 QName typeName = part.getTypeName();
1022
1023 if (typeName != null)
1024 {
1025 schemaType = wsdlContext.getSchemaTypeLoader().findType(typeName);
1026
1027 if (schemaType == null)
1028 {
1029 WsdlRequest.log.error("Could not find type [" + typeName + "] specified in part [" + part.getName()
1030 + "]");
1031 }
1032 }
1033 }
1034 return schemaType;
1035 }
1036
1037 public static SchemaGlobalElement getSchemaElementForPart(WsdlContext wsdlContext, javax.wsdl.Part part)
1038 throws Exception
1039 {
1040 QName elementName = part.getElementName();
1041
1042 if (elementName != null)
1043 {
1044 return wsdlContext.getSchemaTypeLoader().findElement(elementName);
1045 }
1046
1047 return null;
1048 }
1049
1050 public static String replacePortEndpoint(WsdlInterface iface, InputSource inputSource, String endpoint)
1051 throws WSDLException
1052 {
1053 WSDLReader wsdlReader = new NonSchemaImportingWsdlReaderImpl();
1054
1055 wsdlReader.setFeature("javax.wsdl.verbose", true);
1056 wsdlReader.setFeature("javax.wsdl.importDocuments", false);
1057
1058 Definition definition = wsdlReader.readWSDL(null, inputSource);
1059
1060 boolean updated = false;
1061 Map map = definition.getServices();
1062 for (Iterator i = map.values().iterator(); i.hasNext();)
1063 {
1064 Service service = (Service) i.next();
1065 Map portMap = service.getPorts();
1066 for (Iterator i2 = portMap.values().iterator(); i2.hasNext();)
1067 {
1068 Port port = (Port) i2.next();
1069 if (port.getBinding().getQName().equals(iface.getBindingName()))
1070 {
1071 if (replaceSoapEndpoint(port, endpoint))
1072 updated = true;
1073 }
1074 }
1075 }
1076
1077 if (updated)
1078 {
1079 StringWriter writer = new StringWriter();
1080
1081 Map nsMap = definition.getNamespaces();
1082 if (!nsMap.values().contains(Constants.SOAP_HTTP_BINDING_NS))
1083 definition.addNamespace("soaphttp", Constants.SOAP_HTTP_BINDING_NS);
1084
1085 new WSDLWriterImpl().writeWSDL(definition, writer);
1086 return writer.toString();
1087 }
1088
1089 return null;
1090 }
1091
1092 public static BindingOperation findBindingOperation(Binding binding, String bindingOperationName, String inputName,
1093 String outputName)
1094 {
1095 if (binding == null)
1096 return null;
1097
1098 if (inputName == null)
1099 inputName = ":none";
1100
1101 if (outputName == null)
1102 outputName = ":none";
1103
1104 BindingOperation result = binding.getBindingOperation(bindingOperationName, inputName, outputName);
1105
1106 if (result == null && (inputName.equals(":none") || outputName.equals(":none")))
1107 {
1108
1109 result = binding.getBindingOperation(bindingOperationName, inputName.equals(":none") ? null : inputName,
1110 outputName.equals(":none") ? null : outputName);
1111 }
1112 return result;
1113 }
1114
1115 public static boolean isHeaderInputPart(Part part, Message message, BindingOperation bindingOperation)
1116 {
1117 List<SOAPHeader> headers = WsdlUtils.getExtensiblityElements(bindingOperation.getBindingInput()
1118 .getExtensibilityElements(), SOAPHeader.class);
1119
1120 if (headers == null || headers.isEmpty())
1121 return false;
1122
1123 for (SOAPHeader header : headers)
1124 {
1125 if (message.getQName().equals(header.getMessage()) && part.getName().equals(header.getPart()))
1126 return true;
1127 }
1128
1129 return false;
1130 }
1131
1132 public static boolean isHeaderOutputPart(Part part, Message message, BindingOperation bindingOperation)
1133 {
1134 List<SOAPHeader> headers = WsdlUtils.getExtensiblityElements(bindingOperation.getBindingOutput()
1135 .getExtensibilityElements(), SOAPHeader.class);
1136
1137 if (headers == null || headers.isEmpty())
1138 return false;
1139
1140 for (SOAPHeader header : headers)
1141 {
1142 if (message.getQName().equals(header.getMessage()) && part.getName().equals(header.getPart()))
1143 return true;
1144 }
1145
1146 return false;
1147 }
1148
1149 public static DefinitionCacheConfig cacheWsdl(DefinitionLoader loader) throws Exception
1150 {
1151 DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory.newInstance();
1152 definitionCache.setRootPart(loader.getBaseURI());
1153 definitionCache.setType(DefinitionCacheTypeConfig.TEXT);
1154
1155 Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts(loader);
1156
1157 for (Iterator<String> i = urls.keySet().iterator(); i.hasNext();)
1158 {
1159 DefintionPartConfig definitionPart = definitionCache.addNewPart();
1160 String url = i.next();
1161 definitionPart.setUrl(url);
1162 XmlObject xmlObject = urls.get(url);
1163 Node domNode = xmlObject.getDomNode();
1164
1165 if (domNode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
1166 {
1167 Node node = ((DocumentFragment) domNode).getFirstChild();
1168 if (node.getNodeType() == Node.TEXT_NODE)
1169 {
1170 domNode = XmlUtils.parseXml(node.getNodeValue());
1171 xmlObject = XmlObject.Factory.parse(domNode);
1172 }
1173 }
1174
1175 Element contentElement = ((Document) domNode).getDocumentElement();
1176
1177 Node newDomNode = definitionPart.addNewContent().getDomNode();
1178 newDomNode.appendChild(newDomNode.getOwnerDocument().createTextNode(xmlObject.toString()));
1179 definitionPart.setType(contentElement.getNamespaceURI());
1180 }
1181
1182 return definitionCache;
1183 }
1184
1185 public static void getAnonymous(WsdlOperation wsdlOperation)
1186 {
1187 String anonymous = "";
1188
1189 Element[] anonymousElements = WsdlUtils.getExentsibilityElements(wsdlOperation.getBindingOperation(), new QName(
1190 "http://www.w3.org/2006/05/addressing/wsdl", "Anonymous"));
1191 if (anonymousElements.length == 0)
1192 {
1193 anonymousElements = WsdlUtils.getExentsibilityElements(wsdlOperation.getBindingOperation(), new QName(
1194 "http://www.w3.org/2006/02/addressing/wsdl", "Anonymous"));
1195 }
1196
1197 if (anonymousElements != null && anonymousElements.length > 0)
1198 {
1199 anonymous = XmlUtils.getElementText(anonymousElements[0]);
1200 }
1201 wsdlOperation.setAnonymous(anonymous);
1202 }
1203
1204 public static String getDefaultWsaAction(WsdlOperation operation, boolean output)
1205 {
1206
1207 if (operation.getInterface().getSoapVersion() == SoapVersion.Soap11
1208 && StringUtils.hasContent(operation.getAction()))
1209 return operation.getAction();
1210
1211 try
1212 {
1213 AttributeExtensible attributeExtensible = output ? operation.getBindingOperation().getOperation().getOutput()
1214 : operation.getBindingOperation().getOperation().getInput();
1215
1216 if (attributeExtensible == null)
1217 return null;
1218
1219 String[] attrs = WsdlUtils.getExentsibilityAttributes(attributeExtensible, new QName(
1220 WsaUtils.WS_A_VERSION_200408, "Action"));
1221 if (attrs == null || attrs.length == 0)
1222 attrs = WsdlUtils.getExentsibilityAttributes(attributeExtensible, new QName(WsaUtils.WS_A_VERSION_200508,
1223 "Action"));
1224
1225 if (attrs != null && attrs.length > 0)
1226 {
1227 return attrs[0];
1228 }
1229
1230 WsdlInterface iface = operation.getInterface();
1231
1232 Definition definition = iface.getWsdlContext().getDefinition();
1233 String targetNamespace = definition.getTargetNamespace();
1234 String portTypeName = iface.getBinding().getPortType().getQName().getLocalPart();
1235 String operationName = output ? operation.getOutputName() : operation.getInputName();
1236 if (operationName == null)
1237 operationName = operation.getName() + (output ? "Response" : "Request");
1238
1239 StringBuffer result = new StringBuffer(targetNamespace);
1240 if (targetNamespace.charAt(targetNamespace.length() - 1) != '/' && portTypeName.charAt(0) != '/')
1241 result.append('/');
1242 result.append(portTypeName);
1243 if (portTypeName.charAt(portTypeName.length() - 1) != '/' && operationName.charAt(0) != '/')
1244 result.append('/');
1245 result.append(operationName);
1246
1247 return result.toString();
1248 }
1249 catch (Exception e)
1250 {
1251 e.printStackTrace();
1252 log.warn(e.toString());
1253 return null;
1254 }
1255 }
1256
1257 public static void setDefaultWsaAction(WsaConfig wsaConfig, boolean b)
1258 {
1259 String defaultAction = getDefaultWsaAction(wsaConfig.getWsaContainer().getOperation(), b);
1260 if (StringUtils.hasContent(defaultAction))
1261 wsaConfig.setAction(defaultAction);
1262 }
1263
1264 }