weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef REPO_HANDLES_COMMAND_BASE_HANDLE_HPP |
| 21 | #define REPO_HANDLES_COMMAND_BASE_HANDLE_HPP |
| 22 | |
| 23 | #include "common.hpp" |
| 24 | |
| 25 | #include "storage/repo-storage.hpp" |
| 26 | #include "repo-command-response.hpp" |
| 27 | #include "repo-command-parameter.hpp" |
| 28 | #include "repo-command.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/security/validator.hpp> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 32 | |
| 33 | namespace repo { |
| 34 | |
| 35 | class CommandBaseHandle |
| 36 | { |
| 37 | public: |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 41 | using std::runtime_error::runtime_error; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | public: |
| 45 | CommandBaseHandle(Face& face, RepoStorage& storageHandle, |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 46 | Scheduler& scheduler, ndn::security::Validator& validator); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 47 | |
| 48 | virtual |
| 49 | ~CommandBaseHandle() = default; |
| 50 | |
| 51 | ndn::mgmt::Authorization |
| 52 | makeAuthorization(); |
| 53 | |
| 54 | template<typename T> |
| 55 | bool |
| 56 | validateParameters(const ndn::mgmt::ControlParameters& parameters) |
| 57 | { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 58 | const auto* castParams = dynamic_cast<const RepoCommandParameter*>(¶meters); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 59 | BOOST_ASSERT(castParams != nullptr); |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 60 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 61 | T command; |
| 62 | try { |
| 63 | command.validateRequest(*castParams); |
| 64 | } |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 65 | catch (const RepoCommand::ArgumentError&) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | protected: |
| 72 | Face& face; |
| 73 | RepoStorage& storageHandle; |
| 74 | Scheduler& scheduler; |
| 75 | |
| 76 | private: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 77 | ndn::security::Validator& m_validator; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 78 | }; |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 79 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 80 | } // namespace repo |
| 81 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 82 | #endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP |