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