ndmspc v1.2.0-0.1.rc7
Loading...
Searching...
No Matches
NCloudEvent.cxx
1#include <TString.h>
2#include <THttpCallArg.h>
3#include "NCloudEvent.h"
4
6ClassImp(Ndmspc::NCloudEvent);
8
9namespace Ndmspc {
10
11NCloudEvent::NCloudEvent(std::string id, std::string source, std::string specVersion, std::string type)
12 : TObject(), fId(id), fSource(source), fSpecVersion(specVersion), fType(type)
13{
14}
15NCloudEvent::NCloudEvent(THttpCallArg * arg)
16{
18}
20
22{
23 Clear();
24 if (!arg->GetRequestHeader("Content-Type").CompareTo("application/json")) {
25 fId = arg->GetRequestHeader("Ce-Id").Data();
26 fSource = arg->GetRequestHeader("Ce-Source").Data();
27 fSpecVersion = arg->GetRequestHeader("Ce-Specversion").Data();
28 fType = arg->GetRequestHeader("Ce-Type").Data();
29 }
30 else if (!arg->GetRequestHeader("content-type").CompareTo("application/json")) {
31 fId = arg->GetRequestHeader("ce-id").Data();
32 fSource = arg->GetRequestHeader("ce-source").Data();
33 fSpecVersion = arg->GetRequestHeader("ce-specversion").Data();
34 fType = arg->GetRequestHeader("ce-type").Data();
35 }
36 else if (!arg->GetRequestHeader("Content-Type").CompareTo("application/cloudevents+json") ||
37 !arg->GetRequestHeader("content-type").CompareTo("application/cloudevents+json")) {
38 }
39 else {
40 fIsValid = false;
41 return;
42 }
43 std::string postDataString(static_cast<const char *>(arg->GetPostData()), arg->GetPostDataLength());
44 json j = json::parse(postDataString);
45
46 fData = j.dump().c_str();
47 if (j["datacontenttype"].is_null()) {
48 fDataContentType = "application/json";
49 }
50 else {
51 fDataContentType = j["datacontenttype"].get<std::string>();
52 fData = j["data"].get<std::string>();
53 }
54
55 if (fId.empty() && !j["id"].is_null()) fId = j["id"].get<std::string>();
56 if (fSource.empty() && !j["source"].is_null()) fSource = j["source"].get<std::string>();
57 if (fSpecVersion.empty() && !j["specversion"].is_null()) fSpecVersion = j["specversion"].get<std::string>();
58 if (fType.empty() && !j["type"].is_null()) fType = j["type"].get<std::string>();
59
60 if (fId.empty() || fSource.empty() || fSpecVersion.empty() || fType.empty()) {
61 fIsValid = false;
62 }
63}
64std::string NCloudEvent::GetInfo() const
65{
66
67 return TString::Format("NCloudEvent: id=%s source=%s specVersion=%s type=%s datacontenttype=%s data=%s", fId.c_str(),
68 fSource.c_str(), fSpecVersion.c_str(), fType.c_str(), fDataContentType.c_str(), fData.c_str())
69 .Data();
70}
71void NCloudEvent::Clear(Option_t * /*opt*/)
72{
73 fId.clear();
74 fSource.clear();
75 fSpecVersion.clear();
76 fType.clear();
77 fData.clear();
78 fDataContentType.clear();
79 fIsValid = false;
80}
81void NCloudEvent::Print(Option_t * /*opt*/) const
82{
83 Printf("%s", GetInfo().c_str());
84}
85} // namespace Ndmspc
Represents a CloudEvent object for HTTP communication.
Definition NCloudEvent.h:21
std::string fType
Event type.
void HandleNCloudEventRequest(THttpCallArg *arg)
Handles an HTTP request for a CloudEvent.
std::string fSource
Event source URI-reference.
bool fIsValid
Validity flag for the event.
virtual ~NCloudEvent()
Destroys the NCloudEvent instance.
NCloudEvent(std::string id="0", std::string source="unknown", std::string specVersion="1.0", std::string type="unknown")
Constructs a new NCloudEvent with specified parameters.
std::string GetInfo() const
Gets additional event information.
std::string fDataContentType
Event data content type.
std::string fData
Event data payload.
std::string fId
Event identifier.
std::string fSpecVersion
CloudEvent specification version.
virtual void Clear(Option_t *opt="")
Clears the event data.
virtual void Print(Option_t *opt="") const
Prints the event details.
Global callback function for libwebsockets client events.