View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.mock;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.UnknownHostException;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.swing.ImageIcon;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.config.MockOperationConfig;
32  import com.eviware.soapui.config.MockOperationDocumentConfig;
33  import com.eviware.soapui.config.MockServiceConfig;
34  import com.eviware.soapui.config.TestCaseConfig;
35  import com.eviware.soapui.impl.wsdl.AbstractTestPropertyHolderWsdlModelItem;
36  import com.eviware.soapui.impl.wsdl.WsdlInterface;
37  import com.eviware.soapui.impl.wsdl.WsdlOperation;
38  import com.eviware.soapui.impl.wsdl.WsdlProject;
39  import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
40  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
41  import com.eviware.soapui.impl.wsdl.teststeps.BeanPathPropertySupport;
42  import com.eviware.soapui.model.ModelItem;
43  import com.eviware.soapui.model.iface.Operation;
44  import com.eviware.soapui.model.mock.MockOperation;
45  import com.eviware.soapui.model.mock.MockResult;
46  import com.eviware.soapui.model.mock.MockRunListener;
47  import com.eviware.soapui.model.mock.MockRunner;
48  import com.eviware.soapui.model.mock.MockService;
49  import com.eviware.soapui.model.mock.MockServiceListener;
50  import com.eviware.soapui.model.project.Project;
51  import com.eviware.soapui.model.support.ModelSupport;
52  import com.eviware.soapui.settings.SSLSettings;
53  import com.eviware.soapui.support.StringUtils;
54  import com.eviware.soapui.support.UISupport;
55  import com.eviware.soapui.support.resolver.ResolveContext;
56  import com.eviware.soapui.support.resolver.ResolveDialog;
57  import com.eviware.soapui.support.scripting.ScriptEnginePool;
58  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
59  import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
60  
61  /***
62   * A MockService for simulation WsdlInterfaces and their operations
63   * 
64   * @author ole.matzura
65   */
66  
67  public class WsdlMockService extends AbstractTestPropertyHolderWsdlModelItem<MockServiceConfig> implements MockService
68  {
69  	private static final String REQUIRE_SOAP_VERSION = WsdlMockService.class.getName() + "@require-soap-version";
70  	private static final String REQUIRE_SOAP_ACTION = WsdlMockService.class.getName() + "@require-soap-action";
71  
72  	public final static String START_SCRIPT_PROPERTY = WsdlMockService.class.getName() + "@startScript";
73  	public final static String STOP_SCRIPT_PROPERTY = WsdlMockService.class.getName() + "@stopScript";
74  	public static final String INCOMING_WSS = WsdlMockService.class.getName() + "@incoming-wss";
75  	public static final String OUGOING_WSS = WsdlMockService.class.getName() + "@outgoing-wss";
76  
77  	private List<WsdlMockOperation> mockOperations = new ArrayList<WsdlMockOperation>();
78  	private Set<MockRunListener> mockRunListeners = new HashSet<MockRunListener>();
79  	private Set<MockServiceListener> mockServiceListeners = new HashSet<MockServiceListener>();
80  	private MockServiceIconAnimator iconAnimator;
81  	private WsdlMockRunner mockRunner;
82  	private SoapUIScriptEngine startScriptEngine;
83  	private SoapUIScriptEngine stopScriptEngine;
84  	private BeanPathPropertySupport docrootProperty;
85  	private ScriptEnginePool onRequestScriptEnginePool;
86  	private ScriptEnginePool afterRequestScriptEnginePool;
87  	private WsdlMockOperation faultMockOperation;
88  	private String mockServiceEndpoint;
89  
90  	public WsdlMockService( Project project, MockServiceConfig config )
91  	{
92  		super( config, project, "/mockService.gif" );
93  
94  		List<MockOperationConfig> testStepConfigs = config.getMockOperationList();
95  		for( MockOperationConfig tsc : testStepConfigs )
96  		{
97  			WsdlMockOperation testStep = new WsdlMockOperation( this, tsc );
98  			mockOperations.add( testStep );
99  		}
100 
101 		if( !config.isSetPort() || config.getPort() < 1 )
102 			config.setPort( 8080 );
103 
104 		if( !config.isSetPath() )
105 			config.setPath( "/" );
106 
107 		if( !getSettings().isSet( REQUIRE_SOAP_ACTION ) )
108 			setRequireSoapAction( false );
109 
110 		try
111 		{
112 			if( !config.isSetHost() || !StringUtils.hasContent( config.getHost() ) )
113 				config.setHost( InetAddress.getLocalHost().getHostName() );
114 		}
115 		catch( UnknownHostException e )
116 		{
117 			SoapUI.logError( e );
118 		}
119 
120 		iconAnimator = new MockServiceIconAnimator();
121 		addMockRunListener( iconAnimator );
122 
123 		for( MockRunListener listener : SoapUI.getListenerRegistry().getListeners( MockRunListener.class ) )
124 		{
125 			addMockRunListener( listener );
126 		}
127 
128 		if( !getConfig().isSetProperties() )
129 			getConfig().addNewProperties();
130 
131 		setPropertiesConfig( getConfig().getProperties() );
132 		docrootProperty = new BeanPathPropertySupport( this, "docroot" );
133 
134 		if( getConfig().isSetFaultMockOperation() )
135 		{
136 			faultMockOperation = getMockOperationByName( getConfig().getFaultMockOperation() );
137 		}
138 	}
139 
140 	public void addMockRunListener( MockRunListener listener )
141 	{
142 		mockRunListeners.add( listener );
143 	}
144 
145 	public String getPath()
146 	{
147 		return getConfig().getPath();
148 	}
149 
150 	public WsdlMockOperation getMockOperationAt( int index )
151 	{
152 		return mockOperations.get( index );
153 	}
154 
155 	public WsdlMockOperation getMockOperationByName( String name )
156 	{
157 		return ( WsdlMockOperation )getWsdlModelItemByName( mockOperations, name );
158 	}
159 
160 	public int getMockOperationCount()
161 	{
162 		return mockOperations.size();
163 	}
164 
165 	public WsdlProject getProject()
166 	{
167 		return ( WsdlProject )getParent();
168 	}
169 
170 	public int getPort()
171 	{
172 		return getConfig().getPort();
173 	}
174 
175 	public String getHost()
176 	{
177 		return getConfig().getHost();
178 	}
179 
180 	public void setHost( String host )
181 	{
182 		getConfig().setHost( host );
183 	}
184 
185 	public boolean getBindToHostOnly()
186 	{
187 		return getConfig().getBindToHostOnly();
188 	}
189 
190 	public void setBindToHostOnly( boolean bindToHostOnly )
191 	{
192 		getConfig().setBindToHostOnly( bindToHostOnly );
193 	}
194 
195 	public void removeMockRunListener( MockRunListener listener )
196 	{
197 		mockRunListeners.remove( listener );
198 	}
199 
200 	public WsdlMockRunner start( WsdlTestRunContext context ) throws Exception
201 	{
202 		String path = getPath();
203 		if( path == null || path.trim().length() == 0 || path.trim().charAt( 0 ) != '/' )
204 			throw new Exception( "Invalid path; must start with '/'" );
205 
206 		mockRunner = new WsdlMockRunner( this, context );
207 		return mockRunner;
208 	}
209 
210 	public WsdlMockRunner getMockRunner()
211 	{
212 		return mockRunner;
213 	}
214 
215 	public WsdlMockOperation getMockOperation( Operation operation )
216 	{
217 		for( int c = 0; c < getMockOperationCount(); c++ )
218 		{
219 			WsdlMockOperation mockOperation = mockOperations.get( c );
220 			if( mockOperation.getOperation() == operation )
221 				return mockOperation;
222 		}
223 
224 		return null;
225 	}
226 
227 	public WsdlMockOperation addNewMockOperation( WsdlOperation operation )
228 	{
229 		if( getMockOperation( operation ) != null )
230 			return null;
231 
232 		MockOperationConfig config = getConfig().addNewMockOperation();
233 		config.setName( operation.getName() );
234 		WsdlMockOperation mockOperation = new WsdlMockOperation( this, config, operation );
235 
236 		mockOperations.add( mockOperation );
237 		fireMockOperationAdded( mockOperation );
238 
239 		return mockOperation;
240 	}
241 
242 	public void setPort( int port )
243 	{
244 		String oldEndpoint = getLocalEndpoint();
245 
246 		int oldPort = getPort();
247 		if( port != oldPort )
248 		{
249 			getConfig().setPort( port );
250 			notifyPropertyChanged( PORT_PROPERTY, oldPort, port );
251 
252 			for( WsdlInterface iface : getMockedInterfaces() )
253 			{
254 				if( Arrays.asList( iface.getEndpoints() ).contains( oldEndpoint ) )
255 					iface.changeEndpoint( oldEndpoint, getLocalEndpoint() );
256 			}
257 		}
258 	}
259 
260 	public WsdlInterface[] getMockedInterfaces()
261 	{
262 		Set<WsdlInterface> result = new HashSet<WsdlInterface>();
263 
264 		for( WsdlMockOperation mockOperation : mockOperations )
265 		{
266 			WsdlOperation operation = mockOperation.getOperation();
267 			if( operation != null )
268 				result.add( operation.getInterface() );
269 		}
270 
271 		return result.toArray( new WsdlInterface[result.size()] );
272 	}
273 
274 	@Override
275 	public void release()
276 	{
277 		super.release();
278 
279 		if( mockRunner != null )
280 		{
281 			if( mockRunner.isRunning() )
282 				mockRunner.stop();
283 
284 			if( mockRunner != null )
285 				mockRunner.release();
286 		}
287 
288 		for( WsdlMockOperation operation : mockOperations )
289 			operation.release();
290 
291 		mockServiceListeners.clear();
292 
293 		if( onRequestScriptEnginePool != null )
294 			onRequestScriptEnginePool.release();
295 
296 		if( afterRequestScriptEnginePool != null )
297 			afterRequestScriptEnginePool.release();
298 
299 		if( startScriptEngine != null )
300 			startScriptEngine.release();
301 
302 		if( stopScriptEngine != null )
303 			stopScriptEngine.release();
304 	}
305 
306 	public void setPath( String path )
307 	{
308 		String oldEndpoint = getLocalEndpoint();
309 
310 		String oldPath = getPath();
311 		if( !path.equals( oldPath ) )
312 		{
313 			getConfig().setPath( path );
314 			notifyPropertyChanged( PATH_PROPERTY, oldPath, path );
315 
316 			for( WsdlInterface iface : getMockedInterfaces() )
317 			{
318 				if( Arrays.asList( iface.getEndpoints() ).contains( oldEndpoint ) )
319 					iface.changeEndpoint( oldEndpoint, getLocalEndpoint() );
320 			}
321 		}
322 	}
323 
324 	public MockRunListener[] getMockRunListeners()
325 	{
326 		return mockRunListeners.toArray( new MockRunListener[mockRunListeners.size()] );
327 	}
328 
329 	public void removeMockOperation( WsdlMockOperation mockOperation )
330 	{
331 		int ix = mockOperations.indexOf( mockOperation );
332 		if( ix == -1 )
333 			throw new RuntimeException( "Unkonws MockOperation specified to removeMockOperation" );
334 
335 		mockOperations.remove( ix );
336 		fireMockOperationRemoved( mockOperation );
337 		mockOperation.release();
338 		getConfig().removeMockOperation( ix );
339 	}
340 
341 	public void addMockServiceListener( MockServiceListener listener )
342 	{
343 		mockServiceListeners.add( listener );
344 	}
345 
346 	public void removeMockServiceListener( MockServiceListener listener )
347 	{
348 		mockServiceListeners.remove( listener );
349 	}
350 
351 	protected void fireMockOperationAdded( WsdlMockOperation mockOperation )
352 	{
353 		MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
354 				.size()] );
355 		for( MockServiceListener listener : listeners )
356 		{
357 			listener.mockOperationAdded( mockOperation );
358 		}
359 	}
360 
361 	protected void fireMockOperationRemoved( WsdlMockOperation mockOperation )
362 	{
363 		MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
364 				.size()] );
365 		for( MockServiceListener listener : listeners )
366 		{
367 			listener.mockOperationRemoved( mockOperation );
368 		}
369 	}
370 
371 	protected void fireMockResponseAdded( WsdlMockResponse mockResponse )
372 	{
373 		MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
374 				.size()] );
375 		for( MockServiceListener listener : listeners )
376 		{
377 			listener.mockResponseAdded( mockResponse );
378 		}
379 	}
380 
381 	protected void fireMockResponseRemoved( WsdlMockResponse mockResponse )
382 	{
383 		MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
384 				.size()] );
385 		for( MockServiceListener listener : listeners )
386 		{
387 			listener.mockResponseRemoved( mockResponse );
388 		}
389 	}
390 
391 	@Override
392 	public ImageIcon getIcon()
393 	{
394 		return iconAnimator.getIcon();
395 	}
396 
397 	public WsdlMockOperation getFaultMockOperation()
398 	{
399 		return faultMockOperation;
400 	}
401 
402 	public void setFaultMockOperation( WsdlMockOperation mockOperation )
403 	{
404 		faultMockOperation = mockOperation;
405 		if( faultMockOperation == null )
406 		{
407 			if( getConfig().isSetFaultMockOperation() )
408 			{
409 				getConfig().unsetFaultMockOperation();
410 			}
411 		}
412 		else
413 		{
414 			getConfig().setFaultMockOperation( faultMockOperation.getName() );
415 		}
416 	}
417 
418 	private class MockServiceIconAnimator extends ModelItemIconAnimator<WsdlMockService> implements MockRunListener
419 	{
420 		public MockServiceIconAnimator()
421 		{
422 			super( WsdlMockService.this, "/mockService.gif", "/mockService", 4, "gif" );
423 		}
424 
425 		public MockResult onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
426 		{
427 			return null;
428 		}
429 
430 		public void onMockResult( MockResult result )
431 		{
432 		}
433 
434 		public void onMockRunnerStart( MockRunner mockRunner )
435 		{
436 			start();
437 		}
438 
439 		public void onMockRunnerStop( MockRunner mockRunner )
440 		{
441 			stop();
442 			WsdlMockService.this.mockRunner = null;
443 		}
444 	}
445 
446 	public String getLocalEndpoint()
447 	{
448 		String host = getHost();
449 		if( StringUtils.isNullOrEmpty( host ) )
450 		{
451 			host = "127.0.0.1";
452 		}
453 
454 		return getProtocol() + host + ":" + getPort() + getPath();
455 	}
456 
457 	private String getProtocol()
458 	{
459 		try
460 		{
461 			boolean sslEnabled = SoapUI.getSettings().getBoolean( SSLSettings.ENABLE_MOCK_SSL );
462 			String protocol = sslEnabled ? "https://" : "http://";
463 			return protocol;
464 		}
465 		catch( Exception e )
466 		{
467 			return "http://";
468 		}
469 	}
470 
471 	public boolean isRequireSoapVersion()
472 	{
473 		return getSettings().getBoolean( REQUIRE_SOAP_VERSION );
474 	}
475 
476 	public void setRequireSoapVersion( boolean requireSoapVersion )
477 	{
478 		getSettings().setBoolean( REQUIRE_SOAP_VERSION, requireSoapVersion );
479 	}
480 
481 	public boolean isRequireSoapAction()
482 	{
483 		return getSettings().getBoolean( REQUIRE_SOAP_ACTION );
484 	}
485 
486 	public void setRequireSoapAction( boolean requireSoapAction )
487 	{
488 		getSettings().setBoolean( REQUIRE_SOAP_ACTION, requireSoapAction );
489 	}
490 
491 	public WsdlMockRunner start() throws Exception
492 	{
493 		return start( null );
494 	}
495 
496 	public boolean hasMockOperation( Operation operation )
497 	{
498 		return getMockOperation( operation ) != null;
499 	}
500 
501 	public void setStartScript( String script )
502 	{
503 		String oldScript = getStartScript();
504 
505 		if( !getConfig().isSetStartScript() )
506 			getConfig().addNewStartScript();
507 
508 		getConfig().getStartScript().setStringValue( script );
509 
510 		if( startScriptEngine != null )
511 			startScriptEngine.setScript( script );
512 
513 		notifyPropertyChanged( START_SCRIPT_PROPERTY, oldScript, script );
514 	}
515 
516 	public String getStartScript()
517 	{
518 		return getConfig().isSetStartScript() ? getConfig().getStartScript().getStringValue() : null;
519 	}
520 
521 	public void setStopScript( String script )
522 	{
523 		String oldScript = getStopScript();
524 
525 		if( !getConfig().isSetStopScript() )
526 			getConfig().addNewStopScript();
527 
528 		getConfig().getStopScript().setStringValue( script );
529 		if( stopScriptEngine != null )
530 			stopScriptEngine.setScript( script );
531 
532 		notifyPropertyChanged( STOP_SCRIPT_PROPERTY, oldScript, script );
533 	}
534 
535 	public String getStopScript()
536 	{
537 		return getConfig().isSetStopScript() ? getConfig().getStopScript().getStringValue() : null;
538 	}
539 
540 	public Object runStartScript( WsdlMockRunContext runContext, WsdlMockRunner runner ) throws Exception
541 	{
542 		String script = getStartScript();
543 		if( StringUtils.isNullOrEmpty( script ) )
544 			return null;
545 
546 		if( startScriptEngine == null )
547 		{
548 			startScriptEngine = SoapUIScriptEngineRegistry.create( this );
549 			startScriptEngine.setScript( script );
550 		}
551 
552 		startScriptEngine.setVariable( "context", runContext );
553 		startScriptEngine.setVariable( "mockRunner", runner );
554 		startScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
555 		return startScriptEngine.run();
556 	}
557 
558 	public Object runStopScript( WsdlMockRunContext runContext, WsdlMockRunner runner ) throws Exception
559 	{
560 		String script = getStopScript();
561 		if( StringUtils.isNullOrEmpty( script ) )
562 			return null;
563 
564 		if( stopScriptEngine == null )
565 		{
566 			stopScriptEngine = SoapUIScriptEngineRegistry.create( this );
567 			stopScriptEngine.setScript( script );
568 		}
569 
570 		stopScriptEngine.setVariable( "context", runContext );
571 		stopScriptEngine.setVariable( "mockRunner", runner );
572 		stopScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
573 		return stopScriptEngine.run();
574 	}
575 
576 	public void setOnRequestScript( String script )
577 	{
578 		String oldScript = getOnRequestScript();
579 
580 		if( !getConfig().isSetOnRequestScript() )
581 			getConfig().addNewOnRequestScript();
582 
583 		getConfig().getOnRequestScript().setStringValue( script );
584 
585 		if( onRequestScriptEnginePool != null )
586 			onRequestScriptEnginePool.setScript( script );
587 
588 		notifyPropertyChanged( "onRequestScript", oldScript, script );
589 	}
590 
591 	public String getOnRequestScript()
592 	{
593 		return getConfig().isSetOnRequestScript() ? getConfig().getOnRequestScript().getStringValue() : null;
594 	}
595 
596 	public void setAfterRequestScript( String script )
597 	{
598 		String oldScript = getAfterRequestScript();
599 
600 		if( !getConfig().isSetAfterRequestScript() )
601 			getConfig().addNewAfterRequestScript();
602 
603 		getConfig().getAfterRequestScript().setStringValue( script );
604 		if( afterRequestScriptEnginePool != null )
605 			afterRequestScriptEnginePool.setScript( script );
606 
607 		notifyPropertyChanged( "afterRequestScript", oldScript, script );
608 	}
609 
610 	public String getAfterRequestScript()
611 	{
612 		return getConfig().isSetAfterRequestScript() ? getConfig().getAfterRequestScript().getStringValue() : null;
613 	}
614 
615 	public Object runOnRequestScript( WsdlMockRunContext runContext, WsdlMockRunner runner, WsdlMockRequest mockRequest )
616 			throws Exception
617 	{
618 		String script = getOnRequestScript();
619 		if( StringUtils.isNullOrEmpty( script ) )
620 			return null;
621 
622 		if( onRequestScriptEnginePool == null )
623 		{
624 			onRequestScriptEnginePool = new ScriptEnginePool( this );
625 			onRequestScriptEnginePool.setScript( script );
626 		}
627 
628 		SoapUIScriptEngine scriptEngine = onRequestScriptEnginePool.getScriptEngine();
629 
630 		try
631 		{
632 			scriptEngine.setVariable( "context", runContext );
633 			scriptEngine.setVariable( "mockRequest", mockRequest );
634 			scriptEngine.setVariable( "mockRunner", runner );
635 			scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
636 			return scriptEngine.run();
637 		}
638 		finally
639 		{
640 			onRequestScriptEnginePool.returnScriptEngine( scriptEngine );
641 		}
642 	}
643 
644 	public Object runAfterRequestScript( WsdlMockRunContext runContext, WsdlMockRunner runner, MockResult mockResult )
645 			throws Exception
646 	{
647 		String script = getAfterRequestScript();
648 		if( StringUtils.isNullOrEmpty( script ) )
649 			return null;
650 
651 		if( afterRequestScriptEnginePool == null )
652 		{
653 			afterRequestScriptEnginePool = new ScriptEnginePool( this );
654 			afterRequestScriptEnginePool.setScript( script );
655 		}
656 
657 		SoapUIScriptEngine scriptEngine = afterRequestScriptEnginePool.getScriptEngine();
658 
659 		try
660 		{
661 			scriptEngine.setVariable( "context", runContext );
662 			scriptEngine.setVariable( "mockResult", mockResult );
663 			scriptEngine.setVariable( "mockRunner", runner );
664 			scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
665 			return scriptEngine.run();
666 		}
667 		finally
668 		{
669 			afterRequestScriptEnginePool.returnScriptEngine( scriptEngine );
670 		}
671 	}
672 
673 	public List<? extends ModelItem> getChildren()
674 	{
675 		return mockOperations;
676 	}
677 
678 	public List<MockOperation> getMockOperationList()
679 	{
680 		return Collections.unmodifiableList( new ArrayList<MockOperation>( mockOperations ) );
681 	}
682 
683 	public String getIncomingWss()
684 	{
685 		return getConfig().getIncomingWss();
686 	}
687 
688 	public void setIncomingWss( String incomingWss )
689 	{
690 		String old = getIncomingWss();
691 		getConfig().setIncomingWss( incomingWss );
692 		notifyPropertyChanged( INCOMING_WSS, old, incomingWss );
693 	}
694 
695 	public String getOutgoingWss()
696 	{
697 		return getConfig().getOutgoingWss();
698 	}
699 
700 	public void setOutgoingWss( String outgoingWss )
701 	{
702 		String old = getOutgoingWss();
703 		getConfig().setOutgoingWss( outgoingWss );
704 		notifyPropertyChanged( OUGOING_WSS, old, outgoingWss );
705 	}
706 
707 	public boolean isDispatchResponseMessages()
708 	{
709 		return getConfig().getDispatchResponseMessages();
710 	}
711 
712 	public void setDispatchResponseMessages( boolean dispatchResponseMessages )
713 	{
714 		boolean old = isDispatchResponseMessages();
715 		getConfig().setDispatchResponseMessages( dispatchResponseMessages );
716 		notifyPropertyChanged( "dispatchResponseMessages", old, dispatchResponseMessages );
717 	}
718 
719 	public List<WsdlOperation> getMockedOperations()
720 	{
721 		List<WsdlOperation> result = new ArrayList<WsdlOperation>();
722 
723 		for( WsdlMockOperation mockOperation : mockOperations )
724 			result.add( mockOperation.getOperation() );
725 
726 		return result;
727 	}
728 
729 	public void setDocroot( String docroot )
730 	{
731 		docrootProperty.set( docroot, true );
732 	}
733 
734 	public String getDocroot()
735 	{
736 		return docrootProperty.get();
737 	}
738 
739 	@Override
740 	public void resolve( ResolveContext<?> context )
741 	{
742 		super.resolve( context );
743 		docrootProperty.resolveFile( context, "Missing MockService docroot" );
744 	}
745 
746 	public void replace( WsdlMockOperation mockOperation, MockOperationConfig reloadedMockOperation )
747 	{
748 		int ix = mockOperations.indexOf( mockOperation );
749 		if( ix == -1 )
750 			throw new RuntimeException( "Unkonws MockOperation specified to removeMockOperation" );
751 
752 		mockOperations.remove( ix );
753 		fireMockOperationRemoved( mockOperation );
754 		mockOperation.release();
755 		getConfig().removeMockOperation( ix );
756 
757 		MockOperationConfig newConfig = ( MockOperationConfig )getConfig().insertNewMockOperation( ix ).set(
758 				reloadedMockOperation ).changeType( MockOperationConfig.type );
759 		WsdlMockOperation newOperation = new WsdlMockOperation( this, newConfig );
760 		mockOperations.add( ix, newOperation );
761 		newOperation.afterLoad();
762 		fireMockOperationAdded( newOperation );
763 	}
764 
765 	public void export( File file )
766 	{
767 		try
768 		{
769 			this.getConfig().newCursor().save( file );
770 		}
771 		catch( IOException e )
772 		{
773 			e.printStackTrace();
774 		}
775 	}
776 
777 	public void importMockOperation( File file )
778 	{
779 		MockOperationConfig mockOperationNewConfig = null;
780 
781 		if( !file.exists() )
782 		{
783 			UISupport.showErrorMessage( "Error loading mock operation." );
784 			return;
785 		}
786 
787 		try
788 		{
789 			mockOperationNewConfig = MockOperationDocumentConfig.Factory.parse( file ).getMockOperation();
790 		}
791 		catch( Exception e )
792 		{
793 			SoapUI.logError( e );
794 		}
795 
796 		if( mockOperationNewConfig != null )
797 		{
798 			MockOperationConfig newConfig = ( MockOperationConfig )getConfig().addNewMockOperation().set(
799 					mockOperationNewConfig ).changeType( TestCaseConfig.type );
800 			WsdlMockOperation newMockOperation = new WsdlMockOperation( this, newConfig );
801 			ModelSupport.unsetIds( newMockOperation );
802 			newMockOperation.afterLoad();
803 			mockOperations.add( newMockOperation );
804 			fireMockOperationAdded( newMockOperation );
805 
806 			resolveImportedMockOperation( newMockOperation );
807 
808 		}
809 		else
810 		{
811 			UISupport.showErrorMessage( "Not valild mock operation xml" );
812 		}
813 	}
814 
815 	private void resolveImportedMockOperation( WsdlMockOperation mockOperation )
816 	{
817 		ResolveDialog resolver = new ResolveDialog( "Validate MockOperation", "Checks MockOperation for inconsistencies",
818 				null );
819 		resolver.setShowOkMessage( false );
820 		resolver.resolve( mockOperation );
821 	}
822 
823 	public String toString()
824 	{
825 		return getName();
826 	}
827 
828 	public String getMockServiceEndpoint()
829 	{
830 		return mockServiceEndpoint;
831 	}
832 
833 	public void setMockServiceEndpoint( String mockServiceEndpoint )
834 	{
835 		this.mockServiceEndpoint = mockServiceEndpoint;
836 	}
837 
838 	public String getLocalMockServiceEndpoint()
839 	{
840 		if( mockServiceEndpoint != null )
841 			return mockServiceEndpoint + getPath();
842 
843 		String host = getHost();
844 		if( StringUtils.isNullOrEmpty( host ) )
845 			host = "127.0.0.1";
846 
847 		int port = ( int )( getSettings().getBoolean( SSLSettings.ENABLE_MOCK_SSL ) ? getSettings().getLong(
848 				SSLSettings.MOCK_PORT, 443 ) : getPort() );
849 
850 		return getProtocol() + host + ":" + port + getPath();
851 	}
852 
853 }