EpcTools
An event based multi-threaded C++ development framework.
emgmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2019 Sprint
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 
17 #ifndef __EMGMT_H
18 #define __EMGMT_H
19 
22 
23 #include <iostream>
24 #include <pistache/endpoint.h>
25 #include <pistache/http_header.h>
26 #include <pistache/router.h>
27 
28 #define RAPIDJSON_NAMESPACE epctoolsrapidjson
29 
30 #include "rapidjson/rapidjson.h"
31 #include "rapidjson/document.h"
32 
33 #include "elogger.h"
34 #include "estring.h"
35 #include "etime.h"
36 
38 class EManagementUserNameHeader : public Pistache::Http::Header::Header
39 {
40 public:
41  NAME("X-User-Name")
42 
43 
45  {
46  }
47 
50  void parse(const std::string &data)
51  {
52  m_username = data;
53  }
54 
57  void write(std::ostream &os) const
58  {
59  os << m_username;
60  }
61 
63  EString &getUserName() { return m_username; }
64 
65 private:
66  EString m_username;
67 };
68 
71 {
72  friend class EManagementEndpoint;
73 
74 public:
76  enum class HttpMethod
77  {
79  httpGet,
81  httpPost,
83  httpPut,
85  httpDelete,
87  httpPatch,
88  httpOptions
90  };
91 
96  EManagementHandler(HttpMethod mthd, cpStr pth, ELogger &audit);
101  EManagementHandler(HttpMethod mthd, const std::string &pth, ELogger &audit);
102 
106  virtual Void process(const Pistache::Http::Request& request, Pistache::Http::ResponseWriter &response) = 0;
107 
111  Void handler(const Pistache::Http::Request& request, Pistache::Http::ResponseWriter response);
112 
114  const EString &path() { return m_path; }
116  HttpMethod httpMethod() { return m_method; }
117 
118 protected:
119  Pistache::Rest::Route::Handler getHandler();
120 
121 private:
123 
124  EString m_path;
125  HttpMethod m_method;
126  ELogger &m_audit;
127 };
128 
131 {
132 public:
136  EManagementEndpoint(uint16_t port, size_t thrds=1);
140  EManagementEndpoint(Pistache::Address &addr, size_t thrds=1);
141 
143  Void start();
145  Void shutdown();
146 
148  Void registerHandler(EManagementHandler &hndlr);
149 
150 private:
151  Void init(size_t thrds);
152 
153  std::shared_ptr<Pistache::Http::Endpoint> m_endpoint;
154  Pistache::Rest::Router m_router;
155 
156  static Bool m_username_header_registered;
157 };
158 
159 #endif // #ifndef __EMGMT_H
EManagementEndpoint
Implemts the HTTP server endpoint.
Definition: emgmt.h:130
EManagementHandler::HttpMethod::httpPut
HTTP PUT.
EManagementUserNameHeader
Custom HTTP header class for the X-User-Name header.
Definition: emgmt.h:38
EManagementHandler::handler
Void handler(const Pistache::Http::Request &request, Pistache::Http::ResponseWriter response)
HTTP handler that will be called by the Pistache framework.
Definition: emgmt.cpp:34
EManagementHandler::HttpMethod::httpDelete
HTTP DELETE.
EManagementHandler::path
const EString & path()
Returns the route path for this HTTP handler.
Definition: emgmt.h:114
elogger.h
Defines the logging related classes.
EManagementHandler::HttpMethod::httpPost
HTTP POST.
EManagementEndpoint::start
Void start()
Starts the endpoint.
Definition: emgmt.cpp:86
EManagementUserNameHeader::getUserName
EString & getUserName()
Returns the value of the user name.
Definition: emgmt.h:63
EManagementUserNameHeader::write
void write(std::ostream &os) const
Serializes the user name to the output stream.
Definition: emgmt.h:57
EManagementHandler::httpMethod
HttpMethod httpMethod()
Returns the HTTP method for this HTTP handler.
Definition: emgmt.h:116
etime.h
Provides class for manipulating time of day values.
ELogger
Defines a logger.
Definition: elogger.h:78
EManagementHandler::getHandler
Pistache::Rest::Route::Handler getHandler()
Definition: emgmt.cpp:63
EManagementEndpoint::registerHandler
Void registerHandler(EManagementHandler &hndlr)
Registers a REST handler.
Definition: emgmt.cpp:103
EManagementHandler::HttpMethod::httpGet
HTTP GET.
EManagementHandler::process
virtual Void process(const Pistache::Http::Request &request, Pistache::Http::ResponseWriter &response)=0
Pure virtual method that will be called by handler() to perform the processing.
EManagementHandler::HttpMethod
HttpMethod
Represents the type of the handler.
Definition: emgmt.h:76
EManagementHandler
Pure virtual base class for an administrative management interface handler.
Definition: emgmt.h:70
EString
String class.
Definition: estring.h:30
EManagementEndpoint::shutdown
Void shutdown()
Stops and shuts down the endpoint.
Definition: emgmt.cpp:98
estring.h
Encapsulates and extends a std::string object.
EManagementUserNameHeader::parse
void parse(const std::string &data)
Parses the user name from the buffer.
Definition: emgmt.h:50
EManagementEndpoint::EManagementEndpoint
EManagementEndpoint(uint16_t port, size_t thrds=1)
Class constructor.
Definition: emgmt.cpp:73