View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  import java.io.StringWriter;
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import javax.swing.ImageIcon;
22  import javax.swing.JOptionPane;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.w3c.dom.Document;
27  
28  import com.eviware.soapui.SoapUI;
29  import com.eviware.soapui.config.CallConfig;
30  import com.eviware.soapui.config.CredentialsConfig;
31  import com.eviware.soapui.impl.wsdl.actions.request.AddToTestCaseAction;
32  import com.eviware.soapui.impl.wsdl.actions.request.CloneRequestAction;
33  import com.eviware.soapui.impl.wsdl.actions.request.DeleteRequestAction;
34  import com.eviware.soapui.impl.wsdl.actions.request.RenameRequestAction;
35  import com.eviware.soapui.impl.wsdl.panels.request.WsdlRequestPanelBuilder;
36  import com.eviware.soapui.model.PanelBuilder;
37  import com.eviware.soapui.model.iface.Interface;
38  import com.eviware.soapui.model.iface.Operation;
39  import com.eviware.soapui.model.iface.Request;
40  import com.eviware.soapui.model.iface.Submit;
41  import com.eviware.soapui.model.iface.SubmitListener;
42  import com.eviware.soapui.model.support.InterfaceListenerAdapter;
43  import com.eviware.soapui.model.tree.SoapUITreeNode;
44  import com.eviware.soapui.model.tree.nodes.RequestTreeNode;
45  import com.eviware.soapui.support.XmlUtils;
46  
47  /***
48   * Request implementation holding a SOAP request
49   * 
50   * @author Ole.Matzura
51   */
52  
53  public class WsdlRequest extends AbstractWsdlModelItem implements Request
54  {
55  	public static final String RESPONSE_PROPERTY = WsdlRequest.class.getName() + "@response";
56     
57     private CallConfig requestConfig;
58     private final WsdlOperation operation;
59     private PanelBuilder wsdlRequestPanelBuilder;
60     private String response;
61     private final static Log log = LogFactory.getLog( WsdlRequest.class );
62     private IconManager iconManager;
63     private Set<SubmitListener> listeners = new HashSet<SubmitListener>();
64  
65  	private WsdlSubmit submitter;
66  
67  	private InternalInterfaceListener internalInterfaceListener;
68  
69     public WsdlRequest( WsdlOperation operation, CallConfig callConfig )
70     {
71        this.operation = operation;
72        this.requestConfig = callConfig;
73        
74        initActions();
75        initEndpoints();
76        initIcons();
77        
78        // ensure encoding
79        if( requestConfig.getEncoding() == null || requestConfig.getEncoding().length() == 0 )
80        {
81           requestConfig.setEncoding( "UTF-8" );
82        }
83        
84        addSubmitListener( iconManager );
85      
86        internalInterfaceListener = new InternalInterfaceListener();      
87     }
88     
89     public void updateConfig(CallConfig request)
90  	{
91  		this.requestConfig = request;
92  	}
93     
94     protected PanelBuilder initPanelBuilder()
95     {
96        return new WsdlRequestPanelBuilder( this );
97     }
98     
99     protected IconManager getIconManager()
100    {
101    	return iconManager;
102    }
103 
104    protected void initIcons()
105    {
106    	iconManager = new IconManager();
107    }
108 
109    protected void initEndpoints()
110    {
111       String[] endpoints = operation.getInterface().getEndpoints();
112       if( getEndpoint() == null && endpoints.length > 0 )
113       {
114          setEndpoint( endpoints[0] );
115       }
116    }
117 
118    protected void initActions()
119    {
120       addAction( new CloneRequestAction( this ));
121       addAction( new RenameRequestAction( this ));
122       addAction( new DeleteRequestAction( this ));
123       addAction( new AddToTestCaseAction( this ));
124    }
125 
126    public PanelBuilder getPanelBuilder()
127    {
128       if( wsdlRequestPanelBuilder == null )
129          wsdlRequestPanelBuilder = initPanelBuilder();
130       
131       return wsdlRequestPanelBuilder;
132    }
133    
134    public String getRequestContent()
135    {
136      return requestConfig.getRequest();
137    }
138 
139    public void setEndpoint(String endpoint)
140    {
141       String old = getEndpoint();
142       requestConfig.setEndpoint( endpoint );
143       notifyPropertyChanged( ENDPOINT_PROPERTY, old, endpoint);
144    }
145 
146    public String getEndpoint()
147    {
148       return requestConfig.getEndpoint();
149    }
150 
151    public String getEncoding()
152    {
153       return requestConfig.getEncoding();
154    }
155 
156    public void setEncoding(String encoding)
157    {
158       String old = getEncoding();
159       requestConfig.setEncoding( encoding );
160       notifyPropertyChanged( ENCODING_PROPERTY, old, encoding );
161    }
162    
163    public String getResponseContent()
164    {
165       return response;
166    }
167 
168    public String getName()
169    {
170       try
171       {
172          return requestConfig.getName();
173       }
174       catch (Exception e)
175       {
176          return null;
177       }
178    }
179 
180    public void setName(String name)
181    {
182       String old = getName();
183       requestConfig.setName( name );
184       notifyPropertyChanged( NAME_PROPERTY, old, name );
185    }
186 
187    public Operation getOperation()
188    {
189       return operation;
190    }
191 
192    public void setRequest(String request)
193    {
194       String old = getRequestContent();
195       requestConfig.setRequest( request );
196       notifyPropertyChanged( REQUEST_PROPERTY, old, request );
197    }
198    
199    protected CallConfig getConfig()
200    {
201       return requestConfig;
202    }
203 
204    public void setResponse(String response)
205    {
206       String oldResponse = getResponseContent();
207       
208       // try to pretty-print
209       try
210       {
211          Document dom = XmlUtils.parseXml( response.trim() );
212          StringWriter writer = new StringWriter();
213          XmlUtils.serializePretty( dom, writer );
214          this.response = writer.toString();
215       }
216       catch( Exception e )
217       {
218          this.response = response;
219       }
220       
221       // notify property-listeners
222       notifyPropertyChanged( RESPONSE_PROPERTY, oldResponse, response );
223    }
224 
225    public ImageIcon getIcon()
226    {
227    	return iconManager.getIcon();
228    }
229 
230    public String getUsername()
231    {
232    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
233    	if( credentialsConfig == null ) return null;
234    	
235    	return credentialsConfig.getUsername();
236    }
237    
238    public String getPassword()
239    {
240    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
241    	if( credentialsConfig == null ) return null;
242    	
243    	return credentialsConfig.getPassword();
244    }
245    
246    public String getDomain()
247    {
248    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
249    	if( credentialsConfig == null ) return null;
250    	
251    	return credentialsConfig.getDomain();
252    }
253    
254    public void setUsername( String username )
255    {
256    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
257    	if( credentialsConfig == null ) 
258    		credentialsConfig = requestConfig.addNewCredentials();
259    	
260    	credentialsConfig.setUsername( username );
261    }
262    
263    public void setPassword( String password )
264    {
265    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
266    	if( credentialsConfig == null ) 
267    		credentialsConfig = requestConfig.addNewCredentials();
268    	
269    	credentialsConfig.setPassword( password );
270    }
271    
272    public void setDomain( String domain )
273    {
274    	CredentialsConfig credentialsConfig = requestConfig.getCredentials();
275    	if( credentialsConfig == null ) 
276    		credentialsConfig = requestConfig.addNewCredentials();
277    	
278    	credentialsConfig.setDomain( domain );
279    }
280    
281    protected SoapUITreeNode createTreeNode()
282    {
283       return new RequestTreeNode( this );
284    }
285 
286    protected class IconManager implements Runnable, SubmitListener
287    {
288       private int index = 0;
289       private boolean stopped = true;
290       private ImageIcon requestIcon;
291       private ImageIcon [] requestExecIcons;
292 
293       public IconManager()
294       {
295          requestIcon = SoapUI.createImageIcon("/request.gif");
296 
297          requestExecIcons = new ImageIcon[] {
298                      SoapUI.createImageIcon("/exec_request_1.gif"), 
299                      SoapUI.createImageIcon("/exec_request_2.gif"),
300                      SoapUI.createImageIcon("/exec_request_3.gif"),
301                      SoapUI.createImageIcon("/exec_request_4.gif")
302                };
303 
304       }
305       
306       public void stopRequestAnimation()
307       {
308          stopped = true;
309       }
310       
311       public int getIndex()
312       {
313          return index;
314       }
315 
316       public boolean isStopped()
317       {
318          return stopped;
319       }
320       
321       public void startRequestAnimation()
322       {
323          stopped = false;
324          Thread iconAnimationThread = new Thread( this );
325          iconAnimationThread.start();
326       }
327       
328       public ImageIcon getRequestIcon()
329       {
330       	return requestIcon;
331       }
332       
333       public ImageIcon getIcon()
334       {
335          if( !iconManager.isStopped())
336          {
337             return requestExecIcons[iconManager.getIndex()];
338          }
339          
340          return requestIcon;
341       }
342 
343       public void run()
344       {
345          while( !stopped )
346          {
347             try
348             {
349                index = index >= requestExecIcons.length-1 ? 0 : index+1;
350                notifyPropertyChanged( ICON_PROPERTY, null, getIcon() );
351                Thread.sleep( 500 );
352             }
353             catch (InterruptedException e)
354             {
355                e.printStackTrace();
356             }
357          }
358          
359          notifyPropertyChanged( ICON_PROPERTY, null, getIcon() );
360       }
361 
362 		public boolean beforeSubmit(Submit submit) {
363 			if( submit.getRequest() == WsdlRequest.this )
364 				startRequestAnimation();
365 	      return true;
366 		}
367 
368 		public void afterSubmit(Submit submit) {
369 			if( submit.getRequest() == WsdlRequest.this )
370 				stopRequestAnimation();
371 		}
372 
373    }
374 
375 	public void addSubmitListener(SubmitListener listener) 
376 	{
377 		listeners.add( listener );
378 	}
379 
380 	public void removeSubmitListener(SubmitListener listener) 
381 	{
382 		listeners.remove( listener );
383 	}
384 
385 	public Submit submit() 
386 	{
387       if( getEndpoint() == null || getEndpoint().trim().length() == 0 )
388       {
389       	JOptionPane.showMessageDialog( SoapUI.getInstance().getFrame(), 
390       			"Missing endpoint for request [" + getName() + "]", "Submit Request", 
391       			JOptionPane.ERROR_MESSAGE );
392       	return null;
393       }
394 		
395 		submitter = new WsdlSubmit( this, 
396 				(SubmitListener[]) listeners.toArray( new SubmitListener[listeners.size()]) );
397 		submitter.submitRequest();
398 		return submitter;
399 	}
400 	
401 	private class InternalInterfaceListener extends InterfaceListenerAdapter implements PropertyChangeListener
402 	{
403 		public InternalInterfaceListener()
404 		{
405 			operation.getInterface().addPropertyChangeListener( this );
406 	      operation.getInterface().addInterfaceListener( this );
407 		}
408 		
409 		/***
410 		 * Listen here so we can remove ourself
411 		 */
412 		
413 		public void requestRemoved(Request request)
414 		{
415 			if( request == WsdlRequest.this )
416 			{
417 				getOperation().getInterface().removeInterfaceListener( this );
418 				getOperation().getInterface().removePropertyChangeListener( this );
419 			}
420 		}
421 		
422 		public void propertyChange(PropertyChangeEvent evt)
423 		{
424 			if( evt.getPropertyName().equals( Interface.ENDPOINT_PROPERTY ))
425 			{
426 				String endpoint = getEndpoint();
427 				if( evt.getOldValue() != null && evt.getOldValue().equals( endpoint ))
428 				{
429 					setEndpoint( (String) evt.getNewValue() );
430 				}
431 			}
432 		}
433 	}
434 }