Kea 2.5.8
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
10#include <map>
11
12using namespace isc::data;
13
14namespace isc {
15namespace http {
16
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
37void
38HttpResponseJson::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
54void
56 if (!created_) {
57 create();
58 }
59
60 // Parse JSON body and store.
62 finalized_ = true;
63}
64
65void
68 json_.reset();
69}
70
74 return (json_);
75}
76
77void
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
90HttpResponseJson::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
108void
110 try {
111 // Only parse the body if it hasn't been parsed yet.
112 if (!json_ && !context_->body_.empty()) {
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
int version()
returns Kea hooks version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition: data.cc:798
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
Notes: IntElement type is changed to int64_t.
Definition: data.h:615
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
bool created_
Flag indicating whether create was called.
Definition: http_message.h:253
bool finalized_
Flag indicating whether finalize was called.
Definition: http_message.h:256
Exception thrown when body of the HTTP message is not JSON.
Definition: response_json.h:18
void setBodyAsJson(const data::ConstElementPtr &json_body)
Generates JSON content from the data structures represented as data::ConstElementPtr.
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects.
data::ConstElementPtr json_
Pointer to the parsed JSON body.
HttpResponseJson()
Constructor for the inbound HTTP response.
virtual void finalize()
Completes creation of the HTTP response.
virtual void reset()
Reset the state of the object.
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
Represents HTTP response message.
Definition: response.h:98
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition: response.h:241
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition: response.cc:184
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition: response.cc:159
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition: response.cc:166
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition: response.h:129
virtual void reset()
Reset the state of the object.
Definition: response.cc:134
virtual void create()
Commits information held in the context into the response.
Definition: response.cc:69
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition: response.cc:173
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition: response.h:30
Defines the logger used by the top-level component of kea-lfc.
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
Definition: response.h:52
bool set_
A storage for the boolean flag.
Definition: response.h:75
HTTP header context.
HTTP protocol version.
Definition: http_types.h:14