comparison OrthancServer/OrthancRestApi/OrthancRestArchive.cpp @ 2966:10c610e80b15

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Dec 2018 15:27:01 +0100
parents 9d277f8ad698
children 2c16c29b287d
comparison
equal deleted inserted replaced
2965:9c0b0a6d8b54 2966:10c610e80b15
33 33
34 #include "../PrecompiledHeadersServer.h" 34 #include "../PrecompiledHeadersServer.h"
35 #include "OrthancRestApi.h" 35 #include "OrthancRestApi.h"
36 36
37 #include "../../Core/HttpServer/FilesystemHttpSender.h" 37 #include "../../Core/HttpServer/FilesystemHttpSender.h"
38 #include "../../Core/SerializationToolbox.h"
38 #include "../ServerJobs/ArchiveJob.h" 39 #include "../ServerJobs/ArchiveJob.h"
39 40
40 namespace Orthanc 41 namespace Orthanc
41 { 42 {
42 static bool AddResourcesOfInterest(ArchiveJob& job, 43 static const char* const KEY_RESOURCES = "Resources";
43 RestApiPostCall& call) 44 static const char* const KEY_EXTENDED = "Extended";
44 { 45
45 Json::Value resources; 46 static void AddResourcesOfInterestFromArray(ArchiveJob& job,
46 if (call.ParseJsonRequest(resources) && 47 const Json::Value& resources)
47 resources.type() == Json::arrayValue) 48 {
48 { 49 if (resources.type() != Json::arrayValue)
49 for (Json::Value::ArrayIndex i = 0; i < resources.size(); i++) 50 {
50 { 51 throw OrthancException(ErrorCode_BadFileFormat,
51 if (resources[i].type() != Json::stringValue) 52 "Expected a list of strings (Orthanc identifiers)");
52 { 53 }
53 return false; // Bad request 54
54 } 55 for (Json::Value::ArrayIndex i = 0; i < resources.size(); i++)
55 56 {
57 if (resources[i].type() != Json::stringValue)
58 {
59 throw OrthancException(ErrorCode_BadFileFormat,
60 "Expected a list of strings (Orthanc identifiers)");
61 }
62 else
63 {
56 job.AddResource(resources[i].asString()); 64 job.AddResource(resources[i].asString());
57 } 65 }
58 66 }
59 return true; 67 }
60 } 68
61 else 69
62 { 70 static void AddResourcesOfInterest(ArchiveJob& job /* inout */,
63 return false; 71 const Json::Value& body /* in */)
72 {
73 if (body.type() == Json::arrayValue)
74 {
75 AddResourcesOfInterestFromArray(job, body);
76 }
77 else if (body.type() == Json::objectValue)
78 {
79 if (body.isMember(KEY_RESOURCES))
80 {
81 AddResourcesOfInterestFromArray(job, body[KEY_RESOURCES]);
82 }
83 else
84 {
85 throw OrthancException(ErrorCode_BadFileFormat,
86 "Missing field " + std::string(KEY_RESOURCES) +
87 " in the JSON body");
88 }
89 }
90 else
91 {
92 throw OrthancException(ErrorCode_BadFileFormat);
93 }
94 }
95
96
97 static void GetJobParameters(bool& synchronous, /* out */
98 bool& extended, /* out */
99 const Json::Value& body, /* in */
100 const bool defaultExtended /* in */)
101 {
102 synchronous = OrthancRestApi::IsSynchronousJobRequest
103 (true /* synchronous by default */, body);
104
105 if (body.type() == Json::objectValue &&
106 body.isMember(KEY_EXTENDED))
107 {
108 extended = SerializationToolbox::ReadBoolean(body, KEY_EXTENDED);
109 }
110 else
111 {
112 extended = defaultExtended;
64 } 113 }
65 } 114 }
66 115
67 116
68 static void SubmitJob(RestApiCall& call, 117 static void SubmitJob(RestApiCall& call,
69 boost::shared_ptr<TemporaryFile>& tmp,
70 ServerContext& context, 118 ServerContext& context,
71 std::auto_ptr<ArchiveJob>& job, 119 std::auto_ptr<ArchiveJob>& job,
72 const std::string& filename) 120 const std::string& filename)
73 { 121 {
74 if (job.get() == NULL) 122 if (job.get() == NULL)
76 throw OrthancException(ErrorCode_NullPointer); 124 throw OrthancException(ErrorCode_NullPointer);
77 } 125 }
78 126
79 job->SetDescription("REST API"); 127 job->SetDescription("REST API");
80 128
129 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile);
130 job->SetSynchronousTarget(tmp);
131
81 Json::Value publicContent; 132 Json::Value publicContent;
82 if (context.GetJobsEngine().GetRegistry().SubmitAndWait 133 if (context.GetJobsEngine().GetRegistry().SubmitAndWait
83 (publicContent, job.release(), 0 /* TODO priority */)) 134 (publicContent, job.release(), 0 /* TODO priority */))
84 { 135 {
85 // The archive is now created: Prepare the sending of the ZIP file 136 // The archive is now created: Prepare the sending of the ZIP file
99 150
100 static void CreateBatchArchive(RestApiPostCall& call) 151 static void CreateBatchArchive(RestApiPostCall& call)
101 { 152 {
102 ServerContext& context = OrthancRestApi::GetContext(call); 153 ServerContext& context = OrthancRestApi::GetContext(call);
103 154
104 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); 155 Json::Value body;
105 std::auto_ptr<ArchiveJob> job(new ArchiveJob(tmp, context, false, false)); 156 if (call.ParseJsonRequest(body))
106 157 {
107 if (AddResourcesOfInterest(*job, call)) 158 bool synchronous, extended;
108 { 159 GetJobParameters(synchronous, extended, body, false /* by default, not extended */);
109 SubmitJob(call, tmp, context, job, "Archive.zip"); 160
161 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended));
162 AddResourcesOfInterest(*job, body);
163 SubmitJob(call, context, job, "Archive.zip");
164 }
165 else
166 {
167 throw OrthancException(ErrorCode_BadFileFormat,
168 "Expected a list of resources to archive in the body");
110 } 169 }
111 } 170 }
112 171
113 172
114 template <bool Extended> 173 template <bool DEFAULT_EXTENDED>
115 static void CreateBatchMedia(RestApiPostCall& call) 174 static void CreateBatchMedia(RestApiPostCall& call)
116 { 175 {
117 ServerContext& context = OrthancRestApi::GetContext(call); 176 ServerContext& context = OrthancRestApi::GetContext(call);
118 177
119 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); 178 Json::Value body;
120 std::auto_ptr<ArchiveJob> job(new ArchiveJob(tmp, context, true, Extended)); 179 if (call.ParseJsonRequest(body))
121 180 {
122 if (AddResourcesOfInterest(*job, call)) 181 bool synchronous, extended;
123 { 182 GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED);
124 SubmitJob(call, tmp, context, job, "Archive.zip"); 183
184 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended));
185 AddResourcesOfInterest(*job, body);
186 SubmitJob(call, context, job, "Archive.zip");
187 }
188 else
189 {
190 throw OrthancException(ErrorCode_BadFileFormat,
191 "Expected a list of resources to archive in the body");
125 } 192 }
126 } 193 }
127 194
128 195
129 static void CreateArchive(RestApiGetCall& call) 196 static void CreateArchive(RestApiGetCall& call)
130 { 197 {
131 ServerContext& context = OrthancRestApi::GetContext(call); 198 ServerContext& context = OrthancRestApi::GetContext(call);
132 199
133 std::string id = call.GetUriComponent("id", ""); 200 std::string id = call.GetUriComponent("id", "");
134 201
135 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); 202 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false));
136 std::auto_ptr<ArchiveJob> job(new ArchiveJob(tmp, context, false, false));
137 job->AddResource(id); 203 job->AddResource(id);
138 204
139 SubmitJob(call, tmp, context, job, id + ".zip"); 205 SubmitJob(call, context, job, id + ".zip");
140 } 206 }
141 207
142 208
143 static void CreateMedia(RestApiGetCall& call) 209 static void CreateMedia(RestApiGetCall& call)
144 { 210 {
145 ServerContext& context = OrthancRestApi::GetContext(call); 211 ServerContext& context = OrthancRestApi::GetContext(call);
146 212
147 std::string id = call.GetUriComponent("id", ""); 213 std::string id = call.GetUriComponent("id", "");
148 214
149 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); 215 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended")));
150 std::auto_ptr<ArchiveJob> job(new ArchiveJob(tmp, context, true, call.HasArgument("extended")));
151 job->AddResource(id); 216 job->AddResource(id);
152 217
153 SubmitJob(call, tmp, context, job, id + ".zip"); 218 SubmitJob(call, context, job, id + ".zip");
154 } 219 }
155 220
156 221
157 void OrthancRestApi::RegisterArchive() 222 void OrthancRestApi::RegisterArchive()
158 { 223 {