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  
13  package com.eviware.soapui.impl.support;
14  
15  import java.beans.PropertyChangeListener;
16  import java.io.File;
17  import java.io.IOException;
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  import javax.swing.ImageIcon;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.config.AbstractRequestConfig;
27  import com.eviware.soapui.config.AttachmentConfig;
28  import com.eviware.soapui.config.CredentialsConfig;
29  import com.eviware.soapui.impl.rest.RestRequestInterface;
30  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
31  import com.eviware.soapui.impl.wsdl.HttpAttachmentPart;
32  import com.eviware.soapui.impl.wsdl.WsdlRequest;
33  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
34  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.methods.IAfterRequestInjection;
35  import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
36  import com.eviware.soapui.impl.wsdl.support.FileAttachment;
37  import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
38  import com.eviware.soapui.impl.wsdl.support.RequestFileAttachment;
39  import com.eviware.soapui.impl.wsdl.teststeps.SettingPathPropertySupport;
40  import com.eviware.soapui.model.iface.Attachment;
41  import com.eviware.soapui.model.iface.Request;
42  import com.eviware.soapui.model.iface.Submit;
43  import com.eviware.soapui.model.iface.SubmitContext;
44  import com.eviware.soapui.model.iface.SubmitListener;
45  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
46  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
47  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
48  import com.eviware.soapui.settings.CommonSettings;
49  import com.eviware.soapui.settings.WsdlSettings;
50  import com.eviware.soapui.support.StringUtils;
51  import com.eviware.soapui.support.UISupport;
52  import com.eviware.soapui.support.resolver.ResolveContext;
53  import com.eviware.soapui.support.types.StringToStringMap;
54  
55  public abstract class AbstractHttpRequest<T extends AbstractRequestConfig> extends AbstractWsdlModelItem<T> implements
56  		Request, AbstractHttpRequestInterface<T>
57  {
58  
59  	private Set<SubmitListener> submitListeners = new HashSet<SubmitListener>();
60  	private String requestContent;
61  	private RequestIconAnimator<?> iconAnimator;
62  	private HttpResponse response;
63  	private SettingPathPropertySupport dumpFile;
64  	private List<FileAttachment<?>> attachments = new ArrayList<FileAttachment<?>>();
65  	private IAfterRequestInjection afterRequestInjection;
66  
67  	protected AbstractHttpRequest( T config, AbstractHttpOperation parent, String icon, boolean forLoadTest )
68  	{
69  		super( config, parent, icon );
70  
71  		if( !forLoadTest && !UISupport.isHeadless() )
72  		{
73  			iconAnimator = initIconAnimator();
74  			addSubmitListener( iconAnimator );
75  		}
76  
77  		initAttachments();
78  
79  		dumpFile = new SettingPathPropertySupport( this, DUMP_FILE );
80  	}
81  
82  	private void initAttachments()
83  	{
84  		for( AttachmentConfig ac : getConfig().getAttachmentList() )
85  		{
86  			RequestFileAttachment attachment = new RequestFileAttachment( ac, this );
87  			attachments.add( attachment );
88  		}
89  	}
90  
91  	protected List<FileAttachment<?>> getAttachmentsList()
92  	{
93  		return attachments;
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 * 
99  	 * @see
100 	 * com.eviware.soapui.impl.wsdl.AttachmentContainer#attachFile(java.io.File,
101 	 * boolean)
102 	 */
103 
104 	public Attachment attachFile( File file, boolean cache ) throws IOException
105 	{
106 		RequestFileAttachment fileAttachment = new RequestFileAttachment( file, cache, this );
107 		attachments.add( fileAttachment );
108 		notifyPropertyChanged( ATTACHMENTS_PROPERTY, null, fileAttachment );
109 		return fileAttachment;
110 	}
111 
112 	public abstract RestRequestInterface.RequestMethod getMethod();
113 
114 	/***
115 	 * Override just to get a better return type
116 	 * 
117 	 * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachmentPart(java.lang.String)
118 	 */
119 
120 	public abstract HttpAttachmentPart getAttachmentPart( String partName );
121 
122 	/*
123 	 * (non-Javadoc)
124 	 * 
125 	 * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachmentCount()
126 	 */
127 	public int getAttachmentCount()
128 	{
129 		return attachments.size();
130 	}
131 
132 	/*
133 	 * (non-Javadoc)
134 	 * 
135 	 * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachmentAt(int)
136 	 */
137 	public Attachment getAttachmentAt( int index )
138 	{
139 		return attachments.get( index );
140 	}
141 
142 	/*
143 	 * (non-Javadoc)
144 	 * 
145 	 * @see
146 	 * com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachmentsForPart
147 	 * (java.lang.String)
148 	 */
149 	public Attachment[] getAttachmentsForPart( String partName )
150 	{
151 		List<Attachment> result = new ArrayList<Attachment>();
152 
153 		for( Attachment attachment : attachments )
154 		{
155 			if( partName.equals( attachment.getPart() ) )
156 				result.add( attachment );
157 		}
158 
159 		return result.toArray( new Attachment[result.size()] );
160 	}
161 
162 	/*
163 	 * (non-Javadoc)
164 	 * 
165 	 * @see
166 	 * com.eviware.soapui.impl.wsdl.AttachmentContainer#removeAttachment(com.
167 	 * eviware.soapui.model.iface.Attachment)
168 	 */
169 	public void removeAttachment( Attachment attachment )
170 	{
171 		int ix = attachments.indexOf( attachment );
172 		attachments.remove( ix );
173 
174 		try
175 		{
176 			notifyPropertyChanged( ATTACHMENTS_PROPERTY, attachment, null );
177 		}
178 		finally
179 		{
180 			getConfig().removeAttachment( ix );
181 		}
182 	}
183 
184 	/*
185 	 * (non-Javadoc)
186 	 * 
187 	 * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachments()
188 	 */
189 	public Attachment[] getAttachments()
190 	{
191 		return attachments.toArray( new Attachment[attachments.size()] );
192 	}
193 
194 	protected RequestIconAnimator<?> initIconAnimator()
195 	{
196 		return new RequestIconAnimator<AbstractHttpRequest<?>>( this, "/request.gif", "/exec_request", 4, "gif" );
197 	}
198 
199 	public void addSubmitListener( SubmitListener listener )
200 	{
201 		submitListeners.add( listener );
202 	}
203 
204 	public void removeSubmitListener( SubmitListener listener )
205 	{
206 		submitListeners.remove( listener );
207 	}
208 
209 	public boolean isMultipartEnabled()
210 	{
211 		return !getSettings().getBoolean( DISABLE_MULTIPART_ATTACHMENTS );
212 	}
213 
214 	public void setMultipartEnabled( boolean multipartEnabled )
215 	{
216 		getSettings().setBoolean( DISABLE_MULTIPART_ATTACHMENTS, !multipartEnabled );
217 	}
218 
219 	public boolean isEntitizeProperties()
220 	{
221 		return getSettings().getBoolean( CommonSettings.ENTITIZE_PROPERTIES );
222 	}
223 
224 	public void setEntitizeProperties( boolean entitizeProperties )
225 	{
226 		getSettings().setBoolean( CommonSettings.ENTITIZE_PROPERTIES, entitizeProperties );
227 	}
228 
229 	@Override
230 	public void release()
231 	{
232 		submitListeners.clear();
233 
234 		super.release();
235 	}
236 
237 	public SubmitListener[] getSubmitListeners()
238 	{
239 		return submitListeners.toArray( new SubmitListener[submitListeners.size()] );
240 	}
241 
242 	public AbstractHttpOperation getOperation()
243 	{
244 		return ( AbstractHttpOperation )getParent();
245 	}
246 
247 	public void copyAttachmentsTo( WsdlRequest newRequest )
248 	{
249 		if( getAttachmentCount() > 0 )
250 		{
251 			try
252 			{
253 				UISupport.setHourglassCursor();
254 				for( int c = 0; c < getAttachmentCount(); c++ )
255 				{
256 					try
257 					{
258 						Attachment attachment = getAttachmentAt( c );
259 						newRequest.importAttachment( attachment );
260 					}
261 					catch( Exception e )
262 					{
263 						SoapUI.logError( e );
264 					}
265 				}
266 			}
267 			finally
268 			{
269 				UISupport.resetCursor();
270 			}
271 		}
272 	}
273 
274 	public Attachment importAttachment( Attachment attachment )
275 	{
276 		if( attachment instanceof FileAttachment )
277 		{
278 			AttachmentConfig oldConfig = ( ( FileAttachment<?> )attachment ).getConfig();
279 			AttachmentConfig newConfig = ( AttachmentConfig )getConfig().addNewAttachment().set( oldConfig );
280 			RequestFileAttachment newAttachment = new RequestFileAttachment( newConfig, this );
281 			attachments.add( newAttachment );
282 			return newAttachment;
283 		}
284 		else
285 			log.error( "Unkown attachment type: " + attachment );
286 
287 		return null;
288 	}
289 
290 	public void addAttachmentsChangeListener( PropertyChangeListener listener )
291 	{
292 		addPropertyChangeListener( ATTACHMENTS_PROPERTY, listener );
293 	}
294 
295 	public boolean isReadOnly()
296 	{
297 		return false;
298 	}
299 
300 	public void removeAttachmentsChangeListener( PropertyChangeListener listener )
301 	{
302 		removePropertyChangeListener( ATTACHMENTS_PROPERTY, listener );
303 	}
304 
305 	public String getRequestContent()
306 	{
307 		if( getConfig().getRequest() == null )
308 			getConfig().addNewRequest();
309 
310 		if( requestContent == null )
311 			requestContent = CompressedStringSupport.getString( getConfig().getRequest() );
312 
313 		return requestContent;
314 	}
315 
316 	public void setRequestContent( String request )
317 	{
318 		String old = getRequestContent();
319 
320 		if( StringUtils.isNullOrEmpty( request ) && StringUtils.isNullOrEmpty( old )
321 				|| ( request != null && request.equals( old ) ) )
322 			return;
323 
324 		requestContent = request;
325 		notifyPropertyChanged( REQUEST_PROPERTY, old, request );
326 	}
327 
328 	public boolean isPrettyPrint()
329 	{
330 		return getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
331 	}
332 
333 	public void setPrettyPrint( boolean prettyPrint )
334 	{
335 		boolean old = getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
336 		getSettings().setBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES, prettyPrint );
337 		notifyPropertyChanged( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES, old, prettyPrint );
338 	}
339 
340 	public void setEndpoint( String endpoint )
341 	{
342 		String old = getEndpoint();
343 		if( old != null && old.equals( endpoint ) )
344 			return;
345 
346 		getConfig().setEndpoint( endpoint );
347 		notifyPropertyChanged( ENDPOINT_PROPERTY, old, endpoint );
348 	}
349 
350 	public String getEndpoint()
351 	{
352 		return getConfig().getEndpoint();
353 	}
354 
355 	public String getEncoding()
356 	{
357 		return getConfig().getEncoding();
358 	}
359 
360 	public void setEncoding( String encoding )
361 	{
362 		String old = getEncoding();
363 		getConfig().setEncoding( encoding );
364 		notifyPropertyChanged( ENCODING_PROPERTY, old, encoding );
365 	}
366 
367 	public StringToStringMap getRequestHeaders()
368 	{
369 		return StringToStringMap.fromXml( getSettings().getString( REQUEST_HEADERS_PROPERTY, null ) );
370 	}
371 
372 	public RequestIconAnimator<?> getIconAnimator()
373 	{
374 		return iconAnimator;
375 	}
376 
377 	public void setRequestHeaders( StringToStringMap map )
378 	{
379 		StringToStringMap old = getRequestHeaders();
380 		getSettings().setString( REQUEST_HEADERS_PROPERTY, map.toXml() );
381 		notifyPropertyChanged( REQUEST_HEADERS_PROPERTY, old, map );
382 	}
383 
384 	@Override
385 	public ImageIcon getIcon()
386 	{
387 		return iconAnimator == null || UISupport.isHeadless() ? null : iconAnimator.getIcon();
388 	}
389 
390 	public PropertyExpansion[] getPropertyExpansions()
391 	{
392 		PropertyExpansionsResult result = new PropertyExpansionsResult( this, this );
393 
394 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "requestContent" ) );
395 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "endpoint" ) );
396 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "username" ) );
397 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "password" ) );
398 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "domain" ) );
399 
400 		return result.toArray();
401 	}
402 
403 	public String getUsername()
404 	{
405 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
406 		if( credentialsConfig == null )
407 			return null;
408 
409 		return credentialsConfig.getUsername();
410 	}
411 
412 	public String getPassword()
413 	{
414 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
415 		if( credentialsConfig == null )
416 			return null;
417 
418 		return credentialsConfig.getPassword();
419 	}
420 
421 	public String getDomain()
422 	{
423 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
424 		if( credentialsConfig == null )
425 			return null;
426 
427 		return credentialsConfig.getDomain();
428 	}
429 
430 	public void setUsername( String username )
431 	{
432 		String old = getUsername();
433 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
434 		if( credentialsConfig == null )
435 			credentialsConfig = getConfig().addNewCredentials();
436 
437 		credentialsConfig.setUsername( username );
438 		notifyPropertyChanged( "username", old, username );
439 	}
440 
441 	public void setPassword( String password )
442 	{
443 		String old = getPassword();
444 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
445 		if( credentialsConfig == null )
446 			credentialsConfig = getConfig().addNewCredentials();
447 
448 		credentialsConfig.setPassword( password );
449 		notifyPropertyChanged( "password", old, password );
450 	}
451 
452 	public void setDomain( String domain )
453 	{
454 		String old = getDomain();
455 		CredentialsConfig credentialsConfig = getConfig().getCredentials();
456 		if( credentialsConfig == null )
457 			credentialsConfig = getConfig().addNewCredentials();
458 
459 		credentialsConfig.setDomain( domain );
460 		notifyPropertyChanged( "domain", old, domain );
461 	}
462 
463 	public String getSslKeystore()
464 	{
465 		return getConfig().getSslKeystore();
466 	}
467 
468 	public void setSslKeystore( String sslKeystore )
469 	{
470 		String old = getSslKeystore();
471 		getConfig().setSslKeystore( sslKeystore );
472 		notifyPropertyChanged( "sslKeystore", old, sslKeystore );
473 	}
474 
475 	public String getBindAddress()
476 	{
477 		return getSettings().getString( BIND_ADDRESS, "" );
478 	}
479 
480 	public void setBindAddress( String bindAddress )
481 	{
482 		String old = getSettings().getString( BIND_ADDRESS, "" );
483 		getSettings().setString( BIND_ADDRESS, bindAddress );
484 		notifyPropertyChanged( BIND_ADDRESS, old, bindAddress );
485 	}
486 
487 	public long getMaxSize()
488 	{
489 		return getSettings().getLong( MAX_SIZE, 0 );
490 	}
491 
492 	public void setMaxSize( long maxSize )
493 	{
494 		long old = getSettings().getLong( MAX_SIZE, 0 );
495 		getSettings().setLong( MAX_SIZE, maxSize );
496 		notifyPropertyChanged( MAX_SIZE, old, maxSize );
497 	}
498 
499 	public String getDumpFile()
500 	{
501 		return dumpFile.get();
502 	}
503 
504 	public void setDumpFile( String df )
505 	{
506 		String old = getDumpFile();
507 		dumpFile.set( df, false );
508 		notifyPropertyChanged( DUMP_FILE, old, getDumpFile() );
509 	}
510 
511 	public boolean isRemoveEmptyContent()
512 	{
513 		return getSettings().getBoolean( REMOVE_EMPTY_CONTENT );
514 	}
515 
516 	public void setRemoveEmptyContent( boolean removeEmptyContent )
517 	{
518 		boolean old = getSettings().getBoolean( REMOVE_EMPTY_CONTENT );
519 		getSettings().setBoolean( REMOVE_EMPTY_CONTENT, removeEmptyContent );
520 		notifyPropertyChanged( REMOVE_EMPTY_CONTENT, old, removeEmptyContent );
521 	}
522 
523 	public boolean isStripWhitespaces()
524 	{
525 		return getSettings().getBoolean( STRIP_WHITESPACES );
526 	}
527 
528 	public void setStripWhitespaces( boolean stripWhitespaces )
529 	{
530 		boolean old = getSettings().getBoolean( STRIP_WHITESPACES );
531 		getSettings().setBoolean( STRIP_WHITESPACES, stripWhitespaces );
532 		notifyPropertyChanged( STRIP_WHITESPACES, old, stripWhitespaces );
533 	}
534 
535 	public boolean isFollowRedirects()
536 	{
537 		return getSettings().getBoolean( FOLLOW_REDIRECTS );
538 	}
539 
540 	public void setFollowRedirects( boolean followRedirects )
541 	{
542 		boolean old = getSettings().getBoolean( FOLLOW_REDIRECTS );
543 		getSettings().setBoolean( FOLLOW_REDIRECTS, followRedirects );
544 		notifyPropertyChanged( FOLLOW_REDIRECTS, old, followRedirects );
545 	}
546 
547 	@Override
548 	public void beforeSave()
549 	{
550 		super.beforeSave();
551 
552 		if( requestContent != null )
553 		{
554 			if( getConfig().getRequest() == null )
555 				getConfig().addNewRequest();
556 
557 			CompressedStringSupport.setString( getConfig().getRequest(), requestContent );
558 			// requestContent = null;
559 		}
560 	}
561 
562 	public static class RequestIconAnimator<T extends AbstractHttpRequest<?>> extends ModelItemIconAnimator<T> implements
563 			SubmitListener
564 	{
565 		public RequestIconAnimator( T modelItem, String baseIcon, String animIconRoot, int iconCount, String iconExtension )
566 		{
567 			super( modelItem, baseIcon, animIconRoot, iconCount, iconExtension );
568 		}
569 
570 		public boolean beforeSubmit( Submit submit, SubmitContext context )
571 		{
572 			if( isEnabled() && submit.getRequest() == getTarget() )
573 				start();
574 			return true;
575 		}
576 
577 		public void afterSubmit( Submit submit, SubmitContext context )
578 		{
579 			if( submit.getRequest() == getTarget() )
580 				stop();
581 		}
582 	}
583 
584 	public void setIconAnimator( RequestIconAnimator<?> iconAnimator )
585 	{
586 		if( this.iconAnimator != null )
587 			removeSubmitListener( this.iconAnimator );
588 
589 		this.iconAnimator = iconAnimator;
590 		addSubmitListener( this.iconAnimator );
591 	}
592 
593 	public HttpResponse getResponse()
594 	{
595 		return response;
596 	}
597 
598 	public void setResponse( HttpResponse response, SubmitContext context )
599 	{
600 		HttpResponse oldResponse = getResponse();
601 		this.response = response;
602 
603 		notifyPropertyChanged( RESPONSE_PROPERTY, oldResponse, response );
604 	}
605 
606 	public void resolve( ResolveContext<?> context )
607 	{
608 		super.resolve( context );
609 
610 		for( FileAttachment<?> attachment : attachments )
611 			attachment.resolve( context );
612 	}
613 
614 	public boolean hasEndpoint()
615 	{
616 		return StringUtils.hasContent( getEndpoint() );
617 	}
618 
619 	public void setAfterRequestInjection( IAfterRequestInjection afterRequestInjection )
620 	{
621 		this.afterRequestInjection = afterRequestInjection;
622 	}
623 
624 	public IAfterRequestInjection getAfterRequestInjection()
625 	{
626 		return afterRequestInjection;
627 	}
628 }