View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.*;
17  import com.eviware.soapui.impl.WsdlInterfaceFactory;
18  import com.eviware.soapui.impl.support.AbstractHttpRequest;
19  import com.eviware.soapui.impl.support.AbstractInterface;
20  import com.eviware.soapui.impl.support.DefinitionContext;
21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
22  import com.eviware.soapui.impl.wsdl.support.PathUtils;
23  import com.eviware.soapui.impl.wsdl.support.soap.SoapMessageBuilder;
24  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
25  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
26  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlLoader;
27  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils;
28  import com.eviware.soapui.impl.wsdl.teststeps.BeanPathPropertySupport;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
30  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
31  import com.eviware.soapui.model.ModelItem;
32  import com.eviware.soapui.model.iface.Interface;
33  import com.eviware.soapui.model.iface.Operation;
34  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
35  import com.eviware.soapui.settings.WsaSettings;
36  import com.eviware.soapui.settings.WsdlSettings;
37  import com.eviware.soapui.support.StringUtils;
38  import com.eviware.soapui.support.UISupport;
39  import com.eviware.soapui.support.resolver.ResolveContext;
40  import com.eviware.soapui.support.types.StringList;
41  import org.apache.log4j.Logger;
42  import org.w3.x2007.x05.addressing.metadata.AddressingDocument.Addressing;
43  import org.w3.x2007.x05.addressing.metadata.AnonymousResponsesDocument.AnonymousResponses;
44  import org.w3.x2007.x05.addressing.metadata.NonAnonymousResponsesDocument.NonAnonymousResponses;
45  import org.xmlsoap.schemas.ws.x2004.x09.policy.Policy;
46  
47  import javax.wsdl.*;
48  import javax.xml.namespace.QName;
49  import java.io.File;
50  import java.net.MalformedURLException;
51  import java.util.*;
52  
53  /***
54   * WSDL implementation of Interface, maps to a WSDL Binding
55   *
56   * @author Ole.Matzura
57   */
58  
59  public class WsdlInterface extends AbstractInterface<WsdlInterfaceConfig> implements Interface
60  {
61     public static final String STYLE_DOCUMENT = "Document";
62     public static final String STYLE_RPC = "RPC";
63  
64     public static final String JBOSSWS_ACTIONS = "jbossws";
65     public static final String WSTOOLS_ACTIONS = "wstools";
66     public static final String XML_ACTIONS = "xml";
67  
68     private final static Logger log = Logger.getLogger(WsdlInterface.class);
69     private List<WsdlOperation> operations = new ArrayList<WsdlOperation>();
70     private WsdlProject project;
71     private SoapMessageBuilder soapMessageBuilder;
72     private WsdlContext wsdlContext;
73     private boolean updating = false;
74     private BeanPathPropertySupport definitionProperty;
75  
76     public WsdlInterface(WsdlProject project, WsdlInterfaceConfig interfaceConfig)
77     {
78        super(interfaceConfig, project, "/interface2.gif");
79  
80        if (!interfaceConfig.isSetWsaVersion())
81           interfaceConfig.setWsaVersion(WsaVersionTypeConfig.NONE);
82  
83        this.project = project;
84  
85        List<OperationConfig> operationConfigs = interfaceConfig.getOperationList();
86        for (int i = 0; i < operationConfigs.size(); i++)
87        {
88           operations.add(new WsdlOperation(this, operationConfigs.get(i)));
89        }
90  
91        definitionProperty = new BeanPathPropertySupport(this, "definition");
92     }
93  
94     public WsdlOperation getOperationAt(int index)
95     {
96        return operations.get(index);
97     }
98  
99     public int getOperationCount()
100    {
101       return operations.size();
102    }
103 
104    public WsdlOperation addNewOperation(BindingOperation operation)
105    {
106       WsdlOperation operationImpl = new WsdlOperation(this, getConfig().addNewOperation());
107       operations.add(operationImpl);
108 
109       operationImpl.initFromBindingOperation(operation);
110       fireOperationAdded(operationImpl);
111       return operationImpl;
112    }
113 
114    public WsdlProject getProject()
115    {
116       return project;
117    }
118 
119 
120    public void setDefinition(String wsdlUrl) throws Exception
121    {
122       setDefinition(wsdlUrl, true);
123    }
124 
125    public void setDefinition(String wsdlUrl, boolean updateCache) throws Exception
126    {
127       String old = definitionProperty.set(wsdlUrl, false);
128 
129       if (wsdlContext != null)
130       {
131          wsdlContext.setDefinition(definitionProperty.expandUrl(), updateCache );
132       }
133 
134       notifyPropertyChanged(DEFINITION_PROPERTY, old, wsdlUrl);
135       notifyPropertyChanged(UPDATING_PROPERTY, true, false);
136    }
137 
138    public DefinitionCacheConfig cacheDefinition(WsdlLoader loader) throws Throwable
139    {
140       log.debug("Caching definition for [" + loader.getBaseURI() + "]");
141       if (getConfig().isSetDefinitionCache())
142          getConfig().unsetDefinitionCache();
143 
144       DefinitionCacheConfig definitionCache = null;
145       try
146       {
147          definitionCache = getConfig().addNewDefinitionCache();
148          definitionCache.set(WsdlUtils.cacheWsdl(loader));
149       }
150       catch (Throwable e)
151       {
152          getConfig().unsetDefinitionCache();
153          throw e;
154       }
155 
156       return definitionCache;
157    }
158 
159    public String getDefinition()
160    {
161       if (!getConfig().isSetDefinition())
162          return null;
163 
164       String result = definitionProperty.get();
165 
166       if (PathUtils.isFilePath(result) && !PathUtils.isRelativePath(result) && !result.startsWith("file:") && !result.startsWith("$"))
167       {
168          try
169          {
170             result = new File(result).toURI().toURL().toString();
171          }
172          catch (MalformedURLException e)
173          {
174             e.printStackTrace();
175          }
176       }
177 
178       return result;
179    }
180 
181    public String getType()
182    {
183       return WsdlInterfaceFactory.WSDL_TYPE;
184    }
185 
186    public boolean isDefinitionShareble()
187    {
188       return true;
189    }
190 
191    public synchronized WsdlContext getWsdlContext()
192    {
193       if (wsdlContext == null)
194       {
195          wsdlContext = new WsdlContext( PathUtils.expandPath(getDefinition(), this), this );
196       }
197 
198       return wsdlContext;
199    }
200 
201    /***
202     * Used by importer so we dont need to reload the context after importing..
203     *
204     * @param wsdlContext
205     */
206 
207    public void setWsdlContext(WsdlContext wsdlContext)
208    {
209       this.wsdlContext = wsdlContext;
210       this.wsdlContext.setInterface(this);
211 
212       if (soapMessageBuilder != null)
213          soapMessageBuilder.setWsdlContext(wsdlContext);
214    }
215 
216    public SoapMessageBuilder getMessageBuilder()
217    {
218       if (soapMessageBuilder == null)
219       {
220          try
221          {
222             soapMessageBuilder = new SoapMessageBuilder(this);
223          }
224          catch (Exception e)
225          {
226             SoapUI.logError(e);
227          }
228       }
229       return soapMessageBuilder;
230    }
231 
232    public void setSoapMessageBuilder(SoapMessageBuilder builder)
233    {
234       soapMessageBuilder = builder;
235       soapMessageBuilder.setInterface(this);
236    }
237 
238    public QName getBindingName()
239    {
240       return getConfig().getBindingName() == null ? null : QName.valueOf(getConfig().getBindingName());
241    }
242 
243    public void setBindingName(QName name)
244    {
245       getConfig().setBindingName(name.toString());
246    }
247 
248    public SoapVersion getSoapVersion()
249    {
250       if (getConfig().getSoapVersion() == SoapVersionTypesConfig.X_1_2)
251          return SoapVersion.Soap12;
252 
253       return SoapVersion.Soap11;
254    }
255 
256    public void setSoapVersion(SoapVersion version)
257    {
258       if (version == SoapVersion.Soap11)
259          getConfig().setSoapVersion(SoapVersionTypesConfig.X_1_1);
260       else if (version == SoapVersion.Soap12)
261          getConfig().setSoapVersion(SoapVersionTypesConfig.X_1_2);
262       else
263          throw new RuntimeException("Unknown soapVersion [" + version + "], must be 1.1 or 1.2");
264    }
265 
266    public boolean updateDefinition(String url, boolean createRequests) throws Exception
267    {
268       WsdlContext.uncache(url);
269 
270       WsdlContext newContext = new WsdlContext(url, (WsdlInterface) null);
271       if (!newContext.load())
272       {
273          return false;
274       }
275 
276       BindingTuple tuple = findBinding(newContext);
277       if (tuple == null)
278          return false;
279 
280       setBindingName(tuple.binding.getQName());
281 
282       // update name
283       if (getSettings().getBoolean(WsdlSettings.NAME_WITH_BINDING))
284          setName(tuple.binding.getQName().getLocalPart());
285 
286       // update context
287       setWsdlContext( newContext );
288 
289       transferOperations(tuple.binding, createRequests);
290 
291       setDefinition(url, false);
292 
293       transferEndpoints(tuple.port);
294 
295       getProject().fireInterfaceUpdated(this);
296 
297       return true;
298    }
299 
300    public BindingTuple prepareUpdateDefinition(String url) throws Exception
301    {
302       WsdlContext newContext = new WsdlContext( url, (WsdlInterface) null);
303       if (!newContext.load())
304       {
305          return null;
306       }
307 
308       BindingTuple tuple = findBinding(newContext);
309       return tuple;
310    }
311 
312    public void updateDefinition(BindingTuple tuple) throws Exception
313    {
314       setBindingName(tuple.binding.getQName());
315 
316       if (getConfig().isSetDefinitionCache())
317          getConfig().unsetDefinitionCache();
318 
319       // update name
320       if (getSettings().getBoolean(WsdlSettings.NAME_WITH_BINDING))
321          setName(tuple.binding.getQName().getLocalPart());
322 
323       // update context
324       wsdlContext = tuple.context;
325       if (soapMessageBuilder != null)
326          soapMessageBuilder.setWsdlContext(wsdlContext);
327    }
328 
329    public BindingOperation findBindingOperation(Definition definition, String bindingOperationName, String inputName,
330                                                 String outputName)
331    {
332       Binding binding = definition.getBinding(getBindingName());
333       return WsdlUtils.findBindingOperation(binding, bindingOperationName, inputName, outputName);
334    }
335 
336    public Binding getBinding()
337    {
338       try
339       {
340          return findBinding(getWsdlContext()).binding;
341       }
342       catch (Exception e)
343       {
344          SoapUI.logError(e);
345          return null;
346       }
347    }
348 
349    @SuppressWarnings("unchecked")
350    private BindingTuple findBinding(WsdlContext newContext) throws Exception
351    {
352       BindingTuple tuple = new BindingTuple();
353       tuple.context = newContext;
354 
355       // start by finding the old binding in the new definition
356       Definition definition = newContext.getDefinition();
357       Map serviceMap = definition.getAllServices();
358       Iterator<String> i = serviceMap.keySet().iterator();
359       while (i.hasNext())
360       {
361          tuple.service = (Service) serviceMap.get(i.next());
362          Map portMap = tuple.service.getPorts();
363 
364          Iterator i2 = portMap.keySet().iterator();
365          while (i2.hasNext())
366          {
367             tuple.port = (Port) portMap.get(i2.next());
368             if (tuple.port.getBinding().getQName().equals(getBindingName()))
369             {
370                tuple.binding = tuple.port.getBinding();
371             }
372          }
373 
374          if (tuple.binding != null)
375             break;
376          tuple.service = null;
377       }
378 
379       if (tuple.service == null && tuple.binding == null)
380       {
381          tuple.binding = definition.getBinding(getBindingName());
382       }
383 
384       // missing matching binding, prompt for new one to use instead (will
385       // happen if binding has been renamed)
386       if (tuple.binding == null)
387       {
388          Map bindings = definition.getAllBindings();
389 
390          Object retval = UISupport.prompt("Missing matching binding [" + getBindingName()
391                  + "] in definition, select new\nbinding to map to", "Map Binding", bindings.keySet().toArray());
392 
393          if (retval == null)
394             return null;
395 
396          tuple.binding = (Binding) bindings.get(retval);
397       }
398 
399       return tuple;
400    }
401 
402    @SuppressWarnings("unchecked")
403    public void transferOperations(Binding binding, boolean createRequests)
404    {
405       // prepare for transfer of operations/requests
406       List<BindingOperation> newOperations = new ArrayList<BindingOperation>(binding.getBindingOperations());
407       Map<String, WsdlOperation> oldOperations = new HashMap<String, WsdlOperation>();
408       for (int c = 0; c < operations.size(); c++)
409          oldOperations.put(operations.get(c).getBindingOperationName(), operations.get(c));
410 
411       // clear existing from both collections
412       for (int c = 0; c < newOperations.size(); c++)
413       {
414          BindingOperation newOperation = newOperations.get(c);
415          String bindingOperationName = newOperation.getName();
416          if (oldOperations.containsKey(bindingOperationName))
417          {
418             log.info("Synchronizing existing operation [" + bindingOperationName + "]");
419             WsdlOperation wsdlOperation = oldOperations.get(bindingOperationName);
420             wsdlOperation.initFromBindingOperation(newOperation);
421             fireOperationUpdated(wsdlOperation);
422 
423             oldOperations.remove(bindingOperationName);
424             newOperations.remove(c);
425             c--;
426          }
427       }
428 
429       // remove leftover operations
430       Iterator<String> i = oldOperations.keySet().iterator();
431       while (i.hasNext())
432       {
433          String name = i.next();
434 
435          if (newOperations.size() > 0)
436          {
437             List<String> list = new ArrayList<String>();
438             list.add("none - delete operation");
439             for (int c = 0; c < newOperations.size(); c++)
440                list.add(newOperations.get(c).getName());
441 
442             String retval = (String) UISupport.prompt("Binding operation [" + name
443                     + "] not found in new interface, select new\nbinding operation to map to", "Map Operation", list
444                     .toArray(), "none/cancel - delete operation");
445 
446             int ix = retval == null ? -1 : list.indexOf(retval) - 1;
447 
448             // delete operation?
449             if (ix < 0)
450             {
451                deleteOperation(name);
452             }
453             // change operation?
454             else
455             {
456                BindingOperation newOperation = newOperations.get(ix);
457                WsdlOperation wsdlOperation = oldOperations.get(name);
458                wsdlOperation.initFromBindingOperation(newOperation);
459                fireOperationUpdated(wsdlOperation);
460                newOperations.remove(ix);
461             }
462 
463             oldOperations.remove(name);
464          }
465          else
466          {
467             deleteOperation(name);
468             oldOperations.remove(name);
469          }
470 
471          i = oldOperations.keySet().iterator();
472       }
473 
474       // add leftover new operations
475       if (newOperations.size() > 0)
476       {
477          for (int c = 0; c < newOperations.size(); c++)
478          {
479             BindingOperation newOperation = newOperations.get(c);
480             WsdlOperation wsdlOperation = addNewOperation(newOperation);
481 
482             if (createRequests)
483             {
484                WsdlRequest request = wsdlOperation.addNewRequest("Request 1");
485                try
486                {
487                   request.setRequestContent(wsdlOperation.createRequest(true));
488                }
489                catch (Exception e)
490                {
491                   SoapUI.logError(e);
492                }
493             }
494          }
495       }
496    }
497 
498    public void transferEndpoints(Port port)
499    {
500       if (port != null)
501       {
502          String endpoint = WsdlUtils.getSoapEndpoint(port);
503          if (endpoint != null)
504          {
505             StringList list = new StringList(getEndpoints());
506 
507             // expand properties..
508             for (int c = 0; c < list.size(); c++)
509                list.set(c, PropertyExpansionUtils.expandProperties(this, list.get(c)));
510 
511             if (!list.contains(endpoint))
512             {
513                if (UISupport.confirm("Update existing requests with new endpoint\n[" + endpoint + "]",
514                        "Update Definition"))
515                {
516                   for (int c = 0; c < getOperationCount(); c++)
517                   {
518                      Operation operation = getOperationAt(c);
519 
520                      for (int ix = 0; ix < operation.getRequestCount(); ix++)
521                      {
522                         operation.getRequestAt(ix).setEndpoint(endpoint);
523                      }
524                   }
525                }
526 
527                addEndpoint(endpoint);
528             }
529          }
530       }
531    }
532 
533    public void deleteOperation(String bindingOperationName)
534    {
535       for (int c = 0; c < operations.size(); c++)
536       {
537          WsdlOperation wsdlOperation = operations.get(c);
538          if (wsdlOperation.getBindingOperationName().equals(bindingOperationName))
539          {
540             log.info("deleting operation [" + bindingOperationName + "]");
541 
542             // remove requests first (should this be done by some listener?)
543             while (wsdlOperation.getRequestCount() > 0)
544                wsdlOperation.removeRequest(wsdlOperation.getRequestAt(0));
545 
546             operations.remove(c);
547 
548             try
549             {
550                fireOperationRemoved(wsdlOperation);
551             }
552             finally
553             {
554                wsdlOperation.release();
555                getConfig().removeOperation(c);
556             }
557 
558             return;
559          }
560       }
561    }
562 
563    public void removeOperation(WsdlOperation wsdlOperation)
564    {
565       int c = operations.indexOf(wsdlOperation);
566       if (c < 0)
567          throw new IllegalArgumentException(wsdlOperation.getName() + " not found");
568 
569       log.info("deleting operation [" + wsdlOperation.getName() + "]");
570 
571       // remove requests first (should this be done by some listener?)
572       while (wsdlOperation.getRequestCount() > 0)
573          wsdlOperation.removeRequest(wsdlOperation.getRequestAt(0));
574 
575       operations.remove(c);
576 
577       try
578       {
579          fireOperationRemoved(wsdlOperation);
580       }
581       finally
582       {
583          wsdlOperation.release();
584          getConfig().removeOperation(c);
585       }
586    }
587 
588 
589    public WsdlOperation getOperationByName(String name)
590    {
591       return (WsdlOperation) getWsdlModelItemByName(operations, name);
592    }
593 
594    public Map<String, Operation> getOperations()
595    {
596       Map<String, Operation> result = new HashMap<String, Operation>();
597       for (Operation operation : operations)
598          result.put(operation.getName(), operation);
599 
600       return result;
601 
602    }
603 
604    public boolean isCached()
605    {
606       return wsdlContext != null && wsdlContext.isCached();
607    }
608 
609 //   public WsdlLoader createWsdlLoader()
610 //   {
611 //      return isCached() ? new CachedWsdlLoader(getConfig().getDefinitionCache())
612 //              : new UrlWsdlLoader(definitionProperty.expandUrl());
613 //   }
614 
615    public String getStyle()
616    {
617       if (wsdlContext == null || !wsdlContext.isLoaded())
618          return "<not loaded>";
619 
620       try
621       {
622          Binding binding = wsdlContext.getDefinition().getBinding(getBindingName());
623          if (binding == null)
624             return "<missing binding>";
625 
626          if (WsdlUtils.isRpc(binding))
627          {
628             return STYLE_RPC;
629          }
630          else
631          {
632             return STYLE_DOCUMENT;
633          }
634       }
635       catch (Exception e)
636       {
637          SoapUI.logError(e);
638          return "<error>";
639       }
640    }
641 
642    @Override
643    public void release()
644    {
645       super.release();
646 
647       for (WsdlOperation operation : operations)
648          operation.release();
649 
650       if (wsdlContext != null)
651          wsdlContext.release();
652    }
653 
654    public List<Operation> getOperationList()
655    {
656       return new ArrayList<Operation>(operations);
657    }
658 
659    @Override
660    public void beforeSave()
661    {
662       for (WsdlOperation operation : operations)
663          operation.beforeSave();
664    }
665 
666    public static class BindingTuple
667    {
668       public WsdlContext context = null;
669       public Service service = null;
670       public Port port = null;
671       public Binding binding = null;
672    }
673 
674    public boolean isUpdating()
675    {
676       return updating;
677    }
678 
679    public void setUpdating(boolean updating)
680    {
681       if (this.updating == updating)
682          return;
683 
684       if (updating)
685       {
686          List<AbstractWsdlModelItem<?>> messages = getAllMessages();
687          for (AbstractWsdlModelItem<?> modelItem : messages)
688          {
689             modelItem.beforeSave();
690          }
691       }
692 
693       boolean oldValue = this.updating;
694       this.updating = updating;
695 
696       notifyPropertyChanged(UPDATING_PROPERTY, oldValue, updating);
697    }
698 
699    public List<AbstractWsdlModelItem<?>> getAllMessages()
700    {
701       ArrayList<AbstractWsdlModelItem<?>> list = new ArrayList<AbstractWsdlModelItem<?>>();
702       getAllMessages(getProject(), list);
703       return list;
704    }
705 
706    private void getAllMessages(ModelItem modelItem, List<AbstractWsdlModelItem<?>> list)
707    {
708       if (modelItem instanceof AbstractHttpRequest )
709       {
710          AbstractHttpRequest wsdlRequest = (AbstractHttpRequest) modelItem;
711          if (wsdlRequest.getOperation().getInterface() == this)
712             list.add(wsdlRequest);
713       }
714       else if (modelItem instanceof WsdlTestRequestStep)
715       {
716          WsdlTestRequestStep testRequestStep = (WsdlTestRequestStep) modelItem;
717          WsdlTestRequest testRequest = testRequestStep.getTestRequest();
718          if (testRequest.getOperation().getInterface() == this)
719             list.add(testRequest);
720       }
721       else if (modelItem instanceof WsdlMockResponse)
722       {
723          WsdlMockResponse mockResponse = (WsdlMockResponse) modelItem;
724          if (mockResponse.getMockOperation() != null &&
725                  mockResponse.getMockOperation().getOperation() != null &&
726                  mockResponse.getMockOperation().getOperation().getInterface() == this)
727             list.add(mockResponse);
728       }
729 
730       // Traverse the ModelItem hierarchy.
731       for (ModelItem child : modelItem.getChildren())
732       {
733          getAllMessages(child, list);
734       }
735    }
736 
737    @Override
738    public void resolve( ResolveContext context)
739    {
740       super.resolve(context);
741 
742       String definition = definitionProperty.expandUrl();
743       if (definition.startsWith("file:"))
744       {
745          try
746          {
747             File file = new File( definition.substring( 5 ));
748             if (!file.exists())
749             {
750                context.addPathToResolve(this, "Missing WSDL file", definition, new ResolveContext.FileResolver("Select WSDL File",
751                        "wsdl", "WSDL Files (*.wsdl)", file.getParent())
752                {
753 
754                   @Override
755                   public boolean apply(File newFile)
756                   {
757                      try
758                      {
759                         setDefinition(newFile.toURI().toURL().toString());
760                         return true;
761                      }
762                      catch (Exception e)
763                      {
764                         log.error("Invalid URL for new Definition", e);
765                         return false;
766                      }
767                   }
768                });
769             }
770          }
771          catch (Exception e)
772          {
773             e.printStackTrace();
774          }
775       }
776    }
777 
778    public String getInterfaceType()
779    {
780       return WsdlInterfaceFactory.WSDL_TYPE;
781    }
782 
783    public String getTechnicalId()
784    {
785       return getBindingName().toString();
786    }
787 
788    public String getWsaVersion()
789    {
790       if (getConfig().getWsaVersion().equals(WsaVersionTypeConfig.X_200408))
791       {
792          return WsaVersionTypeConfig.X_200408.toString();
793       }
794       else if (getConfig().getWsaVersion().equals(WsaVersionTypeConfig.X_200508))
795       {
796          return WsaVersionTypeConfig.X_200508.toString();
797       }
798 
799       return WsaVersionTypeConfig.NONE.toString();
800    }
801 
802    public void setWsaVersion(String wsAddressing)
803    {
804       if (wsAddressing.equals(WsaVersionTypeConfig.X_200508.toString()))
805          getConfig().setWsaVersion(WsaVersionTypeConfig.X_200508);
806       else if (wsAddressing.equals(WsaVersionTypeConfig.X_200408.toString()))
807          getConfig().setWsaVersion(WsaVersionTypeConfig.X_200408);
808       else
809          getConfig().setWsaVersion(WsaVersionTypeConfig.NONE);
810 
811    }
812 
813    public void setAnonymous(String anonymous)
814    {
815       if (anonymous.equals(AnonymousTypeConfig.REQUIRED.toString()))
816          getConfig().setAnonymous(AnonymousTypeConfig.REQUIRED);
817       else if (anonymous.equals(AnonymousTypeConfig.PROHIBITED.toString()))
818          getConfig().setAnonymous(AnonymousTypeConfig.PROHIBITED);
819       else
820          getConfig().setAnonymous(AnonymousTypeConfig.OPTIONAL);
821 
822    }
823 	public String getAnonymous()
824 	{
825 //		return WsdlUtils.getAnonymous(this);
826       if( getConfig().isSetAnonymous())
827       {
828          if (getConfig().getAnonymous().equals(AnonymousTypeConfig.PROHIBITED))
829          {
830             return AnonymousTypeConfig.PROHIBITED.toString();
831          }
832          else if (getConfig().getAnonymous().equals(AnonymousTypeConfig.REQUIRED))
833          {
834             return AnonymousTypeConfig.REQUIRED.toString();
835          }
836       }
837 
838       return AnonymousTypeConfig.OPTIONAL.toString();
839 }
840 
841    @Override
842    public DefinitionContext getDefinitionContext()
843    {
844       return getWsdlContext();
845    }
846 
847 	// need to fix removing mock response and test cases.
848 	private void replace(WsdlOperation wsdlOperation, OperationConfig reloadedOperation)
849 	{
850 		int index = operations.indexOf( wsdlOperation );
851 	   
852 		int c = operations.indexOf( wsdlOperation );
853 	   if( c < 0)
854 	      throw new IllegalArgumentException( wsdlOperation.getName() + " not found" );
855 	   
856       log.info( "deleting operation [" + wsdlOperation.getName() + "]" );
857       
858       // remove requests first (should this be done by some listener?)
859       while( wsdlOperation.getRequestCount() > 0 )
860          wsdlOperation.removeRequest( wsdlOperation.getRequestAt( 0 ) );
861 
862       operations.remove( c );
863 
864       try
865       {
866          fireOperationRemoved( wsdlOperation );
867       }
868       finally
869       {
870          wsdlOperation.release();
871          getConfig().removeOperation( c );
872       }
873 	   
874 	   OperationConfig newConfig = (OperationConfig) getConfig().addNewOperation().set(reloadedOperation).changeType(OperationConfig.type);
875 	   WsdlOperation newOperation = new WsdlOperation(this, newConfig);
876 	   operations.add(index, newOperation);
877 	   newOperation.afterLoad();
878 	   fireOperationAdded(newOperation);
879 	   
880 	}
881 	/***
882 	 * Method for processing policy on interface level
883 	 * it should include processing of all types of policies, but for now there's only Addressing policy implemented
884 	 * @param policy
885 	 * @return this interface changed in a proper way indicated by the policy
886 	 */
887 	public WsdlInterface processPolicy(Policy policy) throws Exception {
888 		
889 		//default is optional
890 //		String anonymous = AnonymousTypeConfig.OPTIONAL.toString();
891 		String wsaVersion = WsaVersionTypeConfig.NONE.toString();
892 		setAnonymous(AnonymousTypeConfig.OPTIONAL.toString());
893 		
894 		if (policy != null)
895 		{
896 			List<Addressing> addressingList = policy.getAddressingList();
897 			for (Addressing addressing : addressingList)
898 			{
899 				String optional = addressing.getOptional().toString();
900 				if (StringUtils.isNullOrEmpty(optional) || optional.equals("false")
901 						|| (optional.equals("true") && SoapUI.getSettings().getBoolean(WsaSettings.ENABLE_FOR_OPTIONAL)))
902 				{
903 					wsaVersion = WsaVersionTypeConfig.X_200508.toString();
904 				}
905 				Policy innerPolicy = addressing.getPolicy();
906 				if (innerPolicy != null)
907 				{
908 					List<AnonymousResponses> anonymousList = innerPolicy.getAnonymousResponsesList();
909 					List<NonAnonymousResponses> nonAnonymousList = innerPolicy.getNonAnonymousResponsesList();
910 					if (anonymousList.size() > 0 && nonAnonymousList.size() > 0)
911 					{
912 						throw new Exception("Wrong addressing policy, anonymousResponses and nonAnonymousResponses can not be specified together");
913 					}
914 					if (anonymousList.size() > 0)
915 					{
916 						AnonymousResponses anonResp = anonymousList.get(0);
917 						setAnonymous(AnonymousTypeConfig.REQUIRED.toString());
918 					}
919 					else
920 					{
921 						if (nonAnonymousList.size() > 0)
922 						{
923 							NonAnonymousResponses nonAnonResp = nonAnonymousList.get(0);
924 							setAnonymous(AnonymousTypeConfig.PROHIBITED.toString());
925 						}
926 					}
927 				}
928 			}
929 		}
930 		setWsaVersion( wsaVersion);
931 		return this;
932 	}
933 	
934 
935 }