1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl;
14
15 import java.beans.PropertyChangeEvent;
16 import java.beans.PropertyChangeListener;
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import javax.swing.ImageIcon;
26
27 import org.apache.log4j.Logger;
28
29 import com.eviware.soapui.SoapUI;
30 import com.eviware.soapui.config.AttachmentConfig;
31 import com.eviware.soapui.config.CallConfig;
32 import com.eviware.soapui.config.CredentialsConfig;
33 import com.eviware.soapui.impl.wsdl.submit.RequestTransportRegistry;
34 import com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter;
35 import com.eviware.soapui.impl.wsdl.submit.transports.http.AttachmentUtils;
36 import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
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.model.iface.Attachment;
42 import com.eviware.soapui.model.iface.Interface;
43 import com.eviware.soapui.model.iface.MessagePart;
44 import com.eviware.soapui.model.iface.Request;
45 import com.eviware.soapui.model.iface.Submit;
46 import com.eviware.soapui.model.iface.SubmitContext;
47 import com.eviware.soapui.model.iface.SubmitListener;
48 import com.eviware.soapui.model.support.InterfaceListenerAdapter;
49 import com.eviware.soapui.settings.WsdlSettings;
50 import com.eviware.soapui.support.UISupport;
51 import com.eviware.soapui.support.types.StringToStringMap;
52
53 /***
54 * Request implementation holding a SOAP request
55 *
56 * @author Ole.Matzura
57 */
58
59 public class WsdlRequest extends AbstractWsdlModelItem<CallConfig> implements Request, AttachmentContainer
60 {
61 public final static Logger log = Logger.getLogger( WsdlRequest.class );
62
63 public static final String RESPONSE_PROPERTY = WsdlRequest.class.getName() + "@response";
64 public static final String RESPONSE_CONTENT_PROPERTY = WsdlRequest.class.getName() + "@response-content";
65 public static final String ATTACHMENTS_PROPERTY = WsdlRequest.class.getName() + "@attachments";
66 public static final String INLINE_RESPONSE_ATTACHMENTS = WsdlRequest.class.getName() + "@inline-response-attachments";
67 public static final String EXPAND_MTOM_RESPONSE_ATTACHMENTS = WsdlRequest.class.getName() + "@expand-mtom-attachments";
68 public static final String STRIP_WHITESPACES = WsdlRequest.class.getName() + "@strip-whitespaces";
69 public static final String FORCE_MTOM = WsdlRequest.class.getName() + "@force_mtom";
70 public static final String REMOVE_EMPTY_CONTENT = WsdlRequest.class.getName() + "@remove_empty_content";
71 public static final String REQUEST_HEADERS_PROPERTY = WsdlRequest.class.getName() + "@request-headers";
72 public static final String ENCODE_ATTACHMENTS = WsdlRequest.class.getName() + "@encode_attachments";
73 public static final String DISABLE_MULTIPART_ATTACHMENTS = WsdlRequest.class.getName() + "@disable-multipart-attachments";
74 public static final String WSS_TIMETOLIVE = WsdlRequest.class.getName() + "@wss-time-to-live";
75 public static final String BIND_ADDRESS = WsdlRequest.class.getName() + "@bind_address";
76 public static final String OPERATION_PROPERTY = WsdlRequest.class.getName() + "@operation";
77
78 public final static String PW_TYPE_NONE="None";
79 public final static String PW_TYPE_DIGEST="PasswordDigest";
80 public final static String PW_TYPE_TEXT="PasswordText";
81
82 private WsdlOperation operation;
83 private WsdlResponse response;
84 protected List<FileAttachment> attachments = new ArrayList<FileAttachment>();
85
86 private RequestIconAnimator iconAnimator;
87 private Set<SubmitListener> submitListeners = new HashSet<SubmitListener>();
88 private List<WsdlAttachmentPart> definedAttachmentParts;
89 private InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
90 private String requestContent;
91
92 public WsdlRequest( WsdlOperation operation, CallConfig callConfig )
93 {
94 this( operation, callConfig, false );
95 }
96
97 public WsdlRequest( WsdlOperation operation, CallConfig callConfig, boolean forLoadTest )
98 {
99 super( callConfig, operation, null );
100
101 this.operation = operation;
102
103 initEndpoints();
104 initAttachments();
105
106
107 if( callConfig.getEncoding() == null || callConfig.getEncoding().length() == 0 )
108 {
109 callConfig.setEncoding( "UTF-8" );
110 }
111
112 if( !forLoadTest )
113 {
114 iconAnimator = initIconAnimator();
115 addSubmitListener( iconAnimator );
116
117 operation.getInterface().addPropertyChangeListener( interfaceListener );
118 operation.getInterface().addInterfaceListener( interfaceListener );
119 }
120
121 for (SubmitListener listener : SoapUI.getListenerRegistry().getListeners( SubmitListener.class ))
122 {
123 addSubmitListener(listener);
124 }
125 }
126
127 private void initAttachments()
128 {
129 for( AttachmentConfig ac : getConfig().getAttachmentList() )
130 {
131 FileAttachment attachment = new RequestFileAttachment( ac, this );
132 attachments.add( attachment);
133 }
134 }
135
136 public void updateConfig(CallConfig request)
137 {
138 setConfig( request );
139 }
140
141 public ModelItemIconAnimator getIconAnimator()
142 {
143 return iconAnimator;
144 }
145
146 protected RequestIconAnimator initIconAnimator()
147 {
148 return new RequestIconAnimator();
149 }
150
151 protected void initEndpoints()
152 {
153 if( getEndpoint() == null )
154 {
155 String[] endpoints = operation.getInterface().getEndpoints();
156 if( endpoints.length > 0 )
157 {
158 setEndpoint( endpoints[0] );
159 }
160 }
161 }
162
163 public String getRequestContent()
164 {
165 if( getConfig().getRequest() == null )
166 getConfig().addNewRequest();
167
168 if( requestContent == null )
169 requestContent = CompressedStringSupport.getString( getConfig().getRequest() );
170
171 return requestContent;
172 }
173
174 public void setEndpoint(String endpoint)
175 {
176 String old = getEndpoint();
177 if( old != null && old.equals( endpoint ))
178 return;
179
180 getConfig().setEndpoint( endpoint );
181 notifyPropertyChanged( ENDPOINT_PROPERTY, old, endpoint);
182 }
183
184 public String getEndpoint()
185 {
186 return getConfig().getEndpoint();
187 }
188
189 public String getEncoding()
190 {
191 return getConfig().getEncoding();
192 }
193
194 public void setEncoding(String encoding)
195 {
196 String old = getEncoding();
197 getConfig().setEncoding( encoding );
198 notifyPropertyChanged( ENCODING_PROPERTY, old, encoding );
199 }
200
201 public StringToStringMap getRequestHeaders()
202 {
203 return StringToStringMap.fromXml( getSettings().getString( REQUEST_HEADERS_PROPERTY, null ));
204 }
205
206 public void setRequestHeaders( StringToStringMap map )
207 {
208 StringToStringMap old = getRequestHeaders();
209 getSettings().setString( REQUEST_HEADERS_PROPERTY, map.toXml() );
210 notifyPropertyChanged( REQUEST_HEADERS_PROPERTY, old, map );
211 }
212
213 public boolean isInlineResponseAttachments()
214 {
215 return getSettings().getBoolean( INLINE_RESPONSE_ATTACHMENTS );
216 }
217
218 public void setInlineResponseAttachments( boolean inlineResponseAttachments )
219 {
220 boolean old = getSettings().getBoolean( INLINE_RESPONSE_ATTACHMENTS );
221 getSettings().setBoolean( INLINE_RESPONSE_ATTACHMENTS, inlineResponseAttachments );
222 notifyPropertyChanged( INLINE_RESPONSE_ATTACHMENTS, old, inlineResponseAttachments );
223 }
224
225 public boolean isStripWhitespaces()
226 {
227 return getSettings().getBoolean( STRIP_WHITESPACES );
228 }
229
230 public void setStripWhitespaces( boolean stripWhitespaces )
231 {
232 boolean old = getSettings().getBoolean( STRIP_WHITESPACES );
233 getSettings().setBoolean( STRIP_WHITESPACES, stripWhitespaces );
234 notifyPropertyChanged( STRIP_WHITESPACES, old, stripWhitespaces );
235 }
236
237 public boolean isExpandMtomResponseAttachments()
238 {
239 return getSettings().getBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS );
240 }
241
242 public void setExpandMtomResponseAttachments( boolean expandMtomResponseAttachments )
243 {
244 boolean old = getSettings().getBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS );
245 getSettings().setBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS, expandMtomResponseAttachments );
246 notifyPropertyChanged( EXPAND_MTOM_RESPONSE_ATTACHMENTS, old, expandMtomResponseAttachments );
247 }
248
249 /***
250 * Use getResponse().getContentAsString();
251 * @deprecated
252 */
253
254 public String getResponseContent()
255 {
256 return response == null ? null : response.getContentAsString();
257 }
258
259 public WsdlResponse getResponse()
260 {
261 return response;
262 }
263
264 public WsdlOperation getOperation()
265 {
266 return operation;
267 }
268
269 public void setOperation( WsdlOperation wsdlOperation )
270 {
271 WsdlOperation oldOperation = operation;
272 this.operation = wsdlOperation;
273
274 definedAttachmentParts = null;
275 notifyPropertyChanged( OPERATION_PROPERTY, oldOperation, operation );
276 }
277
278 public void setRequestContent(String request)
279 {
280 String old = getRequestContent();
281 if( request.equals( old ))
282 return;
283
284 requestContent = request;
285 definedAttachmentParts = null;
286 notifyPropertyChanged( REQUEST_PROPERTY, old, request );
287 }
288
289 public void setResponse( WsdlResponse response, SubmitContext context )
290 {
291 WsdlResponse oldResponse = (WsdlResponse) getResponse();
292 this.response = response;
293
294 notifyPropertyChanged( RESPONSE_PROPERTY, oldResponse, response );
295 }
296
297 public ImageIcon getIcon()
298 {
299 return iconAnimator.getIcon();
300 }
301
302 public String getUsername()
303 {
304 CredentialsConfig credentialsConfig = getConfig().getCredentials();
305 if( credentialsConfig == null ) return null;
306
307 return credentialsConfig.getUsername();
308 }
309
310 public String getPassword()
311 {
312 CredentialsConfig credentialsConfig = getConfig().getCredentials();
313 if( credentialsConfig == null ) return null;
314
315 return credentialsConfig.getPassword();
316 }
317
318 public String getDomain()
319 {
320 CredentialsConfig credentialsConfig = getConfig().getCredentials();
321 if( credentialsConfig == null ) return null;
322
323 return credentialsConfig.getDomain();
324 }
325
326 public void setUsername( String username )
327 {
328 CredentialsConfig credentialsConfig = getConfig().getCredentials();
329 if( credentialsConfig == null )
330 credentialsConfig = getConfig().addNewCredentials();
331
332 credentialsConfig.setUsername( username );
333 }
334
335 public void setPassword( String password )
336 {
337 CredentialsConfig credentialsConfig = getConfig().getCredentials();
338 if( credentialsConfig == null )
339 credentialsConfig = getConfig().addNewCredentials();
340
341 credentialsConfig.setPassword( password );
342 }
343
344 public void setDomain( String domain )
345 {
346 CredentialsConfig credentialsConfig = getConfig().getCredentials();
347 if( credentialsConfig == null )
348 credentialsConfig = getConfig().addNewCredentials();
349
350 credentialsConfig.setDomain( domain );
351 }
352
353 public void addSubmitListener(SubmitListener listener)
354 {
355 submitListeners.add( listener );
356 }
357
358 public void removeSubmitListener(SubmitListener listener)
359 {
360 submitListeners.remove( listener );
361 }
362
363 public WsdlSubmit submit( SubmitContext submitContext, boolean async ) throws SubmitException
364 {
365 String endpoint = PropertyExpansionRequestFilter.expandProperties( submitContext, getEndpoint());
366 if( endpoint == null || endpoint.trim().length() == 0 )
367 {
368 UISupport.showErrorMessage( "Missing endpoint for request [" + getName() + "]" );
369 return null;
370 }
371
372 try
373 {
374 WsdlSubmit submitter = new WsdlSubmit(this, (SubmitListener[]) submitListeners.toArray(new SubmitListener[submitListeners
375 .size()]), RequestTransportRegistry.getTransport(endpoint, submitContext));
376 submitter.submitRequest(submitContext, async);
377 return submitter;
378 }
379 catch( Exception e )
380 {
381 throw new SubmitException( e.toString() );
382 }
383 }
384
385 private class InternalInterfaceListener extends InterfaceListenerAdapter implements PropertyChangeListener
386 {
387 public void propertyChange(PropertyChangeEvent evt)
388 {
389 if( evt.getPropertyName().equals( Interface.ENDPOINT_PROPERTY ))
390 {
391 String endpoint = getEndpoint();
392 if( evt.getOldValue() != null && evt.getOldValue().equals( endpoint ))
393 {
394 setEndpoint( (String) evt.getNewValue() );
395 }
396 }
397 }
398 }
399
400 public String getWssPasswordType()
401 {
402 String wssPasswordType = getConfig().getWssPasswordType();
403 return wssPasswordType == null || PW_TYPE_NONE.equals( wssPasswordType ) ? null : wssPasswordType;
404 }
405
406 public void setWssPasswordType(String wssPasswordType)
407 {
408 if( wssPasswordType == null || wssPasswordType.equals( PW_TYPE_NONE ))
409 {
410 if( getConfig().isSetWssPasswordType() )
411 getConfig().unsetWssPasswordType();
412 }
413 else
414 {
415 getConfig().setWssPasswordType( wssPasswordType );
416 }
417 }
418
419
420
421
422 public Attachment attachFile( File file, boolean cache )
423 {
424 try
425 {
426 FileAttachment fileAttachment = new RequestFileAttachment( file, cache, this );
427 attachments.add( fileAttachment );
428 notifyPropertyChanged(ATTACHMENTS_PROPERTY, null, fileAttachment );
429 return fileAttachment;
430 }
431 catch (IOException e)
432 {
433 UISupport.showErrorMessage( e );
434 return null;
435 }
436 }
437
438
439
440
441 public int getAttachmentCount()
442 {
443 return attachments.size();
444 }
445
446
447
448
449 public Attachment getAttachmentAt( int index )
450 {
451 return attachments.get( index );
452 }
453
454
455
456
457 public Attachment [] getAttachmentsForPart( String partName )
458 {
459 List<Attachment> result = new ArrayList<Attachment>();
460
461 for( Attachment attachment : attachments )
462 {
463 if( attachment.getPart().equals( partName ))
464 result.add( attachment );
465 }
466
467 return result.toArray( new Attachment[result.size()]);
468 }
469
470
471
472
473 public void removeAttachment( Attachment attachment )
474 {
475 int ix = attachments.indexOf( attachment );
476 attachments.remove( ix );
477
478 try
479 {
480 notifyPropertyChanged(ATTACHMENTS_PROPERTY, attachment, null );
481 }
482 finally
483 {
484 getConfig().removeAttachment( ix);
485 }
486 }
487
488
489
490
491 public Attachment[] getAttachments()
492 {
493 return attachments.toArray( new Attachment[attachments.size()] );
494 }
495
496
497
498
499 public WsdlAttachmentPart [] getDefinedAttachmentParts()
500 {
501 if( definedAttachmentParts == null )
502 {
503 try
504 {
505 UISupport.setHourglassCursor();
506 definedAttachmentParts = AttachmentUtils.extractAttachmentParts(
507 operation, getRequestContent(), true, false );
508 }
509 catch (Exception e)
510 {
511 log.warn( e.toString() );
512 definedAttachmentParts = new ArrayList<WsdlAttachmentPart>();
513 }
514 finally
515 {
516 UISupport.resetCursor();
517 }
518 }
519
520 return definedAttachmentParts.toArray( new WsdlAttachmentPart[definedAttachmentParts.size()] );
521 }
522
523
524
525
526 public WsdlAttachmentPart getAttachmentPart( String partName )
527 {
528 WsdlAttachmentPart[] parts = getDefinedAttachmentParts();
529 for( WsdlAttachmentPart part : parts )
530 {
531 if( part.getName().equals( partName ))
532 return part;
533 }
534
535 return null;
536 }
537
538 public void copyAttachmentsTo(WsdlRequest newRequest)
539 {
540 if( getAttachmentCount() > 0 )
541 {
542 try
543 {
544 UISupport.setHourglassCursor();
545 for (int c = 0; c < getAttachmentCount(); c++)
546 {
547 try
548 {
549 Attachment attachment = getAttachmentAt(c);
550 newRequest.addAttachment( attachment );
551 }
552 catch (Exception e)
553 {
554 SoapUI.logError( e );
555 }
556 }
557 }
558 finally
559 {
560 UISupport.resetCursor();
561 }
562 }
563 }
564
565 private Attachment addAttachment(Attachment attachment)
566 {
567 if( attachment instanceof FileAttachment )
568 {
569 AttachmentConfig oldConfig = ((FileAttachment)attachment).getConfig();
570 AttachmentConfig newConfig = (AttachmentConfig) getConfig().addNewAttachment().set( oldConfig );
571 FileAttachment newAttachment = new RequestFileAttachment( newConfig, this );
572 attachments.add( newAttachment);
573 return newAttachment;
574 }
575 else log.error( "Unkown attachment type: " + attachment );
576
577 return null;
578 }
579
580 public void copyTo(WsdlRequest newRequest, boolean copyAttachments, boolean copyHeaders)
581 {
582 newRequest.setEncoding( getEncoding() );
583 newRequest.setEndpoint( getEndpoint() );
584 newRequest.setRequestContent( getRequestContent() );
585 newRequest.setWssPasswordType( getWssPasswordType() );
586
587 CredentialsConfig credentials = getConfig().getCredentials();
588 if( credentials != null)
589 newRequest.getConfig().setCredentials( (CredentialsConfig) credentials.copy() );
590
591 if( copyAttachments )
592 copyAttachmentsTo( newRequest );
593
594 if( copyHeaders )
595 newRequest.setRequestHeaders( getRequestHeaders() );
596 }
597
598
599
600
601 public boolean isMtomEnabled()
602 {
603 return getSettings().getBoolean( WsdlSettings.ENABLE_MTOM );
604 }
605
606 public void setMtomEnabled( boolean mtomEnabled )
607 {
608 getSettings().setBoolean( WsdlSettings.ENABLE_MTOM, mtomEnabled );
609 definedAttachmentParts = null;
610 }
611
612 public void release()
613 {
614 super.release();
615
616 getOperation().getInterface().removeInterfaceListener( interfaceListener );
617 getOperation().getInterface().removePropertyChangeListener( interfaceListener );
618
619 submitListeners.clear();
620 }
621
622 public MessagePart[] getRequestParts()
623 {
624 try
625 {
626 List<MessagePart> result = new ArrayList<MessagePart>();
627 result.addAll( Arrays.asList( getOperation().getDefaultRequestParts() ));
628 result.addAll( Arrays.asList( getDefinedAttachmentParts()) );
629
630 return result.toArray( new MessagePart[result.size()] );
631 }
632 catch (Exception e)
633 {
634 SoapUI.logError( e );
635 return new MessagePart [0];
636 }
637 }
638
639 public MessagePart[] getResponseParts()
640 {
641 try
642 {
643 List<MessagePart> result = new ArrayList<MessagePart>();
644 result.addAll( Arrays.asList( getOperation().getDefaultResponseParts() ));
645
646 if( getResponse() != null )
647 result.addAll( AttachmentUtils.extractAttachmentParts(
648 (WsdlOperation)getOperation(), getResponse().getContentAsString(), true, true ));
649
650 return result.toArray( new MessagePart[result.size()] );
651 }
652 catch (Exception e)
653 {
654 SoapUI.logError( e );
655 return new MessagePart [0];
656 }
657 }
658
659 protected class RequestIconAnimator extends ModelItemIconAnimator implements SubmitListener
660 {
661 public RequestIconAnimator()
662 {
663 super( WsdlRequest.this, "/request.gif",
664 new String[] {"/exec_request_1.gif", "/exec_request_2.gif",
665 "/exec_request_3.gif", "/exec_request_4.gif"} );
666 }
667
668 public boolean beforeSubmit(Submit submit, SubmitContext context)
669 {
670 if( isEnabled() && submit.getRequest() == getTarget() )
671 start();
672 return true;
673 }
674
675 public void afterSubmit(Submit submit, SubmitContext context)
676 {
677 if( submit.getRequest() == getTarget() )
678 stop();
679 }
680 }
681
682 public boolean isMultipartEnabled()
683 {
684 return !getSettings().getBoolean( DISABLE_MULTIPART_ATTACHMENTS );
685 }
686
687 public void setMultipartEnabled( boolean multipartEnabled )
688 {
689 getSettings().setBoolean( DISABLE_MULTIPART_ATTACHMENTS, !multipartEnabled );
690 }
691
692 public String getWssTimeToLive()
693 {
694 return getSettings().getString( WSS_TIMETOLIVE, null );
695 }
696
697 public void setWssTimeToLive( String ttl )
698 {
699 getSettings().setString( WSS_TIMETOLIVE, ttl );
700 }
701
702 @Override
703 public void onSave()
704 {
705 if( requestContent != null )
706 {
707 CompressedStringSupport.setString( getConfig().getRequest(), requestContent );
708 requestContent = null;
709 }
710 }
711
712 public long getContentLength()
713 {
714 return getRequestContent().length();
715 }
716
717 public boolean isRemoveEmptyContent()
718 {
719 return getSettings().getBoolean( REMOVE_EMPTY_CONTENT );
720 }
721
722 public void setRemoveEmptyContent( boolean removeEmptyContent )
723 {
724 boolean old = getSettings().getBoolean( REMOVE_EMPTY_CONTENT );
725 getSettings().setBoolean( REMOVE_EMPTY_CONTENT, removeEmptyContent );
726 notifyPropertyChanged( REMOVE_EMPTY_CONTENT, old, removeEmptyContent );
727 }
728
729 public boolean isForceMtom()
730 {
731 return getSettings().getBoolean( FORCE_MTOM );
732 }
733
734 public void setForceMtom( boolean forceMtom )
735 {
736 boolean old = getSettings().getBoolean( FORCE_MTOM );
737 getSettings().setBoolean( FORCE_MTOM, forceMtom );
738 notifyPropertyChanged( FORCE_MTOM, old, forceMtom );
739 }
740
741 public boolean isEncodeAttachments()
742 {
743 return getSettings().getBoolean( ENCODE_ATTACHMENTS );
744 }
745
746 public void setEncodeAttachments( boolean encodeAttachments )
747 {
748 boolean old = getSettings().getBoolean( ENCODE_ATTACHMENTS );
749 getSettings().setBoolean( ENCODE_ATTACHMENTS, encodeAttachments );
750 notifyPropertyChanged( ENCODE_ATTACHMENTS, old, encodeAttachments );
751 }
752
753 public String getBindAddress()
754 {
755 return getSettings().getString( BIND_ADDRESS, "" );
756 }
757
758 public void setBindAddress( String bindAddress )
759 {
760 String old = getSettings().getString( BIND_ADDRESS, "" );
761 getSettings().setString( BIND_ADDRESS, bindAddress );
762 notifyPropertyChanged( BIND_ADDRESS, old, bindAddress );
763 }
764
765 public void addAttachmentsChangeListener( PropertyChangeListener listener )
766 {
767 addPropertyChangeListener( ATTACHMENTS_PROPERTY, listener );
768 }
769
770 public boolean isReadOnly()
771 {
772 return false;
773 }
774
775 public void removeAttachmentsChangeListener( PropertyChangeListener listener )
776 {
777 removePropertyChangeListener( ATTACHMENTS_PROPERTY, listener );
778 }
779 }