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