comparison Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 3416:541c787e2230

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Jun 2019 21:38:34 +0200
parents 2a821deece64
children ad263225187c
comparison
equal deleted inserted replaced
3415:2a821deece64 3416:541c787e2230
938 const OrthancPluginHttpRequest* request); 938 const OrthancPluginHttpRequest* request);
939 939
940 940
941 namespace Internals 941 namespace Internals
942 { 942 {
943 inline void NullRestCallback(OrthancPluginRestOutput* output, 943 void NullRestCallback(OrthancPluginRestOutput* output,
944 const char* url, 944 const char* url,
945 const OrthancPluginHttpRequest* request) 945 const OrthancPluginHttpRequest* request);
946 {
947 }
948 946
949 inline IChunkedRequestReader *NullChunkedRestCallback(const char* url, 947 IChunkedRequestReader *NullChunkedRestCallback(const char* url,
950 const OrthancPluginHttpRequest* request) 948 const OrthancPluginHttpRequest* request);
951 {
952 return NULL;
953 }
954 949
955 950
956 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1 951 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1
957 template <ChunkedRestCallback Callback> 952 template <ChunkedRestCallback Callback>
958 static OrthancPluginErrorCode Protect(OrthancPluginServerChunkedRequestReader** reader, 953 static OrthancPluginErrorCode Protect(OrthancPluginServerChunkedRequestReader** reader,
990 { 985 {
991 return OrthancPluginErrorCode_Plugin; 986 return OrthancPluginErrorCode_Plugin;
992 } 987 }
993 } 988 }
994 989
995 static inline OrthancPluginErrorCode ChunkedRequestReaderAddChunk( 990 OrthancPluginErrorCode ChunkedRequestReaderAddChunk(
996 OrthancPluginServerChunkedRequestReader* reader, 991 OrthancPluginServerChunkedRequestReader* reader,
997 const void* data, 992 const void* data,
998 uint32_t size) 993 uint32_t size);
999 { 994
1000 try 995 OrthancPluginErrorCode ChunkedRequestReaderExecute(
1001 {
1002 if (reader == NULL)
1003 {
1004 return OrthancPluginErrorCode_InternalError;
1005 }
1006
1007 reinterpret_cast<IChunkedRequestReader*>(reader)->AddChunk(data, size);
1008 return OrthancPluginErrorCode_Success;
1009 }
1010 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
1011 {
1012 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
1013 }
1014 catch (boost::bad_lexical_cast&)
1015 {
1016 return OrthancPluginErrorCode_BadFileFormat;
1017 }
1018 catch (...)
1019 {
1020 return OrthancPluginErrorCode_Plugin;
1021 }
1022 }
1023
1024 static inline OrthancPluginErrorCode ChunkedRequestReaderExecute(
1025 OrthancPluginServerChunkedRequestReader* reader, 996 OrthancPluginServerChunkedRequestReader* reader,
1026 OrthancPluginRestOutput* output) 997 OrthancPluginRestOutput* output);
1027 { 998
1028 try 999 void ChunkedRequestReaderFinalize(
1029 { 1000 OrthancPluginServerChunkedRequestReader* reader);
1030 if (reader == NULL) 1001
1031 { 1002 #else
1032 return OrthancPluginErrorCode_InternalError; 1003
1033 } 1004 void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
1034 1005 const char* url,
1035 reinterpret_cast<IChunkedRequestReader*>(reader)->Execute(output); 1006 const OrthancPluginHttpRequest* request,
1036 return OrthancPluginErrorCode_Success; 1007 RestCallback GetHandler,
1037 } 1008 ChunkedRestCallback PostHandler,
1038 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) 1009 RestCallback DeleteHandler,
1039 { 1010 ChunkedRestCallback PutHandler);
1040 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
1041 }
1042 catch (boost::bad_lexical_cast&)
1043 {
1044 return OrthancPluginErrorCode_BadFileFormat;
1045 }
1046 catch (...)
1047 {
1048 return OrthancPluginErrorCode_Plugin;
1049 }
1050 }
1051
1052 static inline void ChunkedRequestReaderFinalize(
1053 OrthancPluginServerChunkedRequestReader* reader)
1054 {
1055 if (reader != NULL)
1056 {
1057 delete reinterpret_cast<IChunkedRequestReader*>(reader);
1058 }
1059 }
1060
1061 #else
1062 1011
1063 template< 1012 template<
1064 RestCallback GetHandler, 1013 RestCallback GetHandler,
1065 ChunkedRestCallback PostHandler, 1014 ChunkedRestCallback PostHandler,
1066 RestCallback DeleteHandler, 1015 RestCallback DeleteHandler,
1068 > 1017 >
1069 static void ChunkedRestCompatibility(OrthancPluginRestOutput* output, 1018 static void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
1070 const char* url, 1019 const char* url,
1071 const OrthancPluginHttpRequest* request) 1020 const OrthancPluginHttpRequest* request)
1072 { 1021 {
1073 std::string allowed; 1022 ChunkedRestCompatibility(output, url, request, GetHandler,
1074 1023 PostHandler, DeleteHandler, PutHandler);
1075 if (GetHandler != Internals::NullRestCallback)
1076 {
1077 allowed += "GET";
1078 }
1079
1080 if (PostHandler != Internals::NullChunkedRestCallback)
1081 {
1082 if (!allowed.empty())
1083 {
1084 allowed += ",";
1085 }
1086
1087 allowed += "POST";
1088 }
1089
1090 if (DeleteHandler != Internals::NullRestCallback)
1091 {
1092 if (!allowed.empty())
1093 {
1094 allowed += ",";
1095 }
1096
1097 allowed += "DELETE";
1098 }
1099
1100 if (PutHandler != Internals::NullChunkedRestCallback)
1101 {
1102 if (!allowed.empty())
1103 {
1104 allowed += ",";
1105 }
1106
1107 allowed += "PUT";
1108 }
1109
1110 switch (request->method)
1111 {
1112 case OrthancPluginHttpMethod_Get:
1113 {
1114 if (GetHandler == Internals::NullRestCallback)
1115 {
1116 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
1117 }
1118 else
1119 {
1120 GetHandler(output, url, request);
1121 }
1122 return;
1123 }
1124
1125 case OrthancPluginHttpMethod_Post:
1126 {
1127 if (PostHandler == Internals::NullChunkedRestCallback)
1128 {
1129 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
1130 }
1131 else
1132 {
1133 std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request));
1134 if (reader.get() == NULL)
1135 {
1136 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
1137 }
1138 else
1139 {
1140 reader->AddChunk(request->body, request->bodySize);
1141 reader->Execute(output);
1142 }
1143 }
1144 return;
1145 }
1146
1147 case OrthancPluginHttpMethod_Delete:
1148 {
1149 if (DeleteHandler == Internals::NullRestCallback)
1150 {
1151 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
1152 }
1153 else
1154 {
1155 DeleteHandler(output, url, request);
1156 }
1157 return;
1158 }
1159
1160 case OrthancPluginHttpMethod_Put:
1161 {
1162 if (PutHandler == Internals::NullChunkedRestCallback)
1163 {
1164 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
1165 }
1166 else
1167 {
1168 std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request));
1169 if (reader.get() == NULL)
1170 {
1171 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
1172 }
1173 else
1174 {
1175 reader->AddChunk(request->body, request->bodySize);
1176 reader->Execute(output);
1177 }
1178 }
1179 return;
1180 }
1181
1182 default:
1183 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1184 }
1185 } 1024 }
1186 #endif 1025 #endif
1187 } 1026 }
1188 1027
1189 1028
1207 Internals::ChunkedRequestReaderExecute, 1046 Internals::ChunkedRequestReaderExecute,
1208 Internals::ChunkedRequestReaderFinalize); 1047 Internals::ChunkedRequestReaderFinalize);
1209 #else 1048 #else
1210 LogWarning("Performance warning: The plugin was compiled against a pre-1.5.7 version " 1049 LogWarning("Performance warning: The plugin was compiled against a pre-1.5.7 version "
1211 "of the Orthanc SDK. Multipart transfers will be entirely stored in RAM."); 1050 "of the Orthanc SDK. Multipart transfers will be entirely stored in RAM.");
1051
1212 OrthancPluginRegisterRestCallback( 1052 OrthancPluginRegisterRestCallback(
1213 GetGlobalContext(), uri.c_str(), 1053 GetGlobalContext(), uri.c_str(),
1214 Internals::Protect< Internals::ChunkedRestCompatibility< 1054 Internals::Protect< Internals::ChunkedRestCompatibility<
1215 GetHandler, PostHandler, DeleteHandler, PutHandler> >); 1055 GetHandler, PostHandler, DeleteHandler, PutHandler> >);
1216 #endif 1056 #endif