# HG changeset patch
# User am@osimis.io
# Date 1543856610 -3600
# Node ID 3a8bcc45c22146894b241aa475935760b0a459ad
# Parent 8999823db8b84ec77d7ba786f4ebbcd6f28ab917
moved the HTTP cache from OrthancApiClient to BaseWebService (not implemented yet in WASM)
diff -r 8999823db8b8 -r 3a8bcc45c221 Framework/Toolbox/BaseWebService.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Toolbox/BaseWebService.cpp Mon Dec 03 18:03:30 2018 +0100
@@ -0,0 +1,143 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ **/
+
+
+#include "BaseWebService.h"
+
+#include
+#include "Framework/Messages/IObservable.h"
+#include "Platforms/Generic/IOracleCommand.h"
+
+
+namespace OrthancStone
+{
+
+
+ class BaseWebService::BaseWebServicePayload : public Orthanc::IDynamicObject
+ {
+ private:
+ std::auto_ptr< MessageHandler > userSuccessHandler_;
+ std::auto_ptr< MessageHandler > userFailureHandler_;
+ std::auto_ptr< Orthanc::IDynamicObject> userPayload_;
+
+ public:
+ BaseWebServicePayload(MessageHandler* userSuccessHandler,
+ MessageHandler* userFailureHandler,
+ Orthanc::IDynamicObject* userPayload) :
+ userSuccessHandler_(userSuccessHandler),
+ userFailureHandler_(userFailureHandler),
+ userPayload_(userPayload)
+ {
+ }
+
+ void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const
+ {
+ if (userSuccessHandler_.get() != NULL)
+ {
+ // recreate a success message with the user payload
+ IWebService::HttpRequestSuccessMessage successMessage(message.GetUri(),
+ message.GetAnswer(),
+ message.GetAnswerSize(),
+ message.GetAnswerHttpHeaders(),
+ userPayload_.get());
+ userSuccessHandler_->Apply(successMessage);
+ }
+ else
+ {
+ throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+ }
+ }
+
+ void HandleFailure(const IWebService::HttpRequestErrorMessage& message) const
+ {
+ if (userFailureHandler_.get() != NULL)
+ {
+ // recreate a failure message with the user payload
+ IWebService::HttpRequestErrorMessage failureMessage(message.GetUri(),
+ userPayload_.get());
+
+ userFailureHandler_->Apply(failureMessage);
+ }
+ }
+
+ };
+
+
+ void BaseWebService::GetAsync(const std::string& uri,
+ const HttpHeaders& headers,
+ Orthanc::IDynamicObject* payload /* takes ownership */,
+ MessageHandler* successCallback,
+ MessageHandler* failureCallback,
+ unsigned int timeoutInSeconds)
+ {
+ if (cache_.find(uri) == cache_.end())
+ {
+ GetAsyncInternal(uri, headers,
+ new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered
+ new Callable
+ (*this, &BaseWebService::CacheAndNotifyHttpSuccess),
+ new Callable
+ (*this, &BaseWebService::NotifyHttpError),
+ timeoutInSeconds);
+ }
+ else
+ {
+ // create a command and "post" it to the Oracle so it is executed and commited "later"
+ NotifyHttpSuccessLater(cache_[uri], payload, successCallback);
+ }
+
+ }
+
+
+
+ void BaseWebService::NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
+ {
+ if (message.HasPayload())
+ {
+ dynamic_cast(message.GetPayload()).HandleSuccess(message);
+ }
+ else
+ {
+ throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+ }
+ }
+
+ void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
+ {
+ cache_[message.GetUri()] = boost::shared_ptr(new CachedHttpRequestSuccessMessage(message));
+ NotifyHttpSuccess(message);
+ }
+
+ void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message)
+ {
+ if (message.HasPayload())
+ {
+ dynamic_cast(message.GetPayload()).HandleFailure(message);
+ }
+ else
+ {
+ throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+ }
+ }
+
+
+
+
+}
diff -r 8999823db8b8 -r 3a8bcc45c221 Framework/Toolbox/BaseWebService.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Toolbox/BaseWebService.h Mon Dec 03 18:03:30 2018 +0100
@@ -0,0 +1,131 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ **/
+
+
+#pragma once
+
+#include "IWebService.h"
+
+#include
+#include