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.IOException;
16  import java.io.StringReader;
17  
18  import javax.xml.parsers.DocumentBuilder;
19  import javax.xml.parsers.DocumentBuilderFactory;
20  import javax.xml.parsers.ParserConfigurationException;
21  
22  import org.w3c.dom.Document;
23  import org.xml.sax.InputSource;
24  import org.xml.sax.SAXException;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
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  
32  public abstract class AbstractWssRequestFilter implements RequestFilter
33  {
34  	public static final String WSS_DOC = "WsSecurityAuthenticationRequestFilter@Document";
35  	protected static DocumentBuilderFactory dbf;
36  	protected static DocumentBuilder db;
37  
38  	static
39  	{
40  		dbf = DocumentBuilderFactory.newInstance();
41  		dbf.setValidating(false);
42  	   dbf.setNamespaceAware(true);
43  	   
44  	   try
45  		{
46  			db = dbf.newDocumentBuilder();
47  		}
48  		catch (ParserConfigurationException e)
49  		{
50  			SoapUI.logError( e );
51  		}
52  	}
53  
54  	protected static Document getWssDocument( SubmitContext context ) throws SAXException, IOException
55  	{
56  		String request = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
57  		Document doc = ( Document ) context.getProperty( WSS_DOC );
58  		
59  		// this should be solved with pooling for performance-reasons..
60  		if( doc == null )
61  		{
62  			synchronized( db )
63  			{
64  				doc = db.parse(new InputSource( new StringReader( request )));	
65  				context.setProperty( WSS_DOC, doc );
66  			}
67  		}
68  		return doc;
69  	}
70  
71  	public void afterRequest( SubmitContext context, Response response )
72  	{
73  		context.removeProperty( WSS_DOC );
74  	}
75  }