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