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.actions.monitor;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.WsdlInterface;
17  import com.eviware.soapui.impl.wsdl.WsdlProject;
18  import com.eviware.soapui.impl.wsdl.panels.monitor.SoapMonitorDesktopPanel;
19  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
20  import com.eviware.soapui.model.iface.Interface;
21  import com.eviware.soapui.model.settings.Settings;
22  import com.eviware.soapui.model.support.ModelSupport;
23  import com.eviware.soapui.support.StringUtils;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
26  import com.eviware.soapui.support.types.StringList;
27  import com.eviware.x.form.XForm;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.XFormDialogBuilder;
30  import com.eviware.x.form.XFormFactory;
31  import com.eviware.x.form.XFormField;
32  import com.eviware.x.form.XFormFieldListener;
33  import com.eviware.x.form.support.ADialogBuilder;
34  import com.eviware.x.form.support.AField;
35  import com.eviware.x.form.support.AForm;
36  import com.eviware.x.form.support.APage;
37  import com.eviware.x.form.support.AField.AFieldType;
38  
39  public class SoapMonitorAction extends AbstractSoapUIAction<WsdlProject>
40  {
41  	private static final String HTTPS_PROTOCOL = "https://";
42  	private static final String HTTP_TUNNEL = "HTTP Tunnel";
43  	private static final String HTTP_PROXY = "HTTP Proxy";
44  	private XFormDialog dialog;
45  	private XForm generalForm;
46  	private XForm securityForm;
47  
48  	public SoapMonitorAction()
49  	{
50  		super( "Launch HTTP Monitor", "Launches a HTTP traffic monitor for this project" );
51  	}
52  
53  	public void perform( WsdlProject project, Object param )
54  	{
55  		// if( project.getInterfaceCount() == 0 )
56  		// {
57  		// UISupport.showErrorMessage( "Missing interfaces to monitor" );
58  		// return;
59  		// }
60  
61  		if( dialog == null )
62  		{
63  			dialog = ADialogBuilder.buildTabbedDialog( WizardForm.class, null );
64  			dialog.setSize( 650, 500 );
65  		}
66  
67  		Settings settings = project.getSettings();
68  
69  		StringList endpoints = new StringList();
70  		endpoints.add( null );
71  
72  		for( Interface iface : ModelSupport.getChildren( project, WsdlInterface.class ) )
73  		{
74  			endpoints.addAll( iface.getEndpoints() );
75  		}
76  
77  		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Launch LoadTestRunner" );
78  		generalForm = builder.createForm( "General" );
79  
80  		dialog.setIntValue( LaunchForm.PORT, ( int )settings.getLong( LaunchForm.PORT, 8081 ) );
81  		dialog.setOptions( LaunchForm.REQUEST_WSS, StringUtils.merge( project.getWssContainer().getIncomingWssNames(),
82  				"<none>" ) );
83  		dialog.setOptions( LaunchForm.RESPONSE_WSS, StringUtils.merge( project.getWssContainer().getIncomingWssNames(),
84  				"<none>" ) );
85  		dialog.setValue( LaunchForm.SETSSLMON, settings.getString( LaunchForm.SETSSLMON, "" ).length() > 0 ? settings
86  				.getString( LaunchForm.SETSSLMON, "" ) : HTTPS_PROTOCOL );
87  		dialog.setOptions( LaunchForm.SSLORHTTP, new String[] { HTTP_TUNNEL, HTTP_PROXY } );
88  
89  		dialog
90  				.setValue( SecurityTabForm.SSLTUNNEL_KEYSTORE, settings.getString( SecurityTabForm.SSLTUNNEL_KEYSTORE, "" ) );
91  		dialog
92  				.setValue( SecurityTabForm.SSLTUNNEL_PASSWORD, settings.getString( SecurityTabForm.SSLTUNNEL_PASSWORD, "" ) );
93  		dialog.setValue( SecurityTabForm.SSLTUNNEL_KEYPASSWORD, settings.getString(
94  				SecurityTabForm.SSLTUNNEL_KEYPASSWORD, "" ) );
95  		dialog.setValue( SecurityTabForm.SSLTUNNEL_TRUSTSTORE, settings.getString( SecurityTabForm.SSLTUNNEL_TRUSTSTORE,
96  				"" ) );
97  		dialog.setValue( SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, settings.getString(
98  				SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, "" ) );
99  		dialog.setBooleanValue( LaunchForm.SSLTUNNEL_REUSESTATE, settings.getBoolean( LaunchForm.SSLTUNNEL_REUSESTATE ) );
100 		dialog.setValue( LaunchForm.SET_CONTENT_TYPES, settings.getString( LaunchForm.SET_CONTENT_TYPES,
101 				defaultContentTypes() ) );
102 		dialog.setValue( SecurityTabForm.SSLTUNNEL_KEYSTOREPATH, settings.getString(
103 				SecurityTabForm.SSLTUNNEL_KEYSTOREPATH, "" ) );
104 		dialog.setValue( SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD, settings.getString(
105 				SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD, "" ) );
106 
107 		XFormField sslOrHttp = dialog.getFormField( LaunchForm.SSLORHTTP );
108 		sslOrHttp.setValue( HTTP_PROXY );
109 		setDialogState( HTTP_PROXY );
110 		sslOrHttp.addFormFieldListener( new XFormFieldListener()
111 		{
112 
113 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
114 			{
115 				setDialogState( newValue );
116 			}
117 
118 		} );
119 
120 		if( dialog.show() )
121 		{
122 			try
123 			{
124 				UISupport.setHourglassCursor();
125 
126 				int listenPort = dialog.getIntValue( LaunchForm.PORT, 8080 );
127 				settings.setLong( LaunchForm.PORT, listenPort );
128 
129 				settings.setString( LaunchForm.SETSSLMON, dialog.getValue( LaunchForm.SETSSLMON ) );
130 
131 				settings.setString( SecurityTabForm.SSLTUNNEL_KEYSTORE, dialog
132 						.getValue( SecurityTabForm.SSLTUNNEL_KEYSTORE ) );
133 				settings.setString( SecurityTabForm.SSLTUNNEL_PASSWORD, dialog
134 						.getValue( SecurityTabForm.SSLTUNNEL_PASSWORD ) );
135 				settings.setString( SecurityTabForm.SSLTUNNEL_KEYPASSWORD, dialog
136 						.getValue( SecurityTabForm.SSLTUNNEL_KEYPASSWORD ) );
137 				settings.setString( SecurityTabForm.SSLTUNNEL_TRUSTSTORE, dialog
138 						.getValue( SecurityTabForm.SSLTUNNEL_TRUSTSTORE ) );
139 				settings.setString( SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, dialog
140 						.getValue( SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD ) );
141 				settings.setString( LaunchForm.SSLTUNNEL_REUSESTATE, dialog.getValue( LaunchForm.SSLTUNNEL_REUSESTATE ) );
142 				settings.setString( SecurityTabForm.SSLTUNNEL_KEYSTOREPATH, dialog
143 						.getValue( SecurityTabForm.SSLTUNNEL_KEYSTOREPATH ) );
144 				if( dialog.getValue( LaunchForm.SET_CONTENT_TYPES ) != null
145 						&& dialog.getValue( LaunchForm.SET_CONTENT_TYPES ).trim().equals( "" ) )
146 				{
147 					settings.setString( LaunchForm.SET_CONTENT_TYPES, defaultContentTypes() );
148 				}
149 				else
150 				{
151 					settings.setString( LaunchForm.SET_CONTENT_TYPES, dialog.getValue( LaunchForm.SET_CONTENT_TYPES ) );
152 				}
153 
154 				settings.setString( SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD, dialog
155 						.getValue( SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD ) );
156 
157 				// load all interfaces in project
158 				for( Interface iface : project.getInterfaceList() )
159 				{
160 					iface.getDefinitionContext().loadIfNecessary();
161 				}
162 
163 				if( HTTP_PROXY.equals( dialog.getValue( LaunchForm.SSLORHTTP ) ) )
164 				{
165 					openSoapMonitor( project, listenPort, dialog.getValue( LaunchForm.REQUEST_WSS ), dialog
166 							.getValue( LaunchForm.RESPONSE_WSS ), dialog.getBooleanValue( LaunchForm.SETASPROXY ), null );
167 				}
168 				else
169 				{
170 					openSoapMonitor( project, listenPort, dialog.getValue( LaunchForm.REQUEST_WSS ), dialog
171 							.getValue( LaunchForm.RESPONSE_WSS ), dialog.getBooleanValue( LaunchForm.SETASPROXY ), dialog
172 							.getValue( LaunchForm.SETSSLMON ) );
173 				}
174 			}
175 			catch( Exception e )
176 			{
177 				SoapUI.logError( e );
178 			}
179 			finally
180 			{
181 				UISupport.resetCursor();
182 			}
183 		}
184 	}
185 
186 	public static String defaultContentTypes()
187 	{
188 		return "*/html, */xml, */soap+xml, */json, */x-json, */javascript, */x-amf";
189 	}
190 
191 	protected void openSoapMonitor( WsdlProject target, int listenPort, String incomingRequestWss,
192 			String incomingResponseWss, boolean setAsProxy, String sslEndpoint )
193 	{
194 		if( sslEndpoint == null )
195 		{
196 			UISupport.showDesktopPanel( new SoapMonitorDesktopPanel( target, listenPort, incomingRequestWss,
197 					incomingResponseWss, setAsProxy, null ) );
198 		}
199 		else
200 		{
201 			String ssl = validate( sslEndpoint );
202 			if( ssl == null )
203 			{
204 				UISupport.showErrorMessage( "SSL Monitor needs endpoint." );
205 			}
206 			else
207 			{
208 				UISupport.showDesktopPanel( new SoapMonitorDesktopPanel( target, listenPort, incomingRequestWss,
209 						incomingResponseWss, setAsProxy, ssl ) );
210 			}
211 		}
212 	}
213 
214 	protected String validate( String sslEndpoint )
215 	{
216 		String res = sslEndpoint;
217 		if( res.trim().length() > 0 )
218 		{
219 			return res.trim();
220 		}
221 		return null;
222 	}
223 
224 	private void setDialogState( String newValue )
225 	{
226 		if( HTTP_PROXY.equals( newValue ) )
227 		{
228 			dialog.getFormField( LaunchForm.SETSSLMON ).setEnabled( false );
229 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTORE ).setEnabled( false );
230 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_PASSWORD ).setEnabled( false );
231 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYPASSWORD ).setEnabled( false );
232 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_TRUSTSTORE ).setEnabled( false );
233 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD ).setEnabled( false );
234 			dialog.getFormField( LaunchForm.SSLTUNNEL_REUSESTATE ).setEnabled( false );
235 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTOREPATH ).setEnabled( false );
236 			dialog.getFormField( LaunchForm.SET_CONTENT_TYPES ).setEnabled( true );
237 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD ).setEnabled( false );
238 
239 			dialog.getFormField( LaunchForm.SETASPROXY ).setEnabled( true );
240 			dialog.getFormField( LaunchForm.REQUEST_WSS ).setEnabled( true );
241 			dialog.getFormField( LaunchForm.RESPONSE_WSS ).setEnabled( true );
242 		}
243 		else
244 		{
245 			dialog.getFormField( LaunchForm.SETSSLMON ).setEnabled( true );
246 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTORE ).setEnabled( true );
247 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_PASSWORD ).setEnabled( true );
248 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYPASSWORD ).setEnabled( true );
249 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_TRUSTSTORE ).setEnabled( true );
250 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD ).setEnabled( true );
251 			dialog.getFormField( LaunchForm.SSLTUNNEL_REUSESTATE ).setEnabled( true );
252 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTOREPATH ).setEnabled( true );
253 			dialog.getFormField( LaunchForm.SET_CONTENT_TYPES ).setEnabled( true );
254 			dialog.getFormField( SecurityTabForm.SSLTUNNEL_KEYSTOREPASSWORD ).setEnabled( true );
255 
256 			dialog.getFormField( LaunchForm.SETASPROXY ).setEnabled( false );
257 			dialog.getFormField( LaunchForm.REQUEST_WSS ).setEnabled( false );
258 			dialog.getFormField( LaunchForm.RESPONSE_WSS ).setEnabled( false );
259 		}
260 	}
261 
262 	@AForm( description = "Specify HTTP Monitor settings", name = "General Options", helpUrl = HelpUrls.SOAPMONITOR_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
263 	private interface WizardForm
264 	{
265 		@APage( name = "General" )
266 		public final static LaunchForm general = null;
267 
268 		@APage( name = "Security" )
269 		public final static SecurityTabForm security = null;
270 	}
271 
272 	@AForm( description = "Specify HTTP Monitor settings", name = "Launch HTTP Monitor", helpUrl = HelpUrls.SOAPMONITOR_HELP_URL )
273 	public interface LaunchForm
274 	{
275 		@AField( description = "SSL tunnel or HTTP proxy", name = "Choose one:", type = AFieldType.RADIOGROUP )
276 		public final static String SSLORHTTP = "Choose one:";
277 
278 		@AField( description = "The local port to listen on", name = "Port", type = AFieldType.INT )
279 		public final static String PORT = "Port";
280 
281 		@AField( description = "The Incoming WSS configuration to use for processing requests", name = "Incoming Request WSS", type = AFieldType.ENUMERATION )
282 		public final static String REQUEST_WSS = "Incoming Request WSS";
283 
284 		@AField( description = "The Outgoing WSS configuration to use for processing responses", name = "Incoming Response WSS", type = AFieldType.ENUMERATION )
285 		public final static String RESPONSE_WSS = "Incoming Response WSS";
286 
287 		@AField( description = "Set as Global Proxy", name = "Set as Proxy", type = AFieldType.BOOLEAN )
288 		public final static String SETASPROXY = "Set as Proxy";
289 
290 		@AField( description = "Set endpoint", name = "Set endpoint for HTTP Tunnel:", type = AFieldType.STRING )
291 		public final static String SETSSLMON = "Set endpoint for HTTP Tunnel:";
292 
293 		@AField( description = "Keep request state", name = "Reuse request state", type = AFieldType.BOOLEAN )
294 		public final static String SSLTUNNEL_REUSESTATE = "Reuse request state";
295 
296 		@AField( description = "Content types to monitor, if blank default types will be set!", name = "Content types to monitor", type = AFieldType.STRINGAREA )
297 		public final static String SET_CONTENT_TYPES = "Content types to monitor";
298 
299 	}
300 
301 	@AForm( description = "Specify HTTP tunel security settings", name = "HTTP tunel security", helpUrl = HelpUrls.SOAPMONITOR_HELP_URL )
302 	public interface SecurityTabForm
303 	{
304 		@AField( description = "Set SSL Tunnel KeyStore", name = "HTTP tunnel - KeyStore", type = AFieldType.FILE )
305 		public final static String SSLTUNNEL_KEYSTORE = "HTTP tunnel - KeyStore";
306 
307 		@AField( description = "Set SSL Tunnel Password", name = "HTTP tunnel - Password", type = AFieldType.PASSWORD )
308 		public final static String SSLTUNNEL_PASSWORD = "HTTP tunnel - Password";
309 
310 		@AField( description = "Set SSL Tunnel KeyPassword", name = "HTTP tunnel - KeyPassword", type = AFieldType.PASSWORD )
311 		public final static String SSLTUNNEL_KEYPASSWORD = "HTTP tunnel - KeyPassword";
312 
313 		@AField( description = "Set SSL Tunnel TrustStore", name = "HTTP tunnel - TrustStore", type = AFieldType.FILE )
314 		public final static String SSLTUNNEL_TRUSTSTORE = "HTTP tunnel - TrustStore";
315 
316 		@AField( description = "Set SSL Tunnel TrustStore Password", name = "HTTP tunnel - TrustStore Password", type = AFieldType.PASSWORD )
317 		public final static String SSLTUNNEL_TRUSTSTORE_PASSWORD = "HTTP tunnel - TrustStore Password";
318 
319 		@AField( description = "Set SSL Client Key Store", name = "HTTP tunnel - Set SSL Client Key Store path", type = AFieldType.FILE )
320 		public final static String SSLTUNNEL_KEYSTOREPATH = "HTTP tunnel - Set SSL Client Key Store path";
321 
322 		@AField( description = "Set SSL Client Key Store Password", name = "HTTP tunnel - Set SSL Client Key Store Password", type = AFieldType.PASSWORD )
323 		public final static String SSLTUNNEL_KEYSTOREPASSWORD = "HTTP tunnel - Set SSL Client Key Store Password";
324 	}
325 }