bes  Updated for version 3.20.5
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 
44 #define MODULE "bes"
45 
46 #define prolog std::string("ServerAdministrator::").append(__func__).append("() - ")
47 
48 
61 #define EMAIL_KEY "email"
62 #define EMAIL_DEFAULT "support@opendap.org"
63 
64 #define ORGANIZATION_KEY "organization"
65 #define ORGANIZATION_DEFAULT "OPeNDAP Inc."
66 
67 #define STREET_KEY "street"
68 #define STREET_DEFAULT "165 NW Dean Knauss Dr."
69 
70 #define CITY_KEY "city"
71 #define CITY_DEFAULT "Narragansett"
72 
73 #define REGION_KEY "region"
74 #define STATE_KEY "state"
75 #define REGION_DEFAULT "RI"
76 
77 #define POSTAL_CODE_KEY "postalCode"
78 #define POSTAL_CODE_DEFAULT "02882"
79 
80 #define COUNTRY_KEY "country"
81 #define COUNTRY_DEFAULT "US"
82 
83 #define TELEPHONE_KEY "telephone"
84 #define TELEPHONE_DEFAULT "+1.401.575.4835"
85 
86 #define WEBSITE_KEY "website"
87 #define WEBSITE_DEFAULT "http://www.opendap.org"
88 
89 
90 namespace bes {
91 
97  bool found = false;
98  vector<string> admin_keys;
99  TheBESKeys::TheKeys()->get_values(SERVER_ADMINISTRATOR_KEY, admin_keys, found);
100  if(!found){
101  throw BESInternalFatalError(string("The BES configuration must provide server administrator information using the key: '")+SERVER_ADMINISTRATOR_KEY
102  +"'", __FILE__, __LINE__);
103  BESDEBUG(MODULE,__func__ << "() - ERROR! The BES configuration must provide server administrator information using the key " << SERVER_ADMINISTRATOR_KEY << endl);
104  mk_default();
105  return;
106  }
107 
108  vector<string>::iterator it;
109  for(it=admin_keys.begin(); it!=admin_keys.end(); it++){
110  string admin_info_entry = *it;
111  int index = admin_info_entry.find(":");
112  if(index>0){
113  string key = admin_info_entry.substr(0,index);
114  key = BESUtil::lowercase(key);
115  string value = admin_info_entry.substr(index+1);
116  BESDEBUG(MODULE, prolog << "key: '" << key << "' value: " << value << endl);
117  d_admin_info.insert( std::pair<string,string>(key,value));
118  }
119  else {
120  //throw BESInternalFatalError(string("The configuration entry for the ") + SERVER_ADMINISTRATOR_KEY +
121  // " was incorrectly formatted. entry: "+admin_info_entry, __FILE__,__LINE__);
122  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " << SERVER_ADMINISTRATOR_KEY << " was incorrectly formatted. Offending entry: " << admin_info_entry << endl);
123  mk_default();
124  return;
125  }
126  }
127  bool bad_flag = false;
128 
129  d_organization = get(ORGANIZATION_KEY);
130  if(d_organization.empty()){
131  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
132  SERVER_ADMINISTRATOR_KEY << "[" << ORGANIZATION_KEY << "] was missing." << endl);
133  bad_flag = true;
134  }
135 
136  d_street = get(STREET_KEY);
137  if(d_street.empty()){
138  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
139  SERVER_ADMINISTRATOR_KEY << "[" << STREET_KEY << "] was missing." << endl);
140  bad_flag = true;
141  }
142 
143  d_city = get(CITY_KEY);
144  if(d_city.empty()){
145  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
146  SERVER_ADMINISTRATOR_KEY << "[" << CITY_KEY << "] was missing." << endl);
147  bad_flag = true;
148  }
149 
150  d_region = get(REGION_KEY);
151  if(d_region.empty()){
152  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
153  SERVER_ADMINISTRATOR_KEY << "[" << REGION_KEY << "] was missing." << endl);
154  d_region = get(STATE_KEY);
155 
156  if(d_region.empty()){
157  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
158  SERVER_ADMINISTRATOR_KEY << "[" << STATE_KEY << "] was missing." << endl);
159  bad_flag = true;
160  }
161  }
162 
163  d_postal_code = get(POSTAL_CODE_KEY);
164  if(d_postal_code.empty()){
165  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
166  SERVER_ADMINISTRATOR_KEY << "[" << POSTAL_CODE_KEY << "] was missing." << endl);
167  bad_flag = true;
168  }
169 
170  d_country = get(COUNTRY_KEY);
171  if(d_country.empty()){
172  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
173  SERVER_ADMINISTRATOR_KEY << "[" << COUNTRY_KEY << "] was missing." << endl);
174  bad_flag = true;
175  }
176 
177  d_telephone = get(TELEPHONE_KEY);
178  if(d_telephone.empty()){
179  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
180  SERVER_ADMINISTRATOR_KEY << "[" << TELEPHONE_KEY << "] was missing." << endl);
181  bad_flag = true;
182  }
183 
184  d_email = get(EMAIL_KEY);
185  if(d_email.empty()){
186  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
187  SERVER_ADMINISTRATOR_KEY << "[" << EMAIL_KEY << "] was missing." << endl);
188  bad_flag = true;
189  }
190 
191  d_website = get(WEBSITE_KEY);
192  if(d_website.empty()){
193  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " <<
194  SERVER_ADMINISTRATOR_KEY << "[" << WEBSITE_KEY << "] was missing." << endl);
195  bad_flag = true;
196  }
197 
198  // %TODO This is a pretty simple (and brutal) qc in that any missing value prompts all of it to be rejected. Review. Fix?
199  if(bad_flag ){
200  mk_default();
201  BESDEBUG(MODULE,__func__ << "() - The configuration entry for the " << SERVER_ADMINISTRATOR_KEY << " was missing crucial information. jdump(): " << jdump(true) << endl);
202  }
203 }
204 
205 
206 
207 std::string ServerAdministrator::get(const string &key){
208  string lkey = BESUtil::lowercase(key);
209  std::map<std::string,std::string>::const_iterator result = d_admin_info.find(lkey);
210  if(result == d_admin_info.end()){
211  return "";
212  }
213  return result->second;
214 }
215 
216 
217 
218 
219 
220 std::string ServerAdministrator::xdump() const {
221  std::stringstream ss;
222  std::map<std::string,std::string>::const_iterator it = d_admin_info.begin();
223  ss << "<ServerAdministrator ";
224  for(it=d_admin_info.begin(); it!= d_admin_info.end(); it++){
225  if(it!= d_admin_info.begin())
226  ss << " ";
227  ss << it->first << "=\"" << it->second << "\"";
228  }
229  ss << "/>";
230  return ss.str();
231 }
232 
233 std::string ServerAdministrator::jdump(bool compact) const {
234  std::stringstream ss;
235  std::map<std::string,std::string>::const_iterator it = d_admin_info.begin();
236  ss << "{";
237  if(!compact)
238  ss<< endl << " ";
239  ss << "\"ServerAdministrator\":";
240  if(!compact)
241  ss << " ";
242  ss << "{";
243  if(!compact) ss << " ";
244  if(!compact) ss << " ";
245  for(it=d_admin_info.begin(); it!= d_admin_info.end(); it++){
246  if(it!= d_admin_info.begin())
247  ss << ",";
248  if(!compact)
249  ss << endl << " ";
250  ss << "\"" << it->first << "\"" << ":";
251  if(!compact)
252  ss << " ";
253  ss << "\"" << it->second << "\"";
254  }
255  if(!compact)
256  ss<< endl << " ";
257  ss << "}";
258  if(!compact)
259  ss << endl;
260  ss << "}";
261  if(!compact)
262  ss << endl;
263  return ss.str();
264 }
265 
266 
267 void ServerAdministrator::mk_default() {
268  this->d_admin_info.clear();
269  d_admin_info.insert( std::pair<string,string>(EMAIL_KEY,EMAIL_DEFAULT));
270  d_admin_info.insert( std::pair<string,string>(ORGANIZATION_KEY,ORGANIZATION_DEFAULT));
271  d_admin_info.insert( std::pair<string,string>(STREET_KEY,STREET_DEFAULT));
272  d_admin_info.insert( std::pair<string,string>(CITY_KEY,CITY_DEFAULT));
273  d_admin_info.insert( std::pair<string,string>(REGION_KEY,REGION_DEFAULT));
274  d_admin_info.insert( std::pair<string,string>(POSTAL_CODE_KEY,POSTAL_CODE_DEFAULT));
275  d_admin_info.insert( std::pair<string,string>(COUNTRY_KEY,COUNTRY_DEFAULT));
276  d_admin_info.insert( std::pair<string,string>(TELEPHONE_KEY,TELEPHONE_DEFAULT));
277  d_admin_info.insert( std::pair<string,string>(WEBSITE_KEY,WEBSITE_DEFAULT));
278  BESDEBUG(MODULE,__func__ << "() - ServerAdministrator values have been set to the defaults: " << jdump(true) << endl);
279 }
280 
281 
282 
283 
284 
285 } // namespace bes
exception thrown if an internal error is found and is fatal to the BES
static string lowercase(const string &s)
Definition: BESUtil.cc:197
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:61
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:451