bes  Updated for version 3.20.6
ServerAdministrator.cc
1 
2 // ServerAdministrator.cc
3 // -*- mode: c++; c-basic-offset:4 -*-
4 //
5 //
6 // This file is part of BES httpd_catalog_module
7 //
8 // Copyright (c) 2018 OPeNDAP, Inc.
9 // Author: Nathan Potter <ndp@opendap.org>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26 // Please read the full copyright statement in the file COPYRIGHT_URI.
27 //
28 #include "config.h"
29 
30 #include <vector>
31 #include <map>
32 #include <sstream>
33 
34 #include <TheBESKeys.h>
35 #include <BESDebug.h>
36 #include <BESUtil.h>
37 #include <BESLog.h>
38 #include "BESInternalFatalError.h"
39 
40 #include "ServerAdministrator.h"
41 
42 using std::vector;
43 using std::endl;
44 using std::string;
45 using std::ostream;
46 
47 #define MODULE "bes"
48 
49 #define prolog std::string("ServerAdministrator::").append(__func__).append("() - ")
50 
51 
64 #define EMAIL_KEY "email"
65 #define EMAIL_DEFAULT "support@opendap.org"
66 
67 #define ORGANIZATION_KEY "organization"
68 #define ORGANIZATION_DEFAULT "OPeNDAP Inc."
69 
70 #define STREET_KEY "street"
71 #define STREET_DEFAULT "165 NW Dean Knauss Dr."
72 
73 #define CITY_KEY "city"
74 #define CITY_DEFAULT "Narragansett"
75 
76 #define REGION_KEY "region"
77 #define STATE_KEY "state"
78 #define REGION_DEFAULT "RI"
79 
80 #define POSTAL_CODE_KEY "postalCode"
81 #define POSTAL_CODE_DEFAULT "02882"
82 
83 #define COUNTRY_KEY "country"
84 #define COUNTRY_DEFAULT "US"
85 
86 #define TELEPHONE_KEY "telephone"
87 #define TELEPHONE_DEFAULT "+1.401.575.4835"
88 
89 #define WEBSITE_KEY "website"
90 #define WEBSITE_DEFAULT "http://www.opendap.org"
91 
92 
93 namespace bes {
94 
100  bool found = false;
101  vector<string> admin_keys;
102  TheBESKeys::TheKeys()->get_values(SERVER_ADMINISTRATOR_KEY, admin_keys, found);
103  if(!found){
104  throw BESInternalFatalError(string("The BES configuration must provide server administrator information using the key: '")+SERVER_ADMINISTRATOR_KEY
105  +"'", __FILE__, __LINE__);
106  BESDEBUG(MODULE,__func__ << "() - ERROR! The BES configuration must provide server administrator information using the key " << SERVER_ADMINISTRATOR_KEY << endl);
107  mk_default();
108  return;
109  }
110 
111  vector<string>::iterator it;
112  for(it=admin_keys.begin(); it!=admin_keys.end(); it++){
113  string admin_info_entry = *it;
114  int index = admin_info_entry.find(":");
115  if(index>0){
116  string key = admin_info_entry.substr(0,index);
117  key = BESUtil::lowercase(key);
118  string value = admin_info_entry.substr(index+1);
119  BESDEBUG(MODULE, prolog << "key: '" << key << "' value: " << value << endl);
120  d_admin_info.insert( std::pair<string,string>(key,value));
121  }
122  else {
123  //throw BESInternalFatalError(string("The configuration entry for the ") + SERVER_ADMINISTRATOR_KEY +
124  // " was incorrectly formatted. entry: "+admin_info_entry, __FILE__,__LINE__);
125  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " << SERVER_ADMINISTRATOR_KEY << " was incorrectly formatted. Offending entry: " << admin_info_entry << endl);
126  mk_default();
127  return;
128  }
129  }
130  bool bad_flag = false;
131 
132  d_organization = get(ORGANIZATION_KEY);
133  if(d_organization.empty()){
134  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
135  SERVER_ADMINISTRATOR_KEY << "[" << ORGANIZATION_KEY << "] was missing." << endl);
136  bad_flag = true;
137  }
138 
139  d_street = get(STREET_KEY);
140  if(d_street.empty()){
141  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
142  SERVER_ADMINISTRATOR_KEY << "[" << STREET_KEY << "] was missing." << endl);
143  bad_flag = true;
144  }
145 
146  d_city = get(CITY_KEY);
147  if(d_city.empty()){
148  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
149  SERVER_ADMINISTRATOR_KEY << "[" << CITY_KEY << "] was missing." << endl);
150  bad_flag = true;
151  }
152 
153  d_region = get(REGION_KEY);
154  if(d_region.empty()){
155  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
156  SERVER_ADMINISTRATOR_KEY << "[" << REGION_KEY << "] was missing." << endl);
157  d_region = get(STATE_KEY);
158 
159  if(d_region.empty()){
160  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
161  SERVER_ADMINISTRATOR_KEY << "[" << STATE_KEY << "] was missing." << endl);
162  bad_flag = true;
163  }
164  }
165 
166  d_postal_code = get(POSTAL_CODE_KEY);
167  if(d_postal_code.empty()){
168  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
169  SERVER_ADMINISTRATOR_KEY << "[" << POSTAL_CODE_KEY << "] was missing." << endl);
170  bad_flag = true;
171  }
172 
173  d_country = get(COUNTRY_KEY);
174  if(d_country.empty()){
175  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
176  SERVER_ADMINISTRATOR_KEY << "[" << COUNTRY_KEY << "] was missing." << endl);
177  bad_flag = true;
178  }
179 
180  d_telephone = get(TELEPHONE_KEY);
181  if(d_telephone.empty()){
182  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
183  SERVER_ADMINISTRATOR_KEY << "[" << TELEPHONE_KEY << "] was missing." << endl);
184  bad_flag = true;
185  }
186 
187  d_email = get(EMAIL_KEY);
188  if(d_email.empty()){
189  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
190  SERVER_ADMINISTRATOR_KEY << "[" << EMAIL_KEY << "] was missing." << endl);
191  bad_flag = true;
192  }
193 
194  d_website = get(WEBSITE_KEY);
195  if(d_website.empty()){
196  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
197  SERVER_ADMINISTRATOR_KEY << "[" << WEBSITE_KEY << "] was missing." << endl);
198  bad_flag = true;
199  }
200 
201  // %TODO This is a pretty simple (and brutal) qc in that any missing value prompts all of it to be rejected. Review. Fix?
202  if(bad_flag ){
203  mk_default();
204  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " << SERVER_ADMINISTRATOR_KEY << " was missing crucial information. jdump(): " << jdump(true) << endl);
205  }
206 }
207 
208 
209 
210 std::string ServerAdministrator::get(const string &key){
211  string lkey = BESUtil::lowercase(key);
212  std::map<std::string,std::string>::const_iterator result = d_admin_info.find(lkey);
213  if(result == d_admin_info.end()){
214  return "";
215  }
216  return result->second;
217 }
218 
219 
220 
221 
222 
223 std::string ServerAdministrator::xdump() const {
224  std::stringstream ss;
225  std::map<std::string,std::string>::const_iterator it = d_admin_info.begin();
226  ss << "<ServerAdministrator ";
227  for(it=d_admin_info.begin(); it!= d_admin_info.end(); it++){
228  if(it!= d_admin_info.begin())
229  ss << " ";
230  ss << it->first << "=\"" << it->second << "\"";
231  }
232  ss << "/>";
233  return ss.str();
234 }
235 
236 std::string ServerAdministrator::jdump(bool compact) const {
237  std::stringstream ss;
238  std::map<std::string,std::string>::const_iterator it = d_admin_info.begin();
239  ss << "{";
240  if(!compact)
241  ss<< endl << " ";
242  ss << "\"ServerAdministrator\":";
243  if(!compact)
244  ss << " ";
245  ss << "{";
246  if(!compact) ss << " ";
247  if(!compact) ss << " ";
248  for(it=d_admin_info.begin(); it!= d_admin_info.end(); it++){
249  if(it!= d_admin_info.begin())
250  ss << ",";
251  if(!compact)
252  ss << endl << " ";
253  ss << "\"" << it->first << "\"" << ":";
254  if(!compact)
255  ss << " ";
256  ss << "\"" << it->second << "\"";
257  }
258  if(!compact)
259  ss<< endl << " ";
260  ss << "}";
261  if(!compact)
262  ss << endl;
263  ss << "}";
264  if(!compact)
265  ss << endl;
266  return ss.str();
267 }
268 
269 
270 void ServerAdministrator::mk_default() {
271  this->d_admin_info.clear();
272  d_admin_info.insert( std::pair<string,string>(EMAIL_KEY,EMAIL_DEFAULT));
273  d_admin_info.insert( std::pair<string,string>(ORGANIZATION_KEY,ORGANIZATION_DEFAULT));
274  d_admin_info.insert( std::pair<string,string>(STREET_KEY,STREET_DEFAULT));
275  d_admin_info.insert( std::pair<string,string>(CITY_KEY,CITY_DEFAULT));
276  d_admin_info.insert( std::pair<string,string>(REGION_KEY,REGION_DEFAULT));
277  d_admin_info.insert( std::pair<string,string>(POSTAL_CODE_KEY,POSTAL_CODE_DEFAULT));
278  d_admin_info.insert( std::pair<string,string>(COUNTRY_KEY,COUNTRY_DEFAULT));
279  d_admin_info.insert( std::pair<string,string>(TELEPHONE_KEY,TELEPHONE_DEFAULT));
280  d_admin_info.insert( std::pair<string,string>(WEBSITE_KEY,WEBSITE_DEFAULT));
281  BESDEBUG(MODULE,__func__ << "() - ServerAdministrator values have been set to the defaults: " << jdump(true) << endl);
282 }
283 
284 
285 
286 
287 
288 } // namespace bes
BESInternalFatalError
exception thrown if an internal error is found and is fatal to the BES
Definition: BESInternalFatalError.h:43
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
TheBESKeys::get_values
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Definition: TheBESKeys.cc:303
BESUtil::lowercase
static std::string lowercase(const std::string &s)
Definition: BESUtil.cc:200
bes::ServerAdministrator::ServerAdministrator
ServerAdministrator()
Definition: ServerAdministrator.cc:99