comparison Plugins/Engine/PluginsJob.cpp @ 2814:7d1d3136f6cf

more generic handling of content and serialization in plugin jobs
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 07 Sep 2018 10:09:17 +0200
parents ea7aea6f6a95
children 925d8dc03a23
comparison
equal deleted inserted replaced
2813:6d5b20af216f 2814:7d1d3136f6cf
37 #if ORTHANC_ENABLE_PLUGINS != 1 37 #if ORTHANC_ENABLE_PLUGINS != 1
38 #error The plugin support is disabled 38 #error The plugin support is disabled
39 #endif 39 #endif
40 40
41 41
42 #include "../../Core/Logging.h"
42 #include "../../Core/OrthancException.h" 43 #include "../../Core/OrthancException.h"
43 44
44 #include <json/reader.h> 45 #include <json/reader.h>
45 #include <cassert> 46 #include <cassert>
46 47
47 namespace Orthanc 48 namespace Orthanc
48 { 49 {
49 PluginsJob::PluginsJob(const _OrthancPluginSubmitJob& parameters) : 50 PluginsJob::PluginsJob(const _OrthancPluginSubmitJob& parameters) :
50 job_(parameters.job_), 51 parameters_(parameters)
51 free_(parameters.free_),
52 getProgress_(parameters.getProgress_),
53 step_(parameters.step_),
54 stop_(parameters.stop_),
55 reset_(parameters.reset_)
56 { 52 {
57 if (job_ == NULL || 53 if (parameters_.job == NULL)
58 parameters.type_ == NULL ||
59 free_ == NULL ||
60 getProgress_ == NULL ||
61 step_ == NULL ||
62 stop_ == NULL ||
63 reset_ == NULL)
64 { 54 {
65 throw OrthancException(ErrorCode_NullPointer); 55 throw OrthancException(ErrorCode_NullPointer);
66 } 56 }
67 57
68 type_.assign(parameters.type_); 58 if (parameters_.resultId == NULL ||
69 59 parameters_.freeJob == NULL ||
70 if (parameters.content_ == NULL) 60 parameters_.type == NULL ||
61 parameters_.getProgress == NULL ||
62 parameters_.getContent == NULL ||
63 parameters_.getSerialized == NULL ||
64 parameters_.step == NULL ||
65 parameters_.stop == NULL ||
66 parameters_.reset == NULL)
71 { 67 {
72 publicContent_ = Json::objectValue; 68 parameters_.freeJob(parameters.job);
73 } 69 throw OrthancException(ErrorCode_NullPointer);
74 else
75 {
76 Json::Reader reader;
77 if (!reader.parse(parameters.content_, publicContent_) ||
78 publicContent_.type() != Json::objectValue)
79 {
80 free_(job_);
81 throw OrthancException(ErrorCode_BadFileFormat);
82 }
83 } 70 }
84 71
85 if (parameters.serialized_ == NULL) 72 type_.assign(parameters.type);
86 {
87 hasSerialized_ = false;
88 }
89 else
90 {
91 hasSerialized_ = true;
92
93 Json::Reader reader;
94 if (!reader.parse(parameters.serialized_, serialized_) ||
95 serialized_.type() != Json::objectValue ||
96 !serialized_.isMember("Type") ||
97 serialized_["Type"].type() != Json::stringValue)
98 {
99 free_(job_);
100 throw OrthancException(ErrorCode_BadFileFormat);
101 }
102 }
103 } 73 }
104 74
105 PluginsJob::~PluginsJob() 75 PluginsJob::~PluginsJob()
106 { 76 {
107 assert(job_ != NULL); 77 assert(parameters_.job != NULL);
108 free_(job_); 78 parameters_.freeJob(parameters_.job);
109 } 79 }
110 80
111 JobStepResult PluginsJob::Step() 81 JobStepResult PluginsJob::Step()
112 { 82 {
113 OrthancPluginJobStepStatus status = step_(job_); 83 OrthancPluginJobStepStatus status = parameters_.step(parameters_.job);
114 84
115 switch (status) 85 switch (status)
116 { 86 {
117 case OrthancPluginJobStepStatus_Success: 87 case OrthancPluginJobStepStatus_Success:
118 return JobStepResult::Success(); 88 return JobStepResult::Success();
128 } 98 }
129 } 99 }
130 100
131 void PluginsJob::Reset() 101 void PluginsJob::Reset()
132 { 102 {
133 reset_(job_); 103 parameters_.reset(parameters_.job);
134 } 104 }
135 105
136 void PluginsJob::Stop(JobStopReason reason) 106 void PluginsJob::Stop(JobStopReason reason)
137 { 107 {
138 switch (reason) 108 switch (reason)
139 { 109 {
140 case JobStopReason_Success: 110 case JobStopReason_Success:
141 stop_(job_, OrthancPluginJobStopReason_Success); 111 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success);
142 break; 112 break;
143 113
144 case JobStopReason_Failure: 114 case JobStopReason_Failure:
145 stop_(job_, OrthancPluginJobStopReason_Failure); 115 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure);
146 break; 116 break;
147 117
148 case JobStopReason_Canceled: 118 case JobStopReason_Canceled:
149 stop_(job_, OrthancPluginJobStopReason_Canceled); 119 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled);
150 break; 120 break;
151 121
152 case JobStopReason_Paused: 122 case JobStopReason_Paused:
153 stop_(job_, OrthancPluginJobStopReason_Paused); 123 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused);
154 break; 124 break;
155 125
156 default: 126 default:
157 throw OrthancException(ErrorCode_ParameterOutOfRange); 127 throw OrthancException(ErrorCode_ParameterOutOfRange);
158 } 128 }
159 } 129 }
160 130
161 float PluginsJob::GetProgress() 131 float PluginsJob::GetProgress()
162 { 132 {
163 return getProgress_(job_); 133 return parameters_.getProgress(parameters_.job);
134 }
135
136 void PluginsJob::GetPublicContent(Json::Value& value)
137 {
138 const char* content = parameters_.getContent(parameters_.job);
139
140 if (content == NULL)
141 {
142 value = Json::objectValue;
143 }
144 else
145 {
146 Json::Reader reader;
147
148 if (!reader.parse(content, value) ||
149 value.type() != Json::objectValue)
150 {
151 LOG(ERROR) << "A job plugin must provide a JSON object as its public content";
152 throw OrthancException(ErrorCode_Plugin);
153 }
154 }
164 } 155 }
165 156
166 bool PluginsJob::Serialize(Json::Value& value) 157 bool PluginsJob::Serialize(Json::Value& value)
167 { 158 {
168 if (hasSerialized_) 159 const char* serialized = parameters_.getSerialized(parameters_.job);
160
161 if (serialized == NULL)
169 { 162 {
170 value = serialized_; 163 return false;
171 return true;
172 } 164 }
173 else 165 else
174 { 166 {
175 return false; 167 Json::Reader reader;
168
169 if (!reader.parse(serialized, value) ||
170 value.type() != Json::objectValue)
171 {
172 LOG(ERROR) << "A job plugin must provide a JSON object as its serialized content";
173 throw OrthancException(ErrorCode_Plugin);
174 }
175
176
177 static const char* KEY_TYPE = "Type";
178
179 if (value.isMember(KEY_TYPE))
180 {
181 LOG(ERROR) << "The \"Type\" field is for reserved use for serialized job";
182 throw OrthancException(ErrorCode_Plugin);
183 }
184
185 value[KEY_TYPE] = type_;
186 return true;
176 } 187 }
177 } 188 }
178 } 189 }