ctkXnatSession.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkXnatSession.h"
  16. #include "ctkXnatAssessor.h"
  17. #include "ctkXnatDataModel.h"
  18. #include "ctkXnatDefaultSchemaTypes.h"
  19. #include "ctkXnatException.h"
  20. #include "ctkXnatExperiment.h"
  21. #include "ctkXnatFile.h"
  22. #include "ctkXnatLoginProfile.h"
  23. #include "ctkXnatObject.h"
  24. #include "ctkXnatProject.h"
  25. #include "ctkXnatReconstruction.h"
  26. #include "ctkXnatResource.h"
  27. #include "ctkXnatScan.h"
  28. #include "ctkXnatSubject.h"
  29. #include <QCryptographicHash>
  30. #include <QDateTime>
  31. #include <QTimer>
  32. #include <QDebug>
  33. #include <QDir>
  34. #include <QScopedPointer>
  35. #include <QStringBuilder>
  36. #include <QNetworkCookie>
  37. #include <ctkXnatAPI_p.h>
  38. #include <qRestResult.h>
  39. //----------------------------------------------------------------------------
  40. static const char* HEADER_AUTHORIZATION = "Authorization";
  41. static const char* HEADER_USER_AGENT = "User-Agent";
  42. static const char* HEADER_COOKIE = "Cookie";
  43. static QString SERVER_VERSION = "version";
  44. static QString SESSION_EXPIRATION_DATE = "expires";
  45. //----------------------------------------------------------------------------
  46. class ctkXnatSessionPrivate
  47. {
  48. public:
  49. const ctkXnatLoginProfile loginProfile;
  50. QScopedPointer<ctkXnatAPI> xnat;
  51. QScopedPointer<ctkXnatDataModel> dataModel;
  52. QString sessionId;
  53. QString defaultDownloadDir;
  54. QMap<QString, QString> sessionProperties;
  55. ctkXnatSession* q;
  56. QTimer* timer;
  57. // The time in milliseconds untill the signal sessionAboutToBeTimedOut gets emitted
  58. int timeToSessionTimeOutWarning;
  59. // The time in milliseconds untill the signal sessionTimedOut gets emitted
  60. int timeToSessionTimedOut = 60000;
  61. ctkXnatSessionPrivate(const ctkXnatLoginProfile& loginProfile, ctkXnatSession* q);
  62. ~ctkXnatSessionPrivate();
  63. void throwXnatException(const QString& msg);
  64. void createConnections();
  65. void setDefaultHttpHeaders();
  66. void checkSession() const;
  67. void setSessionProperties();
  68. QDateTime updateExpirationDate(qRestResult* restResult);
  69. void close();
  70. static QList<ctkXnatObject*> results(qRestResult* restResult, QString schemaType);
  71. };
  72. //----------------------------------------------------------------------------
  73. ctkXnatSessionPrivate::ctkXnatSessionPrivate(const ctkXnatLoginProfile& loginProfile,
  74. ctkXnatSession* q)
  75. : loginProfile(loginProfile)
  76. , xnat(new ctkXnatAPI())
  77. , defaultDownloadDir(".")
  78. , q(q)
  79. , timer(new QTimer(q))
  80. {
  81. // TODO This is a workaround for connecting to sites with self-signed
  82. // certificate. Should be replaced with something more clever.
  83. xnat->setSuppressSslErrors(true);
  84. createConnections();
  85. }
  86. //----------------------------------------------------------------------------
  87. ctkXnatSessionPrivate::~ctkXnatSessionPrivate()
  88. {
  89. }
  90. //----------------------------------------------------------------------------
  91. void ctkXnatSessionPrivate::throwXnatException(const QString& msg)
  92. {
  93. QString errorMsg = msg.trimmed();
  94. if (!errorMsg.isEmpty())
  95. {
  96. errorMsg.append(' ');
  97. }
  98. errorMsg.append(xnat->errorString());
  99. switch (xnat->error())
  100. {
  101. case qRestAPI::TimeoutError:
  102. throw ctkXnatTimeoutException(errorMsg);
  103. case qRestAPI::ResponseParseError:
  104. throw ctkXnatProtocolFailureException(errorMsg);
  105. case qRestAPI::UnknownUuidError:
  106. throw ctkInvalidArgumentException(errorMsg);
  107. case qRestAPI::AuthenticationError:
  108. // This signals either an initial authentication error
  109. // or a session timeout.
  110. this->close();
  111. throw ctkXnatAuthenticationException(errorMsg);
  112. default:
  113. throw ctkRuntimeException(errorMsg);
  114. }
  115. }
  116. //----------------------------------------------------------------------------
  117. void ctkXnatSessionPrivate::createConnections()
  118. {
  119. // Q_D(ctkXnatSession);
  120. // connect(d->xnat, SIGNAL(resultReceived(QUuid,QList<QVariantMap>)),
  121. // this, SLOT(processResult(QUuid,QList<QVariantMap>)));
  122. // connect(d->xnat, SIGNAL(progress(QUuid,double)),
  123. // this, SLOT(progress(QUuid,double)));
  124. }
  125. //----------------------------------------------------------------------------
  126. void ctkXnatSessionPrivate::setDefaultHttpHeaders()
  127. {
  128. ctkXnatAPI::RawHeaders rawHeaders;
  129. rawHeaders[HEADER_USER_AGENT] = "Qt";
  130. /*
  131. rawHeaders["Authorization"] = "Basic " +
  132. QByteArray(QString("%1:%2").arg(d->loginProfile.userName())
  133. .arg(d->loginProfile.password()).toAscii()).toBase64();
  134. */
  135. if (!sessionId.isEmpty())
  136. {
  137. rawHeaders[HEADER_COOKIE] = QString("JSESSIONID=%1").arg(sessionId).toLatin1();
  138. }
  139. xnat->setDefaultRawHeaders(rawHeaders);
  140. }
  141. //----------------------------------------------------------------------------
  142. void ctkXnatSessionPrivate::checkSession() const
  143. {
  144. if (sessionId.isEmpty())
  145. {
  146. throw ctkXnatInvalidSessionException("Session closed.");
  147. }
  148. }
  149. //----------------------------------------------------------------------------
  150. void ctkXnatSessionPrivate::setSessionProperties()
  151. {
  152. sessionProperties.clear();
  153. QUuid uuid = xnat->get("/data/version");
  154. QScopedPointer<qRestResult> restResult(xnat->takeResult(uuid));
  155. if (restResult)
  156. {
  157. QString version = restResult->result()["content"].toString();
  158. if (version.isEmpty())
  159. {
  160. throw ctkXnatProtocolFailureException("No version information available.");
  161. }
  162. sessionProperties[SERVER_VERSION] = version;
  163. }
  164. else
  165. {
  166. this->throwXnatException("Retrieving session properties failed.");
  167. }
  168. }
  169. //----------------------------------------------------------------------------
  170. QDateTime ctkXnatSessionPrivate::updateExpirationDate(qRestResult* restResult)
  171. {
  172. QByteArray cookieHeader = restResult->rawHeader("Set-Cookie");
  173. QDateTime expirationDate = QDateTime::currentDateTime();
  174. if (!cookieHeader.isEmpty())
  175. {
  176. QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(cookieHeader);
  177. foreach(const QNetworkCookie& cookie, cookies)
  178. {
  179. if (cookie.name() == "SESSION_EXPIRATION_TIME")
  180. {
  181. QList<QByteArray> expirationCookie = cookie.value().split(',');
  182. if (expirationCookie.size() == 2)
  183. {
  184. unsigned long long startTime = expirationCookie[0].mid(1).toULongLong();
  185. if (startTime > 0)
  186. {
  187. expirationDate = QDateTime::fromTime_t(startTime / 1000);
  188. }
  189. QByteArray timeSpan = expirationCookie[1];
  190. timeSpan.chop(1);
  191. this->timeToSessionTimeOutWarning = timeSpan.toLong() - this->timeToSessionTimedOut;
  192. expirationDate = expirationDate.addMSecs(timeSpan.toLong());
  193. sessionProperties[SESSION_EXPIRATION_DATE] = expirationDate.toString(Qt::ISODate);
  194. this->timer->start(this->timeToSessionTimeOutWarning);
  195. emit q->sessionRenewed(expirationDate);
  196. }
  197. }
  198. }
  199. }
  200. return expirationDate;
  201. }
  202. //----------------------------------------------------------------------------
  203. void ctkXnatSessionPrivate::close()
  204. {
  205. sessionProperties.clear();
  206. sessionId.clear();
  207. this->setDefaultHttpHeaders();
  208. dataModel.reset();
  209. }
  210. //----------------------------------------------------------------------------
  211. QList<ctkXnatObject*> ctkXnatSessionPrivate::results(qRestResult* restResult, QString schemaType)
  212. {
  213. QList<ctkXnatObject*> results;
  214. foreach (const QVariantMap& propertyMap, restResult->results())
  215. {
  216. QString customSchemaType;
  217. if (propertyMap.contains("xsiType"))
  218. {
  219. customSchemaType = propertyMap["xsiType"].toString();
  220. }
  221. int typeId = 0;
  222. // try to create an object based on the custom schema type first
  223. if (!customSchemaType.isEmpty())
  224. {
  225. typeId = QMetaType::type(qPrintable(customSchemaType));
  226. }
  227. // Fall back. Create the default class according to the default schema type
  228. if (!typeId)
  229. {
  230. if (!customSchemaType.isEmpty())
  231. {
  232. qWarning() << QString("No ctkXnatObject sub-class registered for the schema %1. Falling back to the default class %2.").arg(customSchemaType).arg(schemaType);
  233. }
  234. typeId = QMetaType::type(qPrintable(schemaType));
  235. }
  236. if (!typeId)
  237. {
  238. qWarning() << QString("No ctkXnatObject sub-class registered as a meta-type for the schema %1. Skipping result.").arg(schemaType);
  239. continue;
  240. }
  241. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  242. ctkXnatObject* object = reinterpret_cast<ctkXnatObject*>(QMetaType::construct(typeId));
  243. #else
  244. ctkXnatObject* object = reinterpret_cast<ctkXnatObject*>(QMetaType(typeId).create());
  245. #endif
  246. if (!customSchemaType.isEmpty())
  247. {
  248. // We might have created the default ctkXnatObject sub-class, but can still set
  249. // the custom schema type.
  250. object->setSchemaType(customSchemaType);
  251. }
  252. // Fill in the properties
  253. QMapIterator<QString, QVariant> it(propertyMap);
  254. QString description;
  255. while (it.hasNext())
  256. {
  257. it.next();
  258. QString str = it.key().toLatin1().data();
  259. QVariant var = it.value();
  260. object->setProperty(str, var);
  261. description.append (str + QString ("\t::\t") + var.toString() + "\n");
  262. }
  263. QVariant lastModifiedHeader = restResult->rawHeader("Last-Modified");
  264. QDateTime lastModifiedTime;
  265. if (lastModifiedHeader.isValid())
  266. {
  267. lastModifiedTime = lastModifiedHeader.toDateTime();
  268. }
  269. if (lastModifiedTime.isValid())
  270. {
  271. object->setLastModifiedTime(lastModifiedTime);
  272. }
  273. object->setDescription(description);
  274. results.push_back(object);
  275. }
  276. return results;
  277. }
  278. //----------------------------------------------------------------------------
  279. // ctkXnatSession class
  280. //----------------------------------------------------------------------------
  281. ctkXnatSession::ctkXnatSession(const ctkXnatLoginProfile& loginProfile)
  282. : d_ptr(new ctkXnatSessionPrivate(loginProfile, this))
  283. {
  284. Q_D(ctkXnatSession);
  285. qRegisterMetaType<ctkXnatProject>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_PROJECT));
  286. qRegisterMetaType<ctkXnatSubject>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_SUBJECT));
  287. qRegisterMetaType<ctkXnatExperiment>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_EXPERIMENT));
  288. qRegisterMetaType<ctkXnatScan>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_SCAN));
  289. qRegisterMetaType<ctkXnatReconstruction>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_RECONSTRUCTION));
  290. qRegisterMetaType<ctkXnatResource>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_RESOURCE));
  291. qRegisterMetaType<ctkXnatAssessor>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_ASSESSOR));
  292. qRegisterMetaType<ctkXnatFile>(qPrintable(ctkXnatDefaultSchemaTypes::XSI_FILE));
  293. QString url = d->loginProfile.serverUrl().toString();
  294. d->xnat->setServerUrl(url);
  295. // QObject::connect(d->xnat.data(), SIGNAL(uploadFinished()), this, SIGNAL(uploadFinished()));
  296. QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
  297. this, SIGNAL(progress(QUuid,double)));
  298. // QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
  299. // this, SLOT(onProgress(QUuid,double)));
  300. d->setDefaultHttpHeaders();
  301. }
  302. //----------------------------------------------------------------------------
  303. ctkXnatSession::~ctkXnatSession()
  304. {
  305. this->close();
  306. }
  307. //----------------------------------------------------------------------------
  308. void ctkXnatSession::open()
  309. {
  310. Q_D(ctkXnatSession);
  311. if (this->isOpen()) return;
  312. qRestAPI::RawHeaders headers;
  313. headers[HEADER_AUTHORIZATION] = "Basic " +
  314. QByteArray(QString("%1:%2").arg(this->userName())
  315. .arg(this->password()).toLatin1()).toBase64();
  316. QUuid uuid = d->xnat->get("/data/JSESSION", qRestAPI::Parameters(), headers);
  317. QScopedPointer<qRestResult> restResult(d->xnat->takeResult(uuid));
  318. if (restResult)
  319. {
  320. QObject::connect(d->timer, SIGNAL(timeout()), this, SLOT(emitSessionTimeOut()));
  321. QString sessionId = restResult->result()["content"].toString();
  322. d->sessionId = sessionId;
  323. d->setDefaultHttpHeaders();
  324. d->setSessionProperties();
  325. d->updateExpirationDate(restResult.data());
  326. }
  327. else
  328. {
  329. d->throwXnatException("Could not get a session id.");
  330. }
  331. d->dataModel.reset(new ctkXnatDataModel(this));
  332. d->dataModel->setProperty(ctkXnatObject::LABEL, this->url().toString());
  333. emit sessionOpened();
  334. }
  335. //----------------------------------------------------------------------------
  336. void ctkXnatSession::close()
  337. {
  338. Q_D(ctkXnatSession);
  339. if (!this->isOpen()) return;
  340. emit sessionAboutToBeClosed();
  341. d->close();
  342. }
  343. //----------------------------------------------------------------------------
  344. bool ctkXnatSession::isOpen() const
  345. {
  346. Q_D(const ctkXnatSession);
  347. return !d->sessionId.isEmpty();
  348. }
  349. //----------------------------------------------------------------------------
  350. QString ctkXnatSession::version() const
  351. {
  352. Q_D(const ctkXnatSession);
  353. if (d->sessionProperties.contains(SERVER_VERSION))
  354. {
  355. return d->sessionProperties[SERVER_VERSION];
  356. }
  357. else
  358. {
  359. return QString::null;
  360. }
  361. }
  362. //----------------------------------------------------------------------------
  363. QDateTime ctkXnatSession::expirationDate() const
  364. {
  365. Q_D(const ctkXnatSession);
  366. d->checkSession();
  367. return QDateTime::fromString(d->sessionProperties[SESSION_EXPIRATION_DATE], Qt::ISODate);
  368. }
  369. //----------------------------------------------------------------------------
  370. QDateTime ctkXnatSession::renew()
  371. {
  372. Q_D(ctkXnatSession);
  373. d->checkSession();
  374. QUuid uuid = d->xnat->get("/data/auth");
  375. QScopedPointer<qRestResult> restResult(d->xnat->takeResult(uuid));
  376. if (!restResult)
  377. {
  378. d->throwXnatException("Session renewal failed.");
  379. }
  380. return d->updateExpirationDate(restResult.data());
  381. }
  382. //----------------------------------------------------------------------------
  383. ctkXnatLoginProfile ctkXnatSession::loginProfile() const
  384. {
  385. Q_D(const ctkXnatSession);
  386. return d->loginProfile;
  387. }
  388. //----------------------------------------------------------------------------
  389. void ctkXnatSession::onProgress(QUuid /*queryId*/, double /*progress*/)
  390. {
  391. // qDebug() << "ctkXnatSession::progress(QUuid queryId, double progress)";
  392. // qDebug() << "query id:" << queryId;
  393. // qDebug() << "progress:" << (progress * 100.0) << "%";
  394. }
  395. //----------------------------------------------------------------------------
  396. QUrl ctkXnatSession::url() const
  397. {
  398. Q_D(const ctkXnatSession);
  399. return d->loginProfile.serverUrl();
  400. }
  401. //----------------------------------------------------------------------------
  402. QString ctkXnatSession::userName() const
  403. {
  404. Q_D(const ctkXnatSession);
  405. return d->loginProfile.userName();
  406. }
  407. //----------------------------------------------------------------------------
  408. QString ctkXnatSession::password() const
  409. {
  410. Q_D(const ctkXnatSession);
  411. return d->loginProfile.password();
  412. }
  413. //----------------------------------------------------------------------------
  414. QString ctkXnatSession::sessionId() const
  415. {
  416. Q_D(const ctkXnatSession);
  417. return d->sessionId;
  418. }
  419. //----------------------------------------------------------------------------
  420. void ctkXnatSession::setDefaultDownloadDir(const QString &path)
  421. {
  422. Q_D(ctkXnatSession);
  423. QDir directory(path);
  424. if (directory.exists())
  425. {
  426. d->defaultDownloadDir = path;
  427. }
  428. else
  429. {
  430. d->defaultDownloadDir = QDir::currentPath();
  431. qWarning() << "Specified directory: ["<<path<<"] does not exists! Setting default filepath to :"<<d->defaultDownloadDir;
  432. }
  433. }
  434. //----------------------------------------------------------------------------
  435. QString ctkXnatSession::defaultDownloadDir() const
  436. {
  437. Q_D(const ctkXnatSession);
  438. return d->defaultDownloadDir;
  439. }
  440. //----------------------------------------------------------------------------
  441. ctkXnatDataModel* ctkXnatSession::dataModel() const
  442. {
  443. Q_D(const ctkXnatSession);
  444. d->checkSession();
  445. return d->dataModel.data();
  446. }
  447. //----------------------------------------------------------------------------
  448. QUuid ctkXnatSession::httpGet(const QString& resource, const ctkXnatSession::UrlParameters& parameters, const ctkXnatSession::HttpRawHeaders& rawHeaders)
  449. {
  450. Q_D(ctkXnatSession);
  451. d->checkSession();
  452. d->timer->start(d->timeToSessionTimeOutWarning);
  453. return d->xnat->get(resource, parameters, rawHeaders);
  454. }
  455. //----------------------------------------------------------------------------
  456. QList<ctkXnatObject*> ctkXnatSession::httpResults(const QUuid& uuid, const QString& schemaType)
  457. {
  458. Q_D(ctkXnatSession);
  459. d->checkSession();
  460. QScopedPointer<qRestResult> restResult(d->xnat->takeResult(uuid));
  461. if (restResult == NULL)
  462. {
  463. d->throwXnatException("Http request failed.");
  464. }
  465. d->timer->start(d->timeToSessionTimeOutWarning);
  466. return d->results(restResult.data(), schemaType);
  467. }
  468. QUuid ctkXnatSession::httpPut(const QString& resource, const ctkXnatSession::UrlParameters& parameters,
  469. const ctkXnatSession::HttpRawHeaders& /*rawHeaders*/)
  470. {
  471. Q_D(ctkXnatSession);
  472. d->checkSession();
  473. return d->xnat->put(resource, parameters);
  474. }
  475. //----------------------------------------------------------------------------
  476. QList<QVariantMap> ctkXnatSession::httpSync(const QUuid& uuid)
  477. {
  478. Q_D(ctkXnatSession);
  479. d->checkSession();
  480. QList<QVariantMap> result;
  481. qRestResult* restResult = d->xnat->takeResult(uuid);
  482. if (restResult == NULL)
  483. {
  484. d->throwXnatException("Syncing with http request failed.");
  485. }
  486. else
  487. {
  488. d->updateExpirationDate(restResult);
  489. result = restResult->results();
  490. }
  491. return result;
  492. }
  493. //----------------------------------------------------------------------------
  494. const QMap<QByteArray, QByteArray> ctkXnatSession::httpHeadSync(const QUuid &uuid)
  495. {
  496. Q_D(ctkXnatSession);
  497. QScopedPointer<qRestResult> result (d->xnat->takeResult(uuid));
  498. d->timer->start(d->timeToSessionTimeOutWarning);
  499. if (result == NULL)
  500. {
  501. d->throwXnatException("Sending HEAD request failed.");
  502. }
  503. return result->rawHeaders();
  504. }
  505. //----------------------------------------------------------------------------
  506. QUuid ctkXnatSession::httpHead(const QString& resourceUri)
  507. {
  508. Q_D(ctkXnatSession);
  509. QUuid queryId = d->xnat->head(resourceUri);
  510. d->timer->start(d->timeToSessionTimeOutWarning);
  511. return queryId;
  512. }
  513. //----------------------------------------------------------------------------
  514. bool ctkXnatSession::exists(const ctkXnatObject* object)
  515. {
  516. Q_D(ctkXnatSession);
  517. QString query = object->resourceUri();
  518. bool success = d->xnat->sync(d->xnat->get(query));
  519. return success;
  520. }
  521. //----------------------------------------------------------------------------
  522. void ctkXnatSession::remove(ctkXnatObject* object)
  523. {
  524. Q_D(ctkXnatSession);
  525. QString query = object->resourceUri();
  526. bool success = d->xnat->sync(d->xnat->del(query));
  527. d->timer->start(d->timeToSessionTimeOutWarning);
  528. if (!success)
  529. {
  530. d->throwXnatException("Error occurred while removing the data.");
  531. }
  532. }
  533. //----------------------------------------------------------------------------
  534. void ctkXnatSession::download(const QString& fileName,
  535. const QString& resource,
  536. const UrlParameters& parameters,
  537. const HttpRawHeaders& rawHeaders)
  538. {
  539. Q_D(ctkXnatSession);
  540. QUuid queryId = d->xnat->download(fileName, resource, parameters, rawHeaders);
  541. d->xnat->sync(queryId);
  542. d->timer->start(d->timeToSessionTimeOutWarning);
  543. }
  544. //----------------------------------------------------------------------------
  545. void ctkXnatSession::upload(ctkXnatFile *xnatFile,
  546. const UrlParameters &parameters,
  547. const HttpRawHeaders &/*rawHeaders*/)
  548. {
  549. Q_D(ctkXnatSession);
  550. QFile file(xnatFile->localFilePath());
  551. if (!file.exists())
  552. {
  553. QString msg = "Error uploading file! ";
  554. msg.append(QString("File \"%1\" does not exist!").arg(xnatFile->localFilePath()));
  555. throw ctkXnatException(msg);
  556. }
  557. QUuid queryId = d->xnat->upload(xnatFile->localFilePath(), xnatFile->resourceUri(), parameters);
  558. d->xnat->sync(queryId);
  559. // Validating the file upload by requesting the catalog XML
  560. // of the parent resource. Unfortunately for XNAT versions <= 1.6.4
  561. // this is the only way to get the file's MD5 hash form the server.
  562. QString md5Query = xnatFile->parent()->resourceUri();
  563. QUuid md5QueryID = this->httpGet(md5Query);
  564. QList<QVariantMap> result = this->httpSync(md5QueryID);
  565. QString md5ChecksumRemote ("0");
  566. // Newly added files are usually at the end of the catalog
  567. // and hence at the end of the result list.
  568. // So iterating backward is for performance reasons.
  569. QList<QVariantMap>::const_iterator it = result.constEnd()-1;
  570. while (it != result.constBegin()-1)
  571. {
  572. QVariantMap::const_iterator it2 = (*it).find(xnatFile->name());
  573. if (it2 != (*it).constEnd())
  574. {
  575. md5ChecksumRemote = it2.value().toString();
  576. break;
  577. }
  578. --it;
  579. }
  580. QFile localFile(xnatFile->localFilePath());
  581. if (localFile.open(QFile::ReadOnly) && md5ChecksumRemote != "0")
  582. {
  583. QCryptographicHash hash(QCryptographicHash::Md5);
  584. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  585. hash.addData(&localFile);
  586. #else
  587. hash.addData(localFile.readAll());
  588. #endif
  589. QString md5ChecksumLocal(hash.result().toHex());
  590. // Retrieving the md5 checksum on the server and comparing
  591. // it with the local file md5 sum
  592. if (md5ChecksumLocal != md5ChecksumRemote)
  593. {
  594. // Remove corrupted file from server
  595. xnatFile->erase();
  596. throw ctkXnatException("Upload failed! An error occurred during file upload.");
  597. }
  598. }
  599. else
  600. {
  601. qWarning()<<"Could not validate file upload! Remote MD5: "<<md5ChecksumRemote;
  602. }
  603. }
  604. //----------------------------------------------------------------------------
  605. void ctkXnatSession::processResult(QUuid queryId, QList<QVariantMap> parameters)
  606. {
  607. Q_UNUSED(queryId)
  608. Q_UNUSED(parameters)
  609. }
  610. //----------------------------------------------------------------------------
  611. void ctkXnatSession::emitSessionTimeOut()
  612. {
  613. Q_D(ctkXnatSession);
  614. if (d->timer->interval() == d->timeToSessionTimeOutWarning)
  615. {
  616. d->timer->start(d->timeToSessionTimedOut);
  617. emit sessionAboutToBeTimedOut();
  618. }
  619. else if (d->timer->interval() == d->timeToSessionTimedOut)
  620. {
  621. d->timer->stop();
  622. emit sessionTimedOut();
  623. }
  624. }