comparison OrthancServer/main.cpp @ 1645:1558b3226b18

IHttpExceptionFormatter
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 24 Sep 2015 15:55:17 +0200
parents eb8fbcf008b5
children 8040d56cb0b3
comparison
equal deleted inserted replaced
1644:939b921b2c81 1645:1558b3226b18
305 return true; 305 return true;
306 } 306 }
307 }; 307 };
308 308
309 309
310
311 class MyHttpExceptionFormatter : public IHttpExceptionFormatter
312 {
313 private:
314 bool describeErrors_;
315 OrthancPlugins* plugins_;
316
317 public:
318 MyHttpExceptionFormatter(bool describeErrors,
319 OrthancPlugins* plugins) :
320 describeErrors_(describeErrors),
321 plugins_(plugins)
322 {
323 }
324
325 virtual void Format(HttpOutput& output,
326 const OrthancException& exception,
327 HttpMethod method,
328 const char* uri)
329 {
330 if (!describeErrors_)
331 {
332 output.SendStatus(exception.GetHttpStatus());
333 return;
334 }
335
336 Json::Value message = Json::objectValue;
337 message["Method"] = EnumerationToString(method);
338 message["Uri"] = uri;
339
340 ErrorCode errorCode = exception.GetErrorCode();
341 HttpStatus httpStatus = exception.GetHttpStatus();
342
343 bool isPlugin = false;
344
345 #if ORTHANC_PLUGINS_ENABLED == 1
346 if (plugins_ != NULL &&
347 plugins_->GetErrorDictionary().Format(message, httpStatus, exception))
348 {
349 errorCode = ErrorCode_Plugin;
350 isPlugin = true;
351 }
352 #endif
353
354 if (!isPlugin)
355 {
356 message["Message"] = exception.What();
357 }
358
359 message["HttpError"] = EnumerationToString(httpStatus);
360 message["HttpStatus"] = httpStatus;
361 message["OrthancError"] = EnumerationToString(errorCode);
362 message["OrthancStatus"] = errorCode;
363
364 std::string info = message.toStyledString();
365 output.SendStatus(httpStatus, info);
366 }
367 };
368
369
370
310 static void PrintHelp(char* path) 371 static void PrintHelp(char* path)
311 { 372 {
312 std::cout 373 std::cout
313 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl 374 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl
314 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl 375 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl
411 } 472 }
412 473
413 474
414 475
415 static bool StartHttpServer(ServerContext& context, 476 static bool StartHttpServer(ServerContext& context,
416 OrthancRestApi& restApi) 477 OrthancRestApi& restApi,
478 OrthancPlugins* plugins)
417 { 479 {
418 if (!Configuration::GetGlobalBoolParameter("HttpServerEnabled", true)) 480 if (!Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
419 { 481 {
420 LOG(WARNING) << "The HTTP server is disabled"; 482 LOG(WARNING) << "The HTTP server is disabled";
421 return WaitForExit(context, restApi); 483 return WaitForExit(context, restApi);
422 } 484 }
485
486 MyHttpExceptionFormatter exceptionFormatter(Configuration::GetGlobalBoolParameter("HttpDescribeErrors", true), plugins);
487
423 488
424 // HTTP server 489 // HTTP server
425 MyIncomingHttpRequestFilter httpFilter(context); 490 MyIncomingHttpRequestFilter httpFilter(context);
426 MongooseServer httpServer; 491 MongooseServer httpServer;
427 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042)); 492 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042));
428 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false)); 493 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false));
429 httpServer.SetKeepAliveEnabled(Configuration::GetGlobalBoolParameter("KeepAlive", false)); 494 httpServer.SetKeepAliveEnabled(Configuration::GetGlobalBoolParameter("KeepAlive", false));
430 httpServer.SetHttpCompressionEnabled(Configuration::GetGlobalBoolParameter("HttpCompressionEnabled", true)); 495 httpServer.SetHttpCompressionEnabled(Configuration::GetGlobalBoolParameter("HttpCompressionEnabled", true));
431 httpServer.SetDescribeErrorsEnabled(Configuration::GetGlobalBoolParameter("HttpDescribeErrors", true));
432 httpServer.SetIncomingHttpRequestFilter(httpFilter); 496 httpServer.SetIncomingHttpRequestFilter(httpFilter);
497 httpServer.SetHttpExceptionFormatter(exceptionFormatter);
433 498
434 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false)); 499 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false));
435 Configuration::SetupRegisteredUsers(httpServer); 500 Configuration::SetupRegisteredUsers(httpServer);
436 501
437 if (Configuration::GetGlobalBoolParameter("SslEnabled", false)) 502 if (Configuration::GetGlobalBoolParameter("SslEnabled", false))
459 return restart; 524 return restart;
460 } 525 }
461 526
462 527
463 static bool StartDicomServer(ServerContext& context, 528 static bool StartDicomServer(ServerContext& context,
464 OrthancRestApi& restApi) 529 OrthancRestApi& restApi,
530 OrthancPlugins* plugins)
465 { 531 {
466 if (!Configuration::GetGlobalBoolParameter("DicomServerEnabled", true)) 532 if (!Configuration::GetGlobalBoolParameter("DicomServerEnabled", true))
467 { 533 {
468 LOG(WARNING) << "The DICOM server is disabled"; 534 LOG(WARNING) << "The DICOM server is disabled";
469 return StartHttpServer(context, restApi); 535 return StartHttpServer(context, restApi, plugins);
470 } 536 }
471 537
472 MyDicomServerFactory serverFactory(context); 538 MyDicomServerFactory serverFactory(context);
473 539
474 // DICOM server 540 // DICOM server
483 dicomServer.SetApplicationEntityFilter(dicomFilter); 549 dicomServer.SetApplicationEntityFilter(dicomFilter);
484 550
485 dicomServer.Start(); 551 dicomServer.Start();
486 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber(); 552 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
487 553
488 bool restart = StartHttpServer(context, restApi); 554 bool restart = StartHttpServer(context, restApi, plugins);
489 555
490 dicomServer.Stop(); 556 dicomServer.Stop();
491 LOG(WARNING) << " DICOM server has stopped"; 557 LOG(WARNING) << " DICOM server has stopped";
492 558
493 serverFactory.Done(); 559 serverFactory.Done();
520 586
521 // Thirdly, consider the built-in REST API of Orthanc 587 // Thirdly, consider the built-in REST API of Orthanc
522 OrthancRestApi restApi(context); 588 OrthancRestApi restApi(context);
523 context.GetHttpHandler().Register(restApi, true); 589 context.GetHttpHandler().Register(restApi, true);
524 590
525 return StartDicomServer(context, restApi); 591 return StartDicomServer(context, restApi, plugins);
526 } 592 }
527 593
528 594
529 static bool UpgradeDatabase(IDatabaseWrapper& database, 595 static bool UpgradeDatabase(IDatabaseWrapper& database,
530 IStorageArea& storageArea, 596 IStorageArea& storageArea,