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.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 abstract 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  	public String getSource()
81  	{
82  		return getKeystore();
83  	}
84  
85  	@Override
86  	protected void load( XmlObjectConfigurationReader reader )
87  	{
88  		keystore = reader.readString( "keystore", null );
89  		password = reader.readString( "password", null );
90  		type = reader.readString( "type", "jks" );
91  	}
92  
93  	@Override
94  	protected void save( XmlObjectConfigurationBuilder builder )
95  	{
96  		builder.add( "keystore", keystore );
97  		builder.add( "password", password );
98  		builder.add( "type", type );
99  	}
100 
101 	public Crypto getCrypto()
102 	{
103 		Properties properties = new Properties(); 
104 
105 		properties.put( "org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
106 		properties.put( "org.apache.ws.security.crypto.merlin.keystore.type", type.toLowerCase() );
107 		properties.put( "org.apache.ws.security.crypto.merlin.keystore.password" , getPassword() );
108 		properties.put( "org.apache.ws.security.crypto.merlin.file", getKeystore() );
109 		
110 		Crypto crypto = CryptoFactory.getInstance( properties );
111 		
112 		return crypto;
113 	}
114 
115 	public void init( WSSCryptoConfig config, WssContainer container )
116 	{
117 		super.init( config, container, TYPE );
118 	}
119 
120 	public String getKeystore()
121 	{
122 		return keystore;
123 	}
124 
125 	public void setKeystore( String keystore )
126 	{
127 		this.keystore = keystore;
128 		saveConfig();
129 	}
130 
131 	public String getPassword()
132 	{
133 		return password;
134 	}
135 
136 	public void setPassword( String password )
137 	{
138 		this.password = password;
139 		saveConfig();
140 	}
141 
142 	public String getType()
143 	{
144 		return type;
145 	}
146 
147 	public void setType( String type )
148 	{
149 		this.type = type;
150 		saveConfig();
151 	}
152 	
153 	@Override
154 	protected void addPropertyExpansions( PropertyExpansionsResult result )
155 	{
156 		super.addPropertyExpansions( result );
157 		
158 		result.extractAndAddAll( "keystore" );
159 	}
160 
161 	@Override
162 	public String getLabel()
163 	{
164 		if( StringUtils.isNullOrEmpty( keystore ))
165 			return super.getLabel();
166 		
167 	   int ix = keystore.lastIndexOf( File.separatorChar );
168 	   return ix == -1 ? keystore : keystore.substring( ix+1 );
169 	}
170 
171 	public WssContainer getWssContainer()
172 	{
173 		return null;
174 	}
175 }