View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 com.eviware.soapui.config.EndpointConfig;
43  import com.eviware.soapui.impl.support.AbstractHttpRequest;
44  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
45  import com.eviware.soapui.impl.support.http.HttpRequestTestStep;
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.model.iface.Interface;
53  import com.eviware.soapui.model.iface.Operation;
54  import com.eviware.soapui.model.testsuite.TestCase;
55  import com.eviware.soapui.model.testsuite.TestStep;
56  import com.eviware.soapui.model.testsuite.TestSuite;
57  import com.eviware.soapui.support.UISupport;
58  import com.eviware.soapui.support.components.JXToolBar;
59  import com.eviware.soapui.support.components.MetricsPanel;
60  
61  public class DefaultEndpointStrategyConfigurationPanel extends JPanel implements PropertyChangeListener
62  {
63  	private EndpointsTableModel tableModel;
64  	private JTable table;
65  	private JButton deleteButton;
66  	private JButton assignButton;
67  	private Interface iface;
68  	private final DefaultEndpointStrategy strategy;
69  
70  	public DefaultEndpointStrategyConfigurationPanel( Interface iface, DefaultEndpointStrategy strategy )
71  	{
72  		super( new BorderLayout() );
73  
74  		this.iface = iface;
75  		this.strategy = strategy;
76  
77  		buildUI();
78  
79  		enableButtons();
80  	}
81  
82  	private void buildUI()
83  	{
84  		tableModel = iface instanceof WsdlInterface ? new WsdlEndpointsTableModel() : new RestEndpointsTableModel();
85  		table = new JTable( tableModel );
86  		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
87  		table.getSelectionModel().addListSelectionListener( new ListSelectionListener()
88  		{
89  			public void valueChanged( ListSelectionEvent e )
90  			{
91  				enableButtons();
92  			}
93  		} );
94  
95  		for( int c = 0; c < table.getColumnCount(); c++ )
96  			table.getColumnModel().getColumn( c ).setHeaderRenderer(
97  					new MetricsPanel.InternalHeaderRenderer( getBackground() ) );
98  
99  		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 250 );
100 
101 		if( iface instanceof WsdlInterface )
102 		{
103 			JComboBox wssTypeCombo = new JComboBox( new String[] { WsdlRequest.PW_TYPE_NONE, WsdlRequest.PW_TYPE_TEXT,
104 					WsdlRequest.PW_TYPE_DIGEST } );
105 			wssTypeCombo.setEditable( true );
106 
107 			table.getColumnModel().getColumn( 4 ).setCellEditor( new DefaultCellEditor( wssTypeCombo ) );
108 			table.getColumnModel().getColumn( 6 ).setCellEditor(
109 					new OutgoingWssCellEditor( ( ( WsdlInterface )iface ).getProject().getWssContainer() ) );
110 			table.getColumnModel().getColumn( 7 ).setCellEditor(
111 					new IncomingWssCellEditor( ( ( WsdlInterface )iface ).getProject().getWssContainer() ) );
112 			table.getColumnModel().getColumn( 8 ).setCellEditor(
113 					new DefaultCellEditor( new JComboBox( new String[] { EndpointConfig.Mode.OVERRIDE.toString(),
114 							EndpointConfig.Mode.COMPLEMENT.toString(), EndpointConfig.Mode.COPY.toString() } ) ) );
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
209 					.toArray(), ALL_REQUESTS_WITH_NO_ENDPOINT );
210 
211 			if( endpoint == null )
212 				return;
213 
214 			if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT )
215 					|| endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) )
216 			{
217 				for( Operation operation : iface.getAllOperations() )
218 				{
219 					for( int i = 0; i < operation.getRequestCount(); i++ )
220 					{
221 						AbstractHttpRequest<?> request = ( AbstractHttpRequest<?> )operation.getRequestAt( i );
222 						String ep = request.getEndpoint();
223 
224 						if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS )
225 								|| ( endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null )
226 								|| ( ep.equals( endpoint ) ) )
227 						{
228 							request.setEndpoint( selectedEndpoint );
229 
230 							request.setUsername( defaults.getUsername() );
231 							request.setPassword( defaults.getPassword() );
232 							request.setDomain( defaults.getDomain() );
233 
234 							if( request instanceof WsdlRequest )
235 							{
236 								( ( WsdlRequest )request ).setWssPasswordType( defaults.getWssType() );
237 								( ( WsdlRequest )request ).setWssTimeToLive( defaults.getWssTimeToLive() );
238 								( ( WsdlRequest )request ).setOutgoingWss( defaults.getOutgoingWss() );
239 								( ( WsdlRequest )request ).setIncomingWss( defaults.getIncomingWss() );
240 							}
241 						}
242 					}
243 				}
244 			}
245 
246 			if( endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) || endpoint.equals( ALL_TEST_REQUESTS ) )
247 			{
248 				for( TestSuite testSuite : iface.getProject().getTestSuiteList() )
249 				{
250 					for( TestCase testCase : testSuite.getTestCaseList() )
251 					{
252 						for( TestStep testStep : testCase.getTestStepList() )
253 						{
254 							if( testStep instanceof HttpRequestTestStep )
255 							{
256 								AbstractHttpRequest<?> httpRequest = ( ( HttpRequestTestStep )testStep ).getHttpRequest();
257 								if( httpRequest.getOperation() != null && httpRequest.getOperation().getInterface() == iface )
258 								{
259 									httpRequest.setEndpoint( selectedEndpoint );
260 
261 									httpRequest.setUsername( defaults.getUsername() );
262 									httpRequest.setPassword( defaults.getPassword() );
263 									httpRequest.setDomain( defaults.getDomain() );
264 									if( httpRequest instanceof WsdlRequest )
265 									{
266 										WsdlTestRequest testRequest = ( WsdlTestRequest )httpRequest;
267 										testRequest.setWssPasswordType( defaults.getWssType() );
268 										testRequest.setWssTimeToLive( defaults.getWssTimeToLive() );
269 										testRequest.setOutgoingWss( defaults.getOutgoingWss() );
270 										testRequest.setIncomingWss( defaults.getIncomingWss() );
271 									}
272 								}
273 							}
274 						}
275 					}
276 				}
277 			}
278 		}
279 	}
280 
281 	private class DeleteAction extends AbstractAction
282 	{
283 		public DeleteAction()
284 		{
285 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
286 			putValue( Action.SHORT_DESCRIPTION, "Deletes the selected endpoint from the list" );
287 		}
288 
289 		public void actionPerformed( ActionEvent e )
290 		{
291 			int index = table.getSelectedRow();
292 			if( index == -1 )
293 			{
294 				Toolkit.getDefaultToolkit().beep();
295 				return;
296 			}
297 
298 			if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ) )
299 			{
300 				tableModel.removeEndpoint( index );
301 			}
302 		}
303 	}
304 
305 	private abstract class EndpointsTableModel extends AbstractTableModel
306 	{
307 		public String getEndpointAt( int selectedIndex )
308 		{
309 			return iface.getEndpoints()[selectedIndex];
310 		}
311 
312 		public EndpointDefaults getDefaultsAt( int selectedIndex )
313 		{
314 			String endpoint = getEndpointAt( selectedIndex );
315 			return strategy.getEndpointDefaults( endpoint );
316 		}
317 
318 		public void addEndpoint( String endpoint )
319 		{
320 			int rowCount = getRowCount();
321 			iface.addEndpoint( endpoint );
322 
323 			fireTableRowsInserted( rowCount, rowCount );
324 		}
325 
326 		public void removeEndpoint( int index )
327 		{
328 			String ep = getEndpointAt( index );
329 			iface.removeEndpoint( ep );
330 			fireTableRowsDeleted( index, index );
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 		@Override
400 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
401 		{
402 			String endpoint = getEndpointAt( rowIndex );
403 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
404 
405 			if( aValue == null )
406 				aValue = "";
407 
408 			switch( columnIndex )
409 			{
410 			case 0 :
411 			{
412 				// strategy.changeEndpoint( endpoint, aValue.toString() );
413 				iface.changeEndpoint( endpoint, aValue.toString() );
414 				break;
415 			}
416 			case 1 :
417 			{
418 				defaults.setUsername( aValue.toString() );
419 				break;
420 			}
421 			case 2 :
422 			{
423 				defaults.setPassword( aValue.toString() );
424 				break;
425 			}
426 			case 3 :
427 			{
428 				defaults.setDomain( aValue.toString() );
429 				break;
430 			}
431 			case 4 :
432 			{
433 				defaults.setMode( EndpointConfig.Mode.Enum.forString( aValue.toString() ) );
434 				break;
435 			}
436 			}
437 		}
438 	}
439 
440 	private class WsdlEndpointsTableModel extends EndpointsTableModel
441 	{
442 		public int getColumnCount()
443 		{
444 			return 9;
445 		}
446 
447 		@Override
448 		public String getColumnName( int column )
449 		{
450 			switch( column )
451 			{
452 			case 0 :
453 				return "Endpoint";
454 			case 1 :
455 				return "Username";
456 			case 2 :
457 				return "Password";
458 			case 3 :
459 				return "Domain";
460 			case 4 :
461 				return "WSS-Type";
462 			case 5 :
463 				return "WSS-TimeToLive";
464 			case 6 :
465 				return "Outgoing WSS";
466 			case 7 :
467 				return "Incoming WSS";
468 			case 8 :
469 				return "Mode";
470 			}
471 
472 			return null;
473 		}
474 
475 		public Object getValueAt( int rowIndex, int columnIndex )
476 		{
477 			String endpoint = getEndpointAt( rowIndex );
478 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
479 
480 			switch( columnIndex )
481 			{
482 			case 0 :
483 				return endpoint;
484 			case 1 :
485 				return defaults.getUsername();
486 			case 2 :
487 				return defaults.getPassword();
488 			case 3 :
489 				return defaults.getDomain();
490 			case 4 :
491 				return defaults.getWssType();
492 			case 5 :
493 				return defaults.getWssTimeToLive();
494 			case 6 :
495 				return defaults.getOutgoingWss();
496 			case 7 :
497 				return defaults.getIncomingWss();
498 			case 8 :
499 				return defaults.getMode();
500 			}
501 
502 			return null;
503 		}
504 
505 		@Override
506 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
507 		{
508 			String endpoint = getEndpointAt( rowIndex );
509 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
510 
511 			if( aValue == null )
512 				aValue = "";
513 
514 			switch( columnIndex )
515 			{
516 			case 0 :
517 			{
518 				// strategy.changeEndpoint( endpoint, aValue.toString() );
519 				iface.changeEndpoint( endpoint, aValue.toString() );
520 				break;
521 			}
522 			case 1 :
523 			{
524 				defaults.setUsername( aValue.toString() );
525 				break;
526 			}
527 			case 2 :
528 			{
529 				defaults.setPassword( aValue.toString() );
530 				break;
531 			}
532 			case 3 :
533 			{
534 				defaults.setDomain( aValue.toString() );
535 				break;
536 			}
537 			case 4 :
538 			{
539 				defaults.setWssType( aValue.toString() );
540 				break;
541 			}
542 			case 5 :
543 			{
544 				defaults.setWssTimeToLive( aValue.toString() );
545 				break;
546 			}
547 			case 6 :
548 			{
549 				defaults.setOutgoingWss( aValue.toString() );
550 				break;
551 			}
552 			case 7 :
553 			{
554 				defaults.setIncomingWss( aValue.toString() );
555 				break;
556 			}
557 			case 8 :
558 			{
559 				defaults.setMode( EndpointConfig.Mode.Enum.forString( aValue.toString() ) );
560 				break;
561 			}
562 			}
563 		}
564 	}
565 
566 	private static class IncomingWssCellEditor extends DefaultCellEditor
567 	{
568 		private final WssContainer wssContainer;
569 
570 		public IncomingWssCellEditor( WssContainer wssContainer )
571 		{
572 			super( new JComboBox() );
573 			this.wssContainer = wssContainer;
574 		}
575 
576 		@Override
577 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
578 		{
579 			JComboBox comboBox = ( JComboBox )super.getTableCellEditorComponent( table, value, isSelected, row, column );
580 
581 			DefaultComboBoxModel model = new DefaultComboBoxModel( wssContainer.getIncomingWssNames() );
582 			model.addElement( "" );
583 
584 			comboBox.setModel( model );
585 
586 			return comboBox;
587 		}
588 	}
589 
590 	private static class OutgoingWssCellEditor extends DefaultCellEditor
591 	{
592 		private final WssContainer wssContainer;
593 
594 		public OutgoingWssCellEditor( WssContainer wssContainer )
595 		{
596 			super( new JComboBox() );
597 			this.wssContainer = wssContainer;
598 		}
599 
600 		@Override
601 		public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
602 		{
603 			JComboBox comboBox = ( JComboBox )super.getTableCellEditorComponent( table, value, isSelected, row, column );
604 
605 			DefaultComboBoxModel model = new DefaultComboBoxModel( wssContainer.getOutgoingWssNames() );
606 			model.addElement( "" );
607 
608 			comboBox.setModel( model );
609 
610 			return comboBox;
611 		}
612 	}
613 
614 	public void release()
615 	{
616 		iface.removePropertyChangeListener( "endpoints", this );
617 	}
618 
619 	public void propertyChange( PropertyChangeEvent evt )
620 	{
621 		tableModel.refresh();
622 	}
623 }