comparison Plugins/Engine/PluginsHttpHandler.cpp @ 913:3e43de893d88 plugins

POST, DELETE, PUT from Orthanc plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Jun 2014 15:42:14 +0200
parents dcb2469f00f4
children c068671d12a9
comparison
equal deleted inserted replaced
912:dcb2469f00f4 913:3e43de893d88
356 HttpHandler::ParseGetQuery(uri, getArguments, p.uri); 356 HttpHandler::ParseGetQuery(uri, getArguments, p.uri);
357 357
358 StringHttpOutput stream; 358 StringHttpOutput stream;
359 HttpOutput http(stream); 359 HttpOutput http(stream);
360 360
361 LOG(INFO) << "Plugin making REST call on URI " << p.uri; 361 LOG(INFO) << "Plugin making REST GET call on URI " << p.uri;
362 362
363 if (pimpl_->restApi_ != NULL && 363 if (pimpl_->restApi_ != NULL &&
364 pimpl_->restApi_->Handle(http, HttpMethod_Get, uri, headers, getArguments, body)) 364 pimpl_->restApi_->Handle(http, HttpMethod_Get, uri, headers, getArguments, body))
365 { 365 {
366 std::string result; 366 std::string result;
372 throw OrthancException(ErrorCode_BadRequest); 372 throw OrthancException(ErrorCode_BadRequest);
373 } 373 }
374 } 374 }
375 375
376 376
377 void PluginsHttpHandler::RestApiPostPut(bool isPost, const void* parameters)
378 {
379 const _OrthancPluginRestApiPostPut& p =
380 *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters);
381
382 HttpHandler::Arguments headers; // No HTTP header
383 HttpHandler::Arguments getArguments; // No GET argument for POST/PUT
384
385 UriComponents uri;
386 Toolbox::SplitUriComponents(uri, p.uri);
387
388 // TODO Avoid unecessary memcpy
389 std::string body(p.body, p.bodySize);
390
391 StringHttpOutput stream;
392 HttpOutput http(stream);
393
394 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put);
395 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri;
396
397 if (pimpl_->restApi_ != NULL &&
398 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, body))
399 {
400 std::string result;
401 stream.GetOutput(result);
402 CopyToMemoryBuffer(*p.target, result);
403 }
404 else
405 {
406 throw OrthancException(ErrorCode_BadRequest);
407 }
408 }
409
410
411 void PluginsHttpHandler::RestApiDelete(const void* parameters)
412 {
413 // The "parameters" point to the URI
414 UriComponents uri;
415 Toolbox::SplitUriComponents(uri, reinterpret_cast<const char*>(parameters));
416
417 HttpHandler::Arguments headers; // No HTTP header
418 HttpHandler::Arguments getArguments; // No GET argument for POST/PUT
419 std::string body; // No body for DELETE
420
421 StringHttpOutput stream;
422 HttpOutput http(stream);
423
424 LOG(INFO) << "Plugin making REST DELETE call on URI "
425 << reinterpret_cast<const char*>(parameters);
426
427 if (pimpl_->restApi_ == NULL ||
428 !pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments, body))
429 {
430 throw OrthancException(ErrorCode_BadRequest);
431 }
432 }
433
434
377 bool PluginsHttpHandler::InvokeService(_OrthancPluginService service, 435 bool PluginsHttpHandler::InvokeService(_OrthancPluginService service,
378 const void* parameters) 436 const void* parameters)
379 { 437 {
380 switch (service) 438 switch (service)
381 { 439 {
382 case _OrthancPluginService_RegisterRestCallback: 440 case _OrthancPluginService_RegisterRestCallback:
383 {
384 RegisterRestCallback(parameters); 441 RegisterRestCallback(parameters);
385 return true; 442 return true;
386 }
387 443
388 case _OrthancPluginService_AnswerBuffer: 444 case _OrthancPluginService_AnswerBuffer:
389 {
390 AnswerBuffer(parameters); 445 AnswerBuffer(parameters);
391 return true; 446 return true;
392 }
393 447
394 case _OrthancPluginService_CompressAndAnswerPngImage: 448 case _OrthancPluginService_CompressAndAnswerPngImage:
395 {
396 CompressAndAnswerPngImage(parameters); 449 CompressAndAnswerPngImage(parameters);
397 return true; 450 return true;
398 }
399 451
400 case _OrthancPluginService_GetDicomForInstance: 452 case _OrthancPluginService_GetDicomForInstance:
401 {
402 GetDicomForInstance(parameters); 453 GetDicomForInstance(parameters);
403 return true; 454 return true;
404 }
405 455
406 case _OrthancPluginService_RestApiGet: 456 case _OrthancPluginService_RestApiGet:
407 {
408 RestApiGet(parameters); 457 RestApiGet(parameters);
409 return true; 458 return true;
410 } 459
460 case _OrthancPluginService_RestApiPost:
461 RestApiPostPut(true, parameters);
462 return true;
463
464 case _OrthancPluginService_RestApiDelete:
465 RestApiDelete(parameters);
466 return true;
467
468 case _OrthancPluginService_RestApiPut:
469 RestApiPostPut(false, parameters);
470 return true;
411 471
412 default: 472 default:
413 return false; 473 return false;
414 } 474 }
415 } 475 }