comparison Framework/Deprecated/Toolbox/OrthancApiClient.cpp @ 874:debb70fa10b5 am-dev

correct handling of deleted observers in OrthancApiClient
author Alain Mazy <alain@mazy.be>
date Wed, 03 Jul 2019 10:11:22 +0200
parents 4fe4b221a31f
children 861c080ef47b
comparison
equal deleted inserted replaced
873:78f4317eb94b 874:debb70fa10b5
71 std::auto_ptr< OrthancStone::MessageHandler<EmptyResponseReadyMessage> > emptyHandler_; 71 std::auto_ptr< OrthancStone::MessageHandler<EmptyResponseReadyMessage> > emptyHandler_;
72 std::auto_ptr< OrthancStone::MessageHandler<JsonResponseReadyMessage> > jsonHandler_; 72 std::auto_ptr< OrthancStone::MessageHandler<JsonResponseReadyMessage> > jsonHandler_;
73 std::auto_ptr< OrthancStone::MessageHandler<BinaryResponseReadyMessage> > binaryHandler_; 73 std::auto_ptr< OrthancStone::MessageHandler<BinaryResponseReadyMessage> > binaryHandler_;
74 std::auto_ptr< OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> > failureHandler_; 74 std::auto_ptr< OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> > failureHandler_;
75 std::auto_ptr< Orthanc::IDynamicObject > userPayload_; 75 std::auto_ptr< Orthanc::IDynamicObject > userPayload_;
76 76 OrthancStone::MessageBroker& broker_;
77 void NotifyConversionError(const IWebService::HttpRequestSuccessMessage& message) const 77 void NotifyConversionError(const IWebService::HttpRequestSuccessMessage& message) const
78 { 78 {
79 if (failureHandler_.get() != NULL) 79 if (failureHandler_.get() != NULL)
80 { 80 {
81 failureHandler_->Apply(IWebService::HttpRequestErrorMessage 81 failureHandler_->Apply(IWebService::HttpRequestErrorMessage
82 (message.GetUri(), userPayload_.get())); 82 (message.GetUri(), userPayload_.get()));
83 } 83 }
84 } 84 }
85 85
86 public: 86 public:
87 WebServicePayload(OrthancStone::MessageHandler<EmptyResponseReadyMessage>* handler, 87 WebServicePayload(OrthancStone::MessageBroker& broker,
88 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* handler,
88 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, 89 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler,
89 Orthanc::IDynamicObject* userPayload) : 90 Orthanc::IDynamicObject* userPayload) :
90 emptyHandler_(handler), 91 emptyHandler_(handler),
91 failureHandler_(failureHandler), 92 failureHandler_(failureHandler),
92 userPayload_(userPayload) 93 userPayload_(userPayload),
94 broker_(broker)
95
93 { 96 {
94 if (handler == NULL) 97 if (handler == NULL)
95 { 98 {
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 99 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
97 } 100 }
98 } 101 }
99 102
100 WebServicePayload(OrthancStone::MessageHandler<BinaryResponseReadyMessage>* handler, 103 WebServicePayload(OrthancStone::MessageBroker& broker,
104 OrthancStone::MessageHandler<BinaryResponseReadyMessage>* handler,
101 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, 105 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler,
102 Orthanc::IDynamicObject* userPayload) : 106 Orthanc::IDynamicObject* userPayload) :
103 binaryHandler_(handler), 107 binaryHandler_(handler),
104 failureHandler_(failureHandler), 108 failureHandler_(failureHandler),
105 userPayload_(userPayload) 109 userPayload_(userPayload),
110 broker_(broker)
106 { 111 {
107 if (handler == NULL) 112 if (handler == NULL)
108 { 113 {
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 114 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
110 } 115 }
111 } 116 }
112 117
113 WebServicePayload(OrthancStone::MessageHandler<JsonResponseReadyMessage>* handler, 118 WebServicePayload(OrthancStone::MessageBroker& broker,
119 OrthancStone::MessageHandler<JsonResponseReadyMessage>* handler,
114 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler, 120 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureHandler,
115 Orthanc::IDynamicObject* userPayload) : 121 Orthanc::IDynamicObject* userPayload) :
116 jsonHandler_(handler), 122 jsonHandler_(handler),
117 failureHandler_(failureHandler), 123 failureHandler_(failureHandler),
118 userPayload_(userPayload) 124 userPayload_(userPayload),
125 broker_(broker)
119 { 126 {
120 if (handler == NULL) 127 if (handler == NULL)
121 { 128 {
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 129 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
123 } 130 }
125 132
126 void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const 133 void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const
127 { 134 {
128 if (emptyHandler_.get() != NULL) 135 if (emptyHandler_.get() != NULL)
129 { 136 {
130 emptyHandler_->Apply(OrthancApiClient::EmptyResponseReadyMessage 137 if (broker_.IsActive(*(emptyHandler_->GetObserver())))
131 (message.GetUri(), userPayload_.get())); 138 {
139 emptyHandler_->Apply(OrthancApiClient::EmptyResponseReadyMessage
140 (message.GetUri(), userPayload_.get()));
141 }
132 } 142 }
133 else if (binaryHandler_.get() != NULL) 143 else if (binaryHandler_.get() != NULL)
134 { 144 {
135 binaryHandler_->Apply(OrthancApiClient::BinaryResponseReadyMessage 145 if (broker_.IsActive(*(binaryHandler_->GetObserver())))
136 (message.GetUri(), message.GetAnswer(), 146 {
137 message.GetAnswerSize(), userPayload_.get())); 147 binaryHandler_->Apply(OrthancApiClient::BinaryResponseReadyMessage
148 (message.GetUri(), message.GetAnswer(),
149 message.GetAnswerSize(), userPayload_.get()));
150 }
138 } 151 }
139 else if (jsonHandler_.get() != NULL) 152 else if (jsonHandler_.get() != NULL)
140 { 153 {
141 Json::Value response; 154 if (broker_.IsActive(*(jsonHandler_->GetObserver())))
142 if (MessagingToolbox::ParseJson(response, message.GetAnswer(), message.GetAnswerSize()))
143 { 155 {
144 jsonHandler_->Apply(OrthancApiClient::JsonResponseReadyMessage 156 Json::Value response;
145 (message.GetUri(), response, userPayload_.get())); 157 if (MessagingToolbox::ParseJson(response, message.GetAnswer(), message.GetAnswerSize()))
146 } 158 {
147 else 159 jsonHandler_->Apply(OrthancApiClient::JsonResponseReadyMessage
148 { 160 (message.GetUri(), response, userPayload_.get()));
149 NotifyConversionError(message); 161 }
162 else
163 {
164 NotifyConversionError(message);
165 }
150 } 166 }
151 } 167 }
152 else 168 else
153 { 169 {
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 170 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
184 Orthanc::IDynamicObject* payload) 200 Orthanc::IDynamicObject* payload)
185 { 201 {
186 IWebService::HttpHeaders emptyHeaders; 202 IWebService::HttpHeaders emptyHeaders;
187 web_.GetAsync(baseUrl_ + uri, 203 web_.GetAsync(baseUrl_ + uri,
188 emptyHeaders, 204 emptyHeaders,
189 new WebServicePayload(successCallback, failureCallback, payload), 205 new WebServicePayload(IObservable::GetBroker(), successCallback, failureCallback, payload),
190 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> 206 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage>
191 (*this, &OrthancApiClient::NotifyHttpSuccess), 207 (*this, &OrthancApiClient::NotifyHttpSuccess),
192 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> 208 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage>
193 (*this, &OrthancApiClient::NotifyHttpError)); 209 (*this, &OrthancApiClient::NotifyHttpError));
194 } 210 }
214 Orthanc::IDynamicObject* payload) 230 Orthanc::IDynamicObject* payload)
215 { 231 {
216 // printf("GET [%s] [%s]\n", baseUrl_.c_str(), uri.c_str()); 232 // printf("GET [%s] [%s]\n", baseUrl_.c_str(), uri.c_str());
217 233
218 web_.GetAsync(baseUrl_ + uri, headers, 234 web_.GetAsync(baseUrl_ + uri, headers,
219 new WebServicePayload(successCallback, failureCallback, payload), 235 new WebServicePayload(IObservable::GetBroker(), successCallback, failureCallback, payload),
220 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> 236 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage>
221 (*this, &OrthancApiClient::NotifyHttpSuccess), 237 (*this, &OrthancApiClient::NotifyHttpSuccess),
222 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> 238 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage>
223 (*this, &OrthancApiClient::NotifyHttpError)); 239 (*this, &OrthancApiClient::NotifyHttpError));
224 } 240 }
230 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback, 246 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback,
231 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, 247 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
232 Orthanc::IDynamicObject* payload) 248 Orthanc::IDynamicObject* payload)
233 { 249 {
234 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body, 250 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body,
235 new WebServicePayload(successCallback, failureCallback, payload), 251 new WebServicePayload(IObservable::GetBroker(), successCallback, failureCallback, payload),
236 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> 252 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage>
237 (*this, &OrthancApiClient::NotifyHttpSuccess), 253 (*this, &OrthancApiClient::NotifyHttpSuccess),
238 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> 254 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage>
239 (*this, &OrthancApiClient::NotifyHttpError)); 255 (*this, &OrthancApiClient::NotifyHttpError));
240 256
253 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback, 269 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback,
254 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, 270 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
255 Orthanc::IDynamicObject* payload /* takes ownership */) 271 Orthanc::IDynamicObject* payload /* takes ownership */)
256 { 272 {
257 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body, 273 web_.PostAsync(baseUrl_ + uri, IWebService::HttpHeaders(), body,
258 new WebServicePayload(successCallback, failureCallback, payload), 274 new WebServicePayload(IObservable::GetBroker(), successCallback, failureCallback, payload),
259 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> 275 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage>
260 (*this, &OrthancApiClient::NotifyHttpSuccess), 276 (*this, &OrthancApiClient::NotifyHttpSuccess),
261 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> 277 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage>
262 (*this, &OrthancApiClient::NotifyHttpError)); 278 (*this, &OrthancApiClient::NotifyHttpError));
263 } 279 }
300 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback, 316 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback,
301 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, 317 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
302 Orthanc::IDynamicObject* payload) 318 Orthanc::IDynamicObject* payload)
303 { 319 {
304 web_.DeleteAsync(baseUrl_ + uri, IWebService::HttpHeaders(), 320 web_.DeleteAsync(baseUrl_ + uri, IWebService::HttpHeaders(),
305 new WebServicePayload(successCallback, failureCallback, payload), 321 new WebServicePayload(IObservable::GetBroker(), successCallback, failureCallback, payload),
306 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage> 322 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestSuccessMessage>
307 (*this, &OrthancApiClient::NotifyHttpSuccess), 323 (*this, &OrthancApiClient::NotifyHttpSuccess),
308 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage> 324 new OrthancStone::Callable<OrthancApiClient, IWebService::HttpRequestErrorMessage>
309 (*this, &OrthancApiClient::NotifyHttpError)); 325 (*this, &OrthancApiClient::NotifyHttpError));
310 } 326 }