Kea  1.5.0
post_request_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 
11 using namespace isc::data;
12 
13 namespace isc {
14 namespace http {
15 
16 PostHttpRequestJson::PostHttpRequestJson()
17  : PostHttpRequest(), json_() {
18  requireHeaderValue("Content-Type", "application/json");
19 }
20 
21 PostHttpRequestJson::PostHttpRequestJson(const Method& method, const std::string& uri,
22  const HttpVersion& version)
23  : PostHttpRequest(method, uri, version) {
24  requireHeaderValue("Content-Type", "application/json");
25  context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
26 }
27 
28 
29 void
31  if (!created_) {
32  create();
33  }
34 
35  // Parse JSON body and store.
37  finalized_ = true;
38 }
39 
40 void
43  json_.reset();
44 }
45 
49  return (json_);
50 }
51 
52 void
54  if (body) {
55  context_->body_ = body->str();
56  json_ = body;
57 
58  } else {
59  context_->body_.clear();
60  }
61 }
62 
64 PostHttpRequestJson::getJsonElement(const std::string& element_name) const {
65  try {
67  if (body) {
68  const std::map<std::string, ConstElementPtr>& map_value = body->mapValue();
69  auto map_element = map_value.find(element_name);
70  if (map_element != map_value.end()) {
71  return (map_element->second);
72  }
73  }
74 
75  } catch (const std::exception& ex) {
76  isc_throw(HttpRequestJsonError, "unable to get JSON element "
77  << element_name << ": " << ex.what());
78  }
79  return (ConstElementPtr());
80 }
81 
82 void
84  try {
85  // Only parse the body if it hasn't been parsed yet.
86  if (!json_ && !context_->body_.empty()) {
87  json_ = Element::fromJSON(context_->body_);
88  }
89  } catch (const std::exception& ex) {
90  isc_throw(HttpRequestJsonError, "unable to parse the body of the HTTP"
91  " request: " << ex.what());
92  }
93 }
94 
95 } // namespace http
96 } // namespace isc
isc::http::HttpHeaderContext
HTTP header context.
Definition: header_context.h:18
isc::http::HttpMessage::checkFinalized
void checkFinalized() const
Checks if the finalize was called.
Definition: http_message.cc:99
isc::http::HttpRequest::reset
virtual void reset()
Reset the state of the object.
Definition: request.cc:126
version
int version()
returns Kea hooks version.
Definition: high_availability/version.cc:13
isc::http::HttpRequestJsonError
Exception thrown when body of the HTTP message is not JSON.
Definition: post_request_json.h:20
post_request_json.h
isc::http::PostHttpRequestJson::json_
data::ConstElementPtr json_
Pointer to the parsed JSON body.
Definition: post_request_json.h:99
isc::http::HttpRequest::context_
HttpRequestContextPtr context_
Pointer to the HttpRequestContext holding parsed data.
Definition: request.h:171
isc::http::PostHttpRequest
Represents HTTP POST request.
Definition: post_request.h:29
isc::http::PostHttpRequestJson::setBodyAsJson
void setBodyAsJson(const data::ConstElementPtr &body)
Sets JSON body for an outbound message.
Definition: post_request_json.cc:53
isc::data
Definition: cfg_to_element.h:25
isc::http::HttpVersion
HTTP protocol version.
Definition: http_types.h:14
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::http::PostHttpRequestJson::parseBodyAsJson
void parseBodyAsJson()
Interprets body as JSON, which can be later retrieved using data element objects.
Definition: post_request_json.cc:83
isc::http::PostHttpRequestJson::getJsonElement
data::ConstElementPtr getJsonElement(const std::string &element_name) const
Retrieves a single JSON element.
Definition: post_request_json.cc:64
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::HttpRequest::context
const HttpRequestContextPtr & context() const
Returns pointer to the HttpRequestContext.
Definition: request.h:80
isc::http::PostHttpRequestJson::finalize
virtual void finalize()
Complete parsing of the HTTP request.
Definition: post_request_json.cc:30
isc::http::PostHttpRequestJson::getBodyAsJson
data::ConstElementPtr getBodyAsJson() const
Retrieves JSON body.
Definition: post_request_json.cc:47
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::http::HttpMessage::requireHeaderValue
void requireHeaderValue(const std::string &header_name, const std::string &header_value)
Specifies a required value of a header in the message.
Definition: http_message.cc:37
isc::http::HttpRequest::create
virtual void create()
Commits information held in the context into the request.
Definition: request.cc:47
isc::http::PostHttpRequestJson::PostHttpRequestJson
PostHttpRequestJson()
Constructor for inbound HTTP request.
Definition: post_request_json.cc:16
isc::http::HttpRequest::Method
Method
HTTP methods.
Definition: request.h:52
isc::http::PostHttpRequestJson::reset
virtual void reset()
Reset the state of the object.
Definition: post_request_json.cc:41