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.actions.request;
14  
15  import java.awt.event.ActionEvent;
16  import java.io.StringReader;
17  import java.io.StringWriter;
18  
19  import javax.swing.AbstractAction;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  
23  import org.apache.ws.security.WSConstants;
24  import org.apache.ws.security.message.WSSecHeader;
25  import org.apache.ws.security.message.WSSecUsernameToken;
26  import org.w3c.dom.Document;
27  import org.xml.sax.InputSource;
28  
29  import com.eviware.soapui.impl.wsdl.WsdlRequest;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.xml.XmlUtils;
32  
33  /***
34   * Prompts to add a WSS Username Token to the specified WsdlRequests
35   * requestContent
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class AddWSSUsernameTokenAction extends AbstractAction
41  {
42  	private final WsdlRequest request;
43  
44  	public AddWSSUsernameTokenAction( WsdlRequest request )
45  	{
46  		super( "Add WSS Username Token" );
47  		this.request = request;
48  	}
49  
50  	public void actionPerformed( ActionEvent e )
51  	{
52  		if( ( request.getUsername() == null || request.getUsername().length() == 0 )
53  				&& ( request.getPassword() == null || request.getPassword().length() == 0 ) )
54  		{
55  			UISupport.showErrorMessage( "Request is missing username and password" );
56  			return;
57  		}
58  
59  		String req = request.getRequestContent();
60  
61  		try
62  		{
63  			String passwordType = ( String )UISupport.prompt( "Add WSS Username Token", "Specify Password Type",
64  					new String[] { WsdlRequest.PW_TYPE_TEXT, WsdlRequest.PW_TYPE_DIGEST } );
65  
66  			if( passwordType == null )
67  				return;
68  
69  			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
70  			dbf.setNamespaceAware( true );
71  			DocumentBuilder db = dbf.newDocumentBuilder();
72  			Document doc = db.parse( new InputSource( new StringReader( req ) ) );
73  			WSSecUsernameToken addUsernameToken = new WSSecUsernameToken();
74  
75  			if( WsdlRequest.PW_TYPE_DIGEST.equals( passwordType ) )
76  			{
77  				addUsernameToken.setPasswordType( WSConstants.PASSWORD_DIGEST );
78  			}
79  			else
80  			{
81  				addUsernameToken.setPasswordType( WSConstants.PASSWORD_TEXT );
82  			}
83  
84  			addUsernameToken.setUserInfo( request.getUsername(), request.getPassword() );
85  			addUsernameToken.addNonce();
86  			addUsernameToken.addCreated();
87  
88  			StringWriter writer = new StringWriter();
89  
90  			WSSecHeader secHeader = new WSSecHeader();
91  			secHeader.insertSecurityHeader( doc );
92  			XmlUtils.serializePretty( addUsernameToken.build( doc, secHeader ), writer );
93  			request.setRequestContent( writer.toString() );
94  		}
95  		catch( Exception e1 )
96  		{
97  			UISupport.showErrorMessage( e1 );
98  		}
99  	}
100 }