1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.submit.filters;
14
15 import org.apache.log4j.Logger;
16 import org.apache.log4j.Priority;
17 import org.apache.xmlbeans.XmlException;
18 import org.apache.xmlbeans.XmlObject;
19
20 import com.eviware.soapui.impl.wsdl.WsdlRequest;
21 import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
22 import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
23 import com.eviware.soapui.model.iface.SubmitContext;
24 import com.eviware.soapui.model.testsuite.TestRunContext;
25 import com.eviware.soapui.support.xml.XmlUtils;
26
27 /***
28 * RequestFilter that expands properties in request content
29 *
30 * @author Ole.Matzura
31 */
32
33 public class PropertyExpansionRequestFilter implements RequestFilter
34 {
35 private static final char PROPERTY_SEPARATOR = '#';
36 private final static Logger log = Logger.getLogger(PropertyExpansionRequestFilter.class);
37
38 public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
39 {
40 String content = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
41 if( content == null )
42 {
43 log.warn( "Missing request content in context, skipping property expansion" );
44 }
45 else
46 {
47 content = expandProperties(context, content);
48 if( content != null )
49 context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, content );
50 }
51 }
52
53 public static String expandProperties(SubmitContext context, String content)
54 {
55 int ix = content.indexOf( "${" );
56 if( ix == -1 )
57 return content;
58
59 StringBuffer buf = new StringBuffer();
60 int lastIx = 0;
61 while( ix != -1 )
62 {
63 buf.append( content.substring( lastIx, ix ));
64
65 int ix2 = content.indexOf( '}', ix+2 );
66 if( ix2 == -1 )
67 break;
68
69 int ix3 = content.lastIndexOf( "${", ix2 );
70 if( ix3 != ix )
71 {
72 buf.append( content.substring( ix, ix3 ));
73 ix = ix3;
74 }
75
76 String propertyName = content.substring( ix+2, ix2 );
77 Object property = context.getProperty( propertyName );
78 if( property != null )
79 {
80 buf.append( property.toString() );
81 }
82 else if( context instanceof TestRunContext )
83 {
84 int sepIx = propertyName.indexOf( PROPERTY_SEPARATOR );
85 if( sepIx > 0 )
86 {
87 String step = propertyName.substring( 0, sepIx );
88 String name = propertyName.substring( sepIx+1 );
89
90 sepIx = name.indexOf( PROPERTY_SEPARATOR );
91 if( sepIx != -1 )
92 {
93 String xpath = name.substring( sepIx+1 );
94 name = name.substring( 0, sepIx );
95
96 if( step.length() == 0 )
97 property = ((TestRunContext)context).getProperty( name);
98 else
99 property = ((TestRunContext)context).getProperty( step, name);
100
101 if( property != null )
102 {
103 property = extractXPathPropertyValue( property, xpath );
104 }
105 }
106 else
107 {
108 if( step.length() == 0 )
109 property = ((TestRunContext)context).getProperty( name);
110 else
111 property = ((TestRunContext)context).getProperty( step, name);
112 }
113
114 if( property != null )
115 {
116 buf.append( property.toString() );
117 }
118 }
119 }
120 else if( propertyName.charAt( 0 ) == PROPERTY_SEPARATOR )
121 {
122 int sepIx = propertyName.indexOf( PROPERTY_SEPARATOR, 1 );
123 if( sepIx > 0 )
124 {
125 String xpath = propertyName.substring( sepIx+1 );
126 propertyName = propertyName.substring( 1, sepIx );
127
128 property = context.getProperty( propertyName );
129 if( property != null )
130 {
131 property = extractXPathPropertyValue( property, xpath );
132 }
133 }
134 else
135 {
136 property = context.getProperty( propertyName.substring( 1 ) );
137 }
138
139 if( property != null )
140 {
141 buf.append( property.toString() );
142 }
143 }
144
145 if( property == null )
146 {
147 if( log.isEnabledFor( Priority.WARN ))
148 log.warn( "Missing property value for [" + propertyName + "]" );
149
150 buf.append( "${" ).append( propertyName ).append( '}' );
151 }
152
153 lastIx = ix2+1;
154 ix = content.indexOf( "${", lastIx );
155 }
156
157 if( lastIx < content.length() )
158 buf.append( content.substring( lastIx ));
159
160 return buf.toString();
161 }
162
163 public static String extractXPathPropertyValue( Object property, String xpath )
164 {
165 try
166 {
167 XmlObject xmlObject = XmlObject.Factory.parse( property.toString() );
168 String ns = XmlUtils.declareXPathNamespaces( xmlObject );
169 XmlObject[] paths = xmlObject.selectPath( ns + xpath );
170 if( paths.length > 0 )
171 return XmlUtils.getNodeValue( paths[0].getDomNode() );
172 }
173 catch( XmlException e )
174 {
175 e.printStackTrace();
176 }
177
178 return null;
179 }
180 }