summaryrefslogtreecommitdiffstats
path: root/src/lib/http/post_request_json.cc
blob: c871dbbca09126f0b6af519db86d03dabc7abad3 (plain)
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
// Copyright (C) 2016-2017 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/.

#include <http/post_request_json.h>

using namespace isc::data;

namespace isc {
namespace http {

PostHttpRequestJson::PostHttpRequestJson()
    : PostHttpRequest(), json_() {
    requireHeaderValue("Content-Type", "application/json");
}

void
PostHttpRequestJson::finalize() {
    if (!created_) {
        create();
    }

    // Parse JSON body and store.
    parseBodyAsJson();
    finalized_ = true;
}

void
PostHttpRequestJson::reset() {
    PostHttpRequest::reset();
    json_.reset();
}

ConstElementPtr
PostHttpRequestJson::getBodyAsJson() const {
    checkFinalized();
    return (json_);
}

ConstElementPtr
PostHttpRequestJson::getJsonElement(const std::string& element_name) const {
    try {
        ConstElementPtr body = getBodyAsJson();
        if (body) {
            const std::map<std::string, ConstElementPtr>& map_value = body->mapValue();
            auto map_element = map_value.find(element_name);
            if (map_element != map_value.end()) {
                return (map_element->second);
            }
        }

    } catch (const std::exception& ex) {
        isc_throw(HttpRequestJsonError, "unable to get JSON element "
                  << element_name << ": " << ex.what());
    }
    return (ConstElementPtr());
}

void
PostHttpRequestJson::parseBodyAsJson() {
   try {
       // Only parse the body if it hasn't been parsed yet.
       if (!json_ && !context_->body_.empty()) {
           json_ = Element::fromJSON(context_->body_);
       }
    } catch (const std::exception& ex) {
        isc_throw(HttpRequestJsonError, "unable to parse the body of the HTTP"
                  " request: " << ex.what());
    }
}

} // namespace http
} // namespace isc