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