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.crypto;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  import java.io.File;
18  import java.util.Properties;
19  
20  import javax.swing.JButton;
21  import javax.swing.JComponent;
22  
23  import org.apache.ws.security.components.crypto.Crypto;
24  import org.apache.ws.security.components.crypto.CryptoFactory;
25  
26  import com.eviware.soapui.config.WSSCryptoConfig;
27  import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
29  import com.eviware.soapui.support.StringUtils;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.components.SimpleBindingForm;
32  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
33  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
34  import com.jgoodies.binding.PresentationModel;
35  
36  public class MerlinCrypto extends WssCryptoBase
37  {
38  	public static final String TYPE = "Merlin";
39  	private String keystore;
40  	private String password;
41  	private String type;
42  	private JButton validateButton;
43  
44  	@Override
45  	protected JComponent buildUI()
46  	{
47  		SimpleBindingForm form = new SimpleBindingForm( new PresentationModel<MerlinCrypto>( this ) ); 
48  		
49  		form.appendTextField( "keystore", "Keystore", "The keystore file" );
50  		form.appendTextField( "password", "Password", "The keystore password" );
51  		form.appendComboBox( "type", "Type", new String[] {"JKS", "PKCS12"}, "The keystore type" );
52  		
53  		form.addRightComponent( validateButton = new JButton( "Validate" ) );
54  		
55  		validateButton.addActionListener( new ActionListener() {
56  
57  			public void actionPerformed( ActionEvent e )
58  			{
59  				if( StringUtils.isNullOrEmpty( getPassword() ))
60  				{
61  					UISupport.showErrorMessage( "Missing password" );
62  					return;
63  				}
64  				
65  				try
66  				{
67  					Crypto crypto = getCrypto();
68  					UISupport.showInfoMessage( "Loaded keystore of type: " + crypto.getKeyStore().getType() );
69  				}
70  				catch( Throwable t )
71  				{
72  					UISupport.showErrorMessage( t );
73  				}
74  				
75  			}} );
76  		
77  		return form.getPanel();
78  	}
79  
80  	@Override
81  	protected void load( XmlObjectConfigurationReader reader )
82  	{
83  		keystore = reader.readString( "keystore", null );
84  		password = reader.readString( "password", null );
85  		type = reader.readString( "type", "jks" );
86  	}
87  
88  	@Override
89  	protected void save( XmlObjectConfigurationBuilder builder )
90  	{
91  		builder.add( "keystore", keystore );
92  		builder.add( "password", password );
93  		builder.add( "type", type );
94  	}
95  
96  	public Crypto getCrypto()
97  	{
98  		Properties properties = new Properties(); 
99  
100 		properties.put( "org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
101 		properties.put( "org.apache.ws.security.crypto.merlin.keystore.type", type.toLowerCase() );
102 		properties.put( "org.apache.ws.security.crypto.merlin.keystore.password" , getPassword() );
103 		properties.put( "org.apache.ws.security.crypto.merlin.file", getKeystore() );
104 		
105 		Crypto crypto = CryptoFactory.getInstance( properties );
106 		
107 		return crypto;
108 	}
109 
110 	public void init( WSSCryptoConfig config, WssContainer container )
111 	{
112 		super.init( config, container, TYPE );
113 	}
114 
115 	public String getKeystore()
116 	{
117 		return keystore;
118 	}
119 
120 	public void setKeystore( String keystore )
121 	{
122 		this.keystore = keystore;
123 		saveConfig();
124 	}
125 
126 	public String getPassword()
127 	{
128 		return password;
129 	}
130 
131 	public void setPassword( String password )
132 	{
133 		this.password = password;
134 		saveConfig();
135 	}
136 
137 	public String getType()
138 	{
139 		return type;
140 	}
141 
142 	public void setType( String type )
143 	{
144 		this.type = type;
145 		saveConfig();
146 	}
147 	
148 	@Override
149 	protected void addPropertyExpansions( PropertyExpansionsResult result )
150 	{
151 		super.addPropertyExpansions( result );
152 		
153 		result.extractAndAddAll( "keystore" );
154 	}
155 
156 	@Override
157 	public String getLabel()
158 	{
159 		if( StringUtils.isNullOrEmpty( keystore ))
160 			return super.getLabel();
161 		
162 	   int ix = keystore.lastIndexOf( File.separatorChar );
163 	   return ix == -1 ? keystore : keystore.substring( ix+1 );
164 	}
165 
166 	public WssContainer getWssContainer()
167 	{
168 		// TODO Auto-generated method stub
169 		return null;
170 	}
171 }