ndmspc  v1.2.0-0.1.rc3
NCloudEvent.cxx
1 #include <TString.h>
2 #include <THttpCallArg.h>
3 #include "NCloudEvent.h"
4 
6 ClassImp(Ndmspc::NCloudEvent);
8 
9 namespace Ndmspc {
10 
11 NCloudEvent::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 }
15 NCloudEvent::NCloudEvent(THttpCallArg * arg)
16 {
18 }
20 
21 void NCloudEvent::HandleNCloudEventRequest(THttpCallArg * arg)
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 }
64 std::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 }
71 void 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 }
81 void 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.
Definition: NCloudEvent.h:151
void HandleNCloudEventRequest(THttpCallArg *arg)
Handles an HTTP request for a CloudEvent.
Definition: NCloudEvent.cxx:21
std::string fSource
Event source URI-reference.
Definition: NCloudEvent.h:149
bool fIsValid
Validity flag for the event.
Definition: NCloudEvent.h:147
virtual ~NCloudEvent()
Destroys the NCloudEvent instance.
Definition: NCloudEvent.cxx:19
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.
Definition: NCloudEvent.cxx:11
std::string GetInfo() const
Gets additional event information.
Definition: NCloudEvent.cxx:64
std::string fDataContentType
Event data content type.
Definition: NCloudEvent.h:152
std::string fData
Event data payload.
Definition: NCloudEvent.h:153
std::string fId
Event identifier.
Definition: NCloudEvent.h:148
std::string fSpecVersion
CloudEvent specification version.
Definition: NCloudEvent.h:150
virtual void Clear(Option_t *opt="")
Clears the event data.
Definition: NCloudEvent.cxx:71
virtual void Print(Option_t *opt="") const
Prints the event details.
Definition: NCloudEvent.cxx:81