View Javadoc

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