39 #include "BESStopWatch.h"
41 #include "BESSyntaxUserError.h"
42 #include "BESNotFoundError.h"
43 #include "BESInternalError.h"
46 #include "TheBESKeys.h"
47 #include "AllowedHosts.h"
48 #include "BESContextManager.h"
49 #include "CurlUtils.h"
50 #include "HttpUtils.h"
51 #include "RemoteResource.h"
54 #include "NgapContainer.h"
56 #include "NgapNames.h"
58 #define prolog std::string("NgapContainer::").append(__func__).append("() - ")
63 #define UID_CONTEXT "uid"
64 #define AUTH_TOKEN_CONTEXT "edl_auth_token"
65 #define EDL_ECHO_TOKEN_CONTEXT "edl_echo_token"
79 NgapContainer::NgapContainer(
const string &sym_name,
80 const string &real_name,
83 d_dmrpp_rresource(0) {
85 BESDEBUG(MODULE, prolog <<
"object address: "<< (
void *)
this << endl);
93 string uid = BESContextManager::TheManager()->
get_context(UID_CONTEXT, found);
94 string access_token = BESContextManager::TheManager()->
get_context(AUTH_TOKEN_CONTEXT, found);
96 BESDEBUG(MODULE, prolog <<
"UID_CONTEXT(" << UID_CONTEXT <<
"): " << uid << endl);
111 d_dmrpp_rresource(copy_from.d_dmrpp_rresource) {
112 BESDEBUG(MODULE, prolog <<
"BEGIN object address: "<< (
void *)
this <<
" Copying from: " << (
void *) ©_from << endl);
115 if (d_dmrpp_rresource) {
116 string err = (string)
"The Container has already been accessed, "
117 +
"can not create a copy of this container.";
120 BESDEBUG(MODULE, prolog <<
"object address: "<< (
void *)
this << endl);
124 if (copy_to.d_dmrpp_rresource) {
125 string err = (string)
"The Container has already been accessed, "
126 +
"can not duplicate this resource.";
129 BESDEBUG(MODULE, prolog <<
"BEGIN object address: "<< (
void *)
this <<
" Copying to: " << (
void *) ©_to << endl);
130 copy_to.d_dmrpp_rresource = d_dmrpp_rresource;
137 _duplicate(*container);
138 BESDEBUG(MODULE, prolog <<
"object address: "<< (
void *)
this <<
" to: " << (
void *)container << endl);
142 NgapContainer::~NgapContainer() {
143 BESDEBUG(MODULE, prolog <<
"BEGIN object address: "<< (
void *)
this << endl);
144 if (d_dmrpp_rresource) {
147 BESDEBUG(MODULE, prolog <<
"END object address: "<< (
void *)
this << endl);
154 bool cache_terminal_urls(){
163 void cache_final_redirect_url(
string data_access_url_str) {
167 std::map<std::string,std::string> data_access_url_info;
171 http::url target_url(data_access_url_info);
172 found = NgapApi::signed_url_is_expired(target_url);
176 string last_accessed_url_str;
177 curl::find_last_redirect(data_access_url_str, last_accessed_url_str);
178 BESDEBUG(MODULE, prolog <<
"last_accessed_url: " << last_accessed_url_str << endl);
180 http::url last_accessed_url(last_accessed_url_str);
181 last_accessed_url.kvp(data_access_url_info);
197 BESDEBUG(MODULE, prolog <<
"BEGIN object address: "<< (
void *)
this << endl);
203 if(cache_terminal_urls()){
204 curl::cache_final_redirect_url(data_access_url_str, 0);
209 string dmrpp_url = data_access_url_str +
".dmrpp";
211 BESDEBUG(MODULE, prolog <<
"data_access_url: " << data_access_url_str << endl);
212 BESDEBUG(MODULE, prolog <<
"dmrpp_url: " << dmrpp_url << endl);
218 if (!d_dmrpp_rresource) {
219 BESDEBUG(MODULE, prolog <<
"Building new RemoteResource (dmr++)." << endl);
220 string replace_template;
221 string replace_value;
222 if (inject_data_url()) {
223 replace_template = DATA_ACCESS_URL_KEY;
224 replace_value = data_access_url_str;
229 if (BESISDEBUG(MODULE) ||
BESDebug::IsSet(TIMING_LOG_KEY) || BESLog::TheLog()->is_verbose()){
230 besTimer.
start(
"DMR++ retrieval: "+ dmrpp_url);
235 BESDEBUG(MODULE, prolog <<
"Retrieved remote resource: " << dmrpp_url << endl);
239 BESDEBUG(MODULE, prolog <<
"Using local cache file: " << cachedResource << endl);
241 type = d_dmrpp_rresource->
getType();
243 BESDEBUG(MODULE, prolog <<
"Type: " << type << endl);
244 BESDEBUG(MODULE, prolog <<
"Done retrieving: " << dmrpp_url <<
" returning cached file " << cachedResource << endl);
245 BESDEBUG(MODULE, prolog <<
"END" << endl);
247 return cachedResource;
259 if (d_dmrpp_rresource) {
260 BESDEBUG(MODULE, prolog <<
"Releasing RemoteResource" << endl);
261 delete d_dmrpp_rresource;
262 d_dmrpp_rresource = 0;
265 BESDEBUG(MODULE, prolog <<
"Done releasing Ngap response" << endl);
277 strm << BESIndent::LMarg <<
"NgapContainer::dump - (" << (
void *)
this
281 if (d_dmrpp_rresource) {
282 strm << BESIndent::LMarg <<
"RemoteResource.getCacheFileName(): " << d_dmrpp_rresource->
getCacheFileName()
284 strm << BESIndent::LMarg <<
"response headers: ";
289 vector<string>::const_iterator i = hdrs->begin();
290 vector<string>::const_iterator e = hdrs->end();
291 for (; i != e; i++) {
292 string hdr_line = (*i);
293 strm << BESIndent::LMarg << hdr_line << endl;
295 BESIndent::UnIndent();
297 strm <<
"none" << endl;
300 strm << BESIndent::LMarg <<
"response not yet obtained" << endl;
302 BESIndent::UnIndent();
305 bool NgapContainer::inject_data_url(){
310 if (found && key_value ==
"true") {
313 BESDEBUG(MODULE, prolog <<
"NGAP_INJECT_DATA_URL_KEY(" << NGAP_INJECT_DATA_URL_KEY <<
"): " << result << endl);
A container is something that holds data. E.G., a netcdf file or a database entry.
void set_container_type(const std::string &type)
set the type of data that this container represents, such as cedar or netcdf.
virtual void dump(std::ostream &strm) const
dumps information about this object
void set_real_name(const std::string &real_name)
set the real name for this container, such as a file name if reading a data file.
std::string get_container_type() const
retrieve the type of data this container holds, such as cedar or netcdf.
void set_relative_name(const std::string &relative)
Set the relative name of the object in this container.
void _duplicate(BESContainer ©_to)
duplicate this instance into the passed container
std::string get_real_name() const
retrieve the real name for this container, such as a file name.
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
exception thrown if internal error encountered
virtual bool start(std::string name)
static std::string lowercase(const std::string &s)
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
void set_keys(const std::string &key, const std::vector< std::string > &values, bool addto)
allows the user to set key/value pairs from within the application.
std::string getCacheFileName()
std::vector< std::string > * getResponseHeaders()
std::string convert_ngap_resty_path_to_data_access_url(const std::string &restified_path, const std::string &uid="")
Converts an NGAP restified granule path into a CMR metadata query for the granule.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual std::string access()
access the remote target response by making the remote request
virtual BESContainer * ptr_duplicate()
pure abstract method to duplicate this instances of BESContainer
virtual bool release()
release the resources