$darkmode
resource_handler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 #ifndef CLOE_UTILITY_RESOURCE_HANDLER_HPP_
24 #define CLOE_UTILITY_RESOURCE_HANDLER_HPP_
25 
26 #include <fstream>
27 #include <string> // for string
28 
29 #include <cloe/handler.hpp> // for Request, Response
30 #include <cloe/utility/resource.hpp> // for ResourceHandler
31 
32 #ifndef NDEBUG
33 #include <cloe/core.hpp> // for Logger
34 #endif
35 
36 #define RESOURCE_HANDLER(resource, ct) ::cloe::utility::ResourceHandler(RESOURCE(resource), ct)
37 #define RESOURCE_LOADER(resource) ::cloe::utility::ResourceLoader(RESOURCE(resource))
38 
39 namespace cloe {
40 namespace utility {
41 
48  public:
49  explicit ResourceLoader(Resource r) : res_(r) {}
50 
51  std::string to_string() const {
52 #ifndef NDEBUG
53  // Check if the file given in path exists, and if so, load it.
54  std::ifstream ifs{res_.filepath()};
55  std::string data;
56  if (ifs.is_open()) {
57  data = static_cast<std::stringstream const&>(std::stringstream() << ifs.rdbuf()).str();
58  } else {
59  logger::get("cloe")->warn("File does not exist: {}", res_.filepath());
60  data = res_.to_string();
61  }
62  return data;
63 #else
64  return res_.to_string();
65 #endif
66  }
67 
68  friend void to_json(cloe::Json& j, const ResourceLoader& c);
69 
70  private:
71  Resource res_;
72 };
73 
75  public:
76  ResourceHandler(Resource r, ContentType t) : resl_(r), type_(t) {}
77  void operator()(const Request&, Response& r) { r.set_body(resl_.to_string(), type_); }
78 
79  private:
80  ResourceLoader resl_;
81  ContentType type_;
82 };
83 
89 void to_json(cloe::Json& j, const ResourceLoader& c) {
90  try {
91  j = fable::parse_json(c.to_string());
92  } catch (cloe::Json::type_error& e) {
93  j = {c.res_.filepath(), c.to_string()};
94  }
95 }
96 
97 } // namespace utility
98 } // namespace cloe
99 
100 #endif // CLOE_UTILITY_RESOURCE_HANDLER_HPP_
Definition: handler.hpp:197
friend void to_json(cloe::Json &j, const ResourceLoader &c)
Definition: resource_handler.hpp:89
Definition: handler.hpp:95
Definition: resource_handler.hpp:74
void set_body(const std::string &s, ContentType type)
Definition: handler.hpp:265
ContentType
Definition: handler.hpp:72
Definition: coordinator.hpp:36
Definition: resource.hpp:55
Definition: resource_handler.hpp:47