View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.wsmc;
14  
15  import org.apache.log4j.Logger;
16  import org.apache.xmlbeans.XmlCursor;
17  import org.apache.xmlbeans.XmlException;
18  import org.apache.xmlbeans.XmlObject;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.config.HttpRequestConfig;
22  import com.eviware.soapui.config.WsaConfigConfig;
23  import com.eviware.soapui.config.WsrmConfigConfig;
24  import com.eviware.soapui.impl.support.wsa.WsaRequest;
25  import com.eviware.soapui.impl.wsdl.WsdlOperation;
26  import com.eviware.soapui.impl.wsdl.WsdlSubmit;
27  import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
28  import com.eviware.soapui.impl.wsdl.support.soap.SoapMessageBuilder;
29  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
30  import com.eviware.soapui.impl.wsdl.support.wsa.WsaConfig;
31  import com.eviware.soapui.impl.wsdl.support.wsa.WsaContainer;
32  import com.eviware.soapui.impl.wsdl.support.wsa.WsaContainerImpl;
33  import com.eviware.soapui.impl.wsdl.support.wsa.WsaUtils;
34  import com.eviware.soapui.impl.wsdl.support.wsrm.WsrmConfig;
35  import com.eviware.soapui.model.iface.Response;
36  import com.eviware.soapui.model.iface.Request.SubmitException;
37  import com.eviware.soapui.model.iface.Submit.Status;
38  import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
39  
40  public class WsmcUtils
41  {
42  	private static final String WSMC_ACTION = "http://docs.oasis-open.org/ws-rx/wsmc/200702/MakeConnection";
43  	private static final String WSMC_NAMESPACE = "http://docs.oasis-open.org/ws-rx/wsmc/200702";
44  
45  	public void sendMakeConnectionRequest( String endpoint, SoapVersion soapVersion, WsdlOperation operation, String uuid )
46  	{
47  		String identifier = null;
48  
49  		HttpRequestConfig httpRequestConfig = ( HttpRequestConfig )( XmlObject.Factory.newInstance()
50  				.changeType( HttpRequestConfig.type ) );
51  		httpRequestConfig.setEndpoint( endpoint );
52  
53  		WsaConfigConfig wsaConfigConfig = ( WsaConfigConfig )( XmlObject.Factory.newInstance()
54  				.changeType( WsaConfigConfig.type ) );
55  		WsaContainer wsaContainer = new WsaContainerImpl();
56  		wsaContainer.setOperation( operation );
57  		WsaConfig wsaConfig = new WsaConfig( wsaConfigConfig, wsaContainer );
58  
59  		WsrmConfigConfig wsrmConfigConfig = ( WsrmConfigConfig )( XmlObject.Factory.newInstance()
60  				.changeType( WsrmConfigConfig.type ) );
61  		WsrmConfig wsrmConfig = new WsrmConfig( wsrmConfigConfig, null );
62  
63  		WsaRequest makeConnectionRequest = new WsaRequest( httpRequestConfig, wsaConfig, wsrmConfig, false );
64  		makeConnectionRequest.setOperation( operation );
65  
66  		String makeConnectionMessageContent = SoapMessageBuilder.buildEmptyMessage( soapVersion );
67  
68  		makeConnectionRequest.getWsaConfig().setWsaEnabled( true );
69  		makeConnectionRequest.getWsaConfig().setAction( WSMC_ACTION );
70  
71  		makeConnectionRequest.getWsaConfig().setTo(
72  				WsaUtils.getNamespace( makeConnectionRequest.getWsaConfig().getVersion() ) + "/anonymous" );
73  		makeConnectionRequest.getWsaConfig().setGenerateMessageId( true );
74  
75  		try
76  		{
77  			XmlObject object = XmlObject.Factory.parse( makeConnectionMessageContent );
78  			XmlCursor cursor = object.newCursor();
79  
80  			cursor.toFirstContentToken();
81  			cursor.toFirstChild();
82  			cursor.toNextSibling();
83  
84  			cursor.toNextToken();
85  			cursor.insertNamespace( "wsmc", WSMC_NAMESPACE );
86  
87  			cursor.beginElement( "MakeConnection", WSMC_NAMESPACE );
88  			cursor.beginElement( "Address", WSMC_NAMESPACE );
89  			cursor.insertChars( WsaUtils.getNamespace( makeConnectionRequest.getWsaConfig().getVersion() )
90  					+ "/anonymous?id=" + uuid );
91  
92  			cursor.dispose();
93  
94  			cursor.dispose();
95  
96  			WsaUtils wsaUtils = new WsaUtils( object.xmlText(), soapVersion, null, new DefaultPropertyExpansionContext(
97  					makeConnectionRequest ) );
98  			String content = wsaUtils.addWSAddressingRequest( makeConnectionRequest );
99  
100 			makeConnectionRequest.setRequestContent( content );
101 
102 		}
103 		catch( XmlException e )
104 		{
105 			// TODO Auto-generated catch block
106 			e.printStackTrace();
107 		}
108 
109 		try
110 		{
111 
112 			WsdlSubmit wsdlSubmit = makeConnectionRequest.submit( new WsdlSubmitContext( null ), true );
113 
114 			// startSequenceRequest.getWsaConfig().setWsaEnabled(false);
115 			while( wsdlSubmit.getStatus() != Status.FINISHED )
116 			{
117 				wsdlSubmit.waitUntilFinished();
118 			}
119 			Response response = wsdlSubmit.getResponse();
120 			String responseContent = response.getContentAsString();
121 			XmlObject xml = XmlObject.Factory.parse( responseContent );
122 			XmlCursor cursor = xml.newCursor();
123 			cursor.toFirstContentToken();
124 			cursor.toFirstChild();
125 			cursor.toNextSibling();
126 			cursor.toFirstChild();
127 
128 			String sequenceIdentifier = cursor.getTextValue();
129 			Logger.getLogger( "wsrm" ).info( "Sequence response Received, sequence ID: " + sequenceIdentifier );
130 
131 			// WsmcInjection receiveInjection = new WsmcInjection(request);
132 			// request.setAfterRequestInjection(receiveInjection);
133 
134 		}
135 		catch( SubmitException e1 )
136 		{
137 			SoapUI.logError( e1 );
138 		}
139 		catch( XmlException e )
140 		{
141 			SoapUI.logError( e );
142 		}
143 	}
144 }