Kea  2.1.7-git
isc::http::HttpResponseJson Class Reference

Represents HTTP response with JSON content. More...

#include <response_json.h>

+ Inheritance diagram for isc::http::HttpResponseJson:

Public Member Functions

 HttpResponseJson ()
 Constructor for the inbound HTTP response. More...
 
 HttpResponseJson (const HttpVersion &version, const HttpStatusCode &status_code, const CallSetGenericBody &generic_body=CallSetGenericBody::yes())
 Constructor for the outbound HTTP response. More...
 
virtual void finalize ()
 Completes creation of the HTTP response. More...
 
data::ConstElementPtr getBodyAsJson () const
 Retrieves JSON body. More...
 
data::ConstElementPtr getJsonElement (const std::string &element_name) const
 Retrieves a single JSON element. More...
 
virtual void reset ()
 Reset the state of the object. More...
 
void setBodyAsJson (const data::ConstElementPtr &json_body)
 Generates JSON content from the data structures represented as data::ConstElementPtr. More...
 
- Public Member Functions inherited from isc::http::HttpResponse
 HttpResponse ()
 Constructor for the inbound HTTP response. More...
 
 HttpResponse (const HttpVersion &version, const HttpStatusCode &status_code, const CallSetGenericBody &generic_body=CallSetGenericBody::yes())
 Constructor for outbound HTTP response. More...
 
const HttpResponseContextPtrcontext () const
 Returns pointer to the HttpResponseContext. More...
 
virtual void create ()
 Commits information held in the context into the response. More...
 
virtual std::string getBody () const
 Returns HTTP response body as string. More...
 
virtual std::string getDateHeaderValue () const
 Returns current time formatted as required by RFC 1123. More...
 
data::ConstElementPtr getJsonElement (const std::string &element_name) const
 Retrieves a single JSON element. More...
 
HttpStatusCode getStatusCode () const
 Returns HTTP status code. More...
 
std::string getStatusPhrase () const
 Returns HTTP status phrase. More...
 
std::string toBriefString () const
 Returns HTTP version and HTTP status as a string. More...
 
virtual std::string toString () const
 Returns HTTP response as string. More...
 
- Public Member Functions inherited from isc::http::HttpMessage
 HttpMessage (const Direction &direction)
 Constructor. More...
 
virtual ~HttpMessage ()
 Destructor. More...
 
Direction getDirection () const
 Returns HTTP message direction. More...
 
HttpHeaderPtr getHeader (const std::string &header_name) const
 Returns object encapsulating HTTP header. More...
 
std::string getHeaderValue (const std::string &header_name) const
 Returns a value of the specified HTTP header. More...
 
uint64_t getHeaderValueAsUint64 (const std::string &header_name) const
 Returns a value of the specified HTTP header as number. More...
 
HttpVersion getHttpVersion () const
 Returns HTTP version number (major and minor). More...
 
bool isFinalized () const
 Checks if the message has been successfully finalized. More...
 
void requireHeader (const std::string &header_name)
 Specifies a required HTTP header for the HTTP message. More...
 
void requireHeaderValue (const std::string &header_name, const std::string &header_value)
 Specifies a required value of a header in the message. More...
 
void requireHttpVersion (const HttpVersion &version)
 Specifies HTTP version allowed. More...
 
bool requiresBody () const
 Checks if the body is required for the HTTP message. More...
 
void setDirection (const Direction &direction)
 Sets direction for the HTTP message. More...
 

Protected Member Functions

void parseBodyAsJson ()
 Interprets body as JSON, which can be later retrieved using data element objects. More...
 
- Protected Member Functions inherited from isc::http::HttpMessage
void checkCreated () const
 Checks if the create was called. More...
 
void checkFinalized () const
 Checks if the finalize was called. More...
 
template<typename T >
bool inRequiredSet (const T &element, const std::set< T > &element_set) const
 Checks if the set is empty or the specified element belongs to this set. More...
 

Protected Attributes

data::ConstElementPtr json_
 Pointer to the parsed JSON body. More...
 
- Protected Attributes inherited from isc::http::HttpResponse
HttpResponseContextPtr context_
 Pointer to the HttpResponseContext holding parsed data. More...
 
- Protected Attributes inherited from isc::http::HttpMessage
bool created_
 Flag indicating whether create was called. More...
 
Direction direction_
 Message direction (inbound or outbound). More...
 
bool finalized_
 Flag indicating whether finalize was called. More...
 
HttpHeaderMap headers_
 Parsed HTTP headers. More...
 
HttpVersion http_version_
 HTTP version numbers. More...
 
HttpHeaderMap required_headers_
 Map holding required HTTP headers. More...
 
std::set< HttpVersionrequired_versions_
 Set of required HTTP versions. More...
 

Additional Inherited Members

- Public Types inherited from isc::http::HttpMessage
enum  Direction { INBOUND, OUTBOUND }
 Specifies the direction of the HTTP message. More...
 
- Static Public Member Functions inherited from isc::http::HttpResponse
static bool isClientError (const HttpStatusCode &status_code)
 Checks if the status code indicates client error. More...
 
static bool isServerError (const HttpStatusCode &status_code)
 Checks if the status code indicates server error. More...
 
static uint16_t statusCodeToNumber (const HttpStatusCode &status_code)
 Convenience method converting status code to numeric value. More...
 
static std::string statusCodeToString (const HttpStatusCode &status_code)
 Converts status code to string. More...
 
- Protected Types inherited from isc::http::HttpMessage
typedef std::map< std::string, HttpHeaderPtrHttpHeaderMap
 Map of HTTP headers indexed by lower case header names. More...
 

Detailed Description

Represents HTTP response with JSON content.

This is a specialization of the HttpResponse class which includes "Content-Type" equal to "application/json". It also provides methods to create JSON content within HTTP responses.

Definition at line 34 of file response_json.h.

Constructor & Destructor Documentation

◆ HttpResponseJson() [1/2]

isc::http::HttpResponseJson::HttpResponseJson ( )
explicit

Constructor for the inbound HTTP response.

Definition at line 17 of file response_json.cc.

References isc::http::HttpResponse::context().

+ Here is the call graph for this function:

◆ HttpResponseJson() [2/2]

isc::http::HttpResponseJson::HttpResponseJson ( const HttpVersion version,
const HttpStatusCode status_code,
const CallSetGenericBody generic_body = CallSetGenericBody::yes() 
)
explicit

Constructor for the outbound HTTP response.

Parameters
versionHTTP version.
status_codeHTTP status code.
generic_bodyIndicates if the constructor should call setGenericBody to create a generic content for the given status code. This should be set to "no" when the constructor is called by the derived class which provides its own implementation of the setGenericBody method.

Definition at line 23 of file response_json.cc.

References isc::http::HttpResponse::context(), isc::http::HttpResponse::isClientError(), isc::http::HttpResponse::isServerError(), isc::http::CallSetGenericBody::set_, setBodyAsJson(), isc::http::HttpResponse::statusCodeToNumber(), and isc::http::HttpResponse::statusCodeToString().

+ Here is the call graph for this function:

Member Function Documentation

◆ finalize()

void isc::http::HttpResponseJson::finalize ( )
virtual

Completes creation of the HTTP response.

This method marks the response as finalized. The JSON structure is created and can be used to retrieve the parsed data. If this is the outbound message, it can be transmitted over the wire as the body for the message is now committed.

Reimplemented from isc::http::HttpResponse.

Definition at line 55 of file response_json.cc.

References isc::http::HttpResponse::create(), isc::http::HttpMessage::created_, isc::http::HttpMessage::finalized_, and parseBodyAsJson().

+ Here is the call graph for this function:

◆ getBodyAsJson()

ConstElementPtr isc::http::HttpResponseJson::getBodyAsJson ( ) const

Retrieves JSON body.

Returns
Pointer to the root element of the JSON structure.
Exceptions
HttpRequestJsonErrorif an error occurred.

Definition at line 72 of file response_json.cc.

References isc::http::HttpMessage::checkFinalized(), and json_.

Referenced by getJsonElement().

+ Here is the call graph for this function:

◆ getJsonElement()

ConstElementPtr isc::http::HttpResponseJson::getJsonElement ( const std::string &  element_name) const

Retrieves a single JSON element.

The element must be at top level of the JSON structure.

Parameters
element_nameElement name.
Returns
Pointer to the specified element or NULL if such element doesn't exist.
Exceptions
HttpRequestJsonErrorif an error occurred.

Definition at line 90 of file response_json.cc.

References getBodyAsJson(), isc_throw, and isc::Exception::what().

+ Here is the call graph for this function:

◆ parseBodyAsJson()

void isc::http::HttpResponseJson::parseBodyAsJson ( )
protected

Interprets body as JSON, which can be later retrieved using data element objects.

Definition at line 109 of file response_json.cc.

References isc::http::HttpResponse::context_, isc_throw, json_, and isc::Exception::what().

Referenced by finalize().

+ Here is the call graph for this function:

◆ reset()

void isc::http::HttpResponseJson::reset ( )
virtual

Reset the state of the object.

Reimplemented from isc::http::HttpResponse.

Definition at line 66 of file response_json.cc.

References json_, and isc::http::HttpResponse::reset().

+ Here is the call graph for this function:

◆ setBodyAsJson()

void isc::http::HttpResponseJson::setBodyAsJson ( const data::ConstElementPtr json_body)

Generates JSON content from the data structures represented as data::ConstElementPtr.

Parameters
json_bodyA data structure representing JSON content.

Definition at line 78 of file response_json.cc.

References isc::http::HttpResponse::context(), and json_.

Referenced by isc::agent::CtrlAgentResponseCreator::createStockHttpResponse(), isc::config::CmdResponseCreator::createStockHttpResponse(), and HttpResponseJson().

+ Here is the call graph for this function:

Member Data Documentation

◆ json_

data::ConstElementPtr isc::http::HttpResponseJson::json_
protected

Pointer to the parsed JSON body.

Definition at line 108 of file response_json.h.

Referenced by getBodyAsJson(), parseBodyAsJson(), reset(), and setBodyAsJson().


The documentation for this class was generated from the following files: