ctkXnatSession.h 8.0 KB

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