17
|
1 |
/**
|
|
2 |
* Copyright (c) 2010 Sasken Communication Technologies Ltd.
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the "{License}"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "{LicenseUrl}".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* class to handle calls to rest Server API's
|
|
16 |
*/
|
|
17 |
|
|
18 |
#define EMULATORTESTING
|
|
19 |
|
|
20 |
#include "requestSP.h"
|
|
21 |
#include "sessionSP.h"
|
|
22 |
#include "xmlParser.h"
|
|
23 |
#include "errorCodes.h"
|
|
24 |
|
|
25 |
#include <QNetworkRequest>
|
|
26 |
#include <QXmlSimpleReader>
|
|
27 |
#include <QXmlInputSource>
|
|
28 |
#include <QCryptographicHash>
|
|
29 |
#include <QtAlgorithms>
|
|
30 |
#include <QDebug>
|
|
31 |
#include <qnetworkproxy.h>
|
|
32 |
|
|
33 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
34 |
// global
|
|
35 |
|
|
36 |
static const QString kAPIVersion = "1.0";
|
|
37 |
static const QString kAPIFormat = "XML";
|
|
38 |
static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
|
|
39 |
|
|
40 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
41 |
|
|
42 |
static bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
|
|
43 |
{
|
|
44 |
return s1.toLower() < s2.toLower();
|
|
45 |
}
|
|
46 |
|
|
47 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
48 |
// Static class functions
|
|
49 |
|
|
50 |
FBRequest* FBRequest::request()
|
|
51 |
{
|
|
52 |
return FBRequest::requestWithSession(FBSession::session());
|
|
53 |
}
|
|
54 |
|
|
55 |
FBRequest* FBRequest::requestWithSession (FBSession* aSession)
|
|
56 |
{
|
|
57 |
FBRequest* request = new FBRequest(aSession);
|
|
58 |
return request;
|
|
59 |
}
|
|
60 |
|
|
61 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
62 |
// instance public functions
|
|
63 |
FBRequest::FBRequest(FBSession* aSession) : iSession ( aSession ), iNetworkAccessManager ( this )
|
|
64 |
{}
|
|
65 |
|
|
66 |
const QDateTime& FBRequest::timeStamp() const
|
|
67 |
{
|
|
68 |
return iTimestamp;
|
|
69 |
}
|
|
70 |
|
|
71 |
void FBRequest::connect()
|
|
72 |
{
|
|
73 |
qDebug()<<"Inside FBRequest::connect";
|
|
74 |
emit requestLoading();
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
QString url ;
|
|
79 |
/*if (iMethod.length())
|
|
80 |
url = iUrl;
|
|
81 |
else*/
|
|
82 |
url = generateGetURL();
|
|
83 |
|
|
84 |
qDebug() << "Complete URL : " << url;
|
|
85 |
|
|
86 |
QNetworkRequest request;
|
|
87 |
request.setUrl(QUrl(url));
|
|
88 |
|
|
89 |
/* from the Qt docs on QNetworkAccessManager:
|
|
90 |
QNetworkAccessManager by default does not have a set cache.
|
|
91 |
Qt provides a simple disk cache, QNetworkDiskCache, which can be used.
|
|
92 |
|
|
93 |
However we will not use it.*/
|
|
94 |
|
|
95 |
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
|
|
96 |
request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
|
|
97 |
|
|
98 |
iTimestamp = QDateTime::currentDateTime();
|
|
99 |
|
|
100 |
if (iMethod.length())
|
|
101 |
{
|
|
102 |
const QString contentType = "multipart/form-data; boundary=" + kStringBoundary;
|
|
103 |
request.setRawHeader("Content-Type", contentType.toUtf8());
|
|
104 |
|
|
105 |
/* connect all signals from iNetWorkAccessManager to this */
|
|
106 |
QByteArray postBody ;
|
|
107 |
generatePostBody (postBody);
|
|
108 |
|
|
109 |
pbar = new progressbar;
|
|
110 |
pbar->show();
|
|
111 |
|
|
112 |
#ifdef __WINSCW__
|
|
113 |
proxysettings();
|
|
114 |
#endif
|
|
115 |
QNetworkReply* reply = iNetworkAccessManager.get(request);
|
|
116 |
|
|
117 |
QObject::connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished()));
|
|
118 |
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
119 |
this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
|
|
120 |
|
|
121 |
}
|
|
122 |
}
|
|
123 |
void FBRequest::proxysettings()
|
|
124 |
{
|
|
125 |
#ifdef EMULATORTESTING
|
|
126 |
qDebug()<<"proxysettings";
|
|
127 |
|
|
128 |
// Reading the keys, CSM Stubbed - START
|
|
129 |
QFile file("c://data//DoNotShare.txt");
|
|
130 |
if (!file.open(QIODevice::ReadOnly))
|
|
131 |
{
|
|
132 |
qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
|
|
133 |
return;
|
|
134 |
}
|
|
135 |
|
|
136 |
QByteArray arr = file.readAll();
|
|
137 |
QList<QByteArray> list = arr.split(' ');
|
|
138 |
file.close();
|
|
139 |
|
|
140 |
QString username(list[0]);
|
|
141 |
QString password(list[1]);
|
|
142 |
|
|
143 |
QString httpProxy = "10.1.0.214";//ipwproxy.sasken.com
|
|
144 |
QString httpPort = "3128";
|
|
145 |
|
|
146 |
QString httpUser =username;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
|
|
147 |
QString httpPass =password;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
|
|
148 |
|
|
149 |
/*==Classes used from Network Module==*/
|
|
150 |
QNetworkProxy proxy;
|
|
151 |
|
|
152 |
proxy.setType(QNetworkProxy::HttpProxy);
|
|
153 |
proxy.setHostName(httpProxy);
|
|
154 |
proxy.setPort(httpPort.toInt());
|
|
155 |
proxy.setUser(httpUser);
|
|
156 |
proxy.setPassword(httpPass);
|
|
157 |
|
|
158 |
QNetworkProxy::setApplicationProxy(proxy);
|
|
159 |
#endif
|
|
160 |
}
|
|
161 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
162 |
// instance private functions
|
|
163 |
QString FBRequest::md5(const QString& aData)
|
|
164 |
{
|
|
165 |
QByteArray byteArray;
|
|
166 |
byteArray.insert(0, aData.toUtf8());
|
|
167 |
|
|
168 |
QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex();
|
|
169 |
QString returnString ( md5Hash );
|
|
170 |
|
|
171 |
return returnString;
|
|
172 |
}
|
|
173 |
|
|
174 |
bool FBRequest::isSpecialMethod() const {
|
|
175 |
return ( iMethod.compare("facebook.auth.getSession", Qt::CaseInsensitive) == 0
|
|
176 |
|| iMethod.compare("facebook.auth.createToken", Qt::CaseInsensitive) == 0 );
|
|
177 |
}
|
|
178 |
|
|
179 |
QString FBRequest::urlForMethod (const QString& aMethod) const {
|
|
180 |
|
26
|
181 |
Q_UNUSED(aMethod)
|
17
|
182 |
return iSession->apiURL();
|
|
183 |
}
|
|
184 |
|
|
185 |
QString FBRequest::generateGetURL() const
|
|
186 |
{
|
|
187 |
const QUrl url(iUrl);
|
|
188 |
const QString queryPrefix = url.hasQuery() ? "&" : "?";
|
|
189 |
|
|
190 |
QStringList pairs;
|
|
191 |
DictionaryIterator i(iParams);
|
|
192 |
|
|
193 |
while (i.hasNext()) {
|
|
194 |
i.next();
|
|
195 |
pairs << i.key().toUtf8() + "=" + i.value().toUtf8();
|
|
196 |
}
|
|
197 |
|
|
198 |
return iUrl + queryPrefix + pairs.join("&");
|
|
199 |
}
|
|
200 |
|
|
201 |
QString FBRequest::generateCallId() const {
|
|
202 |
QDateTime dateTime = QDateTime::currentDateTime();
|
|
203 |
uint secs = dateTime.toTime_t();
|
|
204 |
QString result = QString::number(secs, 10);
|
|
205 |
return result;
|
|
206 |
}
|
|
207 |
|
|
208 |
QString FBRequest::generateSig(Dictionary Params)
|
|
209 |
{
|
|
210 |
QString joined;
|
|
211 |
QStringList keys = Params.keys();
|
|
212 |
|
|
213 |
qSort(keys.begin(), keys.end(), caseInsensitiveLessThan);
|
|
214 |
|
|
215 |
QListIterator<QString> i(keys);
|
|
216 |
|
|
217 |
joined.append(iSession->apiSecret().toUtf8());
|
|
218 |
|
|
219 |
while (i.hasNext())
|
|
220 |
{
|
|
221 |
const QString key = i.next();
|
|
222 |
joined.append(key.toUtf8());
|
|
223 |
//joined.append("=");
|
|
224 |
joined.append(Params.value(key).toUtf8());
|
|
225 |
}
|
|
226 |
|
|
227 |
return md5(joined);
|
|
228 |
}
|
|
229 |
|
|
230 |
void FBRequest::generatePostBody( QByteArray& body )
|
|
231 |
{
|
|
232 |
QString endLine = "\r\n--" + kStringBoundary + "\r\n";
|
|
233 |
body.append( "--" + kStringBoundary.toUtf8() + "\r\n" ) ;
|
|
234 |
|
|
235 |
|
|
236 |
DictionaryIterator i (iParams);
|
|
237 |
|
|
238 |
while (i.hasNext())
|
|
239 |
{
|
|
240 |
i.next();
|
|
241 |
|
|
242 |
body.append("Content-Disposition: form-data; name=\"" + i.key().toUtf8() + "\"\r\n\r\n" );
|
|
243 |
body.append(i.value().toUtf8());
|
|
244 |
body.append(endLine.toUtf8());
|
|
245 |
}
|
|
246 |
|
|
247 |
|
|
248 |
if (iDataParam.size())
|
|
249 |
{
|
|
250 |
if (iDataParamPicture)
|
|
251 |
{
|
|
252 |
body.append("Content-Disposition: form-data; filename=\"photo\"\r\n" );
|
|
253 |
body.append("Content-Type: image/png\r\n\r\n" );
|
|
254 |
}
|
|
255 |
else
|
|
256 |
{
|
|
257 |
body.append("Content-Disposition: form-data; filename=\"data\"\r\n");
|
|
258 |
body.append("Content-Type: content/unknown\r\n\r\n");
|
|
259 |
}
|
|
260 |
|
|
261 |
body.append(iDataParam);
|
|
262 |
body.append(endLine.toUtf8());
|
|
263 |
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
void FBRequest::handleResponseData( const QByteArray& aResponseData )
|
|
268 |
{
|
|
269 |
qDebug()<<"Inside FBRequest::handleResponseData";
|
|
270 |
FBError error;
|
|
271 |
QVariant result = parseXMLResponse( aResponseData, error);
|
|
272 |
|
|
273 |
qDebug() << "parsed result : " << result;
|
|
274 |
|
|
275 |
if (error.code() != 0)
|
|
276 |
{
|
|
277 |
emit requestFailedWithFlickrError(error);
|
|
278 |
}
|
|
279 |
else
|
|
280 |
{
|
|
281 |
emit requestDidLoad(result);
|
|
282 |
}
|
|
283 |
|
|
284 |
delete pbar;
|
|
285 |
}
|
|
286 |
|
|
287 |
void FBRequest::post( const QString& aUrl, const Dictionary& aParams)
|
|
288 |
{
|
|
289 |
iUrl = aUrl;
|
|
290 |
iParams = aParams;
|
|
291 |
|
|
292 |
iSession->send(this);
|
|
293 |
}
|
|
294 |
|
|
295 |
void FBRequest::cancel()
|
|
296 |
{
|
|
297 |
|
|
298 |
}
|
|
299 |
|
|
300 |
|
|
301 |
void FBRequest::call (const QString& aMethod, const Dictionary& aParams)
|
|
302 |
{
|
|
303 |
qDebug()<<"Inside FBRequest::call";
|
|
304 |
QByteArray dataParams;
|
|
305 |
callWithDataParams(aMethod, aParams, dataParams, false);
|
|
306 |
|
|
307 |
}
|
|
308 |
void FBRequest::callforToken ()
|
|
309 |
{
|
|
310 |
|
|
311 |
iUrl = iSession->apiURL();
|
|
312 |
iMethod = "flickr.auth.getToken";
|
|
313 |
|
|
314 |
Dictionary params;
|
|
315 |
params["method"] = iMethod;
|
|
316 |
params["api_key"] = iSession->apiKey();
|
|
317 |
params["frob"] = iSession->frobKey();
|
|
318 |
|
|
319 |
// XXX: workaround what seems to be a Qt bug with the extras-devel libraries.
|
|
320 |
QString signature = generateSig(params);
|
|
321 |
|
|
322 |
params["api_sig"] = signature;
|
|
323 |
|
|
324 |
iParams = params;
|
|
325 |
// XXX: end workaround.
|
|
326 |
|
|
327 |
iSession->send(this);
|
|
328 |
}
|
|
329 |
|
|
330 |
void FBRequest::callWithDataParams (const QString& aMethod, const Dictionary& aParams, const QByteArray& aDataParam, bool aDataParamPicture)
|
|
331 |
{
|
|
332 |
qDebug()<<"Inside FBRequest::callWithDataParams";
|
|
333 |
iUrl = urlForMethod(aMethod);
|
|
334 |
iMethod = aMethod;
|
|
335 |
iParams = aParams;
|
|
336 |
iDataParam = aDataParam;
|
|
337 |
iDataParamPicture = aDataParamPicture;
|
|
338 |
|
|
339 |
|
|
340 |
iParams["method"] = iMethod;
|
|
341 |
iParams["api_key"] = iSession->apiKey();
|
|
342 |
|
|
343 |
// XXX: workaround what seems to be a Qt bug with the extras-devel libraries.
|
|
344 |
QString signature = generateSig(iParams);
|
|
345 |
iParams["api_sig"] = signature;
|
|
346 |
// XXX: end workaround.
|
|
347 |
|
|
348 |
iSession->send(this);
|
|
349 |
}
|
|
350 |
|
|
351 |
|
|
352 |
QVariant FBRequest::parseXMLResponse ( const QByteArray& aResponseData, FBError& aError)
|
|
353 |
{
|
|
354 |
qDebug()<<"Inside FBRequest::parseXMLResponse";
|
|
355 |
QXmlInputSource input;
|
|
356 |
input.setData(aResponseData);
|
|
357 |
|
|
358 |
FBXMLHandler handler;
|
|
359 |
QXmlSimpleReader parser;
|
|
360 |
parser.setContentHandler(&handler);
|
|
361 |
bool result = parser.parse(&input);
|
|
362 |
|
|
363 |
QVariant rootObject = handler.rootObject();
|
|
364 |
|
|
365 |
|
|
366 |
if (handler.parseError() || !result)
|
|
367 |
{
|
|
368 |
aError.setCode( FBRESPONSE_PARSE_ERROR );
|
|
369 |
aError.setDescription("parser was unable to parse the xml response from facebook server.");
|
|
370 |
|
|
371 |
return QVariant();
|
|
372 |
}
|
|
373 |
else if (handler.rootName().compare("error_response")==0)
|
|
374 |
{
|
|
375 |
QVariantHash errorDict = rootObject.toHash();
|
|
376 |
|
|
377 |
bool result;
|
|
378 |
int errorCode = errorDict.value("error_code").toInt(&result);
|
|
379 |
|
|
380 |
aError.setCode( errorCode );
|
|
381 |
aError.setDescription( errorDict.value("error_msg").toString() );
|
|
382 |
|
|
383 |
return rootObject;
|
|
384 |
}
|
|
385 |
else
|
|
386 |
{
|
|
387 |
return rootObject;
|
|
388 |
}
|
|
389 |
|
|
390 |
}
|
|
391 |
|
|
392 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
393 |
// instance provate slots
|
|
394 |
void FBRequest::networkReplyError ( QNetworkReply::NetworkError aCode )
|
|
395 |
{
|
|
396 |
emit requestFailedWithNetworkError(aCode );
|
|
397 |
}
|
|
398 |
|
|
399 |
void FBRequest::networkReplyFinished ()
|
|
400 |
{
|
|
401 |
qDebug()<<"Inside FBRequest::networkReplyFinished";
|
|
402 |
QNetworkReply* reply = static_cast<QNetworkReply*> ( sender() );
|
|
403 |
QByteArray responseData = reply->readAll();
|
|
404 |
|
|
405 |
qDebug() << "Response Data(QByteArray) : " << responseData;
|
|
406 |
QString str(responseData);
|
|
407 |
qDebug() << "Response Data(QString) : " << str;
|
|
408 |
handleResponseData ( responseData );
|
|
409 |
|
|
410 |
}
|