ctkXnatSession.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #ifndef CTKXNATSESSION_H
  16. #define CTKXNATSESSION_H
  17. #include "ctkXNATCoreExport.h"
  18. #include <QScopedPointer>
  19. #include <QString>
  20. #include <QObject>
  21. #include <QVariantMap>
  22. #include <QUuid>
  23. class QDateTime;
  24. class QNetworkProxy;
  25. class ctkXnatSessionPrivate;
  26. class ctkXnatFile;
  27. class ctkXnatLoginProfile;
  28. class ctkXnatDataModel;
  29. class ctkXnatObject;
  30. class ctkXnatResource;
  31. /**
  32. * @ingroup XNAT_Core
  33. *
  34. * @brief The ctkXnatSession class reprents a session object associated
  35. * with a specific XNAT connection.
  36. */
  37. class CTK_XNAT_CORE_EXPORT ctkXnatSession : public QObject
  38. {
  39. Q_OBJECT
  40. public:
  41. typedef QMap<QString, QString> UrlParameters;
  42. typedef QMap<QByteArray, QByteArray> HttpRawHeaders;
  43. ctkXnatSession(const ctkXnatLoginProfile& loginProfile);
  44. ~ctkXnatSession();
  45. /**
  46. * @brief Open a new XNAT session.
  47. *
  48. * This method must be called on all ctkXnatSession objects before
  49. * any of the methods which communicate with an XNAT server are called.
  50. *
  51. * If the session has already been opened, this method does nothing.
  52. *
  53. * @throws ctkXnatAuthenticationException if the user credentials are invalid.
  54. * @throws ctkXnatException (or one of its subclasses) if a network error occurred.
  55. */
  56. void open();
  57. /**
  58. * @brief Closes this XNAT session.
  59. */
  60. void close();
  61. /**
  62. * @brief Returns the open state of this XNAT session.
  63. * @return \c true if the session is open, \c false otherwise.
  64. */
  65. bool isOpen() const;
  66. /**
  67. * @brief Get the XNAT server version.
  68. * @return The XNAT version running on the remote server. Returns a null string
  69. * if the session is not open.
  70. */
  71. QString version() const;
  72. /**
  73. * @brief Get the expiration date for this XNAT session.
  74. *
  75. * @sa renew()
  76. * @throws ctkXnatInvalidSessionException if the session is closed.
  77. * @return The session expiration date.
  78. */
  79. QDateTime expirationDate() const;
  80. /**
  81. * @brief Re-new the XNAT session.
  82. * @throws ctkXnatInvalidSessionException if the session is closed.
  83. * @return The new session expiration data.
  84. */
  85. QDateTime renew();
  86. /**
  87. * @brief Get the current login profile for this session object.
  88. * @return A copy of the currently used login profile.
  89. */
  90. ctkXnatLoginProfile loginProfile() const;
  91. /**
  92. * @brief Get XNAT server url.
  93. *
  94. * The url is the one specified by the login profile.
  95. *
  96. * @return The XNAT server url.
  97. */
  98. QUrl url() const;
  99. /**
  100. * @brief Get the user name for this XNAT session.
  101. *
  102. * The user name is the one specified by the login profile.
  103. *
  104. * @return The XNAT session user name.
  105. */
  106. QString userName() const;
  107. /**
  108. * @brief Get the password for this XNAT session.
  109. *
  110. * The password is the one specified by the login profile.
  111. *
  112. * @return The XNAT session password.
  113. */
  114. QString password() const;
  115. /**
  116. * @brief Get the sessionId for this XNAT session.
  117. *
  118. * @return The XNAT sessionId.
  119. */
  120. QString sessionId() const;
  121. /**
  122. * @brief Sets the default location where files will be saved after being downloaded
  123. *
  124. * Sets the default directory into which file downloads will be saved.
  125. * By default this value is empty and files will be stored into the current
  126. * working directory.
  127. * If the path does not exists a warning will be printed and the path will be
  128. * set to the current working directory.
  129. *
  130. * @param path the path to the download location
  131. */
  132. void setDefaultDownloadDir(const QString& path);
  133. /**
  134. * @brief Sets a network proxy that will be used to connect with XNAT
  135. *
  136. * Tells the qRestAPI to use a network proxy for the connection to XNAT
  137. *
  138. * @param proxy the network proxy that will be set
  139. */
  140. void setHttpNetworkProxy(const QNetworkProxy& proxy);
  141. /**
  142. * @brief returns the default download location
  143. * @return the default download directory as string
  144. */
  145. QString defaultDownloadDir() const;
  146. ctkXnatDataModel* dataModel() const;
  147. /**
  148. * @brief TODO
  149. * @param resource
  150. * @param parameters
  151. * @param rawHeaders
  152. *
  153. * @throws ctkXnatInvalidSessionException if the session is closed.
  154. * @return
  155. */
  156. QUuid httpGet(const QString& resource,
  157. const UrlParameters& parameters = UrlParameters(),
  158. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  159. /**
  160. * @brief TODO
  161. * @param uuid
  162. * @param schemaType
  163. *
  164. * @throws ctkXnatInvalidSessionException if the session is closed.
  165. * @return
  166. */
  167. QList<ctkXnatObject*> httpResults(const QUuid& uuid, const QString& schemaType);
  168. /**
  169. * @brief TODO
  170. * @param uuid
  171. * @param parameters
  172. *
  173. * @throws ctkXnatInvalidSessionException if the session is closed.
  174. * @return
  175. */
  176. QUuid httpPut(const QString& resource,
  177. const UrlParameters& parameters = UrlParameters(),
  178. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  179. /**
  180. * @brief TODO
  181. * @param uuid
  182. *
  183. * @throws ctkXnatInvalidSessionException if the session is closed.
  184. * @return
  185. */
  186. QList<QVariantMap> httpSync(const QUuid& uuid);
  187. /**
  188. * @brief Reads the result of a head request
  189. * @param uuid the uid of the related query
  190. *
  191. * @throws ctkXnatInvalidSessionException if the session is closed.
  192. * @return a QMap containing the retrieved header information
  193. */
  194. const QMap<QByteArray, QByteArray> httpHeadSync(const QUuid& uuid);
  195. bool exists(const ctkXnatObject* object);
  196. void remove(ctkXnatObject* object);
  197. /// Downloads a file from the web service.
  198. /// \a fileName is the name of the output file.
  199. /// The \a resource and \parameters are used to compose the URL.
  200. /// \a rawHeaders can be used to set the raw headers of the request to send.
  201. /// These headers will be set additionally to those defined by the
  202. /// \a defaultRawHeaders property.
  203. void download(const QString& fileName,
  204. const QString& resource,
  205. const UrlParameters& parameters = UrlParameters(),
  206. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  207. /// Uploads a file to the server.
  208. /// \a fileName is the name of the file.
  209. /// The \a resource and \parameters are used to compose the URL.
  210. /// \a rawHeaders can be used to set the raw headers of the request to send.
  211. /// These headers will be set additionally to those defined by the
  212. /// \a defaultRawHeaders property.
  213. void upload(ctkXnatFile *xnatFile,
  214. const UrlParameters& parameters = UrlParameters(),
  215. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  216. /**
  217. * @brief Sends a http HEAD request to the xnat instance
  218. * @param resourceUri the URL to the server
  219. * @return the query uid
  220. */
  221. QUuid httpHead(const QString& resourceUri);
  222. /**
  223. * @brief Signals that the session was re-newed.
  224. * @param expirationDate The new session expiration date.
  225. */
  226. Q_SIGNAL void sessionRenewed(const QDateTime& expirationDate);
  227. /**
  228. * @brief Signals that the session was just opened.
  229. */
  230. Q_SIGNAL void sessionOpened();
  231. /**
  232. * @brief Signals that the session is about to be closed.
  233. */
  234. Q_SIGNAL void sessionAboutToBeClosed();
  235. // Q_SIGNAL void uploadFinished();
  236. Q_SIGNAL void progress(QUuid, double);
  237. /**
  238. * @brief Signals that the session has timed out.
  239. */
  240. Q_SIGNAL void timedOut();
  241. /**
  242. * @brief Signals that the session will time out in one minute.
  243. */
  244. Q_SIGNAL void aboutToTimeOut();
  245. public slots:
  246. void processResult(QUuid queryId, QList<QVariantMap> parameters);
  247. void onProgress(QUuid queryId, double onProgress);
  248. protected:
  249. QScopedPointer<ctkXnatSessionPrivate> d_ptr;
  250. private:
  251. Q_DECLARE_PRIVATE(ctkXnatSession)
  252. Q_DISABLE_COPY(ctkXnatSession)
  253. Q_SLOT void emitTimeOut();
  254. };
  255. #endif