1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support.wsrm;
14
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.apache.xmlbeans.XmlCursor;
19 import org.apache.xmlbeans.XmlException;
20 import org.apache.xmlbeans.XmlObject;
21 import org.apache.xmlbeans.XmlOptions;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Text;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.config.HttpRequestConfig;
27 import com.eviware.soapui.config.WsaConfigConfig;
28 import com.eviware.soapui.config.WsrmConfigConfig;
29 import com.eviware.soapui.config.WsrmVersionTypeConfig;
30 import com.eviware.soapui.impl.support.wsa.WsaRequest;
31 import com.eviware.soapui.impl.wsdl.WsdlOperation;
32 import com.eviware.soapui.impl.wsdl.WsdlRequest;
33 import com.eviware.soapui.impl.wsdl.WsdlSubmit;
34 import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
35 import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
36 import com.eviware.soapui.impl.wsdl.support.soap.SoapMessageBuilder;
37 import com.eviware.soapui.impl.wsdl.support.soap.SoapUtils;
38 import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
39 import com.eviware.soapui.impl.wsdl.support.wsa.WsaConfig;
40 import com.eviware.soapui.impl.wsdl.support.wsa.WsaContainer;
41 import com.eviware.soapui.impl.wsdl.support.wsa.WsaContainerImpl;
42 import com.eviware.soapui.impl.wsdl.support.wsa.WsaUtils;
43 import com.eviware.soapui.model.iface.Response;
44 import com.eviware.soapui.model.iface.Request.SubmitException;
45 import com.eviware.soapui.model.iface.Submit.Status;
46 import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
47 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
48
49 public class WsrmUtils
50 {
51 private static final String WSRM_CREATE_SEQUENCE = "CreateSequence";
52 private static final String WSRM_REQUEST_ACK = "AckRequested";
53 private static final String WSRM_EXPIRES = "Expires";
54 private static final String WSRM_ACKNOWLEDGMENTS_TO = "AcksTo";
55 private static final String WSRM_CLOSE_SEQUENCE = "CloseSequence";
56 private static final String WSRM_IDENTIFIER = "Identifier";
57 private static final String WSRM_LAST_MSG = "LastMsgNumber";
58 private static final String WSRM_CREATE_SEQUENCE_ACTION = "/CreateSequence";
59 private static final String WSRM_CLOSE_SEQUENCE_ACTION = "/CloseSequence";
60 private static final String WSRM_TERMINATE_SEQUENCE_ACTION = "/TerminateSequence";
61 private static final String WSRM_REQUEST_ACK_ACTION = "/AckRequested";
62
63 public final static String WSRM_NS_1_0 = "http://schemas.xmlsoap.org/ws/2005/02/rm";
64 public final static String WSRM_NS_1_1 = "http://docs.oasis-open.org/ws-rx/wsrm/200702";
65 public final static String WSRM_NS_1_2 = "http://docs.oasis-open.org/ws-rx/wsrm/200702";
66
67 private SoapVersion soapVersion;
68 private XmlObject xmlContentObject;
69 private String content;
70
71 public WsrmUtils( SoapVersion soapVersion )
72 {
73 this.soapVersion = soapVersion;
74 }
75
76 public WsrmUtils( String content, SoapVersion soapVersion, PropertyExpansionContext context )
77 {
78 this.soapVersion = soapVersion;
79 this.content = content;
80
81 try
82 {
83 xmlContentObject = XmlObject.Factory.parse( content );
84 }
85 catch( Exception e )
86 {
87 SoapUI.logError( e );
88 }
89 }
90
91 public String createNewWSReliableMessagingRequest( WsdlRequest wsrmContainer, ExtendedHttpMethod httpMethod,
92 String identifier, long msgNumber, String endpoint )
93 {
94
95 try
96 {
97 Element header = getHeader( wsrmContainer );
98
99 header.setAttribute( "xmlns:" + "wsrm", wsrmContainer.getWsrmConfig().getVersionNameSpace() );
100
101 Element sequence = header.getOwnerDocument().createElementNS(
102 wsrmContainer.getWsrmConfig().getVersionNameSpace(), "Sequence" );
103
104 Element identifierElement = sequence.getOwnerDocument().createElementNS(
105 wsrmContainer.getWsrmConfig().getVersionNameSpace(), "Identifier" );
106 Text txtElm = identifierElement.getOwnerDocument().createTextNode( identifier );
107 identifierElement.appendChild( txtElm );
108 sequence.appendChild( identifierElement );
109
110 Element messageId = sequence.getOwnerDocument().createElementNS(
111 wsrmContainer.getWsrmConfig().getVersionNameSpace(), "MessageNumber" );
112 Text txtElm2 = identifierElement.getOwnerDocument().createTextNode( String.valueOf( msgNumber ) );
113 messageId.appendChild( txtElm2 );
114 sequence.appendChild( messageId );
115
116 header.appendChild( sequence );
117
118 content = xmlContentObject.xmlText();
119
120 wsrmContainer.getWsaConfig().setWsaEnabled( true );
121 if( wsrmContainer.getWsaConfig().getAction() == null )
122 wsrmContainer.getWsaConfig().setAddDefaultAction( true );
123
124 wsrmContainer.getWsaConfig().setTo( endpoint );
125 wsrmContainer.getWsaConfig().setGenerateMessageId( true );
126
127 WsaUtils wsaUtils = new WsaUtils( content, wsrmContainer.getOperation().getInterface().getSoapVersion(), null,
128 new DefaultPropertyExpansionContext( wsrmContainer ) );
129 content = wsaUtils.addWSAddressingRequest( wsrmContainer );
130
131 }
132 catch( Exception e )
133 {
134 SoapUI.logError( e );
135 }
136
137 return content;
138 }
139
140 private Element getHeader( WsrmContainer wsrmContainer ) throws XmlException
141 {
142
143 String wsrmNameSpace = wsrmContainer.getWsrmConfig().getVersionNameSpace();
144
145 Element header = ( Element )SoapUtils.getHeaderElement( xmlContentObject, soapVersion, true ).getDomNode();
146 return header;
147 }
148
149 public WsrmSequence createSequence( String endpoint, SoapVersion soapVersion, String wsrmNamespace, String ackTo,
150 Long expires, WsdlOperation operation, String wsaTo )
151 {
152 String identifier = null;
153
154 HttpRequestConfig httpRequestConfig = ( HttpRequestConfig )( XmlObject.Factory.newInstance()
155 .changeType( HttpRequestConfig.type ) );
156 httpRequestConfig.setEndpoint( endpoint );
157 httpRequestConfig.setMediaType( soapVersion.getContentType() );
158
159 WsaConfigConfig wsaConfigConfig = ( WsaConfigConfig )( XmlObject.Factory.newInstance()
160 .changeType( WsaConfigConfig.type ) );
161 WsaContainer wsaContainer = new WsaContainerImpl();
162 wsaContainer.setOperation( operation );
163 WsaConfig wsaConfig = new WsaConfig( wsaConfigConfig, wsaContainer );
164 wsaConfig.setTo( endpoint );
165
166 WsrmConfigConfig wsrmConfigConfig = ( WsrmConfigConfig )( XmlObject.Factory.newInstance()
167 .changeType( WsrmConfigConfig.type ) );
168 WsrmConfig wsrmConfig = new WsrmConfig( wsrmConfigConfig, null );
169
170 WsaRequest startSequenceRequest = new WsaRequest( httpRequestConfig, wsaConfig, wsrmConfig, false );
171 startSequenceRequest.setOperation( operation );
172
173 String openSequenceMessageContent = SoapMessageBuilder.buildEmptyMessage( soapVersion );
174
175 startSequenceRequest.getWsaConfig().setWsaEnabled( true );
176 startSequenceRequest.getWsaConfig().setAction( wsrmNamespace + WSRM_CREATE_SEQUENCE_ACTION );
177 String uuid = UUID.randomUUID().toString();
178
179
180
181
182
183
184
185
186
187 startSequenceRequest.getWsaConfig().setReplyTo( ackTo );
188 startSequenceRequest.getWsaConfig().setGenerateMessageId( true );
189
190 try
191 {
192 XmlObject object = XmlObject.Factory.parse( openSequenceMessageContent );
193 XmlCursor cursor = object.newCursor();
194
195 cursor.toFirstContentToken();
196 cursor.toFirstChild();
197 cursor.toNextSibling();
198
199 cursor.toNextToken();
200 cursor.insertNamespace( "wsrm", wsrmNamespace );
201
202 cursor.beginElement( WSRM_CREATE_SEQUENCE, wsrmNamespace );
203 cursor.beginElement( "Offer", wsrmNamespace );
204 cursor.beginElement( "Identifier", wsrmNamespace );
205 cursor.insertChars( "blah" );
206
207 cursor.toParent();
208 cursor.toParent();
209
210 cursor.beginElement( WSRM_ACKNOWLEDGMENTS_TO, wsrmNamespace );
211 cursor.insertNamespace( "wsa", WsaUtils.getNamespace( startSequenceRequest.getWsaConfig().getVersion() ) );
212 cursor.beginElement( "Address", WsaUtils.getNamespace( startSequenceRequest.getWsaConfig().getVersion() ) );
213 if( ackTo == null || ackTo.length() < 1 )
214 ackTo = WsaUtils.getNamespace( startSequenceRequest.getWsaConfig().getVersion() ) + "/anonymous" + "?id="
215 + uuid;
216 cursor.insertChars( ackTo );
217
218
219 if( expires != 0 )
220 {
221 cursor.toParent();
222
223 cursor.beginElement( WSRM_EXPIRES, wsrmNamespace );
224 cursor.insertChars( expires.toString() );
225 }
226
227 cursor.dispose();
228
229 WsaUtils wsaUtils = new WsaUtils( object.xmlText(), soapVersion, null, new DefaultPropertyExpansionContext(
230 startSequenceRequest ) );
231 content = wsaUtils.addWSAddressingRequest( startSequenceRequest );
232
233 startSequenceRequest.setRequestContent( content );
234
235 }
236 catch( XmlException e )
237 {
238
239 e.printStackTrace();
240 }
241
242 try
243 {
244
245 WsdlSubmit wsdlSubmit = startSequenceRequest.submit( new WsdlSubmitContext( null ), true );
246 Logger.getLogger( "wsrm" ).info( "StartSequence Request Sent: " + uuid );
247
248
249 while( wsdlSubmit.getStatus() != Status.FINISHED )
250 {
251 wsdlSubmit.waitUntilFinished();
252 }
253 Response response = wsdlSubmit.getResponse();
254 String responseContent = response.getContentAsString();
255 XmlObject xml = XmlObject.Factory.parse( responseContent );
256 XmlCursor cursor = xml.newCursor();
257 cursor.toFirstContentToken();
258 cursor.toFirstChild();
259 cursor.toNextSibling();
260 cursor.toFirstChild();
261 cursor.toFirstChild();
262 String sequenceIdentifier = cursor.getTextValue();
263 Logger.getLogger( "wsrm" ).info( "Sequence response Received, sequence ID: " + sequenceIdentifier );
264
265
266
267
268 WsrmSequence sequence = new WsrmSequence( sequenceIdentifier.trim(), uuid, soapVersion, wsrmNamespace,
269 operation );
270 return sequence;
271 }
272 catch( SubmitException e1 )
273 {
274 SoapUI.logError( e1 );
275 return null;
276 }
277 catch( XmlException e )
278 {
279 SoapUI.logError( e );
280 return null;
281 }
282 }
283
284 public static String getWsrmVersionNamespace( WsrmVersionTypeConfig.Enum wsrmVersion )
285 {
286 if( wsrmVersion == WsrmVersionTypeConfig.X_1_0 )
287 return WSRM_NS_1_0;
288 else if( wsrmVersion == WsrmVersionTypeConfig.X_1_1 )
289 return WSRM_NS_1_1;
290 else
291 return WSRM_NS_1_2;
292 }
293
294 public void closeSequence( String endpoint, SoapVersion soapVersion, String wsrmNamespace, String uuid,
295 String identifier, long lastMsgNum, WsdlOperation operation )
296 {
297
298 HttpRequestConfig httpRequestConfig = ( HttpRequestConfig )( XmlObject.Factory.newInstance()
299 .changeType( HttpRequestConfig.type ) );
300 httpRequestConfig.setEndpoint( endpoint );
301 httpRequestConfig.setMediaType( soapVersion.getContentType() );
302
303 WsaConfigConfig wsaConfigConfig = ( WsaConfigConfig )( XmlObject.Factory.newInstance()
304 .changeType( WsaConfigConfig.type ) );
305 WsaContainer wsaContainer = new WsaContainerImpl();
306 wsaContainer.setOperation( operation );
307 WsaConfig wsaConfig = new WsaConfig( wsaConfigConfig, wsaContainer );
308
309 WsrmConfigConfig wsrmConfigConfig = ( WsrmConfigConfig )( XmlObject.Factory.newInstance()
310 .changeType( WsrmConfigConfig.type ) );
311 WsrmConfig wsrmConfig = new WsrmConfig( wsrmConfigConfig, null );
312
313 if( wsrmNamespace != WSRM_NS_1_0 )
314 {
315 WsaRequest closeSequenceRequest = new WsaRequest( httpRequestConfig, wsaConfig, wsrmConfig, false );
316 closeSequenceRequest.setOperation( operation );
317
318 String openSequenceMessageContent = SoapMessageBuilder.buildEmptyMessage( soapVersion );
319
320 closeSequenceRequest.getWsaConfig().setWsaEnabled( true );
321 closeSequenceRequest.getWsaConfig().setAction( wsrmNamespace + WSRM_CLOSE_SEQUENCE_ACTION );
322
323 closeSequenceRequest.getWsaConfig().setTo( endpoint );
324 closeSequenceRequest.getWsaConfig().setGenerateMessageId( true );
325
326 try
327 {
328 XmlObject object = XmlObject.Factory.parse( openSequenceMessageContent );
329 XmlCursor cursor = object.newCursor();
330
331 cursor.toFirstContentToken();
332 cursor.toFirstChild();
333 cursor.toNextSibling();
334
335 cursor.toNextToken();
336 cursor.insertNamespace( "wsrm", wsrmNamespace );
337
338 cursor.beginElement( WSRM_CLOSE_SEQUENCE, wsrmNamespace );
339
340 cursor.beginElement( WSRM_IDENTIFIER, wsrmNamespace );
341 cursor.insertChars( identifier );
342
343 cursor.toParent();
344
345 cursor.beginElement( WSRM_LAST_MSG, wsrmNamespace );
346 cursor.insertChars( String.valueOf( lastMsgNum ) );
347 cursor.dispose();
348
349 WsaUtils wsaUtils = new WsaUtils( object.xmlText(), soapVersion, null, new DefaultPropertyExpansionContext(
350 closeSequenceRequest ) );
351 content = wsaUtils.addWSAddressingRequest( closeSequenceRequest );
352
353 closeSequenceRequest.setRequestContent( content );
354
355 Logger.getLogger( "wsrm" ).info( "CloseSequence Request Sent for Sequence: " + identifier );
356
357 }
358 catch( XmlException e )
359 {
360 SoapUI.logError( e );
361 }
362
363 try
364 {
365
366 WsdlSubmit wsdlSubmit = closeSequenceRequest.submit( new WsdlSubmitContext( null ), true );
367 while( wsdlSubmit.getStatus() != Status.FINISHED )
368 {
369 wsdlSubmit.waitUntilFinished();
370 }
371 Response response = wsdlSubmit.getResponse();
372 String responseContent = response.getContentAsString();
373 XmlObject xml = XmlObject.Factory.parse( responseContent );
374
375 XmlOptions options = new XmlOptions();
376
377 String namespaceDeclaration = "declare namespace wsrm='" + wsrmNamespace + "';";
378 XmlObject result[] = xml.selectPath( namespaceDeclaration + "//wsrm:AcknowledgementRange", options );
379
380 if( result.length > 0 )
381 {
382 for( int i = 0; i < result.length; i++ )
383 {
384 String upper = result[i].selectAttribute( null, "Upper" ).getDomNode().getNodeValue();
385 String lower = result[i].selectAttribute( null, "Lower" ).getDomNode().getNodeValue();
386
387 if( lower == upper )
388 {
389 Logger.getLogger( "wsrm" ).info(
390 "Acknowledgment for message " + upper + " received for identifier: " + identifier );
391 }
392 else
393 {
394 Logger.getLogger( "wsrm" ).info(
395 "Acknowledgment for messages " + lower + " to " + upper + " received for identifier: "
396 + identifier );
397 }
398 }
399 }
400 else
401 {
402 Logger.getLogger( "wsrm" ).info( "No Acknowledgments received for identifier: " + identifier );
403 }
404
405 }
406 catch( SubmitException e1 )
407 {
408 SoapUI.logError( e1 );
409
410 }
411 catch( XmlException e )
412 {
413 SoapUI.logError( e );
414 }
415 }
416
417
418 WsaRequest terminateSequenceRequest = new WsaRequest( httpRequestConfig, wsaConfig, wsrmConfig, false );
419 terminateSequenceRequest.setOperation( operation );
420
421 String terminateSequenceRequestContent = SoapMessageBuilder.buildEmptyMessage( soapVersion );
422
423 terminateSequenceRequest.getWsaConfig().setWsaEnabled( true );
424 terminateSequenceRequest.getWsaConfig().setAction( wsrmNamespace + WSRM_TERMINATE_SEQUENCE_ACTION );
425
426 terminateSequenceRequest.getWsaConfig().setTo( endpoint );
427 terminateSequenceRequest.getWsaConfig().setGenerateMessageId( true );
428
429 try
430 {
431 XmlObject object = XmlObject.Factory.parse( terminateSequenceRequestContent );
432 XmlCursor cursor = object.newCursor();
433
434 cursor.toFirstContentToken();
435 cursor.toFirstChild();
436 cursor.toNextSibling();
437
438 cursor.toNextToken();
439 cursor.insertNamespace( "wsrm", wsrmNamespace );
440
441 cursor.beginElement( "TerminateSequence", wsrmNamespace );
442 cursor.beginElement( WSRM_IDENTIFIER, wsrmNamespace );
443 cursor.insertChars( identifier );
444
445 cursor.dispose();
446
447
448
449
450 WsaUtils wsaUtils = new WsaUtils( object.xmlText(), soapVersion, null, new DefaultPropertyExpansionContext(
451 terminateSequenceRequest ) );
452 terminateSequenceRequestContent = wsaUtils.addWSAddressingRequest( terminateSequenceRequest );
453
454 terminateSequenceRequest.setRequestContent( terminateSequenceRequestContent );
455
456 }
457 catch( XmlException e )
458 {
459
460 e.printStackTrace();
461 }
462
463 try
464 {
465 WsdlSubmit wsdlSubmit = terminateSequenceRequest.submit( new WsdlSubmitContext( null ), true );
466
467 }
468 catch( SubmitException e1 )
469 {
470 SoapUI.logError( e1 );
471 }
472 }
473
474 public void getAcks( String endpoint, SoapVersion soapVersion, String wsrmNamespace, String uuid, String identifier,
475 WsdlOperation operation )
476 {
477
478 HttpRequestConfig httpRequestConfig = ( HttpRequestConfig )( XmlObject.Factory.newInstance()
479 .changeType( HttpRequestConfig.type ) );
480 httpRequestConfig.setEndpoint( endpoint );
481 httpRequestConfig.setMediaType( soapVersion.getContentType() );
482
483 WsaConfigConfig wsaConfigConfig = ( WsaConfigConfig )( XmlObject.Factory.newInstance()
484 .changeType( WsaConfigConfig.type ) );
485 WsaContainer wsaContainer = new WsaContainerImpl();
486 wsaContainer.setOperation( operation );
487 WsaConfig wsaConfig = new WsaConfig( wsaConfigConfig, wsaContainer );
488
489 WsrmConfigConfig wsrmConfigConfig = ( WsrmConfigConfig )( XmlObject.Factory.newInstance()
490 .changeType( WsrmConfigConfig.type ) );
491 WsrmConfig wsrmConfig = new WsrmConfig( wsrmConfigConfig, null );
492
493 WsaRequest startSequenceRequest = new WsaRequest( httpRequestConfig, wsaConfig, wsrmConfig, false );
494 startSequenceRequest.setOperation( operation );
495
496 String openSequenceMessageContent = SoapMessageBuilder.buildEmptyMessage( soapVersion );
497
498 startSequenceRequest.getWsaConfig().setWsaEnabled( true );
499 startSequenceRequest.getWsaConfig().setAction( wsrmNamespace + WSRM_REQUEST_ACK_ACTION );
500
501 startSequenceRequest.getWsaConfig().setTo( endpoint );
502 startSequenceRequest.getWsaConfig().setGenerateMessageId( true );
503
504 try
505 {
506 XmlObject object = XmlObject.Factory.parse( openSequenceMessageContent );
507 XmlCursor cursor = object.newCursor();
508
509 cursor.toFirstContentToken();
510 cursor.toFirstChild();
511 cursor.toNextSibling();
512
513 cursor.toNextToken();
514 cursor.insertNamespace( "wsrm", wsrmNamespace );
515
516 cursor.beginElement( WSRM_REQUEST_ACK, wsrmNamespace );
517
518 cursor.beginElement( WSRM_IDENTIFIER, wsrmNamespace );
519 cursor.insertChars( identifier );
520
521 cursor.dispose();
522
523 WsaUtils wsaUtils = new WsaUtils( object.xmlText(), soapVersion, null, new DefaultPropertyExpansionContext(
524 startSequenceRequest ) );
525 content = wsaUtils.addWSAddressingRequest( startSequenceRequest );
526
527 startSequenceRequest.setRequestContent( content );
528
529
530
531
532
533 Logger.getLogger( "wsrm" ).info( "Acknowledgments Requested for Sequence: " + identifier );
534
535 }
536 catch( XmlException e )
537 {
538
539 e.printStackTrace();
540 }
541
542 try
543 {
544
545 WsdlSubmit wsdlSubmit = startSequenceRequest.submit( new WsdlSubmitContext( null ), true );
546
547 while( wsdlSubmit.getStatus() != Status.FINISHED )
548 {
549 wsdlSubmit.waitUntilFinished();
550 }
551 Response response = wsdlSubmit.getResponse();
552 String responseContent = response.getContentAsString();
553 XmlObject xml = XmlObject.Factory.parse( responseContent );
554 XmlObject result = xml.selectPath( "Envelope/Header/SequenceAcknowledgment" )[0];
555
556 if( result != null )
557 {
558
559 }
560
561 }
562 catch( SubmitException e1 )
563 {
564 SoapUI.logError( e1 );
565
566 }
567 catch( XmlException e )
568 {
569 SoapUI.logError( e );
570 }
571 }
572 }