bes Updated for version 3.20.13
rjson_utils.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2//
3// This file is part of cmr_module, A C++ module that can be loaded in to
4// the OPeNDAP Back-End Server (BES) and is able to handle remote requests.
5//
6// Copyright (c) 2018 OPeNDAP, Inc.
7// Author: Nathan Potter <ndp@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24//
25#include <sstream>
26
27#include "rapidjson/document.h"
28#include "rapidjson/writer.h"
29#include "rapidjson/prettywriter.h"
30#include "rapidjson/stringbuffer.h"
31#include "rapidjson/filereadstream.h"
32
33#include <BESError.h>
34#include <BESDebug.h>
35#include <BESUtil.h>
36#include "RemoteResource.h"
37
38#include "CmrNames.h"
39
40#include "rjson_utils.h"
41
42using namespace std;
43
44#define prolog std::string("rjson_utils::").append(__func__).append("() - ")
45
46namespace cmr {
56void
57rjson_utils::getJsonDoc(const string &url, rapidjson::Document &doc){
58 BESDEBUG(MODULE,prolog << "Trying url: " << url << endl);
59 shared_ptr<http::url> target_url(new http::url(url));
60 http::RemoteResource rhr(target_url);
61 rhr.retrieveResource();
62 if(BESDebug::IsSet(MODULE)){
63 string cmr_hits = rhr.get_http_response_header("cmr-hits");
64 stringstream msg(prolog);
65 msg << "CMR-Hits: "<< cmr_hits << endl;
66 *(BESDebug::GetStrm()) << msg.str();
67 }
68 FILE* fp = fopen(rhr.getCacheFileName().c_str(), "r"); // non-Windows use "r"
69 char readBuffer[65536];
70 rapidjson::FileReadStream frs(fp, readBuffer, sizeof(readBuffer));
71 doc.ParseStream(frs);
72 fclose(fp);
73}
74
75
84std::string
85rjson_utils::getStringValue(const rapidjson::Value& object, const string &name){
86
87 rapidjson::Value::ConstMemberIterator itr = object.FindMember(name.c_str());
88 bool result = itr != object.MemberEnd();
89
90 BESDEBUG(MODULE, prolog + (result?"Located":"FAILED to locate") + " the value '"+name+"' in object." << endl);
91 if(!result){
92 return "";
93 }
94 const rapidjson::Value& myValue = itr->value;
95 result = myValue.IsString();
96 BESDEBUG(MODULE, prolog + "The value of '"+ name +"' is " + (result?myValue.GetString():" NOT a String type.") << endl);
97 if(!result){
98 return "";
99 }
100 return myValue.GetString();
101}
102
103
110std::string
111rjson_utils::jsonDocToString(rapidjson::Document &d){
112 rapidjson::StringBuffer buffer;
113 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
114 d.Accept(writer);
115 return buffer.GetString();
116}
117
118
119} // namespace cmr
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:187
(Constant) member iterator for a JSON object value
Definition: document.h:177
std::string jsonDocToString(rapidjson::Document &d)
Definition: rjson_utils.cc:111
void getJsonDoc(const std::string &url, rapidjson::Document &d)
Definition: rjson_utils.cc:57
std::string getStringValue(const rapidjson::Value &object, const std::string &name)
Definition: rjson_utils.cc:85
std::string getCacheFileName()
std::string get_http_response_header(const std::string header_name)