comparison Framework/Deprecated/Toolbox/BaseWebService.cpp @ 754:92c400a09f1b

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