bes  Updated for version 3.20.6
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 "RemoteHttpResource.h"
37 
38 #include "CmrNames.h"
39 
40 #include "rjson_utils.h"
41 
42 using namespace std;
43 
44 #define prolog std::string("rjson_utils::").append(__func__).append("() - ")
45 
46 namespace cmr {
56 void
57 rjson_utils::getJsonDoc(const string &url, rapidjson::Document &doc){
58  BESDEBUG(MODULE,prolog << "Trying url: " << url << endl);
59  cmr::RemoteHttpResource rhr(url);
60  rhr.retrieveResource();
61  if(BESDebug::IsSet(MODULE)){
62  string cmr_hits = rhr.get_http_response_header("cmr-hits");
63  stringstream msg(prolog);
64  msg << "CMR-Hits: "<< cmr_hits << endl;
65  *(BESDebug::GetStrm()) << msg.str();
66  }
67  FILE* fp = fopen(rhr.getCacheFileName().c_str(), "r"); // non-Windows use "r"
68  char readBuffer[65536];
69  rapidjson::FileReadStream frs(fp, readBuffer, sizeof(readBuffer));
70  doc.ParseStream(frs);
71  fclose(fp);
72 }
73 
74 
83 std::string
84 rjson_utils::getStringValue(const rapidjson::Value& object, const string &name){
85 
86  rapidjson::Value::ConstMemberIterator itr = object.FindMember(name.c_str());
87  bool result = itr != object.MemberEnd();
88 
89  BESDEBUG(MODULE, prolog + (result?"Located":"FAILED to locate") + " the value '"+name+"' in object." << endl);
90  if(!result){
91  return "";
92  }
93  const rapidjson::Value& myValue = itr->value;
94  result = myValue.IsString();
95  BESDEBUG(MODULE, prolog + "The value of '"+ name +"' is " + (result?myValue.GetString():" NOT a String type.") << endl);
96  if(!result){
97  return "";
98  }
99  return myValue.GetString();
100 }
101 
102 
109 std::string
110 rjson_utils::jsonDocToString(rapidjson::Document &d){
111  rapidjson::StringBuffer buffer;
112  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
113  d.Accept(writer);
114  return buffer.GetString();
115 }
116 
117 
118 } // namespace cmr
GenericMemberIterator
(Constant) member iterator for a JSON object value
Definition: cmr_module/rapidjson/document.h:101
cmr::RemoteHttpResource
Definition: cmr_module/RemoteHttpResource.h:49
Document
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: cmr_module/rapidjson/document.h:2402
cmr::RemoteHttpResource::getCacheFileName
std::string getCacheFileName()
Definition: cmr_module/RemoteHttpResource.h:121
cmr::RemoteHttpResource::get_http_response_header
std::string get_http_response_header(const std::string header_name)
Definition: cmr_module/RemoteHttpResource.cc:384
BESDebug::IsSet
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:157
Value
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: cmr_module/rapidjson/document.h:2010
BESDebug::GetStrm
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:176
cmr::RemoteHttpResource::retrieveResource
void retrieveResource()
Definition: cmr_module/RemoteHttpResource.cc:130