View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.wsdl;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.CredentialsConfig;
17  import com.eviware.soapui.config.WsdlRequestConfig;
18  import com.eviware.soapui.impl.support.AbstractHttpRequest;
19  import com.eviware.soapui.impl.wsdl.submit.RequestTransportRegistry;
20  import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
21  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.AttachmentUtils;
22  import com.eviware.soapui.impl.wsdl.support.wsa.WsaConfig;
23  import com.eviware.soapui.impl.wsdl.support.wsa.WsaContainer;
24  import com.eviware.soapui.model.ModelItem;
25  import com.eviware.soapui.model.iface.Attachment.AttachmentEncoding;
26  import com.eviware.soapui.model.iface.Interface;
27  import com.eviware.soapui.model.iface.MessagePart;
28  import com.eviware.soapui.model.iface.SubmitContext;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
31  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
32  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
33  import com.eviware.soapui.model.support.InterfaceListenerAdapter;
34  import com.eviware.soapui.settings.WsdlSettings;
35  import com.eviware.soapui.support.StringUtils;
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.soapui.support.types.StringToStringMap;
38  import org.apache.log4j.Logger;
39  
40  import java.beans.PropertyChangeEvent;
41  import java.beans.PropertyChangeListener;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.List;
45  
46  /***
47   * Request implementation holding a SOAP request
48   *
49   * @author Ole.Matzura
50   */
51  
52  public class WsdlRequest extends AbstractHttpRequest<WsdlRequestConfig> implements WsdlAttachmentContainer, PropertyExpansionContainer, WsaContainer
53  {
54     public final static Logger log = Logger.getLogger( WsdlRequest.class );
55  
56     public static final String RESPONSE_CONTENT_PROPERTY = WsdlRequest.class.getName() + "@response-content";
57     public static final String INLINE_RESPONSE_ATTACHMENTS = WsdlRequest.class.getName() + "@inline-response-attachments";
58     public static final String EXPAND_MTOM_RESPONSE_ATTACHMENTS = WsdlRequest.class.getName() + "@expand-mtom-attachments";
59     public static final String FORCE_MTOM = WsdlRequest.class.getName() + "@force_mtom";
60     public static final String ENABLE_INLINE_FILES = WsdlRequest.class.getName() + "@enable_inline_files";
61     public static final String SKIP_SOAP_ACTION = WsdlRequest.class.getName() + "@skip_soap_action";
62     public static final String ENCODE_ATTACHMENTS = WsdlRequest.class.getName() + "@encode_attachments";
63     public static final String WSS_TIMETOLIVE = WsdlRequest.class.getName() + "@wss-time-to-live";
64     public static final String OPERATION_PROPERTY = WsdlRequest.class.getName() + "@operation";
65     public static final String INCOMING_WSS = WsdlRequest.class.getName() + "@incoming-wss";
66     public static final String OUGOING_WSS = WsdlRequest.class.getName() + "@outgoing-wss";
67  
68     public final static String PW_TYPE_NONE = "None";
69     public final static String PW_TYPE_DIGEST = "PasswordDigest";
70     public final static String PW_TYPE_TEXT = "PasswordText";
71  
72     private WsdlOperation operation;
73     private List<HttpAttachmentPart> definedAttachmentParts;
74     private InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
75  
76     private WsaConfig wsaConfig;
77  
78     public WsdlRequest( WsdlOperation operation, WsdlRequestConfig callConfig )
79     {
80        this( operation, callConfig, false );
81     }
82  
83     public WsdlRequest( WsdlOperation operation, WsdlRequestConfig callConfig, boolean forLoadTest )
84     {
85        super( callConfig, operation, null, forLoadTest );
86  
87        this.operation = operation;
88  
89        initEndpoints();
90  
91        // ensure encoding
92        if( callConfig.getEncoding() == null || callConfig.getEncoding().length() == 0 )
93        {
94           callConfig.setEncoding( "UTF-8" );
95        }
96  
97        if( !forLoadTest )
98        {
99           operation.getInterface().addPropertyChangeListener( interfaceListener );
100          operation.getInterface().addInterfaceListener( interfaceListener );
101       }
102    }
103 
104    public void updateConfig( WsdlRequestConfig request )
105    {
106       setConfig( request );
107 
108       if( wsaConfig != null )
109       {
110          wsaConfig.setConfig( request.getWsaConfig() );
111       }
112    }
113 
114    protected void initEndpoints()
115    {
116       if( getEndpoint() == null )
117       {
118          String[] endpoints = operation.getInterface().getEndpoints();
119          if( endpoints.length > 0 )
120          {
121             setEndpoint( endpoints[0] );
122          }
123       }
124    }
125 
126    public boolean isInlineResponseAttachments()
127    {
128       return getSettings().getBoolean( INLINE_RESPONSE_ATTACHMENTS );
129    }
130 
131    public void setInlineResponseAttachments( boolean inlineResponseAttachments )
132    {
133       boolean old = getSettings().getBoolean( INLINE_RESPONSE_ATTACHMENTS );
134       getSettings().setBoolean( INLINE_RESPONSE_ATTACHMENTS, inlineResponseAttachments );
135       notifyPropertyChanged( INLINE_RESPONSE_ATTACHMENTS, old, inlineResponseAttachments );
136    }
137 
138    public boolean isExpandMtomResponseAttachments()
139    {
140       return getSettings().getBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS );
141    }
142 
143    public void setExpandMtomResponseAttachments( boolean expandMtomResponseAttachments )
144    {
145       boolean old = getSettings().getBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS );
146       getSettings().setBoolean( EXPAND_MTOM_RESPONSE_ATTACHMENTS, expandMtomResponseAttachments );
147       notifyPropertyChanged( EXPAND_MTOM_RESPONSE_ATTACHMENTS, old, expandMtomResponseAttachments );
148    }
149 
150    /***
151     * Use getResponse().getContentAsString();
152     *
153     * @deprecated
154     */
155 
156    @Deprecated
157    public String getResponseContent()
158    {
159       return getResponse() == null ? null : getResponse().getContentAsString();
160    }
161 
162    public WsdlResponse getResponse()
163    {
164       return (WsdlResponse) super.getResponse();
165    }
166 
167    public WsdlOperation getOperation()
168    {
169       return operation;
170    }
171 
172    public void setOperation( WsdlOperation wsdlOperation )
173    {
174       WsdlOperation oldOperation = operation;
175       this.operation = wsdlOperation;
176 
177       definedAttachmentParts = null;
178       notifyPropertyChanged( OPERATION_PROPERTY, oldOperation, operation );
179    }
180 
181    public void setRequestContent( String request )
182    {
183       definedAttachmentParts = null;
184       super.setRequestContent( request );
185    }
186 
187 //   public void setResponse( WsdlResponse response, SubmitContext context )
188 //   {
189 //   	WsdlResponse oldResponse = getResponse();
190 //		this.response = response;
191 //		
192 //      notifyPropertyChanged( RESPONSE_PROPERTY, oldResponse, response );
193 //   }
194 
195    public WsdlSubmit<WsdlRequest> submit( SubmitContext submitContext, boolean async ) throws SubmitException
196    {
197       String endpoint = PropertyExpansionUtils.expandProperties( submitContext, getEndpoint() );
198       if( endpoint == null || endpoint.trim().length() == 0 )
199       {
200          UISupport.showErrorMessage( "Missing endpoint for request [" + getName() + "]" );
201          return null;
202       }
203 
204       try
205       {
206          WsdlSubmit<WsdlRequest> submitter = new WsdlSubmit<WsdlRequest>( this, getSubmitListeners(),
207                  RequestTransportRegistry.getTransport( endpoint, submitContext ) );
208          submitter.submitRequest( submitContext, async );
209          return submitter;
210       }
211       catch( Exception e )
212       {
213          throw new SubmitException( e.toString() );
214       }
215    }
216 
217    private class InternalInterfaceListener extends InterfaceListenerAdapter implements PropertyChangeListener
218    {
219       public void propertyChange( PropertyChangeEvent evt )
220       {
221          if( evt.getPropertyName().equals( Interface.ENDPOINT_PROPERTY ) )
222          {
223             String endpoint = getEndpoint();
224             if( evt.getOldValue() != null && evt.getOldValue().equals( endpoint ) )
225             {
226                setEndpoint( (String) evt.getNewValue() );
227             }
228          }
229       }
230    }
231 
232    public String getWssPasswordType()
233    {
234       String wssPasswordType = getConfig().getWssPasswordType();
235       return StringUtils.isNullOrEmpty( wssPasswordType ) || PW_TYPE_NONE.equals( wssPasswordType ) ? null : wssPasswordType;
236    }
237 
238    public void setWssPasswordType( String wssPasswordType )
239    {
240       if( wssPasswordType == null || wssPasswordType.equals( PW_TYPE_NONE ) )
241       {
242          if( getConfig().isSetWssPasswordType() )
243             getConfig().unsetWssPasswordType();
244       }
245       else
246       {
247          getConfig().setWssPasswordType( wssPasswordType );
248       }
249    }
250 
251    /* (non-Javadoc)
252      * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getDefinedAttachmentParts()
253      */
254 
255    public synchronized HttpAttachmentPart[] getDefinedAttachmentParts()
256    {
257       if( definedAttachmentParts == null )
258       {
259          try
260          {
261             UISupport.setHourglassCursor();
262             definedAttachmentParts = AttachmentUtils.extractAttachmentParts(
263                     operation, getRequestContent(), true, false, isMtomEnabled() );
264          }
265          catch( Exception e )
266          {
267             log.warn( e.toString() );
268             definedAttachmentParts = new ArrayList<HttpAttachmentPart>();
269          }
270          finally
271          {
272             UISupport.resetCursor();
273          }
274       }
275 
276       return definedAttachmentParts.toArray( new HttpAttachmentPart[definedAttachmentParts.size()] );
277    }
278 
279    public RequestMethod getMethod()
280    {
281       return RequestMethod.POST;
282    }
283 
284    /* (non-Javadoc)
285 	 * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#getAttachmentPart(java.lang.String)
286 	 */
287    public HttpAttachmentPart getAttachmentPart( String partName )
288    {
289       HttpAttachmentPart[] parts = getDefinedAttachmentParts();
290       for( HttpAttachmentPart part : parts )
291       {
292          if( part.getName().equals( partName ) )
293             return part;
294       }
295 
296       return null;
297    }
298 
299    public void copyTo( WsdlRequest newRequest, boolean copyAttachments, boolean copyHeaders )
300    {
301       newRequest.setEncoding( getEncoding() );
302       newRequest.setEndpoint( getEndpoint() );
303       newRequest.setRequestContent( getRequestContent() );
304       newRequest.setWssPasswordType( getWssPasswordType() );
305 
306       CredentialsConfig credentials = getConfig().getCredentials();
307       if( credentials != null )
308          newRequest.getConfig().setCredentials( (CredentialsConfig) credentials.copy() );
309 
310       if( copyAttachments )
311          copyAttachmentsTo( newRequest );
312 
313       if( copyHeaders )
314          newRequest.setRequestHeaders( getRequestHeaders() );
315 
316 //      ((DefaultWssContainer)newRequest.getWssContainer()).updateConfig( ( WSSConfigConfig ) getConfig().getWssConfig().copy() );
317    }
318 
319    /* (non-Javadoc)
320      * @see com.eviware.soapui.impl.wsdl.AttachmentContainer#isMtomEnabled()
321      */
322    public boolean isMtomEnabled()
323    {
324       return getSettings().getBoolean( WsdlSettings.ENABLE_MTOM );
325    }
326 
327    public void setMtomEnabled( boolean mtomEnabled )
328    {
329       getSettings().setBoolean( WsdlSettings.ENABLE_MTOM, mtomEnabled );
330       definedAttachmentParts = null;
331    }
332 
333    public boolean isInlineFilesEnabled()
334    {
335       return getSettings().getBoolean( WsdlRequest.ENABLE_INLINE_FILES );
336    }
337 
338    public void setInlineFilesEnabled( boolean inlineFilesEnabled )
339    {
340       getSettings().setBoolean( WsdlRequest.ENABLE_INLINE_FILES, inlineFilesEnabled );
341    }
342 
343    public boolean isSkipSoapAction()
344    {
345       return getSettings().getBoolean( WsdlRequest.SKIP_SOAP_ACTION );
346    }
347 
348    public void setSkipSoapAction( boolean skipSoapAction )
349    {
350       getSettings().setBoolean( WsdlRequest.SKIP_SOAP_ACTION, skipSoapAction );
351    }
352 
353    @Override
354    public void release()
355    {
356       super.release();
357 
358       getOperation().getInterface().removeInterfaceListener( interfaceListener );
359       getOperation().getInterface().removePropertyChangeListener( interfaceListener );
360    }
361 
362    public MessagePart[] getRequestParts()
363    {
364       try
365       {
366          List<MessagePart> result = new ArrayList<MessagePart>();
367          result.addAll( Arrays.asList( getOperation().getDefaultRequestParts() ) );
368          result.addAll( Arrays.asList( getDefinedAttachmentParts() ) );
369 
370          return result.toArray( new MessagePart[result.size()] );
371       }
372       catch( Exception e )
373       {
374          SoapUI.logError( e );
375          return new MessagePart[0];
376       }
377    }
378 
379    public MessagePart[] getResponseParts()
380    {
381       try
382       {
383          List<MessagePart> result = new ArrayList<MessagePart>();
384          result.addAll( Arrays.asList( getOperation().getDefaultResponseParts() ) );
385 
386          if( getResponse() != null )
387             result.addAll( AttachmentUtils.extractAttachmentParts(
388                     getOperation(), getResponse().getContentAsString(), true, true, isMtomEnabled() ) );
389 
390          return result.toArray( new MessagePart[result.size()] );
391       }
392       catch( Exception e )
393       {
394          SoapUI.logError( e );
395          return new MessagePart[0];
396       }
397    }
398 
399    public String getWssTimeToLive()
400    {
401       return getSettings().getString( WSS_TIMETOLIVE, null );
402    }
403 
404    public void setWssTimeToLive( String ttl )
405    {
406       getSettings().setString( WSS_TIMETOLIVE, ttl );
407    }
408 
409    public long getContentLength()
410    {
411       return getRequestContent().length();
412    }
413 
414    public boolean isForceMtom()
415    {
416       return getSettings().getBoolean( FORCE_MTOM );
417    }
418 
419    public void setForceMtom( boolean forceMtom )
420    {
421       boolean old = getSettings().getBoolean( FORCE_MTOM );
422       getSettings().setBoolean( FORCE_MTOM, forceMtom );
423       notifyPropertyChanged( FORCE_MTOM, old, forceMtom );
424    }
425 
426    public boolean isEncodeAttachments()
427    {
428       return getSettings().getBoolean( ENCODE_ATTACHMENTS );
429    }
430 
431    public void setEncodeAttachments( boolean encodeAttachments )
432    {
433       boolean old = getSettings().getBoolean( ENCODE_ATTACHMENTS );
434       getSettings().setBoolean( ENCODE_ATTACHMENTS, encodeAttachments );
435       notifyPropertyChanged( ENCODE_ATTACHMENTS, old, encodeAttachments );
436    }
437 
438    public String getIncomingWss()
439    {
440       return getConfig().getIncomingWss();
441    }
442 
443    public void setIncomingWss( String incomingWss )
444    {
445       String old = getIncomingWss();
446       getConfig().setIncomingWss( incomingWss );
447       notifyPropertyChanged( INCOMING_WSS, old, incomingWss );
448    }
449 
450    public String getOutgoingWss()
451    {
452       return getConfig().getOutgoingWss();
453    }
454 
455    public void setOutgoingWss( String outgoingWss )
456    {
457       String old = getOutgoingWss();
458       getConfig().setOutgoingWss( outgoingWss );
459       notifyPropertyChanged( OUGOING_WSS, old, outgoingWss );
460    }
461 
462    public boolean isWsAddressing()
463    {
464       return getConfig().getUseWsAddressing();
465    }
466 
467    public void setWsAddressing( boolean wsAddressing )
468    {
469       boolean old = getConfig().getUseWsAddressing();
470       getConfig().setUseWsAddressing( wsAddressing );
471       notifyPropertyChanged( "wsAddressing", old, wsAddressing );
472    }
473 
474 	
475    public PropertyExpansion[] getPropertyExpansions()
476    {
477       PropertyExpansionsResult result = new PropertyExpansionsResult( this, this );
478       result.addAll( super.getPropertyExpansions() );
479 
480       StringToStringMap requestHeaders = getRequestHeaders();
481       for( String key : requestHeaders.keySet() )
482       {
483          result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this,
484                  new RequestHeaderHolder( requestHeaders, key ), "value" ) );
485       }
486 
487       return result.toArray();
488    }
489 
490    public class RequestHeaderHolder
491    {
492       private final StringToStringMap valueMap;
493       private final String key;
494 
495       public RequestHeaderHolder( StringToStringMap valueMap, String key )
496       {
497          this.valueMap = valueMap;
498          this.key = key;
499       }
500 
501       public String getValue()
502       {
503          return valueMap.get( key );
504       }
505 
506       public void setValue( String value )
507       {
508          valueMap.put( key, value );
509          setRequestHeaders( valueMap );
510       }
511    }
512 
513 	public AttachmentEncoding getAttachmentEncoding(String partName)
514 	{
515 		return AttachmentUtils.getAttachmentEncoding( getOperation(), partName, false );
516 	}
517 	public WsaConfig getWsaConfig() {
518 		if (wsaConfig == null)
519 		{
520 			if (!getConfig().isSetWsaConfig())
521 			{
522 				getConfig().addNewWsaConfig();
523 			}
524 			wsaConfig = new WsaConfig(getConfig().getWsaConfig(), this);
525 		}
526 		return wsaConfig;
527 	}
528 
529    public ModelItem getModelItem()
530    {
531       return this;
532    }
533 
534    public boolean isWsaEnabled()
535    {
536       return isWsAddressing();
537    }
538 
539    public void setWsaEnabled( boolean arg0 )
540    {
541       setWsAddressing( arg0 );
542 
543    }
544 }