View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.monitor;
14  
15  import org.mortbay.jetty.bio.SocketConnector;
16  import org.mortbay.jetty.security.SslSocketConnector;
17  import org.mortbay.jetty.servlet.Context;
18  import org.mortbay.jetty.servlet.ServletHolder;
19  import org.mortbay.thread.QueuedThreadPool;
20  
21  import com.eviware.soapui.impl.wsdl.actions.monitor.SoapMonitorAction;
22  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.ProxyServlet;
23  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.Server;
24  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.TunnelServlet;
25  import com.eviware.soapui.model.settings.Settings;
26  import com.eviware.soapui.support.UISupport;
27  
28  public class SoapMonitorEngineImpl implements SoapMonitorEngine
29  {
30  
31  	private static final String ROOT = "/";
32  	private static final String HTTP = "http://";
33  	private static final String HTTPS = "https://";
34  	Server server = new Server();
35  	SocketConnector connector = new SocketConnector();
36  	private SslSocketConnector sslConnector;
37  	private String sslEndpoint = null;
38  	private boolean proxyOrTunnel = true;
39  
40  	public boolean isRunning()
41  	{
42  		return server.isRunning();
43  	}
44  
45  	public void start( SoapMonitor soapMonitor, int localPort )
46  	{
47  
48  		Settings settings = soapMonitor.getProject().getSettings();
49  		QueuedThreadPool threadPool = new QueuedThreadPool();
50  		threadPool.setMaxThreads( 100 );
51  		server.setThreadPool( threadPool );
52  		Context context = new Context( server, ROOT, 0 );
53  
54  		if( sslEndpoint != null )
55  		{
56  			if( sslEndpoint.startsWith( HTTPS ) )
57  			{
58  				sslConnector = new SslSocketConnector();
59  				sslConnector.setKeystore( settings.getString( SoapMonitorAction.LaunchForm.SSLTUNNEL_KEYSTORE, "JKS" ) );
60  				sslConnector.setPassword( settings.getString( SoapMonitorAction.LaunchForm.SSLTUNNEL_PASSWORD, "" ) );
61  				sslConnector.setKeyPassword( settings.getString( SoapMonitorAction.LaunchForm.SSLTUNNEL_KEYPASSWORD, "" ) );
62  				sslConnector.setTruststore( settings.getString( SoapMonitorAction.LaunchForm.SSLTUNNEL_TRUSTSTORE, "JKS" ) );
63  				sslConnector.setTrustPassword( settings.getString(
64  						SoapMonitorAction.LaunchForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, "" ) );
65  				sslConnector.setNeedClientAuth( false );
66  				sslConnector.setMaxIdleTime( 30000 );
67  				sslConnector.setPort( localPort );
68  
69  				server.addConnector( sslConnector );
70  				context.addServlet( new ServletHolder( new TunnelServlet( soapMonitor, sslEndpoint ) ), ROOT );
71  			}
72  			else
73  			{
74  				if( sslEndpoint.startsWith( HTTP ) )
75  				{
76  					connector.setPort( localPort );
77  					server.addConnector( connector );
78  					context.addServlet( new ServletHolder( new TunnelServlet( soapMonitor, sslEndpoint ) ), ROOT );
79  				}
80  				else
81  				{
82  					UISupport.showErrorMessage( "Unsupported/unknown protocol tunnel will not start" );
83  					return;
84  				}
85  			}
86  			proxyOrTunnel = false;
87  		}
88  		else
89  		{
90  			proxyOrTunnel = true;
91  			connector.setPort( localPort );
92  			server.addConnector( connector );
93  			context.addServlet( new ServletHolder( new ProxyServlet( soapMonitor ) ), ROOT );
94  		}
95  		try
96  		{
97  			server.start();
98  		}
99  		catch( Exception e )
100 		{
101 			UISupport.showErrorMessage( "Error starting monitor: " + e.getMessage() );
102 		}
103 
104 	}
105 
106 	public void stop()
107 	{
108 
109 		try
110 		{
111 			if( server != null )
112 			{
113 				server.stop();
114 			}
115 		}
116 		catch( Exception e )
117 		{
118 			e.printStackTrace();
119 		}
120 		finally
121 		{
122 			if( server != null )
123 			{
124 				server.destroy();
125 			}
126 		}
127 
128 	}
129 
130 	protected void setSslEndpoint( String sslEndpoint )
131 	{
132 		this.sslEndpoint = sslEndpoint;
133 	}
134 
135 	/*
136 	 * @return true if proxy, false if ssl tunnel (non-Javadoc)
137 	 * 
138 	 * @see com.eviware.soapui.impl.wsdl.monitor.SoapMonitorEngine#isProxy()
139 	 */
140 	public boolean isProxy()
141 	{
142 		return proxyOrTunnel;
143 	}
144 
145 }