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.support.wss.entries;
14  
15  import java.awt.Component;
16  import java.beans.PropertyChangeListener;
17  import java.security.KeyStore;
18  import java.security.KeyStoreException;
19  import java.util.ArrayList;
20  import java.util.Enumeration;
21  import java.util.List;
22  import java.util.Vector;
23  
24  import javax.swing.AbstractListModel;
25  import javax.swing.ComboBoxModel;
26  import javax.swing.DefaultListCellRenderer;
27  import javax.swing.JComponent;
28  import javax.swing.JList;
29  
30  import org.apache.ws.security.WSEncryptionPart;
31  
32  import com.eviware.soapui.config.WSSEntryConfig;
33  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
34  import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
35  import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto;
36  import com.eviware.soapui.impl.wsdl.support.wss.WssEntry;
37  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
38  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
39  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
40  import com.eviware.soapui.support.StringUtils;
41  import com.eviware.soapui.support.types.StringList;
42  import com.eviware.soapui.support.types.StringToStringMap;
43  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
44  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
45  
46  public abstract class WssEntryBase implements WssEntry, PropertyExpansionContainer
47  {
48  	private WSSEntryConfig config;
49  	private OutgoingWss outgoingWss;
50  	private JComponent configComponent;
51  	private String label;
52  
53  	public void init( WSSEntryConfig config, OutgoingWss outgoingWss, String label )
54  	{
55  		this.config = config;
56  		this.outgoingWss = outgoingWss;
57  		this.label = label;
58  		
59  		if( config.getConfiguration() == null )
60  			config.addNewConfiguration();
61  		
62  		load( new XmlObjectConfigurationReader( config.getConfiguration() ));
63  	}
64  
65  	public OutgoingWss getOutgoingWss()
66  	{
67  		return outgoingWss;
68  	}
69  
70  	public String getPassword()
71  	{
72  		String password = config.getPassword();
73  		if( StringUtils.isNullOrEmpty( password ))
74  			password = outgoingWss.getPassword();
75  		
76  		return password;
77  	}
78  
79  	public String getUsername()
80  	{
81  		String username = config.getUsername();
82  		if( StringUtils.isNullOrEmpty( username ))
83  			username = outgoingWss.getUsername();
84  		
85  		return username;
86  	}
87  
88  	public void setPassword( String arg0 )
89  	{
90  		config.setPassword( arg0 );
91  	}
92  
93  	public void setUsername( String arg0 )
94  	{
95  		config.setUsername( arg0 );
96  	}
97  
98  	public JComponent getConfigurationPanel()
99  	{
100 		if( configComponent == null )
101 			configComponent = buildUI();
102 		
103 		return configComponent;
104 	}
105 	
106 	public String getLabel()
107 	{
108 		return label;
109 	}
110 
111 	protected abstract JComponent buildUI();
112 	
113 	protected abstract void load( XmlObjectConfigurationReader reader );
114 	
115 	public void setConfig( WSSEntryConfig config )
116 	{
117 		this.config = config;
118 	}
119 
120 	public void saveConfig()
121 	{
122 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
123 		save( builder );
124 		config.getConfiguration().set( builder.finish() );
125 	}
126 	
127 	protected abstract void save( XmlObjectConfigurationBuilder builder );
128 
129 	public WssContainer getWssContainer()
130 	{
131 		return outgoingWss.getWssContainer();
132 	}
133 	
134 	public void addPropertyChangeListener( PropertyChangeListener listener )
135 	{}
136 	
137 	public void removePropertyChangeListener( PropertyChangeListener listener )
138 	{}
139 	
140 	public PropertyExpansion[] getPropertyExpansions()
141 	{
142 		PropertyExpansionsResult result = new PropertyExpansionsResult( getWssContainer().getModelItem(), this );
143 		
144 		addPropertyExpansions( result );
145 		
146 		return result.toArray();
147 	}
148 
149 	protected void addPropertyExpansions( PropertyExpansionsResult result )
150 	{
151 		if( StringUtils.hasContent( config.getUsername() ))
152 			result.extractAndAddAll( "username" );
153 		
154 		if( StringUtils.hasContent( config.getPassword() ))
155 			result.extractAndAddAll( "password" );
156 	}
157 
158 	public void udpateConfig( WSSEntryConfig config )
159 	{
160 		this.config = config;
161 	}
162 
163 	@Override
164 	public String toString()
165 	{
166 		return getLabel();
167 	}
168 	
169 	protected List<StringToStringMap> readParts( XmlObjectConfigurationReader reader, String parameterName )
170 	{
171 		List<StringToStringMap> result = new ArrayList<StringToStringMap>();
172 		String[] parts = reader.readStrings( parameterName );
173 		if( parts != null && parts.length > 0 )
174 		{
175 			for( String part : parts )
176 			{
177 				result.add( StringToStringMap.fromXml( part ));
178 			}
179 		}
180 		
181 		return result;
182 	}
183 	
184 	protected Vector<WSEncryptionPart> createWSParts( List<StringToStringMap> parts )
185 	{
186 		Vector<WSEncryptionPart> result = new Vector<WSEncryptionPart>();
187 		
188 		for( StringToStringMap map : parts )
189 		{
190 			if( map.hasValue( "id" ))
191 			{
192 				result.add( new WSEncryptionPart( map.get( "id" ), map.get( "enc" )) );
193 			}
194 			else
195 			{
196 				String ns = map.get( "namespace");
197 				if( ns == null )
198 					ns = "";
199 				result.add( new WSEncryptionPart( map.get(  "name" ), ns, map.get( "enc")) );
200 			}
201 		}
202 		
203 		return result;
204 	}
205 	
206 	protected void saveParts( XmlObjectConfigurationBuilder builder, List<StringToStringMap> parts, String string )
207 	{
208 		for( StringToStringMap part : parts )
209 		{
210 			builder.add( string, part.toXml() );
211 		}
212 	}
213 	
214 	protected class KeyIdentifierTypeRenderer extends DefaultListCellRenderer
215 	{
216 		@Override
217 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
218 					boolean cellHasFocus )
219 		{
220 			Component result = super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
221 
222 			if( value.equals( 0 ) )
223 				setText( "<none>" );
224 			else if( value.equals( 1 ) )
225 				setText( "Binary Security Token" );
226 			else if( value.equals( 2 ) )
227 				setText( "Issuer Name and Serial Number" );
228 			else if( value.equals( 3 ) )
229 				setText( "X509 Certificate" );
230 			else if( value.equals( 4 ) )
231 				setText( "Subject Key Identifier" );
232 			else if( value.equals( 5 ) )
233 				setText( "Embedded KeyInfo" );
234 			else if( value.equals( 6 ) )
235 				setText( "Embed SecurityToken Reference" );
236 			else if( value.equals( 7 ) )
237 				setText( "UsernameToken Signature" );
238 			else if( value.equals( 8 ) )
239 				setText( "Thumbprint SHA1 Identifier" );
240 			else if( value.equals( 9 ) )
241 				setText( "Custom Reference" );
242 
243 			return result;
244 		}
245 	}
246 	
247 	protected class KeyAliasComboBoxModel extends AbstractListModel implements ComboBoxModel
248 	{
249 		private KeyStore keyStore;
250 		private Object alias;
251 		private StringList aliases = new StringList();
252 
253 		public KeyAliasComboBoxModel( WssCrypto crypto )
254 		{
255 			update( crypto );
256 		}
257 
258 		void update( WssCrypto crypto )
259 		{
260 			keyStore = crypto == null || crypto.getCrypto() == null ? null : crypto.getCrypto().getKeyStore();
261 			
262 			if( keyStore != null )
263 			{
264 				if( !aliases.isEmpty() )
265 				{
266 					int sz = aliases.size();
267 					aliases.clear();
268 					fireIntervalRemoved( this, 0, sz-1 );
269 				}
270 				
271 				try
272 				{
273 					for( Enumeration e = keyStore.aliases(); e.hasMoreElements(); )
274 					{
275 						aliases.add( e.nextElement().toString() );
276 					}
277 					
278 					fireIntervalAdded( this, 0, aliases.size()-1 );
279 				}
280 				catch( KeyStoreException e )
281 				{
282 					e.printStackTrace();
283 				}
284 			}
285 		}
286 		
287 		public Object getSelectedItem()
288 		{
289 			return alias;
290 		}
291 
292 		public void setSelectedItem( Object anItem )
293 		{
294 			this.alias = anItem;
295 		}
296 
297 		public Object getElementAt( int index )
298 		{
299 			return aliases.get( index );
300 		}
301 
302 		public int getSize()
303 		{
304 			return aliases.size();
305 		}
306 	}
307 	
308 	public void release()
309 	{}
310 }
311