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