View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.util.ArrayList;
    21  import java.util.Arrays;
    22  import java.util.List;
    23
    24  import javax.swing.AbstractAction;
    25  import javax.swing.Action;
    26  import javax.swing.BorderFactory;
    27  import javax.swing.DefaultCellEditor;
    28  import javax.swing.JButton;
    29  import javax.swing.JComboBox;
    30  import javax.swing.JDialog;
    31  import javax.swing.JPanel;
    32  import javax.swing.JScrollPane;
    33  import javax.swing.ListSelectionModel;
    34  import javax.swing.ScrollPaneConstants;
    35  import javax.swing.event.ListSelectionEvent;
    36  import javax.swing.event.ListSelectionListener;
    37  import javax.swing.table.AbstractTableModel;
    38
    39  import org.apache.log4j.Logger;
    40  import org.jdesktop.swingx.JXTable;
    41
    42  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    43  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    44  import com.eviware.soapui.impl.wsdl.actions.iface.InterfaceEndpointsAction;
    45  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    46  import com.eviware.soapui.impl.wsdl.endpoint.DefaultEndpointStrategy.EndpointDefaults;
    47  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    48  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
    49  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
    50  import com.eviware.soapui.model.iface.Operation;
    51  import com.eviware.soapui.model.iface.Request;
    52  import com.eviware.soapui.model.testsuite.TestCase;
    53  import com.eviware.soapui.model.testsuite.TestStep;
    54  import com.eviware.soapui.model.testsuite.TestSuite;
    55  import com.eviware.soapui.support.UISupport;
    56  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    57  import com.jgoodies.forms.builder.ButtonBarBuilder;
    58
    59  /***
    60   * Manages the service endpoints for a WsdlInterface
    61   *
    62   * @author Ole.Matzura
    63   */
    64
    65  public class DefaultEndpointStrategyConfigureAction extends AbstractSoapUIAction<WsdlInterface>
    66  {
    67  	public static final String SOAPUI_ACTION_ID = "DefaultEndpointStrategyConfigureAction";
    68
    69  	private JDialog dialog;
    70  	private JXTable table;
    71  	private EndpointsTableModel tableModel;
    72  	private WsdlInterface iface;
    73  	private final static Logger log = Logger.getLogger( InterfaceEndpointsAction.class );
    74  	private JButton deleteButton;
    75  	private JButton assignButton;
    76
    77  	public DefaultEndpointStrategyConfigureAction()
    78  	{
    79  		super( "Service Endpoints", "Manage service endpoints available for this interface" );
    80  	}
    81
    82  	private void buildDialog()
    83  	{
    84  		dialog = new JDialog( UISupport.getMainFrame() );
    85  		dialog.setTitle( "Interface Service Endpoints" );
    86
    87  		JPanel contentPanel = new JPanel( new BorderLayout() );
    88  		tableModel = new EndpointsTableModel();
    89  		table = new JXTable( tableModel );
    90  		table.setHorizontalScrollEnabled( true );
    91  		table.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
    92  		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    93  		table.getSelectionModel().addListSelectionListener( new ListSelectionListener()
    94  		{
    95
    96  			public void valueChanged( ListSelectionEvent e )
    97  			{
    98  				enableButtons();
    99  			}
    100 		} );
    101 		table.getColumn( 0 ).setPreferredWidth( 250 );
    102 		table.getColumn( 4 ).setCellEditor( new DefaultCellEditor(
    103 					new JComboBox( new String[] {WsdlRequest.PW_TYPE_NONE, WsdlRequest.PW_TYPE_TEXT, WsdlRequest.PW_TYPE_DIGEST})) );
    104
    105 		JScrollPane scrollPane = new JScrollPane( table );
    106
    107 		scrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ),
    108 					BorderFactory.createLineBorder( Color.GRAY ) ) );
    109
    110 		scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
    111
    112 		contentPanel.add( scrollPane, BorderLayout.CENTER );
    113 		contentPanel.add( createButtons(), BorderLayout.SOUTH );
    114 		Component descriptionPanel = UISupport.buildDescription( "Service Endpoints",
    115 					"Edit available service endpoints for this interface and their defaults in table below", UISupport.OPTIONS_ICON );
    116 		contentPanel.add( descriptionPanel, BorderLayout.NORTH );
    117
    118 		dialog.setContentPane( contentPanel );
    119 		dialog.setSize( 650, 300 );
    120
    121 		dialog.setModal( true );
    122 	}
    123
    124 	protected void enableButtons()
    125 	{
    126 		deleteButton.setEnabled( table.getSelectedRow() != -1 );
    127 		assignButton.setEnabled( table.getSelectedRow() != -1 );
    128 	}
    129
    130 	public void perform( WsdlInterface iface, Object param )
    131 	{
    132 		if( dialog == null )
    133 			buildDialog();
    134
    135 		tableModel.setInterface( iface, ( DefaultEndpointStrategy ) param );
    136
    137 		this.iface = iface;
    138 		enableButtons();
    139 		UISupport.showDialog( dialog );
    140 	}
    141
    142 	private Component createButtons()
    143 	{
    144 		ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
    145 		ShowOnlineHelpAction showOnlineHelpAction = new ShowOnlineHelpAction( HelpUrls.ENDPOINTSEDITOR_HELP_URL );
    146 		builder.addFixed( UISupport.createToolbarButton( showOnlineHelpAction ) );
    147 		builder.addGlue();
    148 		builder.addFixed( new JButton( new AddAction() ) );
    149 		builder.addRelatedGap();
    150 		deleteButton = new JButton( new DeleteAction() );
    151 		builder.addFixed( deleteButton );
    152 		builder.addRelatedGap();
    153 		assignButton = new JButton( new AssignAction() );
    154 		builder.addFixed( assignButton );
    155 		builder.addRelatedGap();
    156 		JButton okButton = new JButton( new OkAction() );
    157 		builder.addFixed( okButton );
    158
    159 		builder
    160 					.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory
    161 								.createMatteBorder( 1, 0, 0, 0, Color.GRAY ), BorderFactory.createMatteBorder( 1, 0, 0, 0,
    162 								Color.WHITE ) ), BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) ) );
    163
    164 		UISupport.initDialogActions( dialog, showOnlineHelpAction, okButton );
    165
    166 		return builder.getPanel();
    167 	}
    168
    169 	private class AddAction extends AbstractAction
    170 	{
    171 		public AddAction()
    172 		{
    173 			super( "Add" );
    174 			putValue( Action.SHORT_DESCRIPTION, "Adds a new endpoint to the list" );
    175 		}
    176
    177 		public void actionPerformed( ActionEvent e )
    178 		{
    179 			String endpoint = UISupport.prompt( "Enter new endpoint URL", "Add Endpoint", "" );
    180
    181 			if( endpoint == null )
    182 				return;
    183
    184 			tableModel.addEndpoint( endpoint );
    185 		}
    186 	}
    187
    188 	private class AssignAction extends AbstractAction
    189 	{
    190 		private static final String ALL_REQUESTS = "- All Requests -";
    191 		private static final String ALL_TEST_REQUESTS = "- All Test Requests -";
    192 		private static final String ALL_REQUESTS_AND_TEST_REQUESTS = "- All Requests and TestRequests -";
    193 		private static final String ALL_REQUESTS_WITH_NO_ENDPOINT = "- All Requests with no endpoint -";
    194
    195 		public AssignAction()
    196 		{
    197 			super( "Assign" );
    198 			putValue( Action.SHORT_DESCRIPTION,
    199 						"Assigns the selected endpoint to Requests/TestRequests for this Interface" );
    200 		}
    201
    202 		public void actionPerformed( ActionEvent e )
    203 		{
    204 			int selectedIndex = table.getSelectedRow();
    205 			if( selectedIndex == -1 )
    206 			{
    207 				Toolkit.getDefaultToolkit().beep();
    208 				return;
    209 			}
    210
    211 			String selectedEndpoint = ( String ) tableModel.getEndpointAt( selectedIndex );
    212
    213 			List<String> list = new ArrayList<String>( Arrays.asList( iface.getEndpoints() ) );
    214 			list.add( 0, ALL_REQUESTS );
    215 			list.add( 1, ALL_TEST_REQUESTS );
    216 			list.add( 2, ALL_REQUESTS_AND_TEST_REQUESTS );
    217 			list.add( 3, ALL_REQUESTS_WITH_NO_ENDPOINT );
    218
    219 			Object endpoint = UISupport.prompt( "Assign selected endpoint to..", "Assign Endpoint", list.toArray(),
    220 						ALL_REQUESTS_WITH_NO_ENDPOINT );
    221
    222 			if( endpoint == null )
    223 				return;
    224
    225 			int changeCount = 0;
    226
    227 			if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT )
    228 						|| endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) )
    229 			{
    230 				for( int c = 0; c < iface.getOperationCount(); c++ )
    231 				{
    232 					Operation operation = iface.getOperationAt( c );
    233 					for( int i = 0; i < operation.getRequestCount(); i++ )
    234 					{
    235 						Request request = operation.getRequestAt( i );
    236 						String ep = request.getEndpoint();
    237
    238 						if( endpoint.equals( ALL_REQUESTS ) || endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS )
    239 									|| ( endpoint.equals( ALL_REQUESTS_WITH_NO_ENDPOINT ) && ep == null )
    240 									|| ( ep.equals( endpoint ) ) )
    241 						{
    242 							request.setEndpoint( selectedEndpoint );
    243 							changeCount++;
    244 						}
    245 					}
    246 				}
    247 			}
    248
    249 			if( endpoint.equals( ALL_REQUESTS_AND_TEST_REQUESTS ) || endpoint.equals( ALL_TEST_REQUESTS ) )
    250 			{
    251 				for( TestSuite testSuite : iface.getProject().getTestSuites() )
    252 				{
    253 					for( TestCase testCase : testSuite.getTestCaseList() )
    254 					{
    255 						for( TestStep testStep : testCase.getTestStepList() )
    256 						{
    257 							if( testStep instanceof WsdlTestRequestStep )
    258 							{
    259 								WsdlTestRequest testRequest = ( ( WsdlTestRequestStep ) testStep ).getTestRequest();
    260 								testRequest.setEndpoint( selectedEndpoint );
    261 								changeCount++;
    262 							}
    263 						}
    264 					}
    265 				}
    266 			}
    267
    268 			log.info( "Assigned endpoint [" + selectedEndpoint + "] to " + changeCount + " Requests" );
    269 		}
    270 	}
    271
    272 	private class DeleteAction extends AbstractAction
    273 	{
    274 		public DeleteAction()
    275 		{
    276 			super( "Delete" );
    277 			putValue( Action.SHORT_DESCRIPTION, "Deletes the selected endpoint from the list" );
    278 		}
    279
    280 		public void actionPerformed( ActionEvent e )
    281 		{
    282 			int index = table.getSelectedRow();
    283 			if( index == -1 )
    284 			{
    285 				Toolkit.getDefaultToolkit().beep();
    286 				return;
    287 			}
    288
    289 			if( UISupport.confirm( "Delete selected endpoint?", "Delete Endpoint" ) )
    290 			{
    291 				tableModel.removeEndpoint( index );
    292 			}
    293 		}
    294 	}
    295
    296 	private class OkAction extends AbstractAction
    297 	{
    298 		public OkAction()
    299 		{
    300 			super( "OK" );
    301 			putValue( Action.SHORT_DESCRIPTION, "Closes this dialog" );
    302 		}
    303
    304 		public void actionPerformed( ActionEvent e )
    305 		{
    306 			dialog.setVisible( false );
    307 		}
    308 	}
    309
    310 	private class EndpointsTableModel extends AbstractTableModel
    311 	{
    312 		private WsdlInterface iface;
    313 		private DefaultEndpointStrategy strategy;
    314
    315 		public int getColumnCount()
    316 		{
    317 			return 6;
    318 		}
    319
    320 		@Override
    321 		public String getColumnName( int column )
    322 		{
    323 			switch( column )
    324 			{
    325 			case 0:
    326 				return "Endpoint";
    327 			case 1:
    328 				return "Username";
    329 			case 2:
    330 				return "Password";
    331 			case 3:
    332 				return "Domain";
    333 			case 4:
    334 				return "WSS-Type";
    335 			case 5:
    336 				return "WSS-TimeToLive";
    337 			}
    338
    339 			return null;
    340 		}
    341
    342 		public String getEndpointAt( int selectedIndex )
    343 		{
    344 			return iface.getEndpoints()[selectedIndex];
    345 		}
    346
    347 		public void addEndpoint( String endpoint )
    348 		{
    349 			int rowCount = getRowCount();
    350 			iface.addEndpoint( endpoint );
    351
    352 			fireTableRowsInserted( rowCount, rowCount );
    353 		}
    354
    355 		public void removeEndpoint( int index )
    356 		{
    357 			String ep = getEndpointAt( index );
    358 			iface.removeEndpoint( ep );
    359 			fireTableRowsDeleted( index, index );
    360 		}
    361
    362 		public void setInterface( WsdlInterface iface, DefaultEndpointStrategy strategy )
    363 		{
    364 			this.iface = iface;
    365 			this.strategy = strategy;
    366
    367 			fireTableDataChanged();
    368 		}
    369
    370 		public int getRowCount()
    371 		{
    372 			return iface == null ? 0 : iface.getEndpoints().length;
    373 		}
    374
    375 		public Object getValueAt( int rowIndex, int columnIndex )
    376 		{
    377 			String endpoint = getEndpointAt( rowIndex );
    378 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
    379
    380 			switch( columnIndex )
    381 			{
    382 			case 0:
    383 				return endpoint;
    384 			case 1:
    385 				return defaults.getUsername();
    386 			case 2:
    387 				return defaults.getPassword();
    388 			case 3:
    389 				return defaults.getDomain();
    390 			case 4:
    391 				return defaults.getWssType();
    392 			case 5:
    393 				return defaults.getWssTimeToLive();
    394 			}
    395
    396 			return null;
    397 		}
    398
    399 		@Override
    400 		public boolean isCellEditable( int rowIndex, int columnIndex )
    401 		{
    402 			return true;
    403 		}
    404
    405 		@Override
    406 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
    407 		{
    408 			String endpoint = getEndpointAt( rowIndex );
    409 			EndpointDefaults defaults = strategy.getEndpointDefaults( endpoint );
    410
    411 			if( aValue == null )
    412 				aValue = "";
    413
    414 			switch( columnIndex )
    415 			{
    416 				case 0 :
    417 				{
    418 					iface.changeEndpoint( endpoint, aValue.toString() );
    419 					break;
    420 				}
    421 				case 1 :
    422 				{
    423 					defaults.setUsername( aValue.toString() );
    424 					break;
    425 				}
    426 				case 2 :
    427 				{
    428 					defaults.setPassword( aValue.toString() );
    429 					break;
    430 				}
    431 				case 3 :
    432 				{
    433 					defaults.setDomain( aValue.toString() );
    434 					break;
    435 				}
    436 				case 4 :
    437 				{
    438 					defaults.setWssType( aValue.toString() );
    439 					break;
    440 				}
    441 				case 5 :
    442 				{
    443 					defaults.setWssTimeToLive( aValue.toString() );
    444 					break;
    445 				}
    446 			}
    447 		}
    448 	}
    449 }