ApiController.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #include "ApiController.hpp"
  25. #include "oatpp/web/server/handler/ErrorHandler.hpp"
  26. namespace oatpp { namespace web { namespace server { namespace api {
  27. const Endpoints& ApiController::getEndpoints() {
  28. return m_endpoints;
  29. }
  30. void ApiController::setEndpointInfo(const std::string& endpointName, const std::shared_ptr<Endpoint::Info>& info){
  31. m_endpointInfo[endpointName] = info;
  32. }
  33. std::shared_ptr<Endpoint::Info> ApiController::getEndpointInfo(const std::string& endpointName) {
  34. return m_endpointInfo[endpointName];
  35. }
  36. void ApiController::setEndpointHandler(const std::string& endpointName, const std::shared_ptr<RequestHandler>& handler) {
  37. m_endpointHandlers[endpointName] = handler;
  38. }
  39. std::shared_ptr<ApiController::RequestHandler> ApiController::getEndpointHandler(const std::string& endpointName) {
  40. return m_endpointHandlers[endpointName];
  41. }
  42. void ApiController::setErrorHandler(const std::shared_ptr<handler::ErrorHandler>& errorHandler){
  43. m_errorHandler = errorHandler;
  44. if(!m_errorHandler) {
  45. m_errorHandler = handler::DefaultErrorHandler::createShared();
  46. }
  47. }
  48. std::shared_ptr<ApiController::OutgoingResponse> ApiController::handleError(const std::exception_ptr& exceptionPtr) const {
  49. if(m_errorHandler) {
  50. return m_errorHandler->handleError(exceptionPtr);
  51. }
  52. return nullptr;
  53. }
  54. void ApiController::setDefaultAuthorizationHandler(const std::shared_ptr<handler::AuthorizationHandler>& authorizationHandler){
  55. m_defaultAuthorizationHandler = authorizationHandler;
  56. }
  57. std::shared_ptr<handler::AuthorizationHandler> ApiController::getDefaultAuthorizationHandler() {
  58. return m_defaultAuthorizationHandler;
  59. }
  60. std::shared_ptr<handler::AuthorizationObject> ApiController::handleDefaultAuthorization(const String &authHeader) const {
  61. if(m_defaultAuthorizationHandler) {
  62. return m_defaultAuthorizationHandler->handleAuthorization(authHeader);
  63. }
  64. // If Authorization is not setup on the server then it's 500
  65. throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "Authorization is not setup.");
  66. }
  67. const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& ApiController::getDefaultObjectMapper() const {
  68. return m_defaultObjectMapper;
  69. }
  70. // Helper methods
  71. std::shared_ptr<ApiController::OutgoingResponse> ApiController::createResponse(const Status& status,
  72. const oatpp::String& str) const {
  73. return ResponseFactory::createResponse(status, str);
  74. }
  75. std::shared_ptr<ApiController::OutgoingResponse> ApiController::createResponse(const ApiController::Status &status) const {
  76. return ResponseFactory::createResponse(status);
  77. }
  78. std::shared_ptr<ApiController::OutgoingResponse> ApiController::createDtoResponse(const Status& status,
  79. const oatpp::Void& dto,
  80. const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper) const {
  81. return ResponseFactory::createResponse(status, dto, objectMapper);
  82. }
  83. std::shared_ptr<ApiController::OutgoingResponse> ApiController::createDtoResponse(const Status& status,
  84. const oatpp::Void& dto) const {
  85. return ResponseFactory::createResponse(status, dto, m_defaultObjectMapper);
  86. }
  87. }}}}