# HG changeset patch # User Sebastien Jodogne # Date 1468498889 -7200 # Node ID 27fd34970c5289224428fd3883b2312c4fb5d97a # Parent 7e6afa0beaf6df44914f76f67659b6dd425d18b4 refactoring diff -r 7e6afa0beaf6 -r 27fd34970c52 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Thu Jul 14 09:05:22 2016 +0200 +++ b/Core/Toolbox.cpp Thu Jul 14 14:21:29 2016 +0200 @@ -1590,5 +1590,87 @@ target.push_back(b < 10 ? b + '0' : b - 10 + 'A'); } } - } + } + + + static bool HasField(const Json::Value& json, + const std::string& key, + Json::ValueType expectedType) + { + if (json.type() != Json::objectValue || + !json.isMember(key)) + { + return false; + } + else if (json[key].type() == expectedType) + { + return true; + } + else + { + throw OrthancException(ErrorCode_BadParameterType); + } + } + + + std::string Toolbox::GetJsonStringField(const Json::Value& json, + const std::string& key, + const std::string& defaultValue) + { + if (HasField(json, key, Json::stringValue)) + { + return json[key].asString(); + } + else + { + return defaultValue; + } + } + + + bool Toolbox::GetJsonBooleanField(const ::Json::Value& json, + const std::string& key, + bool defaultValue) + { + if (HasField(json, key, Json::booleanValue)) + { + return json[key].asBool(); + } + else + { + return defaultValue; + } + } + + + int Toolbox::GetJsonIntegerField(const ::Json::Value& json, + const std::string& key, + int defaultValue) + { + if (HasField(json, key, Json::intValue)) + { + return json[key].asInt(); + } + else + { + return defaultValue; + } + } + + + unsigned int Toolbox::GetJsonUnsignedIntegerField(const ::Json::Value& json, + const std::string& key, + unsigned int defaultValue) + { + int v = GetJsonIntegerField(json, key, defaultValue); + + if (v < 0) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + return static_cast(v); + } + } } diff -r 7e6afa0beaf6 -r 27fd34970c52 Core/Toolbox.h --- a/Core/Toolbox.h Thu Jul 14 09:05:22 2016 +0200 +++ b/Core/Toolbox.h Thu Jul 14 14:21:29 2016 +0200 @@ -228,5 +228,21 @@ void UriEncode(std::string& target, const std::string& source); + + std::string GetJsonStringField(const ::Json::Value& json, + const std::string& key, + const std::string& defaultValue); + + bool GetJsonBooleanField(const ::Json::Value& json, + const std::string& key, + bool defaultValue); + + int GetJsonIntegerField(const ::Json::Value& json, + const std::string& key, + int defaultValue); + + unsigned int GetJsonUnsignedIntegerField(const ::Json::Value& json, + const std::string& key, + unsigned int defaultValue); } } diff -r 7e6afa0beaf6 -r 27fd34970c52 OrthancServer/OrthancInitialization.cpp --- a/OrthancServer/OrthancInitialization.cpp Thu Jul 14 09:05:22 2016 +0200 +++ b/OrthancServer/OrthancInitialization.cpp Thu Jul 14 14:21:29 2016 +0200 @@ -585,7 +585,7 @@ if (v < 0) { LOG(ERROR) << "The configuration option \"" << parameter << "\" must be a positive integer"; - throw OrthancException(ErrorCode_BadParameterType); + throw OrthancException(ErrorCode_ParameterOutOfRange); } else { diff -r 7e6afa0beaf6 -r 27fd34970c52 OrthancServer/OrthancRestApi/OrthancRestModalities.cpp --- a/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp Thu Jul 14 09:05:22 2016 +0200 +++ b/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp Thu Jul 14 14:21:29 2016 +0200 @@ -683,58 +683,16 @@ return; } - static const char* LOCAL_AET = "LocalAet"; - std::string localAet = context.GetDefaultLocalApplicationEntityTitle(); - if (request.type() == Json::objectValue && - request.isMember(LOCAL_AET)) - { - if (request[LOCAL_AET].type() == Json::stringValue) - { - localAet = request[LOCAL_AET].asString(); - } - else - { - throw OrthancException(ErrorCode_BadFileFormat); - } - } - - uint16_t moveOriginatorID = 0; /* By default, not a C-MOVE */ - - static const char* MOVE_ORIGINATOR_ID = "MoveOriginatorID"; - if (request.type() == Json::objectValue && - request.isMember(MOVE_ORIGINATOR_ID)) - { - if (request[MOVE_ORIGINATOR_ID].type() != Json::intValue) - { - throw OrthancException(ErrorCode_BadFileFormat); - } + std::string localAet = Toolbox::GetJsonStringField(request, "LocalAet", context.GetDefaultLocalApplicationEntityTitle()); + bool permissive = Toolbox::GetJsonBooleanField(request, "Permissive", false); + int moveOriginatorID = Toolbox::GetJsonIntegerField(request, "MoveOriginatorID", 0 /* By default, not a C-MOVE */); - int v = request[MOVE_ORIGINATOR_ID].asInt(); - if (v <= 0 || v >= 65536) - { - throw OrthancException(ErrorCode_ParameterOutOfRange); - } - else - { - moveOriginatorID = boost::lexical_cast(v); - } + if (moveOriginatorID < 0 || + moveOriginatorID >= 65536) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); } - - static const char* PERMISSIVE = "Permissive"; - bool permissive = false; - if (request.type() == Json::objectValue && - request.isMember(PERMISSIVE)) - { - if (request[PERMISSIVE].type() == Json::booleanValue) - { - permissive = request[PERMISSIVE].asBool(); - } - else - { - throw OrthancException(ErrorCode_BadFileFormat); - } - } - + RemoteModalityParameters p = Configuration::GetModalityUsingSymbolicName(remote); ServerJob job; @@ -742,7 +700,7 @@ it = instances.begin(); it != instances.end(); ++it) { job.AddCommand(new StoreScuCommand(context, localAet, p, permissive, - moveOriginatorID)).AddInput(*it); + static_cast(moveOriginatorID))).AddInput(*it); } job.SetDescription("HTTP request: Store-SCU to peer \"" + remote + "\""); @@ -784,33 +742,8 @@ ResourceType level = StringToResourceType(request["Level"].asCString()); - static const char* LOCAL_AET = "LocalAet"; - std::string localAet = context.GetDefaultLocalApplicationEntityTitle(); - if (request.isMember(LOCAL_AET)) - { - if (request[LOCAL_AET].type() == Json::stringValue) - { - localAet = request[LOCAL_AET].asString(); - } - else - { - throw OrthancException(ErrorCode_BadFileFormat); - } - } - - static const char* TARGET_AET = "TargetAet"; - std::string targetAet = context.GetDefaultLocalApplicationEntityTitle(); - if (request.isMember(TARGET_AET)) - { - if (request[TARGET_AET].type() == Json::stringValue) - { - targetAet = request[TARGET_AET].asString(); - } - else - { - throw OrthancException(ErrorCode_BadFileFormat); - } - } + std::string localAet = Toolbox::GetJsonStringField(request, "LocalAet", context.GetDefaultLocalApplicationEntityTitle()); + std::string targetAet = Toolbox::GetJsonStringField(request, "TargetAet", context.GetDefaultLocalApplicationEntityTitle()); const RemoteModalityParameters source = Configuration::GetModalityUsingSymbolicName(call.GetUriComponent("id", "")); diff -r 7e6afa0beaf6 -r 27fd34970c52 UnitTestsSources/UnitTestsMain.cpp --- a/UnitTestsSources/UnitTestsMain.cpp Thu Jul 14 09:05:22 2016 +0200 +++ b/UnitTestsSources/UnitTestsMain.cpp Thu Jul 14 14:21:29 2016 +0200 @@ -945,6 +945,43 @@ } +TEST(Toolbox, AccessJson) +{ + Json::Value v = Json::arrayValue; + ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope")); + + v = Json::objectValue; + ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope")); + ASSERT_EQ(-10, Toolbox::GetJsonIntegerField(v, "hello", -10)); + ASSERT_EQ(10, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10)); + ASSERT_TRUE(Toolbox::GetJsonBooleanField(v, "hello", true)); + + v["hello"] = "world"; + ASSERT_EQ("world", Toolbox::GetJsonStringField(v, "hello", "nope")); + ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException); + ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); + ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); + + v["hello"] = -42; + ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); + ASSERT_EQ(-42, Toolbox::GetJsonIntegerField(v, "hello", -10)); + ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); + ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); + + v["hello"] = 42; + ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); + ASSERT_EQ(42, Toolbox::GetJsonIntegerField(v, "hello", -10)); + ASSERT_EQ(42, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10)); + ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); + + v["hello"] = false; + ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); + ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException); + ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); + ASSERT_FALSE(Toolbox::GetJsonBooleanField(v, "hello", true)); +} + + int main(int argc, char **argv) { Logging::Initialize();