# HG changeset patch # User Sebastien Jodogne # Date 1399470863 -7200 # Node ID 566a2fb3c1fb29b8074867d2163c091ecd6f3c3b # Parent 557575fd93e9a0a36010c563f3cf746e48f9b093 update/delete modalities diff -r 557575fd93e9 -r 566a2fb3c1fb NEWS --- a/NEWS Wed May 07 15:22:28 2014 +0200 +++ b/NEWS Wed May 07 15:54:23 2014 +0200 @@ -4,6 +4,7 @@ * Dynamic negotiation of SOP classes for C-Store SCU * Creation of DICOM instances using the REST API * Embedding of images within DICOM instances +* Adding/removal/modification of remote modalities/peers through REST * Reuse of the previous SCU connection to avoid unecessary handshakes * Fix missing licensing terms about reuse of some code from DCMTK * Various code refactorings diff -r 557575fd93e9 -r 566a2fb3c1fb OrthancServer/DicomProtocol/RemoteModalityParameters.cpp --- a/OrthancServer/DicomProtocol/RemoteModalityParameters.cpp Wed May 07 15:22:28 2014 +0200 +++ b/OrthancServer/DicomProtocol/RemoteModalityParameters.cpp Wed May 07 15:54:23 2014 +0200 @@ -34,6 +34,9 @@ #include "../../Core/OrthancException.h" +#include +#include + namespace Orthanc { RemoteModalityParameters::RemoteModalityParameters() @@ -54,4 +57,51 @@ port_ = port; } + + void RemoteModalityParameters::FromJson(const Json::Value& modality) + { + if (!modality.isArray() || + (modality.size() != 3 && modality.size() != 4)) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + SetApplicationEntityTitle(modality.get(0u, "").asString()); + SetHost(modality.get(1u, "").asString()); + + const Json::Value& portValue = modality.get(2u, ""); + try + { + SetPort(portValue.asInt()); + } + catch (std::runtime_error /* error inside JsonCpp */) + { + try + { + SetPort(boost::lexical_cast(portValue.asString())); + } + catch (boost::bad_lexical_cast) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + } + + if (modality.size() == 4) + { + SetManufacturer(modality.get(3u, "").asString()); + } + else + { + SetManufacturer(ModalityManufacturer_Generic); + } + } + + void RemoteModalityParameters::ToJson(Json::Value& value) const + { + value = Json::arrayValue; + value.append(GetApplicationEntityTitle()); + value.append(GetHost()); + value.append(GetPort()); + value.append(EnumerationToString(GetManufacturer())); + } } diff -r 557575fd93e9 -r 566a2fb3c1fb OrthancServer/DicomProtocol/RemoteModalityParameters.h --- a/OrthancServer/DicomProtocol/RemoteModalityParameters.h Wed May 07 15:22:28 2014 +0200 +++ b/OrthancServer/DicomProtocol/RemoteModalityParameters.h Wed May 07 15:54:23 2014 +0200 @@ -35,6 +35,7 @@ #include "../ServerEnumerations.h" #include +#include namespace Orthanc { @@ -103,5 +104,9 @@ { manufacturer_ = StringToModalityManufacturer(manufacturer); } + + void FromJson(const Json::Value& modality); + + void ToJson(Json::Value& value) const; }; } diff -r 557575fd93e9 -r 566a2fb3c1fb OrthancServer/OrthancInitialization.cpp --- a/OrthancServer/OrthancInitialization.cpp Wed May 07 15:22:28 2014 +0200 +++ b/OrthancServer/OrthancInitialization.cpp Wed May 07 15:54:23 2014 +0200 @@ -255,42 +255,14 @@ const Json::Value& modalities = (*configuration_) ["DicomModalities"]; if (modalities.type() != Json::objectValue || - !modalities.isMember(name) || - (modalities[name].size() != 3 && modalities[name].size() != 4)) + !modalities.isMember(name)) { throw OrthancException(ErrorCode_BadFileFormat); } try { - modality.SetApplicationEntityTitle(modalities[name].get(0u, "").asString()); - modality.SetHost(modalities[name].get(1u, "").asString()); - - const Json::Value& portValue = modalities[name].get(2u, ""); - try - { - modality.SetPort(portValue.asInt()); - } - catch (std::runtime_error /* error inside JsonCpp */) - { - try - { - modality.SetPort(boost::lexical_cast(portValue.asString())); - } - catch (boost::bad_lexical_cast) - { - throw OrthancException(ErrorCode_BadFileFormat); - } - } - - if (modalities[name].size() == 4) - { - modality.SetManufacturer(modalities[name].get(3u, "").asString()); - } - else - { - modality.SetManufacturer(ModalityManufacturer_Generic); - } + modality.FromJson(modalities[name]); } catch (OrthancException& e) { @@ -583,4 +555,46 @@ throw OrthancException("Unknown modality for AET: " + aet); } } + + + void UpdateModality(const RemoteModalityParameters& modality) + { + boost::mutex::scoped_lock lock(globalMutex_); + + if (!configuration_->isMember("DicomModalities")) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + Json::Value& modalities = (*configuration_) ["DicomModalities"]; + if (modalities.type() != Json::objectValue) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + modalities.removeMember(modality.GetName().c_str()); + + Json::Value v; + modality.ToJson(v); + modalities[modality.GetName()] = v; + } + + + void RemoveModality(const std::string& symbolicName) + { + boost::mutex::scoped_lock lock(globalMutex_); + + if (!configuration_->isMember("DicomModalities")) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + Json::Value& modalities = (*configuration_) ["DicomModalities"]; + if (modalities.type() != Json::objectValue) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + modalities.removeMember(symbolicName.c_str()); + } } diff -r 557575fd93e9 -r 566a2fb3c1fb OrthancServer/OrthancInitialization.h --- a/OrthancServer/OrthancInitialization.h Wed May 07 15:22:28 2014 +0200 +++ b/OrthancServer/OrthancInitialization.h Wed May 07 15:54:23 2014 +0200 @@ -141,4 +141,8 @@ RemoteModalityParameters GetModalityUsingSymbolicName(const std::string& name); RemoteModalityParameters GetModalityUsingAet(const std::string& aet); + + void UpdateModality(const RemoteModalityParameters& modality); + + void RemoveModality(const std::string& symbolicName); } diff -r 557575fd93e9 -r 566a2fb3c1fb OrthancServer/OrthancRestApi/OrthancRestModalities.cpp --- a/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp Wed May 07 15:22:28 2014 +0200 +++ b/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp Wed May 07 15:54:23 2014 +0200 @@ -464,10 +464,32 @@ } + static void UpdateModality(RestApi::PutCall& call) + { + Json::Value json; + Json::Reader reader; + if (reader.parse(call.GetPutBody(), json)) + { + RemoteModalityParameters modality; + modality.FromJson(json); + modality.SetName(call.GetUriComponent("id", "")); + UpdateModality(modality); + } + } + + + static void DeleteModality(RestApi::DeleteCall& call) + { + RemoveModality(call.GetUriComponent("id", "")); + } + + void OrthancRestApi::RegisterModalities() { Register("/modalities", ListModalities); Register("/modalities/{id}", ListModalityOperations); + Register("/modalities/{id}", UpdateModality); + Register("/modalities/{id}", DeleteModality); Register("/modalities/{id}/find-patient", DicomFindPatient); Register("/modalities/{id}/find-study", DicomFindStudy); Register("/modalities/{id}/find-series", DicomFindSeries); @@ -478,5 +500,6 @@ Register("/peers", ListPeers); Register("/peers/{id}", ListPeerOperations); Register("/peers/{id}/store", PeerStore); + } }