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 String getPropertiesLabel()
188 	{
189 		return "HTTP Params";
190 	}
191 
192 	public void removeTestPropertyListener( TestPropertyListener listener )
193 	{
194 		params.removeTestPropertyListener( listener );
195 	}
196 
197 	public HttpAttachmentPart getAttachmentPart( String partName )
198 	{
199 		return null;
200 	}
201 
202 	public HttpAttachmentPart[] getDefinedAttachmentParts()
203 	{
204 		return new HttpAttachmentPart[0];
205 	}
206 
207 	@Override
208 	public RestRequestInterface.RequestMethod getMethod()
209 	{
210 		String method = getConfig().getMethod();
211 		return method == null ? null : RestRequestInterface.RequestMethod.valueOf( method );
212 	}
213 
214 	public MessagePart[] getRequestParts()
215 	{
216 		List<MessagePart> result = new ArrayList<MessagePart>();
217 
218 		for( int c = 0; c < getPropertyCount(); c++ )
219 		{
220 			result.add( new ParameterMessagePart( getPropertyAt( c ) ) );
221 		}
222 
223 		if( getMethod() == RestRequestInterface.RequestMethod.POST
224 				|| getMethod() == RestRequestInterface.RequestMethod.PUT )
225 		{
226 			result.add( new HttpContentPart() );
227 		}
228 
229 		return result.toArray( new MessagePart[result.size()] );
230 	}
231 
232 	public MessagePart[] getResponseParts()
233 	{
234 		return new MessagePart[0];
235 	}
236 
237 	public String getResponseContentAsXml()
238 	{
239 		HttpResponse response = getResponse();
240 		if( response == null )
241 			return null;
242 
243 		return response.getContentAsXml();
244 	}
245 
246 	public WsdlSubmit<HttpRequest> submit( SubmitContext submitContext, boolean async ) throws SubmitException
247 	{
248 		String endpoint = PropertyExpander.expandProperties( submitContext, getEndpoint() );
249 
250 		if( StringUtils.isNullOrEmpty( endpoint ) )
251 		{
252 			UISupport.showErrorMessage( "Missing endpoint for request [" + getName() + "]" );
253 			return null;
254 		}
255 
256 		try
257 		{
258 			WsdlSubmit<HttpRequest> submitter = new WsdlSubmit<HttpRequest>( this, getSubmitListeners(),
259 					RequestTransportRegistry.getTransport( endpoint, submitContext ) );
260 			submitter.submitRequest( submitContext, async );
261 			return submitter;
262 		}
263 		catch( Exception e )
264 		{
265 			throw new SubmitException( e.toString() );
266 		}
267 	}
268 
269 	public void updateConfig( HttpRequestConfig request )
270 	{
271 		setConfig( request );
272 		if( params == null )
273 			params = new XmlBeansRestParamsTestPropertyHolder( this, request.getParameters() );
274 		else
275 			params.resetPropertiesConfig( request.getParameters() );
276 
277 		List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList();
278 		for( int i = 0; i < attachmentConfigs.size(); i++ )
279 		{
280 			AttachmentConfig config = attachmentConfigs.get( i );
281 			getAttachmentsList().get( i ).updateConfig( config );
282 		}
283 	}
284 
285 	public AbstractHttpOperation getOperation()
286 	{
287 		return null;
288 	}
289 
290 	public class HttpContentPart extends ContentPart implements MessagePart
291 	{
292 		@Override
293 		public SchemaGlobalElement getPartElement()
294 		{
295 			return null;
296 		}
297 
298 		@Override
299 		public QName getPartElementName()
300 		{
301 			return null;
302 		}
303 
304 		@Override
305 		public SchemaType getSchemaType()
306 		{
307 			return null;
308 		}
309 
310 		public String getDescription()
311 		{
312 			return null;
313 		}
314 
315 		public String getName()
316 		{
317 			return null;
318 		}
319 
320 		public String getMediaType()
321 		{
322 			return getConfig().getMediaType();
323 		}
324 	}
325 
326 	public List<TestProperty> getPropertyList()
327 	{
328 		return params.getPropertyList();
329 	}
330 
331 	private JMSHeaderConfig jmsHeaderConfig;
332 	private JMSPropertiesConfig jmsPropertyConfig;
333 
334 	public JMSHeaderConfig getJMSHeaderConfig()
335 	{
336 		if( jmsHeaderConfig == null )
337 		{
338 			if( !getConfig().isSetJmsConfig() )
339 			{
340 				getConfig().addNewJmsConfig();
341 			}
342 			jmsHeaderConfig = new JMSHeaderConfig( getConfig().getJmsConfig(), this );
343 		}
344 		return jmsHeaderConfig;
345 	}
346 
347 	public JMSPropertiesConfig getJMSPropertiesConfig()
348 	{
349 		if( jmsPropertyConfig == null )
350 		{
351 			if( !getConfig().isSetJmsPropertyConfig() )
352 			{
353 				getConfig().addNewJmsPropertyConfig();
354 			}
355 			jmsPropertyConfig = new JMSPropertiesConfig( getConfig().getJmsPropertyConfig(), this );
356 		}
357 		return jmsPropertyConfig;
358 	}
359 
360 	public void notifyPropertyChanged( String responseContentProperty, String oldContent, String responseContent )
361 	{
362 		notifyPropertyChanged( responseContentProperty, ( Object )oldContent, ( Object )responseContent );
363 	}
364 }