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.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 requestContent
    35   *
    36   * @author Ole.Matzura
    37   */
    38
    39  public class AddWSSUsernameTokenAction extends AbstractAction
    40  {
    41     private final WsdlRequest request;
    42
    43  	public AddWSSUsernameTokenAction( WsdlRequest request )
    44     {
    45        super( "Add WSS Username Token" );
    46  		this.request = request;
    47     }
    48
    49     public void actionPerformed(ActionEvent e)
    50     {
    51     	if( (request.getUsername() == null || request.getUsername().length() == 0) &&
    52     		 (request.getPassword() == null || request.getPassword().length() == 0 ))
    53     	{
    54     		UISupport.showErrorMessage( "Request is missing username and password" );
    55     		return;
    56     	}
    57
    58     	String req = request.getRequestContent();
    59
    60     	try
    61  		{
    62     		String passwordType = (String) UISupport.prompt( "Add WSS Username Token", "Specify Password Type",
    63     				new String [] { WsdlRequest.PW_TYPE_TEXT, WsdlRequest.PW_TYPE_DIGEST });
    64
    65     		if( passwordType == null )
    66     			return;
    67
    68  			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    69  			dbf.setNamespaceAware( true );
    70  			DocumentBuilder db = dbf.newDocumentBuilder();
    71  			Document doc = db.parse( new InputSource( new StringReader( req )));
    72  			WSSecUsernameToken addUsernameToken = new WSSecUsernameToken();
    73
    74  			if (WsdlRequest.PW_TYPE_DIGEST.equals(passwordType))
    75           {
    76  				addUsernameToken.setPasswordType(WSConstants.PASSWORD_DIGEST);
    77           }
    78           else
    79           {
    80              addUsernameToken.setPasswordType(WSConstants.PASSWORD_TEXT);
    81           }
    82
    83  			addUsernameToken.setUserInfo(request.getUsername(), request.getPassword() );
    84
    85  			StringWriter writer = new StringWriter();
    86
    87  			WSSecHeader secHeader = new WSSecHeader();
    88  			secHeader.insertSecurityHeader( doc );
    89  			XmlUtils.serializePretty( addUsernameToken.build( doc, secHeader ), writer );
    90  			request.setRequestContent( writer.toString() );
    91  		}
    92  		catch ( Exception e1)
    93  		{
    94  			UISupport.showErrorMessage( e1 );
    95  		}
    96     }
    97  }