View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.IOException;
16  import java.io.StringReader;
17  import java.io.StringWriter;
18  
19  import javax.xml.parsers.DocumentBuilder;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.parsers.ParserConfigurationException;
22  
23  import org.w3c.dom.Document;
24  import org.xml.sax.InputSource;
25  import org.xml.sax.SAXException;
26  
27  import com.eviware.soapui.SoapUI;
28  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
29  import com.eviware.soapui.model.iface.Response;
30  import com.eviware.soapui.model.iface.SubmitContext;
31  import com.eviware.soapui.support.xml.XmlUtils;
32  
33  public abstract class AbstractWssRequestFilter extends AbstractRequestFilter
34  {
35  	private static final String REQUEST_CONTENT_HASH_CODE = "requestContentHashCode";
36  	public static final String WSS_DOC = "WsSecurityAuthenticationRequestFilter@Document";
37  	protected static DocumentBuilderFactory dbf;
38  	protected static DocumentBuilder db;
39  
40  	static
41  	{
42  		dbf = DocumentBuilderFactory.newInstance();
43  		dbf.setValidating( false );
44  		dbf.setNamespaceAware( true );
45  
46  		try
47  		{
48  			db = dbf.newDocumentBuilder();
49  		}
50  		catch( ParserConfigurationException e )
51  		{
52  			SoapUI.logError( e );
53  		}
54  	}
55  
56  	protected static Document getWssDocument( SubmitContext context ) throws SAXException, IOException
57  	{
58  		String request = ( String )context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
59  		Document doc = ( Document )context.getProperty( WSS_DOC );
60  
61  		// this should be solved with pooling for performance-reasons..
62  		if( doc == null || ((Integer)context.getProperty( REQUEST_CONTENT_HASH_CODE )).intValue() != request.hashCode() )
63  		{
64  			synchronized( db )
65  			{
66  				doc = db.parse( new InputSource( new StringReader( request ) ) );
67  				context.setProperty( REQUEST_CONTENT_HASH_CODE, new Integer( request.hashCode()) );
68  				context.setProperty( WSS_DOC, doc );
69  			}
70  		}
71  		
72  		return doc;
73  	}
74  
75  	protected static void updateWssDocument( SubmitContext context, Document dom ) throws IOException
76  	{
77  		StringWriter writer = new StringWriter();
78  		XmlUtils.serialize( dom, writer );
79  		String request = writer.toString();
80  		context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, request );
81  		context.setProperty( REQUEST_CONTENT_HASH_CODE, new Integer( request.hashCode()) );
82  	}
83  	
84  	public void afterRequest( SubmitContext context, Response response )
85  	{
86  		context.removeProperty( WSS_DOC );
87  	}
88  }