36 #include <curl/curl.h>
40 #include <BESCatalogUtils.h>
41 #include <BESCatalogList.h>
42 #include <BESCatalog.h>
44 #include <TheBESKeys.h>
45 #include <BESInternalError.h>
46 #include <BESDapError.h>
47 #include <BESNotFoundError.h>
48 #include <BESSyntaxUserError.h>
62 std::map<string, string> CmrUtils::MimeList;
63 string CmrUtils::ProxyProtocol;
64 string CmrUtils::ProxyHost;
65 string CmrUtils::ProxyUser;
66 string CmrUtils::ProxyPassword;
67 string CmrUtils::ProxyUserPW;
68 int CmrUtils::ProxyPort = 0;
69 int CmrUtils::ProxyAuthType = 0;
70 bool CmrUtils::useInternalCache =
false;
72 string CmrUtils::NoProxyRegex;
74 #define prolog std::string("CmrUtils::").append(__func__).append("() - ")
79 void CmrUtils::Initialize()
83 std::string key = CMR_MIMELIST;
84 std::vector<string> vals;
86 if (found && vals.size()) {
87 std::vector<string>::iterator i = vals.begin();
88 std::vector<string>::iterator e = vals.end();
90 size_t colon = (*i).find(
":");
91 if (colon == string::npos) {
92 string err = (string)
"Malformed " + CMR_MIMELIST +
" " + (*i)
93 +
" specified in the gateway configuration";
96 string mod = (*i).substr(0, colon);
97 string mime = (*i).substr(colon + 1);
105 if (found && !CmrUtils::ProxyHost.empty()) {
112 if (found && !port.empty()) {
113 CmrUtils::ProxyPort = atoi(port.c_str());
114 if (!CmrUtils::ProxyPort) {
115 string err = (string)
"CMR proxy host is specified, but specified port is absent";
124 key = CMR_PROXYPROTOCOL;
126 if (!found || CmrUtils::ProxyProtocol.empty()) {
127 CmrUtils::ProxyProtocol =
"http";
136 CmrUtils::ProxyUser =
"";
142 key = CMR_PROXYPASSWORD;
145 CmrUtils::ProxyPassword =
"";
151 key = CMR_PROXYUSERPW;
154 CmrUtils::ProxyUserPW =
"";
160 key = CMR_PROXYAUTHTYPE;
165 if (authType ==
"basic") {
166 CmrUtils::ProxyAuthType = CURLAUTH_BASIC;
167 BESDEBUG(MODULE, prolog <<
"ProxyAuthType BASIC set." << endl);
169 else if (authType ==
"digest") {
170 CmrUtils::ProxyAuthType = CURLAUTH_DIGEST;
171 BESDEBUG(MODULE, prolog <<
"ProxyAuthType DIGEST set." << endl);
174 else if (authType ==
"ntlm") {
175 CmrUtils::ProxyAuthType = CURLAUTH_NTLM;
176 BESDEBUG(MODULE, prolog <<
"ProxyAuthType NTLM set." << endl);
179 CmrUtils::ProxyAuthType = CURLAUTH_BASIC;
181 prolog <<
"User supplied an invalid value '"<< authType <<
"' for Gateway.ProxyAuthType. Falling back to BASIC authentication scheme." << endl);
185 CmrUtils::ProxyAuthType = CURLAUTH_BASIC;
190 key = CMR_USE_INTERNAL_CACHE;
194 if (use_cache ==
"true" || use_cache ==
"TRUE" || use_cache ==
"True" || use_cache ==
"yes"
195 || use_cache ==
"YES" || use_cache ==
"Yes")
196 CmrUtils::useInternalCache =
true;
198 CmrUtils::useInternalCache =
false;
202 CmrUtils::useInternalCache =
false;
211 void CmrUtils::Get_type_from_disposition(
const string &disp,
string &type)
213 size_t fnpos = disp.find(
"filename");
214 if (fnpos != string::npos) {
217 size_t pos = disp.find(
"#", fnpos);
218 if (pos == string::npos) pos = disp.find(
"=", fnpos);
219 if (pos != string::npos) {
225 size_t sp = disp.find(
" ", pos);
226 if (pos != string::npos) {
228 filename = disp.substr(pos + 1, sp - pos - 1);
232 filename = disp.substr(pos + 1);
236 if (filename[0] ==
'"') {
237 filename = filename.substr(1);
239 if (filename[filename.length() - 1] ==
'"') {
240 filename = filename.substr(0, filename.length() - 1);
251 BESCatalogUtils::match_citer i = utils->match_list_begin();
252 BESCatalogUtils::match_citer ie = utils->match_list_end();
254 for (; i != ie && !done; i++) {
255 BESCatalogUtils::handler_regex match = (*i);
258 prolog <<
"Comparing disp filename " << filename <<
" against expr " << match.regex << endl);
259 BESRegex reg_expr(match.regex.c_str());
260 if (reg_expr.match(filename.c_str(), filename.length()) ==
static_cast<int>(filename.length())) {
261 type = match.handler;
266 string serr = (string)
"Unable to match data type, " +
"malformed Catalog TypeMatch parameter "
267 +
"in bes configuration file around " + match.regex +
": " + e.get_error_message();
268 throw BESDapError(serr,
false, e.get_error_code(), __FILE__, __LINE__);
276 void CmrUtils::Get_type_from_content_type(
const string &ctype,
string &type)
278 BESDEBUG(MODULE, prolog <<
"BEGIN" << endl);
279 std::map<string, string>::iterator i = MimeList.begin();
280 std::map<string, string>::iterator e = MimeList.end();
282 for (; i != e && !done; i++) {
284 prolog <<
"Comparing content type '" << ctype <<
"' against mime list element '" << (*i).second <<
"'"<< endl);
286 prolog <<
"first: " << (*i).first <<
" second: " << (*i).second << endl);
288 if ((*i).second == ctype) {
290 BESDEBUG(MODULE, prolog <<
"MATCH" << endl);
296 BESDEBUG(MODULE,
"GatewayUtils::Get_type_from_content_type() - END" << endl);
299 void CmrUtils::Get_type_from_url(
const string &url,
string &type)
309 BESCatalogUtils::match_citer i = utils->match_list_begin();
310 BESCatalogUtils::match_citer ie = utils->match_list_end();
312 for (; i != ie && !done; i++) {
313 BESCatalogUtils::handler_regex match = (*i);
316 prolog <<
"Comparing url " << url <<
" against type match expr " << match.regex << endl);
317 BESRegex reg_expr(match.regex.c_str());
318 if (reg_expr.match(url.c_str(), url.length()) ==
static_cast<int>(url.length())) {
319 type = match.handler;
321 BESDEBUG(MODULE, prolog <<
"MATCH type: " << type << endl);
325 string serr = (string)
"Unable to match data type! Malformed Catalog TypeMatch parameter "
326 +
"in bes configuration file around " + match.regex +
": " + e.get_error_message();
335 bool GatewayUtils::Is_Whitelisted(
const std::string &url){
336 bool whitelisted =
false;
337 std::vector<std::string>::const_iterator i = WhiteList.begin();
338 std::vector<std::string>::const_iterator e = WhiteList.end();
339 for (; i != e && !whitelisted; i++) {
340 if ((*i).length() <= url.length()) {
341 if (url.substr(0, (*i).length()) == (*i)) {
352 CmrUtils::getTemporalFacetGranule(
const std::string granule_path)
355 BESDEBUG(MODULE, prolog <<
"BEGIN (granule_path: '" << granule_path <<
")" << endl);
358 string facet =
"temporal";
362 string granule_id =
"-";
366 BESDEBUG(MODULE, prolog <<
"path: '" << path <<
"' path_elements.size(): " << path_elements.size() << endl);
368 switch(path_elements.size()){
371 collection = path_elements[0];
372 BESDEBUG(MODULE, prolog <<
"collection: '" << collection << endl);
373 facet = path_elements[1];
374 BESDEBUG(MODULE, prolog <<
"facet: '" << facet << endl);
375 year = path_elements[2];
376 BESDEBUG(MODULE, prolog <<
"year: '" << year << endl);
377 month = path_elements[3];
378 BESDEBUG(MODULE, prolog <<
"month: '" << month << endl);
379 day = path_elements[4];
380 BESDEBUG(MODULE, prolog <<
"day: '" << day << endl);
381 granule_id = path_elements[5];
382 BESDEBUG(MODULE, prolog <<
"granule_id: '" << granule_id << endl);
393 return cmrApi.get_granule( collection, year, month, day, granule_id);
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
virtual BESCatalog * default_catalog() const
The the default catalog.
std::string get_handler_name(const std::string &item) const
Find the handler name that will process.
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
error object created from libdap error objects and can handle those errors
exception thrown if internal error encountered
error thrown if the resource requested cannot be found
error thrown if there is a user syntax error in the request or any other user error
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 lowercase(const std::string &s)
static std::string normalize_path(const std::string &path, bool leading_separator, bool trailing_separator, const std::string separator="/")
Removes duplicate separators and provides leading and trailing separators as directed.
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.