30#include <BESSyntaxUserError.h>
31#include <BESInternalError.h>
32#include <BESNotFoundError.h>
35#include <TheBESKeys.h>
36#include <AllowedHosts.h>
37#include "RemoteResource.h"
39#include "CmrContainer.h"
46#define prolog std::string("CmrContainer::").append(__func__).append("() - ")
60CmrContainer::CmrContainer(
const string &sym_name,
61 const string &real_name,
const string &type) :
62 BESContainer(sym_name, real_name, type), d_remoteResource(0) {
64 BESDEBUG(MODULE, prolog <<
"BEGIN sym_name: " << sym_name
65 <<
" real_name: " << real_name <<
" type: " << type << endl);
70 BESDEBUG(MODULE, prolog <<
"path: '" << path <<
"' path_elements.size(): " << path_elements.size() << endl);
105 BESDEBUG( MODULE, prolog <<
"END" << endl);
113 BESContainer(copy_from), d_remoteResource(copy_from.d_remoteResource) {
116 if (d_remoteResource) {
117 string err = (string)
"The Container has already been accessed, "
118 +
"can not create a copy of this container.";
124 if (copy_to.d_remoteResource) {
125 string err = (string)
"The Container has already been accessed, "
126 +
"can not duplicate this resource.";
129 copy_to.d_remoteResource = d_remoteResource;
136 _duplicate(*container);
140CmrContainer::~CmrContainer() {
141 if (d_remoteResource) {
153 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
157 BESDEBUG( MODULE, prolog <<
"path: " << path << endl);
159 Granule *granule = getTemporalFacetGranule(path);
161 throw BESNotFoundError(
"Failed locate a granule associated with the path " + path, __FILE__, __LINE__);
163 string url = granule->getDataAccessUrl();
171 if(!d_remoteResource) {
172 BESDEBUG( MODULE, prolog <<
"Building new RemoteResource." << endl );
173 shared_ptr<http::url> target_url(
new http::url(url));
177 BESDEBUG( MODULE, prolog <<
"Located remote resource." << endl );
181 BESDEBUG( MODULE, prolog <<
"Using local cache file: " << cachedResource << endl );
183 type = d_remoteResource->
getType();
185 BESDEBUG( MODULE, prolog <<
"Type: " << type << endl );
188 BESDEBUG( MODULE, prolog <<
"Done accessing " <<
get_real_name() <<
" returning cached file " << cachedResource << endl);
189 BESDEBUG( MODULE, prolog <<
"Done accessing " << *
this << endl);
190 BESDEBUG( MODULE, prolog <<
"END" << endl);
192 return cachedResource;
204 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
205 if (d_remoteResource) {
206 BESDEBUG( MODULE, prolog <<
"Releasing RemoteResource" << endl);
207 delete d_remoteResource;
208 d_remoteResource = 0;
210 BESDEBUG( MODULE, prolog <<
"END" << endl);
222 strm << BESIndent::LMarg << prolog <<
"(" << (
void *)
this
226 if (d_remoteResource) {
227 strm << BESIndent::LMarg <<
"RemoteResource.getCacheFileName(): " << d_remoteResource->
getCacheFileName()
229 strm << BESIndent::LMarg <<
"response headers: ";
235 vector<string>::const_iterator i = hdrs->begin();
236 vector<string>::const_iterator e = hdrs->end();
237 for (; i != e; i++) {
238 string hdr_line = (*i);
239 strm << BESIndent::LMarg << hdr_line << endl;
241 BESIndent::UnIndent();
243 strm <<
"none" << endl;
246 strm << BESIndent::LMarg <<
"response not yet obtained" << endl;
248 BESIndent::UnIndent();
251 Granule * CmrContainer::getTemporalFacetGranule(
const std::string granule_path)
254 BESDEBUG(MODULE, prolog <<
"BEGIN (granule_path: '" << granule_path <<
")" << endl);
257 string facet =
"temporal";
261 string granule_id =
"-";
265 BESDEBUG(MODULE, prolog <<
"path: '" << path <<
"' path_elements.size(): " << path_elements.size() << endl);
267 switch(path_elements.size()){
270 collection = path_elements[0];
271 BESDEBUG(MODULE, prolog <<
"collection: '" << collection << endl);
272 facet = path_elements[1];
273 BESDEBUG(MODULE, prolog <<
"facet: '" << facet << endl);
274 year = path_elements[2];
275 BESDEBUG(MODULE, prolog <<
"year: '" << year << endl);
276 month = path_elements[3];
277 BESDEBUG(MODULE, prolog <<
"month: '" << month << endl);
278 day = path_elements[4];
279 BESDEBUG(MODULE, prolog <<
"day: '" << day << endl);
280 granule_id = path_elements[5];
281 BESDEBUG(MODULE, prolog <<
"granule_id: '" << granule_id << endl);
292 return cmrApi.get_granule( collection, year, month, day, granule_id);
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
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.
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
virtual std::string access()
access the remote target response by making the remote request
std::string getCacheFileName()
std::vector< std::string > * getResponseHeaders()