bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
BESInfo.cc
1// BESInfo.cc
2
3// This file is part of bes, A C++ back-end server implementation framework
4// for the OPeNDAP Data Access Protocol.
5
6// Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact University Corporation for Atmospheric Research at
24// 3080 Center Green Drive, Boulder, CO 80301
25
26// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27// Please read the full copyright statement in the file COPYRIGHT_UCAR.
28//
29// Authors:
30// pwest Patrick West <pwest@ucar.edu>
31// jgarcia Jose Garcia <jgarcia@ucar.edu>
32
33#include <cerrno>
34#include <sstream>
35#include <iostream>
36#include <fstream>
37#include <cstring>
38
39#include "BESInfo.h"
40#include "TheBESKeys.h"
41#include "BESInternalError.h"
42
43using namespace std;
44
45#define BES_INFO_FILE_BUFFER_SIZE 4096
46
53 _strm(0), _strm_owned(false), _buffered(true), _response_started(false) {
54 _strm = new ostringstream;
55 _strm_owned = true;
56}
57
71BESInfo::BESInfo(const string &key, ostream *strm, bool strm_owned) :
72 _strm(nullptr), _strm_owned(false), _buffered(true), _response_started(false) {
73 bool found = false;
74 vector<string> vals;
75 string b;
76 TheBESKeys::TheKeys()->get_value(key, b, found);
77 if (b == "true" || b == "True" || b == "TRUE" || b == "yes" || b == "Yes" || b == "YES") {
78 _strm = new ostringstream;
79 _strm_owned = true;
80 _buffered = true;
81 if (strm && strm_owned) delete strm;
82 } else {
83 if (!strm) {
84 string s = "Informational response not buffered but no stream passed";
85 throw BESInternalError(s, __FILE__, __LINE__);
86 }
87 _strm = strm;
88 _strm_owned = strm_owned;
89 _buffered = false;
90 }
91}
92
93BESInfo::~BESInfo() {
94 if (_strm && _strm_owned) {
95 delete _strm;
96 _strm = 0;
97 }
98}
99
107void
108BESInfo::begin_response(const string &response_name, map<string, string, std::less<>> */*attrs*/, BESDataHandlerInterface &/*dhi*/) {
109 _response_started = true;
110 _response_name = response_name;
111}
112
120void BESInfo::begin_response(const string &response_name, BESDataHandlerInterface &/*dhi*/) {
121 _response_started = true;
122 _response_name = response_name;
123}
124
125void BESInfo::end_response() {
126 _response_started = false;
127 if (_tags.size()) {
128 string s = "Not all tags were ended in info response";
129 throw BESInternalError(s, __FILE__, __LINE__);
130 }
131}
132
133void BESInfo::begin_tag(const string &tag_name, map<string, string, std::less<>> */*attrs*/) {
134 _tags.push(tag_name);
135}
136
137void BESInfo::end_tag(const string &tag_name) {
138 if (_tags.empty() || _tags.top() != tag_name) {
139 string s = (string) "tag " + tag_name + " already ended or not started";
140 throw BESInternalError(s, __FILE__, __LINE__);
141 } else {
142 _tags.pop();
143 }
144}
145
151void BESInfo::add_data(const string &s) {
152 // If not buffered then we've been passed a stream to use.
153 // If it is buffered then we created the stream.
154 (*_strm) << s;
155}
156
172void BESInfo::add_data_from_file(const string &key, const string &name) {
173 bool found = false;
174 string file;
175 try {
176 TheBESKeys::TheKeys()->get_value(key, file, found);
177 }
178 catch (...) {
179 found = false;
180 }
181 if (found == false) {
182 add_data(name + " file key " + key + " not found, information not available\n");
183 } else {
184 ifstream ifs(file.c_str());
185 int myerrno = errno;
186 if (!ifs) {
187 string serr = name + " file " + file + " not found, information not available: ";
188 char *err = strerror(myerrno);
189 if (err)
190 serr += err;
191 else
192 serr += "Unknown error";
193
194 serr += "\n";
195
196 add_data(serr);
197 } else {
198 char line[BES_INFO_FILE_BUFFER_SIZE];
199 while (!ifs.eof()) {
200 ifs.getline(line, BES_INFO_FILE_BUFFER_SIZE);
201 if (!ifs.eof()) {
202 add_data(line);
203 add_data("\n");
204 }
205 }
206 ifs.close();
207 }
208 }
209}
210
221void BESInfo::add_exception(const BESError &error, const string &admin) {
222 begin_tag("BESError");
223 ostringstream stype;
224 stype << error.get_bes_error_type();
225 add_tag("Type", stype.str());
226 add_tag("Message", error.get_message());
227 add_tag("Administrator", admin);
228
229 error.add_my_error_details_to(*this);
230
231 begin_tag("Location");
232 add_tag("File", error.get_file());
233 ostringstream sline;
234 sline << error.get_line();
235 add_tag("Line", sline.str());
236 end_tag("Location");
237
238 end_tag("BESError");
239}
240
249void BESInfo::print(ostream &strm) {
250 if (_buffered) {
251 strm << ((ostringstream *) _strm)->str();
252 }
253}
254
262void BESInfo::dump(ostream &strm) const {
263 strm << BESIndent::LMarg << "BESInfo::dump - (" << (void *) this << ")" << endl;
264 BESIndent::Indent();
265 strm << BESIndent::LMarg << "response name: " << _response_name << endl;
266 strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl;
267 strm << BESIndent::LMarg << "has response been started? " << _response_started << endl;
268 if (_tags.size()) {
269 strm << BESIndent::LMarg << "tags:" << endl;
270 BESIndent::Indent();
271 stack<string> temp_tags = _tags;
272 while (!temp_tags.empty()) {
273 string tag = temp_tags.top();
274 strm << BESIndent::LMarg << tag << endl;
275 temp_tags.pop();
276 }
277 BESIndent::UnIndent();
278 } else {
279 strm << BESIndent::LMarg << "tags: empty" << endl;
280 }
281 BESIndent::UnIndent();
282}
283
Structure storing information used by the BES to handle the request.
Base exception class for the BES with basic string message.
Definition BESError.h:66
unsigned int get_line() const
get the line number where the exception was thrown
Definition BESError.h:148
unsigned int get_bes_error_type() const
Return the return code for this error class.
Definition BESError.h:174
std::string get_file() const
get the file name where the exception was thrown
Definition BESError.h:140
std::string get_message() const
get the error message for this exception
Definition BESError.h:132
virtual void add_my_error_details_to(BESInfo &) const
Definition BESError.h:116
virtual void add_data(const std::string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition BESInfo.cc:151
BESInfo()
constructs a BESInfo object
Definition BESInfo.cc:52
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition BESInfo.cc:120
virtual void print(std::ostream &strm)
print the information from this informational object to the specified stream
Definition BESInfo.cc:249
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
Definition BESInfo.cc:262
virtual void add_data_from_file(const std::string &key, const std::string &name)
add data from a file to the informational object.
Definition BESInfo.cc:172
virtual void add_exception(const BESError &e, const std::string &admin)
add exception information to this informational object
Definition BESInfo.cc:221
exception thrown if internal error encountered
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
Access to the singleton.
Definition TheBESKeys.cc:85