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.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 		result.extractAndAddAll( "username" );
152 		result.extractAndAddAll( "password" );
153 	}
154 
155 	public void udpateConfig( WSSEntryConfig config )
156 	{
157 		this.config = config;
158 	}
159 
160 	@Override
161 	public String toString()
162 	{
163 		return getLabel();
164 	}
165 	
166 	protected List<StringToStringMap> readParts( XmlObjectConfigurationReader reader, String parameterName )
167 	{
168 		List<StringToStringMap> result = new ArrayList<StringToStringMap>();
169 		String[] parts = reader.readStrings( parameterName );
170 		if( parts != null && parts.length > 0 )
171 		{
172 			for( String part : parts )
173 			{
174 				result.add( StringToStringMap.fromXml( part ));
175 			}
176 		}
177 		
178 		return result;
179 	}
180 	
181 	protected Vector<WSEncryptionPart> createWSParts( List<StringToStringMap> parts )
182 	{
183 		Vector<WSEncryptionPart> result = new Vector<WSEncryptionPart>();
184 		
185 		for( StringToStringMap map : parts )
186 		{
187 			if( map.hasValue( "id" ))
188 			{
189 				result.add( new WSEncryptionPart( map.get( "id" ), map.get( "enc" )) );
190 			}
191 			else
192 			{
193 				String ns = map.get( "namespace");
194 				if( ns == null )
195 					ns = "";
196 				result.add( new WSEncryptionPart( map.get(  "name" ), ns, map.get( "enc")) );
197 			}
198 		}
199 		
200 		return result;
201 	}
202 	
203 	protected void saveParts( XmlObjectConfigurationBuilder builder, List<StringToStringMap> parts, String string )
204 	{
205 		for( StringToStringMap part : parts )
206 		{
207 			builder.add( string, part.toXml() );
208 		}
209 	}
210 	
211 	protected class KeyIdentifierTypeRenderer extends DefaultListCellRenderer
212 	{
213 		@Override
214 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
215 					boolean cellHasFocus )
216 		{
217 			Component result = super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
218 
219 			if( value.equals( 0 ) )
220 				setText( "<none>" );
221 			else if( value.equals( 1 ) )
222 				setText( "Binary Security Token" );
223 			else if( value.equals( 2 ) )
224 				setText( "Issuer Name and Serial Number" );
225 			else if( value.equals( 3 ) )
226 				setText( "X509 Certificate" );
227 			else if( value.equals( 4 ) )
228 				setText( "Subject Key Identifier" );
229 			else if( value.equals( 5 ) )
230 				setText( "Embedded KeyInfo" );
231 			else if( value.equals( 6 ) )
232 				setText( "Embed SecurityToken Reference" );
233 			else if( value.equals( 8 ) )
234 				setText( "Thumbprint SHA1 Identifier" );
235 
236 			return result;
237 		}
238 	}
239 	
240 	protected class KeyAliasComboBoxModel extends AbstractListModel implements ComboBoxModel
241 	{
242 		private KeyStore keyStore;
243 		private Object alias;
244 		private StringList aliases = new StringList();
245 
246 		public KeyAliasComboBoxModel( WssCrypto crypto )
247 		{
248 			update( crypto );
249 		}
250 
251 		void update( WssCrypto crypto )
252 		{
253 			keyStore = crypto == null || crypto.getCrypto() == null ? null : crypto.getCrypto().getKeyStore();
254 			
255 			if( keyStore != null )
256 			{
257 				if( !aliases.isEmpty() )
258 				{
259 					int sz = aliases.size();
260 					aliases.clear();
261 					fireIntervalRemoved( this, 0, sz-1 );
262 				}
263 				
264 				try
265 				{
266 					for( Enumeration e = keyStore.aliases(); e.hasMoreElements(); )
267 					{
268 						aliases.add( e.nextElement().toString() );
269 					}
270 					
271 					fireIntervalAdded( this, 0, aliases.size()-1 );
272 				}
273 				catch( KeyStoreException e )
274 				{
275 					e.printStackTrace();
276 				}
277 			}
278 		}
279 		
280 		public Object getSelectedItem()
281 		{
282 			return alias;
283 		}
284 
285 		public void setSelectedItem( Object anItem )
286 		{
287 			this.alias = anItem;
288 		}
289 
290 		public Object getElementAt( int index )
291 		{
292 			return aliases.get( index );
293 		}
294 
295 		public int getSize()
296 		{
297 			return aliases.size();
298 		}
299 	}
300 	
301 	public void release()
302 	{}
303 }
304