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