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.submit.filters;
    14
    15  import java.io.StringReader;
    16  import java.io.StringWriter;
    17
    18  import javax.xml.parsers.DocumentBuilder;
    19  import javax.xml.parsers.DocumentBuilderFactory;
    20  import javax.xml.parsers.ParserConfigurationException;
    21
    22  import org.apache.ws.security.WSConstants;
    23  import org.apache.ws.security.message.WSSecHeader;
    24  import org.apache.ws.security.message.WSSecTimestamp;
    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.SoapUI;
    30  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    31  import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
    32  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
    33  import com.eviware.soapui.model.iface.SubmitContext;
    34  import com.eviware.soapui.support.xml.XmlUtils;
    35
    36  /***
    37   * Modifies the request message to include WS-Securty Username and Timestamp tokens
    38   *
    39   * @author Ole.Matzura
    40   */
    41
    42  public class WsSecurityAuthenticationRequestFilter implements RequestFilter
    43  {
    44  	private static DocumentBuilderFactory dbf;
    45  	private static DocumentBuilder db;
    46
    47  	static
    48  	{
    49  		dbf = DocumentBuilderFactory.newInstance();
    50  		dbf.setValidating(false);
    51  	   dbf.setNamespaceAware(true);
    52
    53  	   try
    54  		{
    55  			db = dbf.newDocumentBuilder();
    56  		}
    57  		catch (ParserConfigurationException e)
    58  		{
    59  			SoapUI.logError( e );
    60  		}
    61  	}
    62
    63  	public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
    64  	{
    65  		String pwType = wsdlRequest.getWssPasswordType();
    66        String wsTimestamp = wsdlRequest.getWssTimeToLive();
    67
    68  		if (  (WsdlRequest.PW_TYPE_NONE.equals(pwType) ||  pwType == null || pwType.length() == 0) &&
    69        		(wsTimestamp == null || wsTimestamp.length() == 0 ))
    70        		return;
    71
    72        try
    73        {
    74           {
    75           	String request = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
    76           	Document doc = null;
    77
    78           	// this should be solved with pooling for performance-reasons..
    79           	synchronized( db )
    80  				{
    81           		doc = db.parse(new InputSource( new StringReader( request )));
    82  				}
    83
    84     			if( pwType != null && pwType.length() > 0 && !pwType.equals(  WsdlRequest.PW_TYPE_NONE ))
    85     				addWssUsernameToken( wsdlRequest, pwType, doc );
    86
    87     			if( wsTimestamp != null && wsTimestamp.length() > 0 )
    88     				addWsTimestampToken( wsdlRequest, wsTimestamp, doc );
    89
    90              StringWriter writer = new StringWriter();
    91      			XmlUtils.serializePretty( doc, writer );
    92      			context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, writer.toString() );
    93           }
    94        }
    95  		catch (Throwable e)
    96  		{
    97            SoapUI.logError( e );
    98        }
    99  	}
    100
    101 	private void addWsTimestampToken( WsdlRequest wsdlRequest, String ttl, Document doc )
    102 	{
    103 		WSSecTimestamp addTimestamp = new WSSecTimestamp();
    104 		addTimestamp.setTimeToLive( Integer.parseInt( ttl ));
    105 		WSSecHeader secHeader = new WSSecHeader();
    106 		secHeader.insertSecurityHeader( doc );
    107 		addTimestamp.build( doc, secHeader );
    108 	}
    109
    110 	private void addWssUsernameToken( WsdlRequest wsdlRequest, String pwType, Document doc )
    111 	{
    112 		WSSecUsernameToken wsa = new WSSecUsernameToken();
    113 		if (WsdlRequest.PW_TYPE_DIGEST.equals(pwType))
    114 		{
    115 		   wsa.setPasswordType(WSConstants.PASSWORD_DIGEST);
    116 		}
    117 		else
    118 		{
    119 		   wsa.setPasswordType(WSConstants.PASSWORD_TEXT);
    120 		}
    121
    122 		wsa.setUserInfo(wsdlRequest.getUsername(), wsdlRequest.getPassword() );
    123
    124 		WSSecHeader secHeader = new WSSecHeader();
    125 		secHeader.insertSecurityHeader( doc );
    126 		wsa.build(doc, secHeader );
    127 	}
    128 }