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