File: response.cpp

package info (click to toggle)
qxmlrpc 0.0.svn6-2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 392 kB
  • ctags: 200
  • sloc: cpp: 1,621; python: 29; makefile: 23
file content (266 lines) | stat: -rw-r--r-- 5,865 bytes parent folder | download | duplicates (3)
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
// vim:tabstop=4:shiftwidth=4:expandtab:cinoptions=(s,U1,m1
// Copyright (C) 2007
// Author Dmitry Poplavsky <dmitry.poplavsky@gmail.com>

#include "response.h"

#include <QtXml>

using namespace xmlrpc;

class Response::Private : public QSharedData {
public:
    Private() 
    :isNull(true), isFault(false), faultCode(0) {};

    Private( const Private& other ) 
    :QSharedData(other)
    {
        isNull = other.isNull;

        isFault = other.isFault;

        faultCode = other.faultCode;
        faultString = other.faultString;

        returnValue = other.returnValue;

    }

    bool isNull;

    bool isFault;
    int faultCode;
    QString faultString;

    Variant returnValue;
};

/**
 * Construct an emty XmlRPC request.
 */
Response::Response()
{
    d = new Private();
}

/**
 * Construct XmlRPC successful response with returnValue value
 */
Response::Response( const Variant& returnValue )
{
    d = new Private();
    d->isNull = false;

    d->isFault = false;

    d->returnValue = returnValue;
}

/**
 * Construct XmlRPC failed response with faultCode and
 * faultString values
 */
Response::Response( int faultCode, const QString& faultString )
{
    d = new Private();
    d->isNull = false;

    d->isFault = true;

    d->faultCode = faultCode;
    d->faultString = faultString;
}

Response::~Response()
{
}

/**
 * Returns true if the response is null ( not initialised );
 * otherwise returns false.
 **/
bool Response::isNull() const
{
    return d->isNull;
}

/**
 * Returns true is this response is fault response.
 */
bool Response::isFault() const
{
    return d->isFault;
}

/**
 * Returns fault code assotiated with this response.
 * 
 * It is valid only for fault responses.
 */
int Response::faultCode() const
{
    Q_ASSERT( isFault() );
    return d->faultCode;
}

/**
 * Returns fault string assotiated with this response.
 * 
 * It is valid only for fault responses.
 */
QString Response::faultString() const
{
    Q_ASSERT( isFault() );
    return d->faultString;
}

/**
 * Returns return value assotiated with this response.
 * 
 * It is valid only for not fault responses.
 */
Variant Response::returnValue() const
{
    Q_ASSERT( !isFault() );
    return d->returnValue;
}


/**
 * This function parses the XML-RPC response from the byte array
 * \a requestData. If a parse error occurs, this function
 * returns false and the error message is placed in * \a
 * errorMessage.
 */
bool Response::setContent( const QByteArray& responseData, QString *errorMessage )
{
    d->isNull = true;

    QDomDocument doc;

    QString domErrorMsg;
    int domErrorLine;
    int domErrorColumn;



    if ( !doc.setContent( responseData, &domErrorMsg, &domErrorLine, &domErrorColumn) ) {
        if ( errorMessage ) {
            *errorMessage = QString("%1\nline: %2 column:%3")\
                            .arg(domErrorMsg)\
                            .arg(domErrorLine)\
                            .arg(domErrorColumn);
        }

        return false;
    }

    QString errorString;


    do {
        QDomElement methodResponse = doc.firstChildElement("methodResponse");

        if ( methodResponse.isNull() ) {
            errorString = "Missing methodResponse tag";
            break;
        }

        QDomElement resNode = methodResponse.firstChildElement();

        if ( resNode.tagName() == "params" ) {
            // the call was successful

            d->isFault = false;

            QDomElement paramValue = resNode.firstChild().firstChild().toElement();

            d->returnValue = Variant( paramValue );

            if ( !d->returnValue.isValid() ) {
                errorString = "Invalid return value";
                break;
            }

            d->isNull = false;
            break;
        };


        if ( resNode.tagName() == "fault" ) {
            // server returned fault code, return error id and string

            d->isFault = true;

            QDomElement paramValue = resNode.firstChildElement();

            Variant res(paramValue); // should be a map
            if ( res.type() != QVariant::Map ) {
                errorString = QString("Expected struct for xmlrpc fault, received %1").arg( paramValue.firstChildElement().nodeName() );
                break;
            }

            QMap<QString,QVariant> map = res.toMap();

            if ( !map.contains("faultCode") || !map.contains("faultString") ) {
                errorString = "Fault struct doesn't contain fault code or string values";
                break;
            }

            d->faultCode = map["faultCode"].toInt();
            d->faultString = map["faultString"].toString();

            d->isNull = false;
            break;
        }

        errorString = QString("Unexpected tag: %1").arg( resNode.tagName() );

    } while (0);


    if ( d->isNull ) {
        if ( errorMessage ) {
            *errorMessage = errorString;
            return false;
        }
    }

    return true;
}

/**
 * Compose XmlRPC response data.
 */
QByteArray Response::composeResponse() const
{
    Q_ASSERT( !isNull() );

    QDomDocument doc;


    QDomElement methodResponse = doc.createElement("methodResponse");
    doc.appendChild( methodResponse );

    if ( isFault() ) {
        QDomElement fault = doc.createElement("fault");
        methodResponse.appendChild( fault );

        QMap<QString, Variant> faultValue;
        faultValue[ "faultCode" ] = faultCode();
        faultValue[ "faultString" ] = faultString();

        fault.appendChild( Variant(faultValue).toDomElement(doc) );
    } else {
        QDomElement params = doc.createElement("params");
        methodResponse.appendChild( params );
        QDomElement param = doc.createElement("param");
        params.appendChild( param );

        param.appendChild( d->returnValue.toDomElement(doc) );
    }

    return doc.toByteArray(2);
}