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