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.endpoint;
14  
15  import com.eviware.soapui.config.EndpointConfig;
16  import com.eviware.soapui.impl.support.AbstractHttpRequest;
17  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
18  import com.eviware.soapui.impl.support.http.HttpRequestTestStep;
19  import com.eviware.soapui.impl.wsdl.WsdlInterface;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.impl.wsdl.endpoint.DefaultEndpointStrategy.EndpointDefaults;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
25  import com.eviware.soapui.model.iface.Interface;
26  import com.eviware.soapui.model.iface.Operation;
27  import com.eviware.soapui.model.testsuite.TestCase;
28  import com.eviware.soapui.model.testsuite.TestStep;
29  import com.eviware.soapui.model.testsuite.TestSuite;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.components.JXToolBar;
32  import com.eviware.soapui.support.components.MetricsPanel;
33  
34  import javax.swing.*;
35  import javax.swing.event.ListSelectionEvent;
36  import javax.swing.event.ListSelectionListener;
37  import javax.swing.table.AbstractTableModel;
38  import java.awt.*;
39  import java.awt.event.ActionEvent;
40  import java.beans.PropertyChangeEvent;
41  import java.beans.PropertyChangeListener;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.List;
45  
46  public class DefaultEndpointStrategyConfigurationPanel extends JPanel implements PropertyChangeListener
47  {
48  	private EndpointsTableModel tableModel;
49  	private JTable table;
50  	private JButton deleteButton;
51  	private JButton assignButton;
52  	private Interface iface;
53  	private final DefaultEndpointStrategy strategy;
54  
55  	public DefaultEndpointStrategyConfigurationPanel( Interface iface, DefaultEndpointStrategy strategy )
56  	{
57  		super( new BorderLayout() );
58  		
59  		this.iface = iface;
60  		this.strategy = strategy;
61  		
62  		buildUI();
63  		
64  		enableButtons();
65  	}
66  
67  	private void buildUI()
68  	{
69  		tableModel = iface instanceof WsdlInterface ? new WsdlEndpointsTableModel() : new RestEndpointsTableModel();
70  		table = new JTable( tableModel );
71  		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
72  		table.getSelectionModel().addListSelectionListener( new ListSelectionListener()
73  		{
74  			public void valueChanged( ListSelectionEvent e )
75  			{
76  				enableButtons();
77  			}
78  		} );
79  		
80  		for( int c = 0; c < table.getColumnCount(); c++ )
81  			table.getColumnModel().getColumn( c ).setHeaderRenderer( new MetricsPanel.InternalHeaderRenderer( getBackground() ) );
82  		
83  		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 250 );
84  
85  		if( iface instanceof WsdlInterface )
86  		{
87           JComboBox wssTypeCombo = new JComboBox( new String[] {WsdlRequest.PW_TYPE_NONE, WsdlRequest.PW_TYPE_TEXT, WsdlRequest.PW_TYPE_DIGEST});
88           wssTypeCombo.setEditable( true );
89  
90  			table.getColumnModel().getColumn( 4 ).setCellEditor( new DefaultCellEditor( wssTypeCombo) );
91  			table.getColumnModel().getColumn( 6 ).setCellEditor( new OutgoingWssCellEditor( ((WsdlInterface)iface).getProject().getWssContainer() ) );
92  			table.getColumnModel().getColumn( 7 ).setCellEditor( new IncomingWssCellEditor( ((WsdlInterface)iface).getProject().getWssContainer() ) );
93  			table.getColumnModel().getColumn( 8 ).setCellEditor( new DefaultCellEditor(
94  						new JComboBox( new String[] {
95  									EndpointConfig.Mode.OVERRIDE.toString(),
96  									EndpointConfig.Mode.COMPLEMENT.toString(),
97  									EndpointConfig.Mode.COPY.toString()
98  						})) );
99  		}
100 		
101 		table.getTableHeader().setReorderingAllowed( false );
102 		
103 		setBackground( Color.WHITE );
104 		setOpaque( true );
105 		
106 		JScrollPane scrollPane = new JScrollPane( table );
107 		scrollPane.setBorder( BorderFactory.createEmptyBorder( 5, 2, 5, 2 ));
108 		scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
109 		scrollPane.setBackground( Color.WHITE );
110 
111 		add( scrollPane, BorderLayout.CENTER );
112 		add( createButtons(), BorderLayout.NORTH );
113 		
114 		iface.addPropertyChangeListener( "endpoints", this );
115 	}
116 
117 	protected void enableButtons()
118 	{
119 		deleteButton.setEnabled( table.getSelectedRow() != -1 );
120 		assignButton.setEnabled( table.getSelectedRow() != -1 );
121 	}
122 
123 	private Component createButtons()
124 	{
125 		JXToolBar toolbar = UISupport.createToolbar();
126 		
127 		toolbar.addFixed( UISupport.createToolbarButton( new AddAction() ) );
128 		deleteButton = UISupport.createToolbarButton( new DeleteAction() );
129 		toolbar.addFixed( deleteButton );
130 		toolbar.addRelatedGap();
131 		assignButton = new JButton( new AssignAction() );
132 		toolbar.addFixed( assignButton );
133 
134 		toolbar.addGlue();
135 		ShowOnlineHelpAction showOnlineHelpAction = new ShowOnlineHelpAction( HelpUrls.ENDPOINTSEDITOR_HELP_URL );
136 		toolbar.addFixed( UISupport.createToolbarButton( showOnlineHelpAction ) );
137 		
138 		return toolbar;
139 	}
140 
141 	private class AddAction extends AbstractAction
142 	{
143 		public AddAction()
144 		{
145 			putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ));
146 			putValue( Action.SHORT_DESCRIPTION, "Adds a new endpoint to the list" );
147 		}
148 
149 		public void actionPerformed( ActionEvent e )
150 		{
151 			String endpoint = UISupport.prompt( "Enter new endpoint URL", "Add Endpoint", "" );
152 
153 			if( endpoint == null )
154 				return;
155 
156 			tableModel.addEndpoint( endpoint );
157 		}
158 	}
159 
160 	private class AssignAction extends AbstractAction
161 	{
162 		private static final String ALL_REQUESTS = "- All Requests -";
163 		private static final String ALL_TEST_REQUESTS = "- All Test Requests -";
164 		private static final String ALL_REQUESTS_AND_TEST_REQUESTS = "- All Requests and TestRequests -";
165 		private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- All Requests with no endpoint -";
166 
167 		public AssignAction()
168 		{
169 			super( "Assign" );
170 			putValue( Action.SHORT_DESCRIPTION,
171 						"Assigns the selected endpoint to Requests/TestRequests for this Interface" );
172 		}
173 
174 		public void actionPerformed( ActionEvent e )
175 		{
176 			int selectedIndex = table.getSelectedRow();
177 			if( selectedIndex == -1 )
178 			{
179 				Toolkit.getDefaultToolkit().beep();
180 				return;
181 			}
182 
183 			String selectedEndpoint = tableModel.getEndpointAt( selectedIndex );
184 			EndpointDefaults defaults = tableModel.getDefaultsAt( selectedIndex );
185 
186 			List<String> list = new ArrayList<String>( Arrays.asList( iface.getEndpoints() ) );
187 			list.add( 0, ALL_REQUESTS );
188 			list.add( 1, ALL_TEST_REQUESTS );
189 			list.add( 2, ALL_REQUESTS_AND_TEST_REQUESTS );
190 			list.add( 3, ALL_REQUESTS_WITH_NO_ENDPOINT );
191 
192 			Object endpoint = UISupport.prompt( "Assign selected endpoint and authorization to..", "Assign Endpoint", list.toArray(),
193 						ALL_REQUESTS_WITH_NO_ENDPOINT );
194 
195 			if( endpoint == null )
196 				return;
197 
198 			if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT )
199 						|| endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) )
200 			{
201 				for( Operation operation : iface.getAllOperations() )
202 				{
203 					for( int i = 0; i < operation.getRequestCount(); i++ )
204 					{
205 						AbstractHttpRequest request = (AbstractHttpRequest) operation.getRequestAt( i );
206 						String ep = request.getEndpoint();
207 
208 						if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS )
209 									|| ( endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null )
210 									|| ( ep.equals( endpoint ) ) )
211 						{
212 							request.setEndpoint( selectedEndpoint );
213 							
214 							request.setUsername( defaults.getUsername() );
215 							request.setPassword( defaults.getPassword() );
216 							request.setDomain( defaults.getDomain() );
217 
218                      if( request instanceof WsdlRequest )
219                      {
220                         ((WsdlRequest)request).setWssPasswordType( defaults.getWssType() );
221                         ((WsdlRequest)request).setWssTimeToLive( defaults.getWssTimeToLive() );
222                         ((WsdlRequest)request).setOutgoingWss( defaults.getOutgoingWss() );
223                         ((WsdlRequest)request).setIncomingWss( defaults.getIncomingWss() );
224                      }
225 						}
226 					}
227 				}
228 			}
229 
230 			if( endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) || endpoint.equals( ALL_TEST_REQUESTS ) )
231 			{
232 				for( TestSuite testSuite : iface.getProject().getTestSuiteList() )
233 				{
234 					for( TestCase testCase : testSuite.getTestCaseList() )
235 					{
236 						for( TestStep testStep : testCase.getTestStepList() )
237 						{
238 							if( testStep instanceof HttpRequestTestStep )
239 							{
240                         AbstractHttpRequest httpRequest = ((HttpRequestTestStep)testStep).getHttpRequest();
241 								if( httpRequest.getOperation() != null && httpRequest.getOperation().getInterface() == iface )
242 								{
243 									httpRequest.setEndpoint( selectedEndpoint );
244 									
245 									httpRequest.setUsername( defaults.getUsername() );
246 									httpRequest.setPassword( defaults.getPassword() );
247 									httpRequest.setDomain( defaults.getDomain() );
248                            if( httpRequest instanceof WsdlRequest )
249                            {
250                               WsdlTestRequest testRequest = (WsdlTestRequest) httpRequest;
251                               testRequest.setWssPasswordType( defaults.getWssType() );
252                               testRequest.setWssTimeToLive( defaults.getWssTimeToLive() );
253                               testRequest.setOutgoingWss( defaults.getOutgoingWss() );
254                               testRequest.setIncomingWss( defaults.getIncomingWss() );
255                            }
256 								}
257 							}
258 						}
259 					}
260 				}
261 			}
262 		}
263 	}
264 
265 	private class DeleteAction extends AbstractAction
266 	{
267 		public DeleteAction()
268 		{
269 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ));
270 			putValue( Action.SHORT_DESCRIPTION, "Deletes the selected endpoint from the list" );
271 		}
272 
273 		public void actionPerformed( ActionEvent e )
274 		{
275 			int index = table.getSelectedRow();
276 			if( index == -1 )
277 			{
278 				Toolkit.getDefaultToolkit().beep();
279 				return;
280 			}
281 
282 			if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ) )
283 			{
284 				tableModel.removeEndpoint( index );
285 			}
286 		}
287 	}
288 	
289 	private abstract class EndpointsTableModel extends AbstractTableModel
290 	{
291 		public String getEndpointAt( int selectedIndex )
292 		{
293 			return iface.getEndpoints()[selectedIndex];
294 		}
295 		
296 		public EndpointDefaults getDefaultsAt( int selectedIndex )
297 		{
298 			String endpoint = getEndpointAt( selectedIndex );
299 			return strategy.getEndpointDefaults( endpoint );
300 		}
301 
302 		public void addEndpoint( String endpoint )
303 		{
304 			int rowCount = getRowCount();
305 			iface.addEndpoint( endpoint );
306 			
307 			fireTableRowsInserted( rowCount, rowCount );
308 		}
309 		
310 		public void removeEndpoint( int index )
311 		{
312 			String ep = getEndpointAt( index );
313 			iface.removeEndpoint( ep );
314 			fireTableRowsDeleted( index, index );
315 		}
316 
317 		
318 		public int getRowCount()
319 		{
320 			return iface == null ? 0 : iface.getEndpoints().length;
321 		}
322 		
323 		@Override
324 		public boolean isCellEditable( int rowIndex, int columnIndex )
325 		{
326 			return true;
327 		}
328 		
329 		public void refresh()
330 		{
331 			fireTableDataChanged();
332 		}
333 	}
334 	
335 	private class RestEndpointsTableModel extends EndpointsTableModel
336 	{
337 		public int getColumnCount()
338 		{
339 			return 5;
340 		}
341 
342 		@Override
343 		public String getColumnName( int column )
344 		{
345 			switch( column )
346 			{
347 			case 0:
348 				return "Endpoint";
349 			case 1:
350 				return "Username";
351 			case 2:
352 				return "Password";
353 			case 3:
354 				return "Domain";
355 			case 4:
356 				return "Mode";
357 			}
358 
359 			return null;
360 		}
361 
362 		public Object getValueAt( int rowIndex, int columnIndex )
363 		{
364 			String endpoint = getEndpointAt( rowIndex );
365 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
366 
367 			switch( columnIndex )
368 			{
369 			case 0:
370 				return endpoint;
371 			case 1:
372 				return defaults.getUsername();
373 			case 2:
374 				return defaults.getPassword();
375 			case 3:
376 				return defaults.getDomain();
377 			case 4:
378 				return defaults.getMode();
379 			}
380 
381 			return null;
382 		}
383 
384 
385 		@Override
386 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
387 		{
388 			String endpoint = getEndpointAt( rowIndex );
389 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
390 			
391 			if( aValue == null )
392 				aValue = "";
393 			
394 			switch( columnIndex )
395 			{
396 				case 0 :
397 				{
398 //					strategy.changeEndpoint( endpoint, aValue.toString() );
399 					iface.changeEndpoint( endpoint, aValue.toString() );
400 					break;
401 				}
402 				case 1 :
403 				{
404 					defaults.setUsername( aValue.toString() );
405 					break;
406 				}
407 				case 2 :
408 				{
409 					defaults.setPassword( aValue.toString() );
410 					break;
411 				}
412 				case 3 :
413 				{
414 					defaults.setDomain( aValue.toString() );
415 					break;
416 				}
417 				case 4 :
418 				{
419 					defaults.setMode( EndpointConfig.Mode.Enum.forString( aValue.toString() ) );
420 					break;
421 				}
422 			}
423 		}
424 	}
425 
426 	private class WsdlEndpointsTableModel extends EndpointsTableModel
427 	{
428 		public int getColumnCount()
429 		{
430 			return 9;
431 		}
432 
433 		@Override
434 		public String getColumnName( int column )
435 		{
436 			switch( column )
437 			{
438 			case 0:
439 				return "Endpoint";
440 			case 1:
441 				return "Username";
442 			case 2:
443 				return "Password";
444 			case 3:
445 				return "Domain";
446 			case 4:
447 				return "WSS-Type";
448 			case 5:
449 				return "WSS-TimeToLive";
450 			case 6 :
451 				return "Outgoing WSS";
452 			case 7 : 
453 				return "Incoming WSS";
454 			case 8:
455 				return "Mode";
456 			}
457 
458 			return null;
459 		}
460 
461 		public Object getValueAt( int rowIndex, int columnIndex )
462 		{
463 			String endpoint = getEndpointAt( rowIndex );
464 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
465 
466 			switch( columnIndex )
467 			{
468 			case 0:
469 				return endpoint;
470 			case 1:
471 				return defaults.getUsername();
472 			case 2:
473 				return defaults.getPassword();
474 			case 3:
475 				return defaults.getDomain();
476 			case 4:
477 				return defaults.getWssType();
478 			case 5:
479 				return defaults.getWssTimeToLive();
480 			case 6:
481 				return defaults.getOutgoingWss();
482 			case 7:
483 				return defaults.getIncomingWss();
484 			case 8:
485 				return defaults.getMode();
486 			}
487 
488 			return null;
489 		}
490 
491 		@Override
492 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
493 		{
494 			String endpoint = getEndpointAt( rowIndex );
495 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
496 			
497 			if( aValue == null )
498 				aValue = "";
499 			
500 			switch( columnIndex )
501 			{
502 				case 0 :
503 				{
504 //					strategy.changeEndpoint( endpoint, aValue.toString() );
505 					iface.changeEndpoint( endpoint, aValue.toString() );
506 					break;
507 				}
508 				case 1 :
509 				{
510 					defaults.setUsername( aValue.toString() );
511 					break;
512 				}
513 				case 2 :
514 				{
515 					defaults.setPassword( aValue.toString() );
516 					break;
517 				}
518 				case 3 :
519 				{
520 					defaults.setDomain( aValue.toString() );
521 					break;
522 				}
523 				case 4 :
524 				{
525 					defaults.setWssType( aValue.toString() );
526 					break;
527 				}
528 				case 5 :
529 				{
530 					defaults.setWssTimeToLive( aValue.toString() );
531 					break;
532 				}
533 				case 6 :
534 				{
535 					defaults.setOutgoingWss( aValue.toString() );
536 					break;
537 				}
538 				case 7 :
539 				{
540 					defaults.setIncomingWss( aValue.toString() );
541 					break;
542 				}
543 				case 8 :
544 				{
545 					defaults.setMode( EndpointConfig.Mode.Enum.forString( aValue.toString() ) );
546 					break;
547 				}
548 			}
549 		}
550 	}
551 	
552 	private static class IncomingWssCellEditor extends DefaultCellEditor
553 	{
554 		private final WssContainer wssContainer;
555 
556 		public IncomingWssCellEditor( WssContainer wssContainer )
557 		{
558 			super( new JComboBox() );
559 			this.wssContainer = wssContainer;
560 		}
561 
562 		@Override
563 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
564 		{
565 			JComboBox comboBox = ( JComboBox ) super.getTableCellEditorComponent( table, value, isSelected, row, column );
566 			
567 			DefaultComboBoxModel model = new DefaultComboBoxModel( wssContainer.getIncomingWssNames() );
568 			model.addElement( "" );
569 			
570 			comboBox.setModel( model );
571 			
572 			return comboBox;
573 		}
574 	}
575 	
576 	private static class OutgoingWssCellEditor extends DefaultCellEditor
577 	{
578 		private final WssContainer wssContainer;
579 
580 		public OutgoingWssCellEditor( WssContainer wssContainer )
581 		{
582 			super( new JComboBox() );
583 			this.wssContainer = wssContainer;
584 		}
585 
586 		@Override
587 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
588 		{
589 			JComboBox comboBox = ( JComboBox ) super.getTableCellEditorComponent( table, value, isSelected, row, column );
590 			
591 			DefaultComboBoxModel model = new DefaultComboBoxModel( wssContainer.getOutgoingWssNames() );
592 			model.addElement( "" );
593 			
594 			comboBox.setModel( model );
595 			
596 			return comboBox;
597 		}
598 	}
599 	
600 	public void release()
601 	{
602 		iface.removePropertyChangeListener("endpoints", this );
603 	}
604 
605 	public void propertyChange(PropertyChangeEvent evt)
606 	{
607 		tableModel.refresh();
608 	}
609 }