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