summaryrefslogtreecommitdiffstats
path: root/tags/cpp-sca-20060405/runtime/core/src/tuscany/sca/ws/SDOStub.cpp
blob: 704097833e93b6b3a0403c2faab0e23ed7b67c56 (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
/*
 *
 *  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 "SDOStub.h"
#include "commonj/sdo/HelperProvider.h"
#include "osoa/sca/export.h"
#include "tuscany/sca/util/Logging.h"
#include "tuscany/sca/util/Exceptions.h"
#include "tuscany/sca/util/Utils.h"


// For Axis stub code
#include <axis/AxisWrapperAPI.hpp>
#include <axis/client/Stub.hpp>
#include <axis/OtherFaultException.hpp>
#include <axis/ISoapFault.hpp>

using namespace std;

AXIS_CPP_NAMESPACE_USE

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


namespace tuscany
{
namespace sca
{
    namespace ws
    {
        /** Construct an SDOStub using the default HTTP 1.1 transport.
         */
        SDOStub::SDOStub() 
                : Stub(" ", APTHTTP1_1),
                  m_strEndpoint(""), 
                  m_strTargetNamespace(""),
                  m_strOperationName(""),
                  m_strOperationResponseName(""),
                  m_strSoapAction("")
        {
        }


        /** Construct an SDOStub using the default HTTP 1.1 transport 
         *  and set the web service endpoint URI.
         *  @param endpoint - web service endpoint URI
         */
        SDOStub::SDOStub(const char* endpoint)
                : Stub(endpoint, APTHTTP1_1), 
                  m_strEndpoint(endpoint),
                  m_strTargetNamespace(""),
                  m_strOperationName(""),
                  m_strOperationResponseName(""),
                  m_strSoapAction("")
        {
        }


        /** Destruct the SDOStub
         */
        SDOStub::~SDOStub()
        {
        }

        /** Set the web service endpoint URI.
         *  The endpoint is the value of the soap:address element in the WSDL.
         *  @param endpoint - web service endpoint URI.
         */
        void SDOStub::setEndpoint(const char* endpoint)
        {
            m_strEndpoint = endpoint;
            // Stub::setEndPoint
            setEndPoint(endpoint);
        }

        /** Get the web service endpoint URI. 
         *  @return web service endpoint URI.
         */
        const char* SDOStub::getEndpoint()
        {
            return m_strEndpoint.c_str();
        }

        /** Set the target namespace for elements and operations.
         *  @param targetNamespace - the target namespace.
          */
        void SDOStub::setTargetNamespace(const char* targetNamespace)
        {
            m_strTargetNamespace = targetNamespace;
        }

        /** Get the target namespace.
         *  @return - target namespace.
         */
        const char* SDOStub::getTargetNamesapce()
        {
            return m_strTargetNamespace.c_str();
        }

        /** Set the name of the operation to invoke. 
         *  @param operationName - operation name
         */
        void SDOStub::setOperationName(const char* operationName)
        {
            m_strOperationName = operationName;
        }

        /** Get the operation name.
         *  @return operation name.
         */
        const char* SDOStub::getOperationName()
        {
            return m_strOperationName.c_str();
        }
        
        /** Set the name of the element that is returned as the result of a soap method.
         *  @param operationResponseName - name of the response element.
         */
        void SDOStub::setOperationResponseName(const char* operationResponseName)
        {
            m_strOperationResponseName = operationResponseName;
        }

        /** Get the name of the element that is returned as the result of a soap method.
         *  @return operation response name.
         */
        const char* SDOStub::getOperationResponseName()
        {
            return m_strOperationResponseName.c_str();
        }

        /** Set the SOAPAction header.
         *  @param soapAction - the soap action.
         */
        void SDOStub::setSoapAction(const char* soapAction)
        {
            m_strSoapAction = soapAction;
        }
        
        /** Get the SoapAction header.
         *  @return soap action.
         */
        const char* SDOStub::getSoapAction()
        {
            return m_strSoapAction.c_str();
        }

        /** Invoke the operation on the web service.
         *  @param requestDO - SDO containing the input parameters for the operation.
         *  @param dataFactory - DataFactory for de/serialzing the SDOs.
         *  @return DataObjectPtr - SDO containg the response from the web service.
         */
        DataObjectPtr SDOStub::invoke(DataObjectPtr requestDO, DataFactoryPtr dataFactory)
        {
        	LOGENTRY(2, "SDOStub::invoke");
        	
            // SDO return object
            DataObjectPtr pReturn;

            // For faults
            //const char* pcCmplxFaultName = NULL;

            //  Get an XMLHelper
            XMLHelperPtr xmlHelper = HelperProvider::getXMLHelper(dataFactory);


            try
            {
                // Initialize the call structure for Doc Literal request/response.
                if (AXIS_SUCCESS != m_pCall->initialize(CPP_DOC_PROVIDER))
                {
                    return pReturn; // Return if there is a failure
                }

                // Set the SoapAction if it is not already set.
                if (NULL == m_pCall->getTransportProperty("SOAPAction",false))
                {
                    m_pCall->setTransportProperty(SOAPACTION_HEADER , m_strSoapAction.c_str());
                }

                // Set the Soap Version.
                m_pCall->setSOAPVersion(SOAP_VER_1_1);

                // Set the Operation (operationName and targetNamespace)
                m_pCall->setOperation(m_strOperationName.c_str(), m_strTargetNamespace.c_str());

                //includeSecure(); 
                //applyUserPreferences(); 

                //
                // Serialize the DataObject.
                //

                // DEBUG
                XMLDocumentPtr requestDoc = 
                    xmlHelper->createDocument(requestDO, 
                                              m_strTargetNamespace.c_str(),
                                              m_strOperationName.c_str());
                requestDoc->setXMLDeclaration(false);
                char *requestXML = xmlHelper->save(requestDoc);
                LOGINFO_1(2, "SDOStub::invoke: Request XML=%s", requestXML);

                // Loop through the top level data object, serializing individual properties.
                // We cannot simply serialize the request DataObject to an XML string and add 
                // the XML as an AnyType parameter because Axis will wrap the request with an 
                // element named for the operationName (Doc Literal/Wrapped), duplicating the
                // top-level element.
                PropertyList properties = requestDO->getInstanceProperties();
                for (int i=0; i < properties.size() ; i++)
                {
                    Property& property = properties[i];
                    const char *propertyName = property.getName();
                    
                    // If the property is not set, don't bother adding it as a parameter.
                    if (!requestDO->isValid(property)) continue;

                    switch (property.getTypeEnum())
                    {
                       // Simple string property - add it as an XSD_STRING parameter.
                       case Type::StringType:
                            {
                                if (!property.isMany())
                                {
                                    string strPrefixAndParamName;
                                    const char *namespacePrefix = 
                                        m_pCall->getNamespacePrefix(m_strTargetNamespace.c_str());
                                    strPrefixAndParamName.append(namespacePrefix);
                                    strPrefixAndParamName.append(":");
                                    strPrefixAndParamName.append(propertyName);
                                    const char *paramValue = requestDO->getCString(i);
                                    m_pCall->addParameter((void *)paramValue, 
                                                          strPrefixAndParamName.c_str(), 
                                                          XSD_STRING);
                                }
                            }
                            break;

                        // DataObject property.
                        case Type::DataObjectType:
                            {
                                const char *dobXML = NULL; 
                                if (property.isMany())
                                {
                                    // Many valued property - get the list.
                                    DataObjectList& dobList = requestDO->getList(property);
                                    for (int j=0; j< dobList.size(); j++)
                                    {
                                        DataObjectPtr dob = dobList[j];
                                        if (dob != 0)
                                        {
                                            // Create an XML string from the DataObject.
                                            XMLDocumentPtr doc = 
                                            xmlHelper->createDocument(dob, 
                                                                        m_strTargetNamespace.c_str(),
                                                                        propertyName);
                                            doc->setXMLDeclaration(false);
                                            dobXML = xmlHelper->save(doc);

                                            // Add the XML to the call as an AnyType.
                                            AnyType* pAny = new AnyType();
                                            pAny->_size = 1;
                                            pAny->_array = new char*[1];
                                            pAny->_array[0] = strdup(dobXML);
                                            m_pCall->addAnyObject(pAny);
                                        }
                                    } 
                                } // if isMany
                                else
                                {
                                    // Single valued property.
                                    DataObjectPtr dob = requestDO->getDataObject(property);
                                    if (dob != 0)
                                    {
                                        // Create an XML string from the DataObject.
                                        XMLDocumentPtr doc = 
                                            xmlHelper->createDocument(dob,
                                                                        m_strTargetNamespace.c_str(),
                                                                        propertyName);
                                        doc->setXMLDeclaration(false);
                                        dobXML = xmlHelper->save(doc);

                                        // Add the XML to the call as an AnyType.
                                        AnyType* pAny = new AnyType();
                                        pAny->_size = 1;
                                        pAny->_array = new char*[1];
                                        pAny->_array[0] = strdup(dobXML);
                                        m_pCall->addAnyObject(pAny);
                                    }
                                } 
                             }
                            break;

                        default:
                            break;
                    }
                }

                // Invoke the operation.
                if (AXIS_SUCCESS == m_pCall->invoke())
                {
                    // Make sure the response element is what we expect; ie that the element matches
                    // the operation response name set on the stub and its targetNamespace matches
                    // the targetNamespace set on the stub.
                    if (AXIS_SUCCESS == m_pCall->checkMessage(m_strOperationResponseName.c_str(), 
                                                              m_strTargetNamespace.c_str()))
                    {
                        LOGINFO(2, "SDOStub::invoke: invoke returned AXIS_SUCCESS");
                        
                        // Deserialize the soap response.
                        string strResponse = getSoapResponse();
                        
                        // Create an XMLDocument from the soap reponse
                        XMLDocumentPtr returnDoc = 
                            xmlHelper->load(strResponse.c_str(), m_strTargetNamespace.c_str());
                        // Get the root DataObject to return as the result.
                        pReturn = returnDoc->getRootDataObject();
                        
                    }
                }
                else
                {
                    LOGINFO_2(2, "SDOStub::invoke: checkMessage failed. Expected: %s \' element with namespace \' %s \'.",  
                              m_strOperationResponseName.c_str(),
                              m_strTargetNamespace.c_str());
                }


                // Invoked the operation successfully - uninitialize the Call object.
                m_pCall->unInitialize();
                
            }
            catch (AxisException& e)
            {
                // Get the exception code.
                int iExceptionCode = e.getExceptionCode();
                LOGINFO_2(0, "SDOStub::invoke Caught AxisException: %d code: %s", iExceptionCode, e.what());
                if (AXISC_NODE_VALUE_MISMATCH_EXCEPTION != iExceptionCode)
                {
                    m_pCall->unInitialize();
                     throw ServiceRuntimeException("AxisException", ServiceRuntimeException::Error, e.what());
                }

                // Get the details of the SoapFault.
                ISoapFault* pSoapFault = (ISoapFault*) m_pCall->checkFault("Fault", m_strEndpoint.c_str());

                if (pSoapFault)
                {
                    const char *detail = pSoapFault->getSimpleFaultDetail();
                    bool deleteDetail=false;

                    if (NULL==detail || 0==strlen(detail))
                    {
                        detail=m_pCall->getFaultAsXMLString();

                        
                        if (NULL==detail)
                        {
                            detail="";
                        }
                        else
                        {
                            deleteDetail=true;
                        }
                    }

                    OtherFaultException ofe(pSoapFault->getFaultcode(),
                                            pSoapFault->getFaultstring(), 
                                            pSoapFault->getFaultactor(),
                                            detail, iExceptionCode);

                    if (deleteDetail && NULL!=detail)
                    {
                        delete [] const_cast<char*>(detail);
                    }

                    m_pCall->unInitialize();
                    throw ofe;
                }
                else
                {
                    m_pCall->unInitialize();
                    throw ServiceRuntimeException("AxisException", ServiceRuntimeException::Error, e.what());
                }
            }
            catch(SDORuntimeException sdoE)
            {
                throw ServiceRuntimeException("SDORuntimeException", ServiceRuntimeException::Error, sdoE.getMessageText());
            }

        	LOGEXIT(2, "SDOStub::invoke");
            return pReturn;

        }

        // Deserialize the soap response from the web service.
        string SDOStub::getSoapResponse()
        {
            // Get the response as an XML string.
            AnyType* soapAny = m_pCall->getAnyObject();

            // The Doc Literal response does not include the top level
            // response element if we use getAnyObject - we need to wrap
            // the response with the response element.
            string strResponse("<");
            strResponse.append(m_strOperationResponseName);
            strResponse.append(" xmlns=\"");
            strResponse.append(m_strTargetNamespace);
            strResponse.append("\">");
            
            for (int j=0; j < soapAny->_size ; j++)
            {
                // DEBUG
                cout << "SDOStub::getSoapResponse: soapAny->_array[" << j << "]=" << endl;
                cout << soapAny->_array[j] << endl;
                cout << endl;
                // DEBUG
                
                strResponse.append(soapAny->_array[j]);
            }
            
            strResponse.append("</");
            strResponse.append(m_strOperationResponseName);
            strResponse.append(">");

            return strResponse;
        }

    } // end namespace ws
} // end namespace sca
} // end namespace tuscany