Mercurial > hg > orthanc
changeset 2970:eea66afed0db
remove redundancies
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 06 Dec 2018 10:10:58 +0100 |
parents | 2c16c29b287d |
children | 4be6a2b46f1e cb5d75143da0 |
files | OrthancServer/OrthancRestApi/OrthancRestApi.cpp OrthancServer/OrthancRestApi/OrthancRestApi.h OrthancServer/OrthancRestApi/OrthancRestArchive.cpp |
diffstat | 3 files changed, 65 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.cpp Wed Dec 05 18:02:11 2018 +0100 +++ b/OrthancServer/OrthancRestApi/OrthancRestApi.cpp Thu Dec 06 10:10:58 2018 +0100 @@ -196,6 +196,20 @@ } } + + unsigned int OrthancRestApi::GetJobRequestPriority(const Json::Value& body) + { + if (body.type() != Json::objectValue || + !body.isMember(KEY_PRIORITY)) + { + return 0; // Default priority + } + else + { + return SerializationToolbox::ReadInteger(body, KEY_PRIORITY); + } + } + void OrthancRestApi::SubmitGenericJob(RestApiPostCall& call, IJob* job, @@ -214,18 +228,11 @@ throw OrthancException(ErrorCode_BadFileFormat); } - int priority = 0; - - if (body.isMember(KEY_PRIORITY)) - { - priority = SerializationToolbox::ReadInteger(body, KEY_PRIORITY); - } - if (IsSynchronousJobRequest(isDefaultSynchronous, body)) { Json::Value successContent; if (context_.GetJobsEngine().GetRegistry().SubmitAndWait - (successContent, raii.release(), priority)) + (successContent, raii.release(), GetJobRequestPriority(body))) { // Success in synchronous execution call.GetOutput().AnswerJson(successContent); @@ -240,7 +247,8 @@ { // Asynchronous mode: Submit the job, but don't wait for its completion std::string id; - context_.GetJobsEngine().GetRegistry().Submit(id, raii.release(), priority); + context_.GetJobsEngine().GetRegistry().Submit + (id, raii.release(), GetJobRequestPriority(body)); Json::Value v; v["ID"] = id;
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.h Wed Dec 05 18:02:11 2018 +0100 +++ b/OrthancServer/OrthancRestApi/OrthancRestApi.h Thu Dec 06 10:10:58 2018 +0100 @@ -106,6 +106,8 @@ static bool IsSynchronousJobRequest(bool isDefaultSynchronous, const Json::Value& body); + static unsigned int GetJobRequestPriority(const Json::Value& body); + void SubmitGenericJob(RestApiPostCall& call, IJob* job, bool isDefaultSynchronous,
--- a/OrthancServer/OrthancRestApi/OrthancRestArchive.cpp Wed Dec 05 18:02:11 2018 +0100 +++ b/OrthancServer/OrthancRestApi/OrthancRestArchive.cpp Thu Dec 06 10:10:58 2018 +0100 @@ -96,12 +96,15 @@ static void GetJobParameters(bool& synchronous, /* out */ bool& extended, /* out */ + int& priority, /* out */ const Json::Value& body, /* in */ const bool defaultExtended /* in */) { synchronous = OrthancRestApi::IsSynchronousJobRequest (true /* synchronous by default */, body); + priority = OrthancRestApi::GetJobRequestPriority(body); + if (body.type() == Json::objectValue && body.isMember(KEY_EXTENDED)) { @@ -117,6 +120,7 @@ static void SubmitJob(RestApiOutput& output, ServerContext& context, std::auto_ptr<ArchiveJob>& job, + int priority, bool synchronous, const std::string& filename) { @@ -134,7 +138,7 @@ Json::Value publicContent; if (context.GetJobsEngine().GetRegistry().SubmitAndWait - (publicContent, job.release(), 0 /* TODO priority */)) + (publicContent, job.release(), priority)) { // The archive is now created: Prepare the sending of the ZIP file FilesystemHttpSender sender(tmp->GetPath()); @@ -156,7 +160,9 @@ } - static void CreateBatchArchive(RestApiPostCall& call) + template <bool IS_MEDIA, + bool DEFAULT_IS_EXTENDED /* only makes sense for media (i.e. not ZIP archives) */ > + static void CreateBatch(RestApiPostCall& call) { ServerContext& context = OrthancRestApi::GetContext(call); @@ -164,34 +170,12 @@ if (call.ParseJsonRequest(body)) { bool synchronous, extended; - GetJobParameters(synchronous, extended, body, false /* by default, not extended */); + int priority; + GetJobParameters(synchronous, extended, priority, body, DEFAULT_IS_EXTENDED); - std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended)); + std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended)); AddResourcesOfInterest(*job, body); - SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip"); - } - else - { - throw OrthancException(ErrorCode_BadFileFormat, - "Expected a list of resources to archive in the body"); - } - } - - - template <bool DEFAULT_EXTENDED> - static void CreateBatchMedia(RestApiPostCall& call) - { - ServerContext& context = OrthancRestApi::GetContext(call); - - Json::Value body; - if (call.ParseJsonRequest(body)) - { - bool synchronous, extended; - GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED); - - std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended)); - AddResourcesOfInterest(*job, body); - SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip"); + SubmitJob(call.GetOutput(), context, job, priority, synchronous, "Archive.zip"); } else { @@ -201,44 +185,53 @@ } - static void CreateArchive(RestApiGetCall& call) - { - ServerContext& context = OrthancRestApi::GetContext(call); - - std::string id = call.GetUriComponent("id", ""); - - std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false)); - job->AddResource(id); - - SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip"); - } - - - static void CreateMedia(RestApiGetCall& call) + template <bool IS_MEDIA, + bool DEFAULT_IS_EXTENDED /* only makes sense for media (i.e. not ZIP archives) */ > + static void CreateSingleGet(RestApiGetCall& call) { ServerContext& context = OrthancRestApi::GetContext(call); std::string id = call.GetUriComponent("id", ""); - std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended"))); + bool extended; + if (IS_MEDIA) + { + extended = call.HasArgument("extended"); + } + else + { + extended = false; + } + + std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended)); job->AddResource(id); - SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip"); + SubmitJob(call.GetOutput(), context, job, 0 /* priority */, + true /* synchronous */, id + ".zip"); } void OrthancRestApi::RegisterArchive() { - Register("/patients/{id}/archive", CreateArchive); - Register("/studies/{id}/archive", CreateArchive); - Register("/series/{id}/archive", CreateArchive); + Register("/patients/{id}/archive", + CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); + Register("/studies/{id}/archive", + CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); + Register("/series/{id}/archive", + CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); - Register("/patients/{id}/media", CreateMedia); - Register("/studies/{id}/media", CreateMedia); - Register("/series/{id}/media", CreateMedia); + Register("/patients/{id}/media", + CreateSingleGet<true /* media */, false /* not extended by default */>); + Register("/studies/{id}/media", + CreateSingleGet<true /* media */, false /* not extended by default */>); + Register("/series/{id}/media", + CreateSingleGet<true /* media */, false /* not extended by default */>); - Register("/tools/create-archive", CreateBatchArchive); - Register("/tools/create-media", CreateBatchMedia<false>); - Register("/tools/create-media-extended", CreateBatchMedia<true>); + Register("/tools/create-archive", + CreateBatch<false /* ZIP */, false /* extended makes no sense in ZIP */>); + Register("/tools/create-media", + CreateBatch<true /* media */, false /* not extended by default */>); + Register("/tools/create-media-extended", + CreateBatch<true /* media */, true /* extended by default */>); } }