Kea: response_json.cc Source File

| Kea 3.0.2-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
12using namespace isc::data;
13
14namespace isc {
15namespace http {
16
17HttpResponseJson::HttpResponseJson()
18 : HttpResponse() {
19 context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
20}
21
22
23 HttpResponseJson::HttpResponseJson(const HttpVersion& version,
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"] =
45 ConstElementPtr(new IntElement(statusCodeToNumber(status_code)));
46 map_elements["text"] =
47 ConstElementPtr(new StringElement(statusCodeToString(status_code)));
48 auto body = Element::createMap();
49 body->setValue(map_elements);
50 setBodyAsJson(body);
51 }
52}
53
54void
55HttpResponseJson::finalize() {
56 if (!created_) {
57 create();
58 }
59
60 // Parse JSON body and store.
61 parseBodyAsJson();
62 finalized_ = true;
63}
64
65void
66HttpResponseJson::reset() {
67 HttpResponse::reset();
68 json_.reset();
69}
70
71 ConstElementPtr
72HttpResponseJson::getBodyAsJson() const {
73 checkFinalized();
74 return (json_);
75}
76
77void
78HttpResponseJson::setBodyAsJson(const ConstElementPtr& json_body) {
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
89 ConstElementPtr
90HttpResponseJson::getJsonElement(const std::string& element_name) const {
91 try {
92 ConstElementPtr body = getBodyAsJson();
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
109HttpResponseJson::parseBodyAsJson() {
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
Element::fromJSON
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
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::data::IntElement
Notes: IntElement type is changed to int64_t.
Definition data.h:615
isc::data::StringElement
Definition data.h:709
isc::http::HttpMessage::checkFinalized
void checkFinalized() const
Checks if the finalize was called.
Definition http_message.cc:99
isc::http::HttpMessage::created_
bool created_
Flag indicating whether create was called.
Definition http_message.h:253
isc::http::HttpMessage::finalized_
bool finalized_
Flag indicating whether finalize was called.
Definition http_message.h:256
isc::http::HttpResponseJsonError
Exception thrown when body of the HTTP message is not JSON.
Definition response_json.h:18
isc::http::HttpResponseJson::setBodyAsJson
void setBodyAsJson(const data::ConstElementPtr &json_body)
Generates JSON content from the data structures represented as data::ConstElementPtr.
Definition response_json.cc:78
isc::http::HttpResponseJson::parseBodyAsJson
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects.
Definition response_json.cc:109
isc::http::HttpResponseJson::json_
data::ConstElementPtr json_
Pointer to the parsed JSON body.
Definition response_json.h:108
isc::http::HttpResponseJson::HttpResponseJson
HttpResponseJson()
Constructor for the inbound HTTP response.
Definition response_json.cc:17
isc::http::HttpResponseJson::finalize
virtual void finalize()
Completes creation of the HTTP response.
Definition response_json.cc:55
isc::http::HttpResponseJson::reset
virtual void reset()
Reset the state of the object.
Definition response_json.cc:66
isc::http::HttpResponseJson::getJsonElement
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
Definition response_json.cc:90
isc::http::HttpResponseJson::getBodyAsJson
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
Definition response_json.cc:72
isc::http::HttpResponse::context_
HttpResponseContextPtr context_
Pointer to the HttpResponseContext holding parsed data.
Definition response.h:241
isc::http::HttpResponse::HttpResponse
HttpResponse()
Constructor for the inbound HTTP response.
Definition response.cc:49
isc::http::HttpResponse::statusCodeToNumber
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition response.cc:184
isc::http::HttpResponse::isClientError
static bool isClientError(const HttpStatusCode &status_code)
Checks if the status code indicates client error.
Definition response.cc:159
isc::http::HttpResponse::isServerError
static bool isServerError(const HttpStatusCode &status_code)
Checks if the status code indicates server error.
Definition response.cc:166
isc::http::HttpResponse::context
const HttpResponseContextPtr & context() const
Returns pointer to the HttpResponseContext.
Definition response.h:129
isc::http::HttpResponse::reset
virtual void reset()
Reset the state of the object.
Definition response.cc:134
isc::http::HttpResponse::create
virtual void create()
Commits information held in the context into the response.
Definition response.cc:69
isc::http::HttpResponse::statusCodeToString
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition response.cc:173
version
int version()
returns Kea hooks version.
Definition d2/gss_tsig/version.cc:13
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::http
Definition auth_config.h:19
isc::http::HttpStatusCode
HttpStatusCode
HTTP status codes (cf RFC 2068).
Definition response.h:30
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
response_json.h
isc::http::CallSetGenericBody
Encapsulates the boolean value indicating if the HttpResponse constructor should call its setGenericB...
Definition response.h:52
isc::http::CallSetGenericBody::set_
bool set_
A storage for the boolean flag.
Definition response.h:75
isc::http::HttpHeaderContext
HTTP header context.
Definition header_context.h:19
isc::http::HttpVersion
HTTP protocol version.
Definition http_types.h:14