annotate OrthancServer/ServerContext.cpp @ 226:8a26a8e85edf

refactoring to read files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 09:45:29 +0100
parents 4eb0c7ce86c9
children 209ca3f6db62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
224
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "ServerContext.h"
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
35 #include "../Core/HttpServer/FilesystemHttpSender.h"
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
36
224
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <glog/logging.h>
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace Orthanc
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 ServerContext::ServerContext(const boost::filesystem::path& path) :
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 storage_(path.string()),
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
43 index_(*this, path.string())
224
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 StoreStatus ServerContext::Store(const char* dicomFile,
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 size_t dicomSize,
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 const DicomMap& dicomSummary,
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 const Json::Value& dicomJson,
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 const std::string& remoteAet)
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 std::string fileUuid = storage_.Create(dicomFile, dicomSize);
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 std::string jsonUuid = storage_.Create(dicomJson.toStyledString());
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 StoreStatus status = index_.Store(dicomSummary, fileUuid, dicomSize, jsonUuid, remoteAet);
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 if (status != StoreStatus_Success)
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 storage_.Remove(fileUuid);
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 storage_.Remove(jsonUuid);
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 switch (status)
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 case StoreStatus_Success:
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 LOG(INFO) << "New instance stored";
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 break;
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 case StoreStatus_AlreadyStored:
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 LOG(INFO) << "Already stored";
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 break;
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 case StoreStatus_Failure:
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 LOG(ERROR) << "Store failure";
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 break;
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return status;
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
80
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
81
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
82 void ServerContext::AnswerFile(RestApiOutput& output,
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
83 const std::string& instancePublicId,
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
84 AttachedFileType content)
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
85 {
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
86 CompressionType compressionType;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
87 std::string fileUuid;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
88
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
89 if (index_.GetFile(fileUuid, compressionType,
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
90 instancePublicId, AttachedFileType_Dicom))
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
91 {
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
92 assert(compressionType == CompressionType_None);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
93
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
94 FilesystemHttpSender sender(storage_, fileUuid);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
95 sender.SetDownloadFilename(fileUuid + ".dcm");
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
96 sender.SetContentType("application/dicom");
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
97 output.AnswerFile(sender);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
98 }
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
99 }
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
100
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
101
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
102 void ServerContext::ReadJson(Json::Value& result,
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
103 const std::string& instancePublicId)
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
104 {
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
105 CompressionType compressionType;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
106 std::string fileUuid;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
107 if (!index_.GetFile(fileUuid, compressionType, instancePublicId, AttachedFileType_Json))
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
108 {
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
109 throw OrthancException(ErrorCode_InternalError);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
110 }
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
111
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
112 assert(compressionType == CompressionType_None);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
113
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
114 std::string s;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
115 storage_.ReadFile(s, fileUuid);
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
116
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
117 Json::Reader reader;
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
118 if (!reader.parse(s, result))
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
119 {
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
120 throw OrthancException("Corrupted JSON file");
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
121 }
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
122 }
224
4eb0c7ce86c9 refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }