summaryrefslogtreecommitdiffstats
path: root/tags/cpp-stable-20060304/sca/runtime/core/src/tuscany/sca/model/Wsdl.cpp
blob: fcc86c78e53a965d5ae067739db09cdb9d33d4c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/* $Rev$ $Date: 2005/12/22 11:33:21 $ */

#include "tuscany/sca/util/Logging.h"
#include "tuscany/sca/util/Utils.h"
#include "tuscany/sca/model/Wsdl.h"
#include "tuscany/sca/util/Exceptions.h"

using namespace tuscany::sca;

using namespace commonj::sdo;

namespace tuscany
{
    namespace sca
    {
        namespace model
        {
            
            
            // Constructor
            Wsdl::Wsdl(DataObjectPtr wsdlModel) 
                : wsdlModel(wsdlModel)
            {
                LOGENTRY(1, "Wsdl::constructor");

                // Trace
                Utils::printDO(wsdlModel);


                
                LOGEXIT(1, "Wsdl::constructor");
            }

            Wsdl::~Wsdl()
            {
            }


            /// 
            /// The namespace of the service and other definitions defined in this wsdl definition
            string Wsdl::getNamespace()
            {

                return wsdlModel->getCString("targetNamespace");

            }

            ///
            /// Find the operation defined in this wsdl
            ///
            WsdlOperation Wsdl::findOperation(const string& serviceName, 
                                        const string& portName, 
                                        const string& operationName)
            {
                string message;
                
                // Find the service
                DataObjectPtr service = findService(serviceName);
                if (!service)
                {
                    // Service not found
                    message = "Unable to find service ";
                    message = message + serviceName;
                    message = message + " in the WSDL definition";
                    throw SystemConfigurationException(message.c_str());
                }
                else
                {
                    
                    
                    // Found the service
                    DataObjectList& portList = service->getList("port");
                    for (int j=0; j<portList.size();j++)
                    {
                        string portListName(portList[j]->getCString("name"));
                        if (portListName.compare(portName) == 0)
                        {
                            
                            // found port
                            // Add address at this point
                            string targetAddress(portList[j]->getCString("address/location"));
                            
                            // find operation by traversing the binding, portType then operation
                            string wsBindingName(portList[j]->getCString("binding"));
                            
                            DataObjectPtr wsBinding = findBinding(wsBindingName);
                            if (!wsBinding)
                            {
                                message = "Unable to find binding ";
                                message = message + wsBindingName;
                                message = message + " in the WSDL definition";
                                throw SystemConfigurationException(message.c_str());
                            }

                            // Get the soapAction
                            string soapAction = getSoapAction(wsBinding, operationName);
                            
                            
                            // Found the binding, get the portType
                            string wsPortTypeName(wsBinding->getCString("type"));
                            DataObjectPtr wsPortType = findPortType(wsPortTypeName);
                            if (!wsPortType)
                            {
                                message = "Unable to find PortType ";
                                message = message + wsPortTypeName;
                                message = message + " in the WSDL definition";
                                throw SystemConfigurationException(message.c_str());
                            }

                            Utils::printDO(wsPortType);
                            
                            // Found the portType, find the operation
                            DataObjectList& operationList = wsPortType->getList("operation");
                            for (int k=0; k< operationList.size(); k++)
                            {
                                string opName(operationList[k]->getCString("name"));
                                if( opName.compare(operationName) == 0)
                                {
                                    // Found the operation

                                    // Find the type of the request message
                                    string inputMessageType =  string(operationList[k]->getCString("input/message"));

                                    DataObjectPtr wsMessageIn = findMessage(inputMessageType);
                                    if (!wsMessageIn)
                                    {
                                        message = "Unable to find message ";
                                        message = message + inputMessageType;
                                        message = message + " in the WSDL definition";
                                        throw SystemConfigurationException(message.c_str());
                                    }

                                    string requestType(wsMessageIn->getList("part")[0]->getCString("element"));
                                    string requestTypeName;
                                    string requestTypeUri;
                                    // Utils::tokeniseQName(requestType, requestTypeUri, requestTypeName); 
                                    Utils::rTokeniseString(":", requestType, requestTypeUri, requestTypeName); 

                                    // Find the type of the response message
                                    string outputMessageType =  string(operationList[k]->getCString("output/message"));

                                    DataObjectPtr wsMessageOut = findMessage(outputMessageType);
                                    if (!wsMessageOut)
                                    {
                                        message = "Unable to find message ";
                                        message = message + outputMessageType;
                                        message = message + " in the WSDL definition";
                                        throw SystemConfigurationException(message.c_str());
                                    }

                                    string responseType(wsMessageOut->getList("part")[0]->getCString("element"));
                                    string responseTypeName;
                                    string responseTypeUri;
                                    // Utils::tokeniseQName(responseType, responseTypeUri, responseTypeName); 
                                    Utils::rTokeniseString(":", responseType, responseTypeUri, responseTypeName);

                                    return WsdlOperation(requestTypeName, 
                                        soapAction, 
                                        targetAddress, 
                                        responseTypeName);
                                }
                                
                            }
                            
                            message = "Unable to find Operation ";
                            message = message + operationName;
                            message = message + " in the WSDL definition";
                            throw SystemConfigurationException(message.c_str());
                            
                            
                            
                            
                        }
                        
                        
                        
                        
                    }
                    // cannot find the port
                    message = "Unable to find port ";
                    message = message + portName;
                    message = message + " in the WSDL definition";
                    throw SystemConfigurationException(message.c_str());
                }
                
            }
            
        

            ///
            /// Find a service
            ///
            DataObjectPtr Wsdl::findService(const string& serviceName)
            {
                DataObjectPtr service = 0;
        

                // Find the binding
                DataObjectList& serviceList = wsdlModel->getList("service");
                for (int i=0; i<serviceList.size(); i++)
                {
                    string name(serviceList[i]->getCString("name"));

                    if (name.compare(serviceName) == 0)
                    {
                        return serviceList[i];
                    }
                }

                return service;
            }


            ///
            /// Find a named binding
            ///
            DataObjectPtr Wsdl::findBinding(const string& bindingName)
            {
                DataObjectPtr binding = 0;
                string uri;
                string name;


                //Utils::tokeniseQName(bindingName, uri, name);
                Utils::rTokeniseString(":", bindingName, uri, name);


                // Find the binding
                DataObjectList& bindingList = wsdlModel->getList("binding");
                for (int i=0; i<bindingList.size(); i++)
                {
                    string nameBinding(bindingList[i]->getCString("name"));

                    if (nameBinding.compare(name) == 0)
                    {
                        return bindingList[i];
                    }
                }

                return binding;
            }

            ///
            /// Find a named portType
            ///
            DataObjectPtr Wsdl::findPortType(const string& portTypeName)
            {
                DataObjectPtr portType = 0;
                string uri;
                string name;


                // Utils::tokeniseQName(portTypeName, uri, name);
                Utils::rTokeniseString(":", portTypeName, uri, name);


                // Find the binding
                DataObjectList& portTypeList = wsdlModel->getList("portType");
                for (int i=0; i<portTypeList.size(); i++)
                {
                    string namePortType(portTypeList[i]->getCString("name"));

                    if (namePortType.compare(name) == 0)
                    {
                        return portTypeList[i];
                    }
                }

                return portType;
            }

            ///
            /// Find a named message
            ///
            DataObjectPtr Wsdl::findMessage(const string& messageName)
            {
                DataObjectPtr message = 0;
                string uri;
                string name;


                // Utils::tokeniseQName(messageName, uri, name);
                Utils::rTokeniseString(":", messageName, uri, name);


                // Find the binding
                DataObjectList& messageList = wsdlModel->getList("message");
                for (int i=0; i<messageList.size(); i++)
                {
                    string nameMessage(messageList[i]->getCString("name"));

                    if (nameMessage.compare(name) == 0)
                    {
                        return messageList[i];
                    }
                }

                return message;
            }

            ///
            /// Get the soap action
            ///
            string Wsdl::getSoapAction(DataObjectPtr binding, const string& operationName)
            {
                
        
                // Find the binding operation
                DataObjectList& bindingOperationList = binding->getList("operation");
                for (int i=0; i<bindingOperationList.size(); i++)
                {
                    string name(bindingOperationList[i]->getCString("name"));

                    if (name.compare(operationName) == 0)
                    {
                        // Found the binding operation, return the soap action
                        string soapAction(bindingOperationList[i]->getCString("operation/soapAction"));
                        return soapAction;
                    }
                }

                return string("");
            }

            
        } // End namespace model
    } // End namespace sca
} // End namespace tuscany