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

Protected Member Functions

void parseBodyAsJson ()
 Interprets body as JSON, which can be later retrieved using data element objects.
 
- Protected Member Functions inherited from isc::http::HttpMessage
void checkCreated () const
 Checks if the create was called.
 
void checkFinalized () const
 Checks if the finalize was called.
 
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.
 

Protected Attributes

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

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.
 
static bool isServerError (const HttpStatusCode &status_code)
 Checks if the status code indicates server error.
 
static uint16_t statusCodeToNumber (const HttpStatusCode &status_code)
 Convenience method converting status code to numeric value.
 
static std::string statusCodeToString (const HttpStatusCode &status_code)
 Converts status code to string.
 
- Protected Types inherited from isc::http::HttpMessage
typedef std::map< std::string, HttpHeaderPtrHttpHeaderMap
 Map of HTTP headers indexed by lower case header names.
 

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().

+ 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::data::Element::fromJSON(), 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_.

+ 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: