33#include <BESSyntaxUserError.h>
34#include <BESInternalError.h>
35#include <BESNotFoundError.h>
38#include <TheBESKeys.h>
39#include <AllowedHosts.h>
40#include "RemoteResource.h"
42#include "CmrContainer.h"
49#define prolog std::string("CmrContainer::").append(__func__).append("() - ")
63CmrContainer::CmrContainer(
const string &sym_name,
64 const string &real_name,
const string &type) :
65 BESContainer(sym_name, real_name, type), d_remoteResource(0) {
67 BESDEBUG(MODULE, prolog <<
"BEGIN sym_name: " << sym_name
68 <<
" real_name: " << real_name <<
" type: " << type << endl);
73 BESDEBUG(MODULE, prolog <<
"path: '" << path <<
"' path_elements.size(): " << path_elements.size() << endl);
84 BESDEBUG( MODULE, prolog <<
"END" << endl);
91CmrContainer::CmrContainer(
const CmrContainer ©_from) :
92 BESContainer(copy_from), d_remoteResource(copy_from.d_remoteResource) {
95 if (d_remoteResource) {
96 string err = (string)
"The Container has already been accessed, "
97 +
"can not create a copy of this container.";
103 if (copy_to.d_remoteResource) {
104 string err = (string)
"The Container has already been accessed, "
105 +
"can not duplicate this resource.";
108 copy_to.d_remoteResource = d_remoteResource;
114 CmrContainer *container =
new CmrContainer;
115 _duplicate(*container);
119CmrContainer::~CmrContainer() {
120 if (d_remoteResource) {
132 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
136 BESDEBUG( MODULE, prolog <<
"path: " << path << endl);
140 throw BESNotFoundError(
"Failed to locate a granule associated with the path " + path, __FILE__, __LINE__);
142 string granule_url = granule->getDataGranuleUrl();
144 if(!d_remoteResource) {
145 BESDEBUG( MODULE, prolog <<
"Building new RemoteResource." << endl );
146 shared_ptr<http::url> target_url(
new http::url(granule_url,
true));
148 d_remoteResource->retrieve_resource();
150 BESDEBUG( MODULE, prolog <<
"Retrieved RemoteResource." << endl );
152 string cachedResource = d_remoteResource->get_filename();
153 BESDEBUG( MODULE, prolog <<
"Using local cache file: " << cachedResource << endl );
155 string type = d_remoteResource->get_type();
157 BESDEBUG( MODULE, prolog <<
"Type: " << type << endl );
159 BESDEBUG( MODULE, prolog <<
"Done accessing " <<
get_real_name() <<
" returning cached file " << cachedResource << endl);
160 BESDEBUG( MODULE, prolog <<
"Done accessing " << *
this << endl);
161 BESDEBUG( MODULE, prolog <<
"END" << endl);
163 return cachedResource;
175 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
176 if (d_remoteResource) {
177 BESDEBUG( MODULE, prolog <<
"Releasing RemoteResource" << endl);
178 delete d_remoteResource;
179 d_remoteResource =
nullptr;
181 BESDEBUG( MODULE, prolog <<
"END" << endl);
193 strm << BESIndent::LMarg << prolog <<
"(" << (
void *)
this
197 if (d_remoteResource) {
198 strm << BESIndent::LMarg <<
"RemoteResource.get_filename(): " << d_remoteResource->get_filename()
201 strm << BESIndent::LMarg <<
"response not yet obtained" << endl;
203 BESIndent::UnIndent();
213 const unsigned int PATH_SIZE = 7;
215 BESDEBUG(MODULE, prolog <<
"BEGIN (granule_path: '" << granule_path <<
")" << endl);
219 BESDEBUG(MODULE, prolog <<
"path: '" << path <<
"' path_elements.size(): " << path_elements.size() << endl);
221 if(path_elements.size() != PATH_SIZE){
223 msg <<
"The path component: '" << granule_path <<
"' of your request has ";
224 msg << (path_elements.size()<PATH_SIZE?
"too few components. ":
"too many components. ");
225 msg <<
"I was expecting " << PATH_SIZE <<
" elements but I found " << path_elements.size() <<
". ";
226 msg <<
"I was unable to locate a granule from what you provided.";
234 string collection = path_elements[1];
235 BESDEBUG(MODULE, prolog <<
"collection: '" << collection << endl);
236 string facet = path_elements[2];
237 BESDEBUG(MODULE, prolog <<
" facet: '" << facet << endl);
238 string year = path_elements[3];
239 BESDEBUG(MODULE, prolog <<
" year: '" << year << endl);
240 string month = path_elements[4];
241 BESDEBUG(MODULE, prolog <<
" month: '" << month << endl);
242 string day = path_elements[5];
243 BESDEBUG(MODULE, prolog <<
" day: '" << day << endl);
244 string granule_id = path_elements[6];
245 BESDEBUG(MODULE, prolog <<
"granule_id: '" << granule_id << endl);
248 return cmrApi.get_granule( collection, year, month, day, granule_id);
void set_container_type(const std::string &type)
set the type of data that this container represents, such as cedar or netcdf.
void dump(std::ostream &strm) const override
dumps information about this object
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.
exception thrown if internal error encountered
error thrown if the resource requested cannot be found
static std::vector< std::string > split(const std::string &s, char delim='/', bool skip_empty=true)
Splits the string s into the return vector of tokens using the delimiter delim and skipping empty val...
static std::string normalize_path(const std::string &path, bool leading_separator, bool trailing_separator, std::string separator="/")
Removes duplicate separators and provides leading and trailing separators as directed.
Container representing a remote request.
virtual bool release()
release the resources
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual BESContainer * ptr_duplicate()
pure abstract method to duplicate this instances of BESContainer
static std::unique_ptr< Granule > getTemporalFacetGranule(const std::string &granule_path)
virtual std::string access()
access the remote target response by making the remote request
Parse a URL into the protocol, host, path and query parts.