Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestModalities.cpp @ 3167:860aed8258c3
Fix issue #73 (/modalities/{modalityId}/store raises 500 errors instead of 404)
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Jan 2019 13:10:52 +0100 |
parents | ac72032d69a2 |
children | 595bfee4391a |
comparison
equal
deleted
inserted
replaced
3166:6953a4e475b3 | 3167:860aed8258c3 |
---|---|
811 | 811 |
812 /*************************************************************************** | 812 /*************************************************************************** |
813 * DICOM C-Store SCU | 813 * DICOM C-Store SCU |
814 ***************************************************************************/ | 814 ***************************************************************************/ |
815 | 815 |
816 static bool GetInstancesToExport(Json::Value& otherArguments, | 816 static void GetInstancesToExport(Json::Value& otherArguments, |
817 SetOfInstancesJob& job, | 817 SetOfInstancesJob& job, |
818 const std::string& remote, | 818 const std::string& remote, |
819 RestApiPostCall& call) | 819 RestApiPostCall& call) |
820 { | 820 { |
821 otherArguments = Json::objectValue; | 821 otherArguments = Json::objectValue; |
832 request.append(Toolbox::StripSpaces(s)); | 832 request.append(Toolbox::StripSpaces(s)); |
833 } | 833 } |
834 else if (!call.ParseJsonRequest(request)) | 834 else if (!call.ParseJsonRequest(request)) |
835 { | 835 { |
836 // Bad JSON request | 836 // Bad JSON request |
837 return false; | 837 throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON value"); |
838 } | 838 } |
839 | 839 |
840 if (request.isString()) | 840 if (request.isString()) |
841 { | 841 { |
842 std::string item = request.asString(); | 842 std::string item = request.asString(); |
843 request = Json::arrayValue; | 843 request = Json::arrayValue; |
844 request.append(item); | 844 request.append(item); |
845 } | 845 } |
846 else if (!request.isArray() && | |
847 !request.isObject()) | |
848 { | |
849 throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON object, or a JSON array of strings"); | |
850 } | |
846 | 851 |
847 const Json::Value* resources; | 852 const Json::Value* resources; |
848 if (request.isArray()) | 853 if (request.isArray()) |
849 { | 854 { |
850 resources = &request; | 855 resources = &request; |
852 else | 857 else |
853 { | 858 { |
854 if (request.type() != Json::objectValue || | 859 if (request.type() != Json::objectValue || |
855 !request.isMember(KEY_RESOURCES)) | 860 !request.isMember(KEY_RESOURCES)) |
856 { | 861 { |
857 return false; | 862 throw OrthancException(ErrorCode_BadFileFormat, |
863 "Missing field in JSON: \"" + std::string(KEY_RESOURCES) + "\""); | |
858 } | 864 } |
859 | 865 |
860 resources = &request[KEY_RESOURCES]; | 866 resources = &request[KEY_RESOURCES]; |
861 if (!resources->isArray()) | 867 if (!resources->isArray()) |
862 { | 868 { |
863 return false; | 869 throw OrthancException(ErrorCode_BadFileFormat, |
870 "JSON field \"" + std::string(KEY_RESOURCES) + "\" must contain an array"); | |
864 } | 871 } |
865 | 872 |
866 // Copy the remaining arguments | 873 // Copy the remaining arguments |
867 Json::Value::Members members = request.getMemberNames(); | 874 Json::Value::Members members = request.getMemberNames(); |
868 for (Json::Value::ArrayIndex i = 0; i < members.size(); i++) | 875 for (Json::Value::ArrayIndex i = 0; i < members.size(); i++) |
880 | 887 |
881 for (Json::Value::ArrayIndex i = 0; i < resources->size(); i++) | 888 for (Json::Value::ArrayIndex i = 0; i < resources->size(); i++) |
882 { | 889 { |
883 if (!(*resources) [i].isString()) | 890 if (!(*resources) [i].isString()) |
884 { | 891 { |
885 return false; | 892 throw OrthancException(ErrorCode_BadFileFormat, |
893 "Resources to be exported must be specified as a JSON array of strings"); | |
886 } | 894 } |
887 | 895 |
888 std::string stripped = Toolbox::StripSpaces((*resources) [i].asString()); | 896 std::string stripped = Toolbox::StripSpaces((*resources) [i].asString()); |
889 if (!Toolbox::IsSHA1(stripped)) | 897 if (!Toolbox::IsSHA1(stripped)) |
890 { | 898 { |
891 return false; | 899 throw OrthancException(ErrorCode_BadFileFormat, |
892 } | 900 "This string is not a valid Orthanc identifier: " + stripped); |
901 } | |
902 | |
903 context.AddChildInstances(job, stripped); | |
893 | 904 |
894 if (logExportedResources) | 905 if (logExportedResources) |
895 { | 906 { |
896 context.GetIndex().LogExportedResource(stripped, remote); | 907 context.GetIndex().LogExportedResource(stripped, remote); |
897 } | 908 } |
898 | 909 } |
899 context.AddChildInstances(job, stripped); | |
900 } | |
901 | |
902 return true; | |
903 } | 910 } |
904 | 911 |
905 | 912 |
906 static void DicomStore(RestApiPostCall& call) | 913 static void DicomStore(RestApiPostCall& call) |
907 { | 914 { |
910 std::string remote = call.GetUriComponent("id", ""); | 917 std::string remote = call.GetUriComponent("id", ""); |
911 | 918 |
912 Json::Value request; | 919 Json::Value request; |
913 std::auto_ptr<DicomModalityStoreJob> job(new DicomModalityStoreJob(context)); | 920 std::auto_ptr<DicomModalityStoreJob> job(new DicomModalityStoreJob(context)); |
914 | 921 |
915 if (GetInstancesToExport(request, *job, remote, call)) | 922 GetInstancesToExport(request, *job, remote, call); |
916 { | 923 |
917 std::string localAet = Toolbox::GetJsonStringField | 924 std::string localAet = Toolbox::GetJsonStringField |
918 (request, "LocalAet", context.GetDefaultLocalApplicationEntityTitle()); | 925 (request, "LocalAet", context.GetDefaultLocalApplicationEntityTitle()); |
919 std::string moveOriginatorAET = Toolbox::GetJsonStringField | 926 std::string moveOriginatorAET = Toolbox::GetJsonStringField |
920 (request, "MoveOriginatorAet", context.GetDefaultLocalApplicationEntityTitle()); | 927 (request, "MoveOriginatorAet", context.GetDefaultLocalApplicationEntityTitle()); |
921 int moveOriginatorID = Toolbox::GetJsonIntegerField | 928 int moveOriginatorID = Toolbox::GetJsonIntegerField |
922 (request, "MoveOriginatorID", 0 /* By default, not a C-MOVE */); | 929 (request, "MoveOriginatorID", 0 /* By default, not a C-MOVE */); |
923 | 930 |
924 job->SetLocalAet(localAet); | 931 job->SetLocalAet(localAet); |
925 job->SetRemoteModality(MyGetModalityUsingSymbolicName(remote)); | 932 job->SetRemoteModality(MyGetModalityUsingSymbolicName(remote)); |
926 | 933 |
927 if (moveOriginatorID != 0) | 934 if (moveOriginatorID != 0) |
928 { | 935 { |
929 job->SetMoveOriginator(moveOriginatorAET, moveOriginatorID); | 936 job->SetMoveOriginator(moveOriginatorAET, moveOriginatorID); |
930 } | 937 } |
931 | 938 |
932 OrthancRestApi::GetApi(call).SubmitCommandsJob | 939 OrthancRestApi::GetApi(call).SubmitCommandsJob |
933 (call, job.release(), true /* synchronous by default */, request); | 940 (call, job.release(), true /* synchronous by default */, request); |
934 } | |
935 } | 941 } |
936 | 942 |
937 | 943 |
938 /*************************************************************************** | 944 /*************************************************************************** |
939 * DICOM C-Move SCU | 945 * DICOM C-Move SCU |
1057 std::string remote = call.GetUriComponent("id", ""); | 1063 std::string remote = call.GetUriComponent("id", ""); |
1058 | 1064 |
1059 Json::Value request; | 1065 Json::Value request; |
1060 std::auto_ptr<OrthancPeerStoreJob> job(new OrthancPeerStoreJob(context)); | 1066 std::auto_ptr<OrthancPeerStoreJob> job(new OrthancPeerStoreJob(context)); |
1061 | 1067 |
1062 if (GetInstancesToExport(request, *job, remote, call)) | 1068 GetInstancesToExport(request, *job, remote, call); |
1063 { | 1069 |
1064 OrthancConfiguration::ReaderLock lock; | 1070 OrthancConfiguration::ReaderLock lock; |
1065 | 1071 |
1066 WebServiceParameters peer; | 1072 WebServiceParameters peer; |
1067 if (lock.GetConfiguration().LookupOrthancPeer(peer, remote)) | 1073 if (lock.GetConfiguration().LookupOrthancPeer(peer, remote)) |
1068 { | 1074 { |
1069 job->SetPeer(peer); | 1075 job->SetPeer(peer); |
1070 OrthancRestApi::GetApi(call).SubmitCommandsJob | 1076 OrthancRestApi::GetApi(call).SubmitCommandsJob |
1071 (call, job.release(), true /* synchronous by default */, request); | 1077 (call, job.release(), true /* synchronous by default */, request); |
1072 } | 1078 } |
1073 else | 1079 else |
1074 { | 1080 { |
1075 throw OrthancException(ErrorCode_UnknownResource, | 1081 throw OrthancException(ErrorCode_UnknownResource, |
1076 "No peer with symbolic name: " + remote); | 1082 "No peer with symbolic name: " + remote); |
1077 } | |
1078 } | 1083 } |
1079 } | 1084 } |
1080 | 1085 |
1081 | 1086 |
1082 // DICOM bridge ------------------------------------------------------------- | 1087 // DICOM bridge ------------------------------------------------------------- |