View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.transports.http.BaseHttpRequestTransport;
28  import com.eviware.soapui.model.iface.Response;
29  import com.eviware.soapui.model.iface.SubmitContext;
30  
31  public abstract class AbstractWssRequestFilter extends AbstractRequestFilter
32  {
33  	public static final String WSS_DOC = "WsSecurityAuthenticationRequestFilter@Document";
34  	protected static DocumentBuilderFactory dbf;
35  	protected static DocumentBuilder db;
36  
37  	static
38  	{
39  		dbf = DocumentBuilderFactory.newInstance();
40  		dbf.setValidating(false);
41  	   dbf.setNamespaceAware(true);
42  	   
43  	   try
44  		{
45  			db = dbf.newDocumentBuilder();
46  		}
47  		catch (ParserConfigurationException e)
48  		{
49  			SoapUI.logError( e );
50  		}
51  	}
52  
53  	protected static Document getWssDocument( SubmitContext context ) throws SAXException, IOException
54  	{
55  		String request = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
56  		Document doc = ( Document ) context.getProperty( WSS_DOC );
57  		
58  		// this should be solved with pooling for performance-reasons..
59  		if( doc == null )
60  		{
61  			synchronized( db )
62  			{
63  				doc = db.parse(new InputSource( new StringReader( request )));	
64  				context.setProperty( WSS_DOC, doc );
65  			}
66  		}
67  		return doc;
68  	}
69  
70  	public void afterRequest( SubmitContext context, Response response )
71  	{
72  		context.removeProperty( WSS_DOC );
73  	}
74  }