|
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 #include "requestSP.h" |
|
19 #include "sessionSP.h" |
|
20 #include "xmlParser.h" |
|
21 #include "errorCodes.h" |
|
22 |
|
23 #include <QNetworkRequest> |
|
24 #include <QXmlSimpleReader> |
|
25 #include <QXmlInputSource> |
|
26 #include <QCryptographicHash> |
|
27 #include <QtAlgorithms> |
|
28 #include <QDebug> |
|
29 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
30 // global |
|
31 |
|
32 static const QString kAPIVersion = "1.0"; |
|
33 static const QString kAPIFormat = "XML"; |
|
34 static const char kUserAgent[] = "FacebookConnect"; |
|
35 static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; |
|
36 |
|
37 static const double kTimeoutInterval = 180.0; |
|
38 |
|
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 emit requestLoading(); |
|
74 |
|
75 QString url ; |
|
76 if (iMethod.length()) |
|
77 url = iUrl; |
|
78 else |
|
79 url = generateGetURL(); |
|
80 |
|
81 QNetworkRequest request; |
|
82 request.setUrl(QUrl(url)); |
|
83 request.setRawHeader("User-Agent", kUserAgent); |
|
84 |
|
85 /* from the Qt docs on QNetworkAccessManager: |
|
86 QNetworkAccessManager by default does not have a set cache. |
|
87 Qt provides a simple disk cache, QNetworkDiskCache, which can be used. |
|
88 |
|
89 However we will not use it.*/ |
|
90 |
|
91 request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork); |
|
92 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false); |
|
93 |
|
94 iTimestamp = QDateTime::currentDateTime(); |
|
95 |
|
96 //if (iMethod.length()) |
|
97 //{ |
|
98 const QString contentType = "multipart/form-data; boundary=" + kStringBoundary; |
|
99 request.setRawHeader("Content-Type", contentType.toUtf8()); |
|
100 |
|
101 /* connect all signals from iNetWorkAccessManager to this */ |
|
102 QByteArray postBody ; |
|
103 generatePostBody (postBody); |
|
104 |
|
105 pbar = new progressbar; |
|
106 pbar->show(); |
|
107 |
|
108 QNetworkReply* reply = iNetworkAccessManager.post(request, postBody); |
|
109 |
|
110 QObject::connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); |
|
111 QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
|
112 this, SLOT(networkReplyError(QNetworkReply::NetworkError))); |
|
113 |
|
114 //} |
|
115 } |
|
116 |
|
117 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
118 // instance private functions |
|
119 QString FBRequest::md5(const QString& aData) |
|
120 { |
|
121 QByteArray byteArray; |
|
122 byteArray.insert(0, aData.toUtf8()); |
|
123 |
|
124 QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex(); |
|
125 QString returnString ( md5Hash ); |
|
126 |
|
127 return returnString; |
|
128 } |
|
129 |
|
130 bool FBRequest::isSpecialMethod() const { |
|
131 return ( iMethod.compare("facebook.auth.getSession", Qt::CaseInsensitive) == 0 |
|
132 || iMethod.compare("facebook.auth.createToken", Qt::CaseInsensitive) == 0 ); |
|
133 } |
|
134 |
|
135 QString FBRequest::urlForMethod (const QString& aMethod) const { |
|
136 |
|
137 return iSession->apiURL(); |
|
138 } |
|
139 |
|
140 QString FBRequest::generateGetURL() const |
|
141 { |
|
142 const QUrl url(iUrl); |
|
143 const QString queryPrefix = url.hasQuery() ? "&" : "?"; |
|
144 |
|
145 QStringList pairs; |
|
146 DictionaryIterator i(iParams); |
|
147 |
|
148 while (i.hasNext()) { |
|
149 i.next(); |
|
150 pairs << i.key().toUtf8() + "=" + i.value().toUtf8(); |
|
151 } |
|
152 |
|
153 return iUrl + queryPrefix + pairs.join("&"); |
|
154 } |
|
155 |
|
156 QString FBRequest::generateCallId() const { |
|
157 QDateTime dateTime = QDateTime::currentDateTime(); |
|
158 uint secs = dateTime.toTime_t(); |
|
159 QString result = QString::number(secs, 10); |
|
160 return result; |
|
161 } |
|
162 |
|
163 QString FBRequest::generateSig() |
|
164 { |
|
165 QString joined; |
|
166 QStringList keys = iParams.keys(); |
|
167 |
|
168 qSort(keys.begin(), keys.end(), caseInsensitiveLessThan); |
|
169 |
|
170 QListIterator<QString> i(keys); |
|
171 while (i.hasNext()) |
|
172 { |
|
173 const QString key = i.next(); |
|
174 joined.append(key.toUtf8()); |
|
175 joined.append("="); |
|
176 joined.append(iParams.value(key).toUtf8()); |
|
177 } |
|
178 |
|
179 if (isSpecialMethod()) |
|
180 { |
|
181 if (iSession->apiSecret().length()) |
|
182 { |
|
183 joined.append(iSession->apiSecret()); |
|
184 } |
|
185 } |
|
186 else if (iSession->sessionSecret().length()) |
|
187 { |
|
188 joined.append(iSession->sessionSecret()); |
|
189 } |
|
190 else if (iSession->apiSecret().length()) |
|
191 { |
|
192 joined.append(iSession->apiSecret()); |
|
193 } |
|
194 |
|
195 return md5(joined); |
|
196 } |
|
197 |
|
198 void FBRequest::generatePostBody( QByteArray& body ) |
|
199 { |
|
200 QString endLine = "\r\n--" + kStringBoundary + "\r\n"; |
|
201 body.append( "--" + kStringBoundary.toUtf8() + "\r\n" ) ; |
|
202 |
|
203 |
|
204 DictionaryIterator i (iParams); |
|
205 |
|
206 while (i.hasNext()) |
|
207 { |
|
208 i.next(); |
|
209 |
|
210 body.append("Content-Disposition: form-data; name=\"" + i.key().toUtf8() + "\"\r\n\r\n" ); |
|
211 body.append(i.value().toUtf8()); |
|
212 body.append(endLine.toUtf8()); |
|
213 } |
|
214 |
|
215 |
|
216 if (iDataParam.size()) |
|
217 { |
|
218 if (iDataParamPicture) |
|
219 { |
|
220 body.append("Content-Disposition: form-data; filename=\"photo\"\r\n" ); |
|
221 body.append("Content-Type: image/png\r\n\r\n" ); |
|
222 } |
|
223 else |
|
224 { |
|
225 body.append("Content-Disposition: form-data; filename=\"data\"\r\n"); |
|
226 body.append("Content-Type: content/unknown\r\n\r\n"); |
|
227 } |
|
228 |
|
229 body.append(iDataParam); |
|
230 body.append(endLine.toUtf8()); |
|
231 |
|
232 } |
|
233 } |
|
234 |
|
235 void FBRequest::handleResponseData( const QByteArray& aResponseData ) |
|
236 { |
|
237 FBError error; |
|
238 QVariant result = parseXMLResponse( aResponseData, error); |
|
239 if (error.code() != 0) |
|
240 { |
|
241 emit requestFailedWithFacebookError(error); |
|
242 } |
|
243 else |
|
244 { |
|
245 emit requestDidLoad(result); |
|
246 } |
|
247 |
|
248 delete pbar; |
|
249 } |
|
250 |
|
251 void FBRequest::post( const QString& aUrl, const Dictionary& aParams) |
|
252 { |
|
253 iUrl = aUrl; |
|
254 iParams = aParams; |
|
255 |
|
256 iSession->send(this); |
|
257 } |
|
258 |
|
259 void FBRequest::cancel() |
|
260 { |
|
261 |
|
262 } |
|
263 |
|
264 |
|
265 void FBRequest::call (const QString& aMethod, const Dictionary& aParams) |
|
266 { |
|
267 QByteArray dataParams; |
|
268 callWithDataParams(aMethod, aParams, dataParams, false); |
|
269 |
|
270 } |
|
271 |
|
272 void FBRequest::callWithDataParams (const QString& aMethod, const Dictionary& aParams, const QByteArray& aDataParam, bool aDataParamPicture) |
|
273 { |
|
274 iUrl = urlForMethod(aMethod); |
|
275 iMethod = aMethod; |
|
276 iParams = aParams; |
|
277 iDataParam = aDataParam; |
|
278 iDataParamPicture = aDataParamPicture; |
|
279 |
|
280 iParams["method"] = iMethod; |
|
281 iParams["api_key"] = iSession->apiKey(); |
|
282 iParams["v"] = kAPIVersion; |
|
283 iParams["format"] = kAPIFormat; |
|
284 |
|
285 if (!isSpecialMethod()) |
|
286 { |
|
287 iParams["session_key"] = iSession->sessionKey(); |
|
288 iParams["call_id"] = generateCallId(); |
|
289 |
|
290 if (iSession->sessionSecret().length()) |
|
291 { |
|
292 iParams["ss"] = "1"; |
|
293 } |
|
294 } |
|
295 |
|
296 // XXX: workaround what seems to be a Qt bug with the extras-devel libraries. |
|
297 QString signature = generateSig(); |
|
298 iParams["sig"] = signature; |
|
299 // XXX: end workaround. |
|
300 iSession->send(this); |
|
301 } |
|
302 |
|
303 |
|
304 QVariant FBRequest::parseXMLResponse ( const QByteArray& aResponseData, FBError& aError) |
|
305 { |
|
306 QXmlInputSource input; |
|
307 input.setData(aResponseData); |
|
308 |
|
309 FBXMLHandler handler; |
|
310 QXmlSimpleReader parser; |
|
311 parser.setContentHandler(&handler); |
|
312 bool result = parser.parse(&input); |
|
313 |
|
314 QVariant rootObject = handler.rootObject(); |
|
315 |
|
316 if (handler.parseError() || !result) |
|
317 { |
|
318 aError.setCode( FBRESPONSE_PARSE_ERROR ); |
|
319 aError.setDescription("parser was unable to parse the xml response from facebook server."); |
|
320 |
|
321 return QVariant(); |
|
322 } |
|
323 else if (handler.rootName().compare("error_response")==0) |
|
324 { |
|
325 QVariantHash errorDict = rootObject.toHash(); |
|
326 |
|
327 bool result; |
|
328 int errorCode = errorDict.value("error_code").toInt(&result); |
|
329 |
|
330 aError.setCode( errorCode ); |
|
331 aError.setDescription( errorDict.value("error_msg").toString() ); |
|
332 |
|
333 return rootObject; |
|
334 } |
|
335 else |
|
336 { |
|
337 return rootObject; |
|
338 } |
|
339 |
|
340 } |
|
341 |
|
342 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
343 // instance provate slots |
|
344 void FBRequest::networkReplyError ( QNetworkReply::NetworkError aCode ) |
|
345 { |
|
346 emit requestFailedWithNetworkError(aCode ); |
|
347 } |
|
348 |
|
349 void FBRequest::networkReplyFinished () |
|
350 { |
|
351 |
|
352 QNetworkReply* reply = static_cast<QNetworkReply*> ( sender() ); |
|
353 QByteArray responseData = reply->readAll(); |
|
354 |
|
355 handleResponseData ( responseData ); |
|
356 |
|
357 } |