comparison Framework/Deprecated/Toolbox/BaseWebService.cpp @ 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.cpp@4f2416d519b4
children c0fcb2757b0a
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 #include "BaseWebService.h"
23
24 #include <Core/OrthancException.h>
25 #include "Framework/Messages/IObservable.h"
26 #include "Platforms/Generic/IOracleCommand.h"
27 #include <boost/shared_ptr.hpp>
28
29 namespace Deprecated
30 {
31
32
33 class BaseWebService::BaseWebServicePayload : public Orthanc::IDynamicObject
34 {
35 private:
36 std::auto_ptr< OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage> > userSuccessHandler_;
37 std::auto_ptr< OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> > userFailureHandler_;
38 std::auto_ptr< Orthanc::IDynamicObject> userPayload_;
39
40 public:
41 BaseWebServicePayload(OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* userSuccessHandler,
42 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* userFailureHandler,
43 Orthanc::IDynamicObject* userPayload) :
44 userSuccessHandler_(userSuccessHandler),
45 userFailureHandler_(userFailureHandler),
46 userPayload_(userPayload)
47 {
48 }
49
50 void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const
51 {
52 if (userSuccessHandler_.get() != NULL)
53 {
54 // recreate a success message with the user payload
55 IWebService::HttpRequestSuccessMessage successMessage(message.GetUri(),
56 message.GetAnswer(),
57 message.GetAnswerSize(),
58 message.GetAnswerHttpHeaders(),
59 userPayload_.get());
60 userSuccessHandler_->Apply(successMessage);
61 }
62 else
63 {
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
65 }
66 }
67
68 void HandleFailure(const IWebService::HttpRequestErrorMessage& message) const
69 {
70 if (userFailureHandler_.get() != NULL)
71 {
72 // recreate a failure message with the user payload
73 IWebService::HttpRequestErrorMessage failureMessage(message.GetUri(),
74 userPayload_.get());
75
76 userFailureHandler_->Apply(failureMessage);
77 }
78 }
79
80 };
81
82
83 void BaseWebService::GetAsync(const std::string& uri,
84 const HttpHeaders& headers,
85 Orthanc::IDynamicObject* payload /* takes ownership */,
86 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
87 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
88 unsigned int timeoutInSeconds)
89 {
90 if (cache_.find(uri) == cache_.end())
91 {
92 GetAsyncInternal(uri, headers,
93 new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered
94 new OrthancStone::Callable<BaseWebService, IWebService::HttpRequestSuccessMessage>
95 (*this, &BaseWebService::CacheAndNotifyHttpSuccess),
96 new OrthancStone::Callable<BaseWebService, IWebService::HttpRequestErrorMessage>
97 (*this, &BaseWebService::NotifyHttpError),
98 timeoutInSeconds);
99 }
100 else
101 {
102 // create a command and "post" it to the Oracle so it is executed and commited "later"
103 NotifyHttpSuccessLater(cache_[uri], payload, successCallback);
104 }
105
106 }
107
108
109
110 void BaseWebService::NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
111 {
112 if (message.HasPayload())
113 {
114 dynamic_cast<const BaseWebServicePayload&>(message.GetPayload()).HandleSuccess(message);
115 }
116 else
117 {
118 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
119 }
120 }
121
122 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
123 {
124 cache_[message.GetUri()] = boost::shared_ptr<CachedHttpRequestSuccessMessage>(new CachedHttpRequestSuccessMessage(message));
125 NotifyHttpSuccess(message);
126 }
127
128 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message)
129 {
130 if (message.HasPayload())
131 {
132 dynamic_cast<const BaseWebServicePayload&>(message.GetPayload()).HandleFailure(message);
133 }
134 else
135 {
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
137 }
138 }
139
140
141
142
143 }