View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 groovy.lang.GroovyShell;
16  
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import org.apache.xmlbeans.XmlCursor;
22  import org.apache.xmlbeans.XmlObject;
23  import org.apache.xmlbeans.XmlOptions;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  
28  import com.eviware.soapui.impl.wsdl.WsdlRequest;
29  import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
30  import com.eviware.soapui.model.iface.SubmitContext;
31  import com.eviware.soapui.support.ScriptingSupport;
32  
33  /***
34   * RequestFilter that expands scripts in request content - not used for now, we need to fix validations first
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public class ScriptExpansionRequestFilter implements RequestFilter
40  {
41  	public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
42  	{
43  	/*	String content = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
44  	
45  		content = expandScripts(context, content);
46  		if( content != null )
47  			context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, content );
48  			*/
49  	}
50  
51  	public static String expandScripts(SubmitContext context, String content)
52  	{
53  		try
54  		{
55  			XmlObject obj = XmlObject.Factory.parse(content);
56  			XmlCursor cursor = obj.newCursor();
57  			boolean replaced = false;
58  			
59  			while (!cursor.isEnddoc())
60  			{
61  				Node node = cursor.getDomNode();
62  				if ( node.getNodeType() == Node.ELEMENT_NODE)
63  				{
64  					if( node.getNamespaceURI().equals( "http://www.soapui.org/wsp" ) && node.getNodeName().equals( "script"))
65  					{
66  						GroovyShell shell = ScriptingSupport.createGroovyShell( null );
67  						String type = ((Element)node).getAttribute( "type" );
68  						String result = shell.evaluate( cursor.getTextValue() ).toString();
69  						
70  						if( type == null || type.length() == 0 || type.equals( "content"))
71  						{
72  							cursor.removeXml();
73  							cursor.insertChars( result );
74  						}
75  						else if( type.equals( "markup" ))
76  						{
77  							Node parent = node.getParentNode();
78  							XmlOptions options = new XmlOptions();
79  							Map map = new HashMap();
80  							cursor.getAllNamespaces( map );
81  							
82  							StringBuffer buf = new StringBuffer();
83  							buf.append( "<result" );
84  							
85  							for( Iterator i = map.keySet().iterator(); i.hasNext(); )
86  							{
87  								buf.append( " xmlns" );
88  								String next = (String) i.next();
89  								if( next.length() > 0 )
90  									buf.append( ':' ).append( next);
91  
92  								buf.append( "=\"" ).append( map.get( next )).append( "\"" );
93  							}
94  							
95  							buf.append( ">" ).append( result ).append( "</result>" );
96  							result = buf.toString();
97  							
98  							XmlObject newObj = XmlObject.Factory.parse( result );
99  							Element docElm = ((Document)newObj.getDomNode()).getDocumentElement();
100 							
101 							parent.replaceChild( parent.getOwnerDocument().importNode( docElm.getFirstChild(), true ), node );
102 						}
103 						
104 						replaced = true;
105 					}
106 				}
107 
108 				cursor.toNextToken();
109 			}
110 			
111 			return replaced ? obj.toString() : null;
112 		}
113 		catch (Exception e)
114 		{
115 			e.printStackTrace();
116 			return null;
117 		}		
118 	}
119 }