summaryrefslogtreecommitdiffstats
path: root/tags/native-sca-1.0.incubating-M3/runtime/core/src/tuscany/sca/model/WSDLDefinition.cpp
blob: 5a98db3cddb632e9bba1585f4ac4dd1d0b11239c (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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$ */

#include "tuscany/sca/model/WSDLDefinition.h"
#include "tuscany/sca/model/WSDLOperation.h"
#include "tuscany/sca/util/Logging.h"
#include "tuscany/sca/util/Utils.h"
#include "tuscany/sca/core/Exceptions.h"

using namespace std;
using namespace commonj::sdo;
using namespace tuscany::sca::util;

namespace tuscany
{
    namespace sca
    {
        namespace model
        {
            
            // Constructor
            WSDLDefinition::WSDLDefinition(DataObjectPtr wsdlModel)
            {
                logentry(); 

                wsdlModels.insert(wsdlModels.end(), wsdlModel);
            }

            WSDLDefinition::~WSDLDefinition()
            {
                logentry(); 
            }

            /// 
            /// The namespace of the service and other definitions defined in this wsdl definition
            string WSDLDefinition::getNamespace()
            {
                logentry(); 
                return wsdlModels[0]->getCString("targetNamespace");
            }

            void WSDLDefinition::addWSDLModel(DataObjectPtr wsdlModel)
            {
                logentry(); 
                wsdlModels.insert(wsdlModels.end(), wsdlModel);
            }

            ///
            /// Find the operation defined in this wsdl
            ///
            const WSDLOperation& WSDLDefinition::findOperation(const string& serviceName, 
                                        const string& portName, 
                                        const string& operationName)
            {
                logentry(); 

                string message;
                
                string operationKey = serviceName+"#"+portName+"#"+operationName;
                OPERATION_MAP::iterator iter = operationMap.find(operationKey);
                if (iter != operationMap.end())
                {
                	return iter->second;
                }
                
                // 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";
                    throwException(SystemConfigurationException, message.c_str());
                }
                else
                {
                    
                    
                    // Found the service
                    DataObjectList& portList = service->getList("port");
                    for (unsigned 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";
                                throwException(SystemConfigurationException, message.c_str());
                            }
                            

                            string soapAction = "";
                            bool documentStyle = true;
                            bool wrappedStyle = true;
                            bool useEncoded = false;
                            WSDLOperation::soapVersion soapVer = WSDLOperation::SOAP11;

                            // Find the binding operation
                            DataObjectList& bindingOperationList = wsBinding->getList("operation");
                            for (unsigned int i=0; i<bindingOperationList.size(); i++)
                            {
                                string name(bindingOperationList[i]->getCString("name"));
                                
                                if (name.compare(operationName) == 0)
                                {
                                    DataObjectPtr op = bindingOperationList[i]->getDataObject("operation");
                                    string opType = op->getType().getURI();
                                    if (opType == "http://schemas.xmlsoap.org/wsdl/soap12/")
                                    {
                                        soapVer = WSDLOperation::SOAP12;
                                    }
 
                                    // Get the soapAction
                                    soapAction = bindingOperationList[i]->getCString("operation/soapAction");
                                    
                                    // Get the style
                                    string style = bindingOperationList[i]->getCString("operation/style");
                                    if (style == "")
                                    {
                                        style = wsBinding->getCString("binding/style");
                                    }
                                    if (style != "document")
                                    {
                                        documentStyle = false;
                                        wrappedStyle = false;
                                    }
 
                                    // get the use
                                    string use = bindingOperationList[i]->getCString("input/body/use");
                                    if (use == "encoded")
                                    {
                                        useEncoded = true;
                                    }
                                }
                            }

                            
                            // TODO - get the style from the binding or operation????
                            
                            // 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";
                                throwException(SystemConfigurationException, message.c_str());
                            }

                            //Utils::printDO(wsPortType);
                            
                            // Found the portType, find the operation
                            DataObjectList& operationList = wsPortType->getList("operation");
                            for (unsigned 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";
                                        throwException(SystemConfigurationException, message.c_str());
                                    }

                                    string requestType(wsMessageIn->getList("part")[0]->getCString("element"));

                                    // 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";
                                        throwException(SystemConfigurationException, message.c_str());
                                    }

                                    string responseType(wsMessageOut->getList("part")[0]->getCString("element"));
                                    
                                    WSDLOperation& wsdlOp = operationMap[operationKey];
                                    wsdlOp.setOperationName(operationName);
                                    wsdlOp.setSoapAction(soapAction);
                                    wsdlOp.setEndpoint(targetAddress);
                                    wsdlOp.setSoapVersion(soapVer);
                                    wsdlOp.setDocumentStyle(documentStyle);
                                    wsdlOp.setWrappedStyle(wrappedStyle);
                                    wsdlOp.setEncoded(useEncoded);
                                    wsdlOp.setInputType(requestType);
                                    wsdlOp.setOutputType(responseType);
                                    return wsdlOp;
                                }
                                
                            }
                            
                            message = "Unable to find Operation ";
                            message = message + operationName;
                            message = message + " in the WSDL definition";
                            throwException(SystemConfigurationException, message.c_str());                           
                        }
                    }
                    // cannot find the port
                    message = "Unable to find port ";
                    message = message + portName;
                    message = message + " in the WSDL definition";
                    throwException(SystemConfigurationException, message.c_str());
                }
                
            }
            
            ///
            /// Find the operation defined in this wsdl
            ///
            const WSDLOperation& WSDLDefinition::findOperation(const string& portTypeName, 
                                        const string& operationName)
            {
                logentry(); 

                string operationKey = portTypeName+"#"+operationName;
                OPERATION_MAP::iterator iter = operationMap.find(operationKey);
                if (iter != operationMap.end())
                {
                    return iter->second;
                }
                
                string soapAction = getNamespace() + "#" + operationName;
                bool documentStyle = true;
                bool wrappedStyle = true;
                bool useEncoded = false;
                WSDLOperation::soapVersion soapVer = WSDLOperation::SOAP11;

                // Get the portType
                DataObjectPtr wsPortType = findPortType(portTypeName);
                if (!wsPortType)
                {
                    string message = "Unable to find PortType ";
                    message = message + portTypeName;
                    message = message + " in the WSDL definition";
                    throwException(SystemConfigurationException, message.c_str());
                }

                // Found the portType, find the operation
                DataObjectList& operationList = wsPortType->getList("operation");
                for (unsigned 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)
                        {
                            string message = "Unable to find message ";
                            message = message + inputMessageType;
                            message = message + " in the WSDL definition";
                            throwException(SystemConfigurationException, message.c_str());
                        }

                        string requestType(wsMessageIn->getList("part")[0]->getCString("element"));

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

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

                        string responseType(wsMessageOut->getList("part")[0]->getCString("element"));
                        
                        WSDLOperation& wsdlOp = operationMap[operationKey];
                        wsdlOp.setOperationName(operationName);
                        wsdlOp.setSoapAction(soapAction);
                        wsdlOp.setEndpoint("");
                        wsdlOp.setSoapVersion(soapVer);
                        wsdlOp.setDocumentStyle(documentStyle);
                        wsdlOp.setWrappedStyle(wrappedStyle);
                        wsdlOp.setEncoded(useEncoded);
                        wsdlOp.setInputType(requestType);
                        wsdlOp.setOutputType(responseType);
                        return wsdlOp;
                    }
                }
                
                string message = "Unable to find Operation ";
                message = message + operationName;
                message = message + " in the WSDL definition";
                throwException(SystemConfigurationException, message.c_str());
                
            }
            
            ///
            /// Find a service
            ///
            DataObjectPtr WSDLDefinition::findService(const string& serviceName)
            {
                logentry(); 

                DataObjectPtr service = 0;

                // Find the binding
                for (unsigned int m = 0; m < wsdlModels.size(); m++)
                {
                    DataObjectList& serviceList = wsdlModels[m]->getList("service");
                    for (unsigned 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 WSDLDefinition::findBinding(const string& bindingName)
            {
                logentry(); 

                DataObjectPtr binding = 0;
                string uri;
                string name;

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

                // Find the binding
                for (unsigned int m = 0; m < wsdlModels.size(); m++)
                {
                    DataObjectList& bindingList = wsdlModels[m]->getList("binding");
                    for (unsigned 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 WSDLDefinition::findPortType(const string& portTypeName)
            {
                logentry(); 

                DataObjectPtr portType = 0;
                string uri;
                string name;

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

                // Find the binding
                for (unsigned int m = 0; m < wsdlModels.size(); m++)
                {
                    DataObjectList& portTypeList = wsdlModels[m]->getList("portType");
                    for (unsigned 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 WSDLDefinition::findMessage(const string& messageName)
            {
                logentry(); 

                DataObjectPtr message = 0;
                string uri;
                string name;

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

                // Find the binding
                for (unsigned int m = 0; m < wsdlModels.size(); m++)
                {
                    DataObjectList& messageList = wsdlModels[m]->getList("message");
                    for (unsigned int i=0; i<messageList.size(); i++)
                    {
                        string nameMessage(messageList[i]->getCString("name"));
    
                        if (nameMessage.compare(name) == 0)
                        {
                            return messageList[i];
                        }
                    }
                }

                return message;
            }
            
        } // End namespace model
    } // End namespace sca
} // End namespace tuscany