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