Kea  1.9.9-git
response_json.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <http/response_json.h>
10 #include <map>
11 
12 using namespace isc::data;
13 
14 namespace isc {
15 namespace http {
16 
17 HttpResponseJson::HttpResponseJson()
18  : HttpResponse() {
19  context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
20 }
21 
22 
24  const HttpStatusCode& status_code,
25  const CallSetGenericBody& generic_body)
26  : HttpResponse(version, status_code, CallSetGenericBody::no()) {
27  context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
28  // This class provides its own implementation of the setGenericBody.
29  // We call it here unless the derived class calls this constructor
30  // from its own constructor and indicates that we shouldn't set the
31  // generic content in the body.
32  if (generic_body.set_) {
33  setGenericBody(status_code);
34  }
35 }
36 
37 void
38 HttpResponseJson::setGenericBody(const HttpStatusCode& status_code) {
39  // Only generate the content for the client or server errors. For
40  // other status codes (status OK in particular) the content should
41  // be created using setBodyAsJson or setBody.
42  if (isClientError(status_code) || isServerError(status_code)) {
43  std::map<std::string, ConstElementPtr> map_elements;
44  map_elements["result"] =
46  map_elements["text"] =
48  auto body = Element::createMap();
49  body->setValue(map_elements);
50  setBodyAsJson(body);
51  }
52 }
53 
54 void
56  if (!created_) {
57  create();
58  }
59 
60  // Parse JSON body and store.
62  finalized_ = true;
63 }
64 
65 void
68  json_.reset();
69 }
70 
74  return (json_);
75 }
76 
77 void
79  if (json_body) {
80  context()->body_ = json_body->str();
81 
82  } else {
83  context()->body_.clear();
84  }
85 
86  json_ = json_body;
87 }
88 
90 HttpResponseJson::getJsonElement(const std::string& element_name) const {
91  try {
93  if (body) {
94  const std::map<std::string, ConstElementPtr>& map_value = body->mapValue();
95  auto map_element = map_value.find(element_name);
96  if (map_element != map_value.end()) {
97  return (map_element->second);
98  }
99  }
100 
101  } catch (const std::exception& ex) {
102  isc_throw(HttpResponseJsonError, "unable to get JSON element "
103  << element_name << ": " << ex.what());
104  }
105  return (ConstElementPtr());
106 }
107 
108 void
110  try {
111  // Only parse the body if it hasn't been parsed yet.
112  if (!json_ && !context_->body_.empty()) {
113  json_ = Element::fromJSON(context_->body_);
114  }
115  } catch (const std::exception& ex) {
116  isc_throw(HttpResponseJsonError, "unable to parse the body of the HTTP"
117  " response: " << ex.what());
118  }
119 }
120 
121 } // namespace http
122 } // namespace isc
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition: response.cc:163
bool finalized_
Flag indicating whether finalize was called.
Definition: http_message.h:256
HTTP protocol version.
Definition: http_types.h:14
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition: response.h:129
data::ConstElementPtr json_
Pointer to the parsed JSON body.
virtual void create()
Commits information held in the context into the response.
Definition: response.cc:69
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool created_
Flag indicating whether create was called.
Definition: http_message.h:253
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition: response.h:237
Represents HTTP response message.
Definition: response.h:98
Notes: IntElement type is changed to int64_t.
Definition: data.h:544
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
int version()
returns Kea hooks version.
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition: response.cc:170
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition: response.cc:177
Defines the logger used by the top-level component of kea-dhcp-ddns.
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
virtual void reset()
Reset the state of the object.
Definition: response.cc:138
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects. ...
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition: response.cc:188
Exception thrown when body of the HTTP message is not JSON.
Definition: response_json.h:18
virtual void finalize()
Completes creation of the HTTP response.
HTTP header context.
void setBodyAsJson(const data::ConstElementPtr &json_body)
Generates JSON content from the data structures represented as data::ConstElementPtr.
bool set_
A storage for the boolean flag.
Definition: response.h:75
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
Definition: response.h:52
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30
HttpResponseJson()
Constructor for the inbound HTTP response.
virtual void reset()
Reset the state of the object.