View Javadoc

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