comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3786:3801435e34a1 SylvainRouquette/fix-issue169-95b752c

integration Orthanc-1.6.0->SylvainRouquette
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Mar 2020 11:48:30 +0100
parents 4edeef72de75
children 7e33516965f8
comparison
equal deleted inserted replaced
3785:763533d6dd67 3786:3801435e34a1
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium 5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 * 6 *
7 * This program is free software: you can redistribute it and/or 7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as 8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the 9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version. 10 * License, or (at your option) any later version.
31 **/ 31 **/
32 32
33 33
34 #include "OrthancPluginCppWrapper.h" 34 #include "OrthancPluginCppWrapper.h"
35 35
36 #include <boost/algorithm/string/predicate.hpp>
37 #include <boost/move/unique_ptr.hpp>
36 #include <boost/thread.hpp> 38 #include <boost/thread.hpp>
37 #include <boost/algorithm/string/predicate.hpp>
38 #include <json/reader.h> 39 #include <json/reader.h>
39 #include <json/writer.h> 40 #include <json/writer.h>
40 41
41 42
42 namespace OrthancPlugins 43 namespace OrthancPlugins
2155 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); 2156 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt());
2156 #endif 2157 #endif
2157 } 2158 }
2158 } 2159 }
2159 } 2160 }
2161 }
2162
2163
2164 void OrthancJob::SubmitFromRestApiPost(OrthancPluginRestOutput* output,
2165 const Json::Value& body,
2166 OrthancJob* job)
2167 {
2168 static const char* KEY_SYNCHRONOUS = "Synchronous";
2169 static const char* KEY_ASYNCHRONOUS = "Asynchronous";
2170 static const char* KEY_PRIORITY = "Priority";
2171
2172 boost::movelib::unique_ptr<OrthancJob> protection(job);
2173
2174 if (body.type() != Json::objectValue)
2175 {
2176 #if HAS_ORTHANC_EXCEPTION == 1
2177 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2178 "Expected a JSON object in the body");
2179 #else
2180 LogError("Expected a JSON object in the body");
2181 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2182 #endif
2183 }
2184
2185 bool synchronous = true;
2186
2187 if (body.isMember(KEY_SYNCHRONOUS))
2188 {
2189 if (body[KEY_SYNCHRONOUS].type() != Json::booleanValue)
2190 {
2191 #if HAS_ORTHANC_EXCEPTION == 1
2192 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2193 "Option \"" + std::string(KEY_SYNCHRONOUS) +
2194 "\" must be Boolean");
2195 #else
2196 LogError("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean");
2197 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2198 #endif
2199 }
2200 else
2201 {
2202 synchronous = body[KEY_SYNCHRONOUS].asBool();
2203 }
2204 }
2205
2206 if (body.isMember(KEY_ASYNCHRONOUS))
2207 {
2208 if (body[KEY_ASYNCHRONOUS].type() != Json::booleanValue)
2209 {
2210 #if HAS_ORTHANC_EXCEPTION == 1
2211 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2212 "Option \"" + std::string(KEY_ASYNCHRONOUS) +
2213 "\" must be Boolean");
2214 #else
2215 LogError("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean");
2216 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2217 #endif
2218 }
2219 else
2220 {
2221 synchronous = !body[KEY_ASYNCHRONOUS].asBool();
2222 }
2223 }
2224
2225 int priority = 0;
2226
2227 if (body.isMember(KEY_PRIORITY))
2228 {
2229 if (body[KEY_PRIORITY].type() != Json::booleanValue)
2230 {
2231 #if HAS_ORTHANC_EXCEPTION == 1
2232 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2233 "Option \"" + std::string(KEY_PRIORITY) +
2234 "\" must be an integer");
2235 #else
2236 LogError("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer");
2237 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2238 #endif
2239 }
2240 else
2241 {
2242 priority = !body[KEY_PRIORITY].asInt();
2243 }
2244 }
2245
2246 Json::Value result;
2247
2248 if (synchronous)
2249 {
2250 OrthancPlugins::OrthancJob::SubmitAndWait(result, protection.release(), priority);
2251 }
2252 else
2253 {
2254 std::string id = OrthancPlugins::OrthancJob::Submit(protection.release(), priority);
2255
2256 result = Json::objectValue;
2257 result["ID"] = id;
2258 result["Path"] = "/jobs/" + id;
2259 }
2260
2261 std::string s = result.toStyledString();
2262 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(),
2263 s.size(), "application/json");
2160 } 2264 }
2161 2265
2162 #endif 2266 #endif
2163 2267
2164 2268
2954 { 3058 {
2955 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); 3059 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
2956 } 3060 }
2957 else 3061 else
2958 { 3062 {
2959 std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request)); 3063 boost::movelib::unique_ptr<IChunkedRequestReader> reader(PostHandler(url, request));
2960 if (reader.get() == NULL) 3064 if (reader.get() == NULL)
2961 { 3065 {
2962 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); 3066 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
2963 } 3067 }
2964 else 3068 else
2987 { 3091 {
2988 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); 3092 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
2989 } 3093 }
2990 else 3094 else
2991 { 3095 {
2992 std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request)); 3096 boost::movelib::unique_ptr<IChunkedRequestReader> reader(PutHandler(url, request));
2993 if (reader.get() == NULL) 3097 if (reader.get() == NULL)
2994 { 3098 {
2995 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); 3099 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
2996 } 3100 }
2997 else 3101 else
3034 return OrthancPluginErrorCode_Plugin; 3138 return OrthancPluginErrorCode_Plugin;
3035 } 3139 }
3036 } 3140 }
3037 #endif 3141 #endif
3038 } 3142 }
3143
3144
3145 #if HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP == 1
3146 OrthancPluginErrorCode IStorageCommitmentScpHandler::Lookup(
3147 OrthancPluginStorageCommitmentFailureReason* target,
3148 void* rawHandler,
3149 const char* sopClassUid,
3150 const char* sopInstanceUid)
3151 {
3152 assert(target != NULL &&
3153 rawHandler != NULL);
3154
3155 try
3156 {
3157 IStorageCommitmentScpHandler& handler = *reinterpret_cast<IStorageCommitmentScpHandler*>(rawHandler);
3158 *target = handler.Lookup(sopClassUid, sopInstanceUid);
3159 return OrthancPluginErrorCode_Success;
3160 }
3161 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
3162 {
3163 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
3164 }
3165 catch (...)
3166 {
3167 return OrthancPluginErrorCode_Plugin;
3168 }
3169 }
3170 #endif
3171
3172
3173 #if HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP == 1
3174 void IStorageCommitmentScpHandler::Destructor(void* rawHandler)
3175 {
3176 assert(rawHandler != NULL);
3177 delete reinterpret_cast<IStorageCommitmentScpHandler*>(rawHandler);
3178 }
3179 #endif
3039 } 3180 }