View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  package com.eviware.soapui.impl.support.http;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.Map;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.xmlbeans.SchemaGlobalElement;
21  import org.apache.xmlbeans.SchemaType;
22  
23  import com.eviware.soapui.config.AttachmentConfig;
24  import com.eviware.soapui.config.HttpRequestConfig;
25  import com.eviware.soapui.impl.rest.RestRequestInterface;
26  import com.eviware.soapui.impl.rest.RestRequest.ParameterMessagePart;
27  import com.eviware.soapui.impl.rest.RestRequestInterface.RequestMethod;
28  import com.eviware.soapui.impl.rest.support.RestParamProperty;
29  import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
30  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
31  import com.eviware.soapui.impl.support.AbstractHttpOperation;
32  import com.eviware.soapui.impl.support.AbstractHttpRequest;
33  import com.eviware.soapui.impl.wsdl.HttpAttachmentPart;
34  import com.eviware.soapui.impl.wsdl.WsdlSubmit;
35  import com.eviware.soapui.impl.wsdl.submit.RequestTransportRegistry;
36  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
37  import com.eviware.soapui.impl.wsdl.support.jms.header.JMSHeaderConfig;
38  import com.eviware.soapui.impl.wsdl.support.jms.property.JMSPropertiesConfig;
39  import com.eviware.soapui.model.ModelItem;
40  import com.eviware.soapui.model.iface.MessagePart;
41  import com.eviware.soapui.model.iface.SubmitContext;
42  import com.eviware.soapui.model.iface.MessagePart.ContentPart;
43  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
44  import com.eviware.soapui.model.testsuite.TestProperty;
45  import com.eviware.soapui.model.testsuite.TestPropertyListener;
46  import com.eviware.soapui.support.StringUtils;
47  import com.eviware.soapui.support.UISupport;
48  
49  public class HttpRequest extends AbstractHttpRequest<HttpRequestConfig> implements
50  		HttpRequestInterface<HttpRequestConfig>
51  {
52  	private XmlBeansRestParamsTestPropertyHolder params;
53  
54  	protected HttpRequest( HttpRequestConfig config, boolean forLoadTest )
55  	{
56  		super( config, null, "/http_request.gif", forLoadTest );
57  
58  		if( config.getParameters() == null )
59  			config.addNewParameters();
60  
61  		params = new XmlBeansRestParamsTestPropertyHolder( this, config.getParameters() );
62  	}
63  
64  	public TestProperty addProperty( String name )
65  	{
66  		return params.addProperty( name );
67  	}
68  
69  	public void moveProperty( String propertyName, int targetIndex )
70  	{
71  		params.moveProperty( propertyName, targetIndex );
72  	}
73  
74  	public TestProperty removeProperty( String propertyName )
75  	{
76  		return params.removeProperty( propertyName );
77  	}
78  
79  	public boolean renameProperty( String name, String newName )
80  	{
81  		return params.renameProperty( name, newName );
82  	}
83  
84  	public void addTestPropertyListener( TestPropertyListener listener )
85  	{
86  		params.addTestPropertyListener( listener );
87  	}
88  
89  	public ModelItem getModelItem()
90  	{
91  		return this;
92  	}
93  
94  	public String getMediaType()
95  	{
96  		return getConfig().getMediaType() != null ? getConfig().getMediaType() : "application/xml";
97  	}
98  
99  	public String getPath()
100 	{
101 		return getEndpoint();
102 	}
103 
104 	public boolean hasRequestBody()
105 	{
106 		RestRequestInterface.RequestMethod method = getMethod();
107 		return method == RestRequestInterface.RequestMethod.POST || method == RestRequestInterface.RequestMethod.PUT;
108 	}
109 
110 	public RestParamsPropertyHolder getParams()
111 	{
112 		return params;
113 	}
114 
115 	public Map<String, TestProperty> getProperties()
116 	{
117 		return params.getProperties();
118 	}
119 
120 	public RestParamProperty getProperty( String name )
121 	{
122 		return params.getProperty( name );
123 	}
124 
125 	public RestParamProperty getPropertyAt( int index )
126 	{
127 		return params.getPropertyAt( index );
128 	}
129 
130 	public int getPropertyCount()
131 	{
132 		return params.getPropertyCount();
133 	}
134 
135 	public String[] getPropertyNames()
136 	{
137 		return params.getPropertyNames();
138 	}
139 
140 	public String getPropertyValue( String name )
141 	{
142 		return params.getPropertyValue( name );
143 	}
144 
145 	public boolean isPostQueryString()
146 	{
147 		return hasRequestBody() && getConfig().getPostQueryString();
148 	}
149 
150 	public boolean hasProperty( String name )
151 	{
152 		return params.hasProperty( name );
153 	}
154 
155 	public void setPropertyValue( String name, String value )
156 	{
157 		params.setPropertyValue( name, value );
158 	}
159 
160 	public void setMediaType( String mediaType )
161 	{
162 		String old = getMediaType();
163 		getConfig().setMediaType( mediaType );
164 		notifyPropertyChanged( "mediaType", old, mediaType );
165 	}
166 
167 	public void setPostQueryString( boolean b )
168 	{
169 		boolean old = isPostQueryString();
170 		getConfig().setPostQueryString( b );
171 		notifyPropertyChanged( "postQueryString", old, b );
172 
173 		if( !"multipart/form-data".equals( getMediaType() ) )
174 		{
175 			setMediaType( b ? "application/x-www-form-urlencoded" : getMediaType() );
176 		}
177 	}
178 
179 	public void setMethod( RequestMethod method )
180 	{
181 		RestRequestInterface.RequestMethod old = getMethod();
182 		getConfig().setMethod( method.toString() );
183 		setIcon( UISupport.createImageIcon( "/" + method.toString().toLowerCase() + "_method.gif" ) );
184 		notifyPropertyChanged( "method", old, method );
185 	}
186 
187 	public void setDownloadIncludedResources( boolean downloadIncludedResources )
188 	{
189 		boolean old = getDownloadIncludedResources();
190 		getConfig().setDownloadIncludedResources( downloadIncludedResources );
191 		notifyPropertyChanged( "downloadIncludedResources", old, downloadIncludedResources );
192 	}
193 
194 	public boolean getDownloadIncludedResources()
195 	{
196 		return getConfig().getDownloadIncludedResources();
197 	}
198 
199 	public String getPropertiesLabel()
200 	{
201 		return "HTTP Params";
202 	}
203 
204 	public void removeTestPropertyListener( TestPropertyListener listener )
205 	{
206 		params.removeTestPropertyListener( listener );
207 	}
208 
209 	public HttpAttachmentPart getAttachmentPart( String partName )
210 	{
211 		return null;
212 	}
213 
214 	public HttpAttachmentPart[] getDefinedAttachmentParts()
215 	{
216 		return new HttpAttachmentPart[0];
217 	}
218 
219 	@Override
220 	public RestRequestInterface.RequestMethod getMethod()
221 	{
222 		String method = getConfig().getMethod();
223 		return method == null ? null : RestRequestInterface.RequestMethod.valueOf( method );
224 	}
225 
226 	public MessagePart[] getRequestParts()
227 	{
228 		List<MessagePart> result = new ArrayList<MessagePart>();
229 
230 		for( int c = 0; c < getPropertyCount(); c++ )
231 		{
232 			result.add( new ParameterMessagePart( getPropertyAt( c ) ) );
233 		}
234 
235 		if( getMethod() == RestRequestInterface.RequestMethod.POST
236 				|| getMethod() == RestRequestInterface.RequestMethod.PUT )
237 		{
238 			result.add( new HttpContentPart() );
239 		}
240 
241 		return result.toArray( new MessagePart[result.size()] );
242 	}
243 
244 	public MessagePart[] getResponseParts()
245 	{
246 		return new MessagePart[0];
247 	}
248 
249 	public String getResponseContentAsXml()
250 	{
251 		HttpResponse response = getResponse();
252 		if( response == null )
253 			return null;
254 
255 		return response.getContentAsXml();
256 	}
257 
258 	public WsdlSubmit<HttpRequest> submit( SubmitContext submitContext, boolean async ) throws SubmitException
259 	{
260 		String endpoint = PropertyExpander.expandProperties( submitContext, getEndpoint() );
261 
262 		if( StringUtils.isNullOrEmpty( endpoint ) )
263 		{
264 			UISupport.showErrorMessage( "Missing endpoint for request [" + getName() + "]" );
265 			return null;
266 		}
267 
268 		try
269 		{
270 			WsdlSubmit<HttpRequest> submitter = new WsdlSubmit<HttpRequest>( this, getSubmitListeners(),
271 					RequestTransportRegistry.getTransport( endpoint, submitContext ) );
272 			submitter.submitRequest( submitContext, async );
273 			return submitter;
274 		}
275 		catch( Exception e )
276 		{
277 			throw new SubmitException( e.toString() );
278 		}
279 	}
280 
281 	public void updateConfig( HttpRequestConfig request )
282 	{
283 		setConfig( request );
284 		if( params == null )
285 			params = new XmlBeansRestParamsTestPropertyHolder( this, request.getParameters() );
286 		else
287 			params.resetPropertiesConfig( request.getParameters() );
288 
289 		List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList();
290 		for( int i = 0; i < attachmentConfigs.size(); i++ )
291 		{
292 			AttachmentConfig config = attachmentConfigs.get( i );
293 			getAttachmentsList().get( i ).updateConfig( config );
294 		}
295 
296 		if( jmsHeaderConfig != null )
297 		{
298 			jmsHeaderConfig.setJMSHeaderConfConfig( request.getJmsConfig() );
299 		}
300 
301 		if( jmsPropertyConfig != null )
302 		{
303 			jmsPropertyConfig.setJmsPropertyConfConfig( request.getJmsPropertyConfig() );
304 		}
305 
306 	}
307 
308 	public AbstractHttpOperation getOperation()
309 	{
310 		return null;
311 	}
312 
313 	public class HttpContentPart extends ContentPart implements MessagePart
314 	{
315 		@Override
316 		public SchemaGlobalElement getPartElement()
317 		{
318 			return null;
319 		}
320 
321 		@Override
322 		public QName getPartElementName()
323 		{
324 			return null;
325 		}
326 
327 		@Override
328 		public SchemaType getSchemaType()
329 		{
330 			return null;
331 		}
332 
333 		public String getDescription()
334 		{
335 			return null;
336 		}
337 
338 		public String getName()
339 		{
340 			return null;
341 		}
342 
343 		public String getMediaType()
344 		{
345 			return getConfig().getMediaType();
346 		}
347 	}
348 
349 	public List<TestProperty> getPropertyList()
350 	{
351 		return params.getPropertyList();
352 	}
353 
354 	private JMSHeaderConfig jmsHeaderConfig;
355 	private JMSPropertiesConfig jmsPropertyConfig;
356 
357 	public JMSHeaderConfig getJMSHeaderConfig()
358 	{
359 		if( jmsHeaderConfig == null )
360 		{
361 			if( !getConfig().isSetJmsConfig() )
362 			{
363 				getConfig().addNewJmsConfig();
364 			}
365 			jmsHeaderConfig = new JMSHeaderConfig( getConfig().getJmsConfig(), this );
366 		}
367 		return jmsHeaderConfig;
368 	}
369 
370 	public JMSPropertiesConfig getJMSPropertiesConfig()
371 	{
372 		if( jmsPropertyConfig == null )
373 		{
374 			if( !getConfig().isSetJmsPropertyConfig() )
375 			{
376 				getConfig().addNewJmsPropertyConfig();
377 			}
378 			jmsPropertyConfig = new JMSPropertiesConfig( getConfig().getJmsPropertyConfig(), this );
379 		}
380 		return jmsPropertyConfig;
381 	}
382 
383 	public void notifyPropertyChanged( String responseContentProperty, String oldContent, String responseContent )
384 	{
385 		notifyPropertyChanged( responseContentProperty, ( Object )oldContent, ( Object )responseContent );
386 	}
387 
388 }