comparison Framework/Deprecated/Toolbox/BaseWebService.h @ 732:c35e98d22764

move Deprecated classes to a separate folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 May 2019 14:27:35 +0200
parents Framework/Toolbox/BaseWebService.h@4f2416d519b4
children 733c6db3e5a3
comparison
equal deleted inserted replaced
729:529189f399ec 732:c35e98d22764
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "IWebService.h"
25
26 #include <string>
27 #include <map>
28
29 namespace Deprecated
30 {
31 // This is an intermediate of IWebService that implements some caching on
32 // the HTTP GET requests
33 class BaseWebService : public IWebService, public OrthancStone::IObserver
34 {
35 public:
36 class CachedHttpRequestSuccessMessage
37 {
38 protected:
39 std::string uri_;
40 void* answer_;
41 size_t answerSize_;
42 IWebService::HttpHeaders answerHeaders_;
43
44 public:
45 CachedHttpRequestSuccessMessage(const IWebService::HttpRequestSuccessMessage& message) :
46 uri_(message.GetUri()),
47 answerSize_(message.GetAnswerSize()),
48 answerHeaders_(message.GetAnswerHttpHeaders())
49 {
50 answer_ = malloc(answerSize_);
51 memcpy(answer_, message.GetAnswer(), answerSize_);
52 }
53
54 ~CachedHttpRequestSuccessMessage()
55 {
56 free(answer_);
57 }
58
59 const std::string& GetUri() const
60 {
61 return uri_;
62 }
63
64 const void* GetAnswer() const
65 {
66 return answer_;
67 }
68
69 size_t GetAnswerSize() const
70 {
71 return answerSize_;
72 }
73
74 const IWebService::HttpHeaders& GetAnswerHttpHeaders() const
75 {
76 return answerHeaders_;
77 }
78
79 };
80 protected:
81 class BaseWebServicePayload;
82
83 bool cacheEnabled_;
84 std::map<std::string, boost::shared_ptr<CachedHttpRequestSuccessMessage> > cache_; // TODO: this is currently an infinite cache !
85
86 public:
87
88 BaseWebService(OrthancStone::MessageBroker& broker) :
89 IWebService(broker),
90 IObserver(broker),
91 cacheEnabled_(true)
92 {
93 }
94
95 virtual ~BaseWebService()
96 {
97 }
98
99 virtual void EnableCache(bool enable)
100 {
101 cacheEnabled_ = enable;
102 }
103
104 virtual void GetAsync(const std::string& uri,
105 const HttpHeaders& headers,
106 Orthanc::IDynamicObject* payload /* takes ownership */,
107 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
108 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
109 unsigned int timeoutInSeconds = 60);
110
111 protected:
112 virtual void GetAsyncInternal(const std::string& uri,
113 const HttpHeaders& headers,
114 Orthanc::IDynamicObject* payload /* takes ownership */,
115 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
116 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
117 unsigned int timeoutInSeconds = 60) = 0;
118
119 virtual void NotifyHttpSuccessLater(boost::shared_ptr<BaseWebService::CachedHttpRequestSuccessMessage> cachedHttpMessage,
120 Orthanc::IDynamicObject* payload, // takes ownership
121 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback) = 0;
122
123 private:
124 void NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
125
126 void NotifyHttpError(const IWebService::HttpRequestErrorMessage& message);
127
128 void CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
129
130 };
131 }