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