ctkXnatSession.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 Sets the default location where files will be save after being downloaded
  116. *
  117. * Sets the default directory into which file downloads will be saved.
  118. * By default this value is empty and files will be stored into the directory
  119. * of the executable.
  120. * If the path does not exists a warning will be printed and the path will be
  121. * set to the current working directory
  122. *
  123. * @param path the path to the download location
  124. */
  125. void setDefaultFilePath(const QString& path);
  126. /**
  127. * @brief returns the default download location
  128. * @return the default download directory as string
  129. */
  130. QString defaultFilePath() const;
  131. ctkXnatDataModel* dataModel() const;
  132. /**
  133. * @brief TODO
  134. * @param resource
  135. * @param parameters
  136. * @param rawHeaders
  137. *
  138. * @throws ctkXnatInvalidSessionException if the session is closed.
  139. * @return
  140. */
  141. QUuid httpGet(const QString& resource,
  142. const UrlParameters& parameters = UrlParameters(),
  143. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  144. /**
  145. * @brief TODO
  146. * @param uuid
  147. * @param schemaType
  148. *
  149. * @throws ctkXnatInvalidSessionException if the session is closed.
  150. * @return
  151. */
  152. QList<ctkXnatObject*> httpResults(const QUuid& uuid, const QString& schemaType);
  153. /**
  154. * @brief TODO
  155. * @param uuid
  156. * @param parameters
  157. *
  158. * @throws ctkXnatInvalidSessionException if the session is closed.
  159. * @return
  160. */
  161. QUuid httpPut(const QString& resource,
  162. const UrlParameters& parameters = UrlParameters(),
  163. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  164. /**
  165. * @brief TODO
  166. * @param uuid
  167. *
  168. * @throws ctkXnatInvalidSessionException if the session is closed.
  169. * @return
  170. */
  171. QList<QVariantMap> httpSync(const QUuid& uuid);
  172. /**
  173. * @brief Reads the result of a head request
  174. * @param uuid the uid of the related query
  175. *
  176. * @throws ctkXnatInvalidSessionException if the session is closed.
  177. * @return a QMap containing the retrieved header information
  178. */
  179. const QMap<QByteArray, QByteArray> httpHeadSync(const QUuid& uuid);
  180. bool exists(const ctkXnatObject* object);
  181. void remove(ctkXnatObject* object);
  182. /// Downloads a file from the web service.
  183. /// \a fileName is the name of the output file.
  184. /// The \a resource and \parameters are used to compose the URL.
  185. /// \a rawHeaders can be used to set the raw headers of the request to send.
  186. /// These headers will be set additionally to those defined by the
  187. /// \a defaultRawHeaders property.
  188. void download(const QString& fileName,
  189. const QString& resource,
  190. const UrlParameters& parameters = UrlParameters(),
  191. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  192. /// Uploads a file to the web service.
  193. /// \a fileName is the name of the file.
  194. /// The \a resource and \parameters are used to compose the URL.
  195. /// \a rawHeaders can be used to set the raw headers of the request to send.
  196. /// These headers will be set additionally to those defined by the
  197. /// \a defaultRawHeaders property.
  198. void upload(const QString& fileName,
  199. const QString& resource,
  200. const UrlParameters& parameters = UrlParameters(),
  201. const HttpRawHeaders& rawHeaders = HttpRawHeaders());
  202. /**
  203. * @brief Sends a http HEAD request to the xnat instance
  204. * @param resourceUri the URL to the server
  205. * @return the query uid
  206. */
  207. QUuid httpHead(const QString& resourceUri);
  208. /**
  209. * @brief Signals that the session was re-newed.
  210. * @param expirationDate The new session expiration date.
  211. */
  212. Q_SIGNAL void sessionRenewed(const QDateTime& expirationDate);
  213. /**
  214. * @brief Signals that the session was just opened.
  215. */
  216. Q_SIGNAL void sessionOpened();
  217. /**
  218. * @brief Signals that the session is about to be closed.
  219. */
  220. Q_SIGNAL void sessionAboutToBeClosed();
  221. Q_SIGNAL void uploadFinished();
  222. Q_SIGNAL void progress(QUuid, double);
  223. public slots:
  224. void processResult(QUuid queryId, QList<QVariantMap> parameters);
  225. void onProgress(QUuid queryId, double onProgress);
  226. protected:
  227. QScopedPointer<ctkXnatSessionPrivate> d_ptr;
  228. private:
  229. Q_DECLARE_PRIVATE(ctkXnatSession)
  230. Q_DISABLE_COPY(ctkXnatSession)
  231. };
  232. #endif