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