summaryrefslogtreecommitdiffstats
path: root/tags/cpp-sca-20060405/runtime/axis_binding/wrapper/src/SCAWSWrapper.cpp
blob: 57f3f40148563c1d3074e5db45a522c5081b1ba4 (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
/*
 *
 *  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 "SCAWSWrapper.h"

#include "commonj/sdo/SDO.h"
#include "tuscany/sca/util/Exceptions.h"
#include "tuscany/sca/core/SCAEntryPoint.h"

#include "axis/GDefine.h"
#include "AxisServiceException.h"


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

/** Construct an SCAWSWrapper.
 */
SCAWSWrapper::SCAWSWrapper()
{ 
}

/** Destruct an SCAWSWrapper.
 */
SCAWSWrapper::~SCAWSWrapper()
{
}


//
// Implementation of WrapperClassHandler interface
//

/** Perform any necessary initialization.
 */
int SCAWSWrapper::init()
{
    return AXIS_SUCCESS;
}

/** Perform an necessary finalization.
 */
int SCAWSWrapper::fini()
{
    return AXIS_SUCCESS;
}

/** Invoke a web service operation.  The invoke method expects SCA specific
 *  properties to have been set into the MessageData by the SCAWSHandler.
 *  Using those properties, the wrapper will invoke the correct SCA Entry
 *  Point.
 *  @param pMsg - pointer to IMessageData.
 *
 *  @see SCAWSHandler
 */
int SCAWSWrapper::invoke(void *pMsg)
{
    
    IMessageData *pIMsg = (IMessageData*)pMsg;
    const AxisChar *operationName = pIMsg->getOperationName();
    
    int axisReturn = invokeService(pIMsg, operationName);
    return axisReturn;
}

/** Handle Faults.
 */
void SCAWSWrapper::onFault(void *pMsg)
{
}


// Invoke an operation on an SCA Entry Point.
int SCAWSWrapper::invokeService(IMessageData *pIMsg, 
                                const AxisChar *operationName)
{
    int axisReturn = AXIS_SUCCESS;
    
    // Get the SoapSerializer.
    IWrapperSoapSerializer* pIWSSZ;
    pIMsg->getSoapSerializer(&pIWSSZ);
    if (!pIWSSZ)
    {
        return AXIS_FAIL;
    }

    // Get the SoapDeSerializer.
    IWrapperSoapDeSerializer* pIWSDZ;
    pIMsg->getSoapDeSerializer(&pIWSDZ);
    if (!pIWSDZ)
    {
        return AXIS_FAIL;
    }

    // Target Namespace was set into the MessageData by the SCAWSHandler.
    const AxisChar *targetNamespace = (const AxisChar *)pIMsg->getProperty("targetNamespace");

    // Make sure we have the correct message.
    if (AXIS_SUCCESS != pIWSDZ->checkMessageBody(operationName, targetNamespace))
    {
        return AXIS_FAIL;
    }

    try
    {
        //
        // Create the SCA EntryPoint
        //
        const AxisChar *scaEntryPointName = (const AxisChar *)pIMsg->getProperty("scaEntryPoint");
        SCAEntryPoint entrypoint(scaEntryPointName);        

        // Get the DataFactory which has Types loaded from WSDLs
        DataFactoryPtr dataFactory = entrypoint.getDataFactory();
    
        //
        // Get the Soap body and create an SDO request object from it.
        //
        AnyType *soapAny = pIWSDZ->getAnyObject();

        // The Doc Literal soap message does not include the root element (operation name) when
        // we use getAnyObject. Axis calls this element the 'soap method' but there is no 
        // 'getSoapMethod' method on the deserializer - we need to wrap the soap body with an
        // element named for the operation name.
                
        string soapBody("<");
        soapBody.append(operationName);
        soapBody.append(" ");
        soapBody.append("xmlns");
        soapBody.append("=\"");
        soapBody.append(targetNamespace);
        soapBody.append("\">");
        for (int i=0; i < soapAny->_size ; i++)
        {
            // The soap body is stored in the _array member of AnyType.
            soapBody.append(soapAny->_array[i]);
        }
        soapBody.append("</");
        soapBody.append(operationName);
        soapBody.append(">");

        //
        // Create the SDO request object from the soap body.
        //
        XMLHelperPtr xmlHelper = HelperProvider::getXMLHelper(dataFactory);
        XMLDocumentPtr xmlDoc = xmlHelper->load(soapBody.c_str(), targetNamespace);
        DataObjectPtr requestSDO = xmlDoc->getRootDataObject();

        //
        // Invoke the operation on the SCA EntryPoint.
        //
        DataObjectPtr responseSDO = entrypoint.invoke(operationName, requestSDO);
        
        //
        // Serialize the responseSDO into a SOAP response.
        //
        // Get the name of the response SDO. The entry point was invoked using an
        // SDOStub and the stub makes sure that the response includes the root 
        // element (operation response name).
        
        const Type &responseType = responseSDO->getType();
        const char *operationResponseName = responseType.getName();

        // Get the first child of the root element - this is the soap body 
        // to return in the response. The body will be wrapped with the root
        // element (operation response name) when we call the Axis method 
        // 'createSoapmMethod'.
        DataObjectPtr soapBodySDO = responseSDO->getDataObject((unsigned int)0);
        const Type& soapBodyType = soapBodySDO->getType();
        const char *soapBodyName = soapBodyType.getName();
        // Convert the soap body DataObject into XML.
        XMLDocumentPtr responseDoc =  xmlHelper->createDocument(soapBodySDO, 
                                                                targetNamespace,
                                                                soapBodyName);
        responseDoc->setXMLDeclaration(false);
        char *responseXML = xmlHelper->save(responseDoc);
    
        //
        // Serialize the response
        //
        // Wrap the soap body with the root element (operation response name).
        pIWSSZ->createSoapMethod(operationResponseName, targetNamespace);
        // Add the XML response document (soap body) as an AnyType.
        AnyType *soapAnyResponse = new AnyType();
        soapAnyResponse->_size = 1;
        soapAnyResponse->_array = new char*[1];
        soapAnyResponse->_array[0] = strdup(responseXML);

        pIWSSZ->addOutputAnyObject(soapAnyResponse);
        
    }
    catch(ServiceRuntimeException e)
    {
    	
        // Throw a useful fault
        string faultCode = string("Server.TuscanySCA.") + string(e.getEClassName());
        string faultString = string(e.getEClassName()) + string(":") + string (e.getMessageText());
        
        pIWSSZ->createSoapFault("ServiceRuntimeException","tempURI", faultCode.c_str(), faultString.c_str());
        throw AxisServiceException();
    }
    catch(SDORuntimeException e)
    {
    	// Throw a useful fault
        string faultCode = string("Server.TuscanySDO.") + string(e.getEClassName());
        string faultString = string(e.getEClassName()) + string(":") + string (e.getMessageText());
        
        pIWSSZ->createSoapFault("SDORuntimeException","tempURI", faultCode.c_str(), faultString.c_str());
        throw AxisServiceException();
    }


    return axisReturn;
}

//
// These functions are exported from the SCAWSWrapper DLL and are called by the Axis Engine
// to create/destroy instances of the service wrapper class.
//
extern "C" 
{
STORAGE_CLASS_INFO
int GetClassInstance(BasicHandler **inst)
{
    *inst = new BasicHandler();
    WrapperClassHandler* pWCH = new SCAWSWrapper();
    (*inst)->_functions = 0;
    if (pWCH)
    {
        (*inst)->_object = pWCH;
        return pWCH->init();
    }
    return AXIS_FAIL;
}
STORAGE_CLASS_INFO 
int DestroyInstance(BasicHandler *inst)
{
    if (inst)
    {
        WrapperClassHandler* pWCH = reinterpret_cast<WrapperClassHandler*>(inst);
        pWCH->fini();
        delete pWCH;
        delete inst;
        return AXIS_SUCCESS;
    }
    return AXIS_FAIL;
}
} // extern "C"