1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HTTP_RESPONSE_JSON_H
#define HTTP_RESPONSE_JSON_H

#include <cc/data.h>
#include <exceptions/exceptions.h>
#include <http/response.h>

namespace isc {
namespace http {

/// @brief Exception thrown when body of the HTTP message is not JSON.
class HttpResponseJsonError : public HttpResponseError {
public:
    HttpResponseJsonError(const char* file, size_t line, const char* what) :
        HttpResponseError(file, line, what) { };
};

class HttpResponseJson;

/// @brief Pointer to the @ref HttpResponseJson object.
typedef boost::shared_ptr<HttpResponseJson> HttpResponseJsonPtr;

/// @brief Represents HTTP response with JSON content.
///
/// This is a specialization of the @ref HttpResponse class which
/// includes "Content-Type" equal to "application/json". It also provides
/// methods to create JSON content within HTTP responses.
class HttpResponseJson : public HttpResponse {
public:

    /// @brief Constructor for the inbound HTTP response.
    explicit HttpResponseJson();

    /// @brief Constructor for the outbound HTTP response.
    ///
    /// @param version HTTP version.
    /// @param status_code HTTP status code.
    /// @param generic_body Indicates if the constructor should call
    /// @c 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 @c setGenericBody method.
    explicit HttpResponseJson(const HttpVersion& version,
                              const HttpStatusCode& status_code,
                              const CallSetGenericBody& generic_body =
                              CallSetGenericBody::yes());

    /// @brief 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.
    virtual void finalize();<--- Function in derived class

    /// @brief Reset the state of the object.
    virtual void reset();<--- Function in derived class

    /// @brief Retrieves JSON body.
    ///
    /// @return Pointer to the root element of the JSON structure.
    /// @throw HttpRequestJsonError if an error occurred.
    data::ConstElementPtr getBodyAsJson() const;

    /// @brief Generates JSON content from the data structures represented
    /// as @ref data::ConstElementPtr.
    ///
    /// @param json_body A data structure representing JSON content.
    void setBodyAsJson(const data::ConstElementPtr& json_body);

    /// @brief Retrieves a single JSON element.
    ///
    /// The element must be at top level of the JSON structure.
    ///
    /// @param element_name Element name.
    ///
    /// @return Pointer to the specified element or NULL if such element
    /// doesn't exist.
    /// @throw HttpRequestJsonError if an error occurred.
    data::ConstElementPtr getJsonElement(const std::string& element_name) const;<--- Derived function 'HttpResponseJson::getJsonElement'

private:

    /// @brief Sets generic body for the given status code.
    ///
    /// This method generates JSON content for the HTTP client and server
    /// errors. The generated JSON structure is a map containing "result"
    /// value holding HTTP status code (e.g. 400) and the "text" string
    /// holding a status code description.
    ///
    /// @param status_code Status code for which the body should be
    /// generated.
    void setGenericBody(const HttpStatusCode& status_code);

protected:

    /// @brief Interprets body as JSON, which can be later retrieved using
    /// data element objects.
    void parseBodyAsJson();

    /// @brief Pointer to the parsed JSON body.
    data::ConstElementPtr json_;
};

} // end of namespace isc::http
} // end of namespace isc

#endif