View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.BorderLayout;
16  
17  import javax.swing.JComponent;
18  import javax.swing.JPanel;
19  import javax.swing.JScrollPane;
20  
21  import org.apache.ws.security.message.WSSecHeader;
22  import org.apache.ws.security.message.WSSecSAMLToken;
23  import org.opensaml.SAMLAssertion;
24  import org.w3c.dom.Document;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.config.WSSEntryConfig;
28  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
31  import com.eviware.soapui.support.DocumentListenerAdapter;
32  import com.eviware.soapui.support.StringUtils;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.xml.JXEditTextArea;
35  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
36  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
37  import com.eviware.soapui.support.xml.XmlUtils;
38  
39  public class AddSAMLEntry extends WssEntryBase
40  {
41  	public static final String TYPE = "SAML";
42  
43  	private String samlAssertion;
44  
45  	private JXEditTextArea editor;
46  
47  	public void init( WSSEntryConfig config, OutgoingWss container )
48  	{
49  		super.init( config, container, TYPE );
50  	}
51  
52  	@Override
53  	protected JComponent buildUI()
54  	{
55  		JPanel panel = new JPanel( new BorderLayout() );
56  
57  		editor = JXEditTextArea.createXmlEditor( true );
58  		editor.setText( samlAssertion );
59  		editor.getDocument().addDocumentListener( new DocumentListenerAdapter()
60  		{
61  
62  			@Override
63  			public void update( javax.swing.text.Document document )
64  			{
65  				samlAssertion = editor.getText();
66  				saveConfig();
67  
68  			}
69  		} );
70  		panel.add( new JScrollPane( editor ), BorderLayout.CENTER );
71  
72  		return UISupport.addTitledBorder( panel, "Enter SAML Assertion" );
73  	}
74  
75  	@Override
76  	protected void load( XmlObjectConfigurationReader reader )
77  	{
78  		samlAssertion = reader.readString( "samlAssertion", null );
79  	}
80  
81  	@Override
82  	protected void save( XmlObjectConfigurationBuilder builder )
83  	{
84  		builder.add( "samlAssertion", samlAssertion );
85  	}
86  
87  	public void process( WSSecHeader secHeader, Document doc, PropertyExpansionContext context )
88  	{
89  		if( StringUtils.isNullOrEmpty( samlAssertion ) )
90  			return;
91  
92  		try
93  		{
94  			WSSecSAMLToken samlToken = new WSSecSAMLToken();
95  			Document dom = XmlUtils.parseXml( XmlUtils.stripWhitespaces( context.expand( samlAssertion ) ) );
96  			SAMLAssertion assertion = new SAMLAssertion( dom.getDocumentElement() );
97  			samlToken.build( doc, assertion, secHeader );
98  		}
99  		catch( Exception e )
100 		{
101 			SoapUI.logError( e );
102 		}
103 	}
104 
105 	public String getSamlAssertion()
106 	{
107 		return samlAssertion;
108 	}
109 
110 	public void setSamlAssertion( String samlAssertion )
111 	{
112 		this.samlAssertion = samlAssertion;
113 		saveConfig();
114 
115 		if( editor != null )
116 			editor.setText( samlAssertion );
117 	}
118 
119 	@Override
120 	protected void addPropertyExpansions( PropertyExpansionsResult result )
121 	{
122 		super.addPropertyExpansions( result );
123 		result.extractAndAddAll( "samlAssertion" );
124 	}
125 }