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.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.BoundedThreadPool;
20  
21  import com.eviware.soapui.impl.wsdl.actions.monitor.SoapMonitorAction;
22  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.TunnelServlet;
23  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.ProxyServlet;
24  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.Server;
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  		BoundedThreadPool threadPool = new BoundedThreadPool();
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  				} else {
80  					UISupport.showErrorMessage("Unsupported/unknown protocol tunnel will not start");
81  					return;
82  				}
83  			}
84  			proxyOrTunnel = false;
85  		}
86  		else
87  		{
88  			proxyOrTunnel = true;
89  			connector.setPort(localPort);
90  			server.addConnector(connector);
91  			context.addServlet(new ServletHolder(new ProxyServlet(soapMonitor)), ROOT);
92  		}
93  		try
94  		{
95  			server.start();
96  		}
97  		catch (Exception e)
98  		{
99  			UISupport.showErrorMessage("Error starting monitor: " + e.getMessage());
100 		}
101 
102 	}
103 
104 	public void stop()
105 	{
106 
107 		try
108 		{
109 			if (server != null)
110 			{
111 				server.stop();
112 			}
113 		}
114 		catch (Exception e)
115 		{
116 			e.printStackTrace();
117 		}
118 		finally
119 		{
120 			if (server != null)
121 			{
122 				server.destroy();
123 			}
124 		}
125 
126 	}
127 
128 	protected void setSslEndpoint(String sslEndpoint)
129 	{
130 		this.sslEndpoint = sslEndpoint;
131 	}
132 
133 	/*
134 	 * @return true if proxy, false if ssl tunnel (non-Javadoc)
135 	 * 
136 	 * @see com.eviware.soapui.impl.wsdl.monitor.SoapMonitorEngine#isProxy()
137 	 */
138 	public boolean isProxy()
139 	{
140 		return proxyOrTunnel;
141 	}
142 
143 }