bes  Updated for version 3.20.6
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 
43 using namespace std;
44 
45 #define BES_INFO_FILE_BUFFER_SIZE 4096
46 
53  _strm(0), _strm_owned(false), _buffered(true), _response_started(false)
54 {
55  _strm = new ostringstream;
56  _strm_owned = true;
57 }
58 
72 BESInfo::BESInfo(const string &key, ostream *strm, bool strm_owned) :
73  _strm(0), _strm_owned(false), _buffered(true), _response_started(false), _response_name("")
74 {
75  bool found = false;
76  vector < string > vals;
77  string b;
78  TheBESKeys::TheKeys()->get_value(key, b, found);
79  if (b == "true" || b == "True" || b == "TRUE" || b == "yes" || b == "Yes" || b == "YES") {
80  _strm = new ostringstream;
81  _strm_owned = true;
82  _buffered = true;
83  if (strm && strm_owned) delete strm;
84  }
85  else {
86  if (!strm) {
87  string s = "Informational response not buffered but no stream passed";
88  throw BESInternalError(s, __FILE__, __LINE__);
89  }
90  _strm = strm;
91  _strm_owned = strm_owned;
92  _buffered = false;
93  }
94 }
95 
96 BESInfo::~BESInfo()
97 {
98  if (_strm && _strm_owned) {
99  delete _strm;
100  _strm = 0;
101  }
102 }
103 
111 void BESInfo::begin_response(const string &response_name, map<string, string> */*attrs*/, BESDataHandlerInterface &/*dhi*/)
112 {
113  _response_started = true;
114  _response_name = response_name;
115 }
116 
124 void BESInfo::begin_response(const string &response_name, BESDataHandlerInterface &/*dhi*/)
125 {
126  _response_started = true;
127  _response_name = response_name;
128 }
129 
130 void BESInfo::end_response()
131 {
132  _response_started = false;
133  if (_tags.size()) {
134  string s = "Not all tags were ended in info response";
135  throw BESInternalError(s, __FILE__, __LINE__);
136  }
137 }
138 
139 void BESInfo::begin_tag(const string &tag_name, map<string, string> */*attrs*/)
140 {
141  _tags.push(tag_name);
142 }
143 
144 void BESInfo::end_tag(const string &tag_name)
145 {
146  if (_tags.size() == 0 || _tags.top() != tag_name) {
147  string s = (string) "tag " + tag_name + " already ended or not started";
148  throw BESInternalError(s, __FILE__, __LINE__);
149  }
150  else {
151  _tags.pop();
152  }
153 }
154 
160 void BESInfo::add_data(const string &s)
161 {
162  // If not buffered then we've been passed a stream to use.
163  // If it is buffered then we created the stream.
164  (*_strm) << s;
165 }
166 
182 void BESInfo::add_data_from_file(const string &key, const string &name)
183 {
184  bool found = false;
185  string file;
186  try {
187  TheBESKeys::TheKeys()->get_value(key, file, found);
188  }
189  catch (...) {
190  found = false;
191  }
192  if (found == false) {
193  add_data(name + " file key " + key + " not found, information not available\n");
194  }
195  else {
196  ifstream ifs(file.c_str());
197  int myerrno = errno;
198  if (!ifs) {
199  string serr = name + " file " + file + " not found, information not available: ";
200  char *err = strerror(myerrno);
201  if (err)
202  serr += err;
203  else
204  serr += "Unknown error";
205 
206  serr += "\n";
207 
208  add_data(serr);
209  }
210  else {
211  char line[BES_INFO_FILE_BUFFER_SIZE];
212  while (!ifs.eof()) {
213  ifs.getline(line, BES_INFO_FILE_BUFFER_SIZE);
214  if (!ifs.eof()) {
215  add_data(line);
216  add_data("\n");
217  }
218  }
219  ifs.close();
220  }
221  }
222 }
223 
234 void BESInfo::add_exception(BESError &e, const string &admin)
235 {
236  begin_tag("BESError");
237  ostringstream stype;
238  stype << e.get_bes_error_type();
239  add_tag("Type", stype.str());
240  add_tag("Message", e.get_message());
241  add_tag("Administrator", admin);
242 
243  begin_tag( "Location" );
244  add_tag( "File", e.get_file() );
245  ostringstream sline;
246  sline << e.get_line();
247  add_tag( "Line", sline.str() );
248  end_tag( "Location" );
249 
250  end_tag("BESError");
251 }
252 
261 void BESInfo::print(ostream &strm)
262 {
263  if (_buffered) {
264  strm << ((ostringstream *) _strm)->str();
265  }
266 }
267 
275 void BESInfo::dump(ostream &strm) const
276 {
277  strm << BESIndent::LMarg << "BESInfo::dump - (" << (void *) this << ")" << endl;
278  BESIndent::Indent();
279  strm << BESIndent::LMarg << "response name: " << _response_name << endl;
280  strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl;
281  strm << BESIndent::LMarg << "has response been started? " << _response_started << endl;
282  if (_tags.size()) {
283  strm << BESIndent::LMarg << "tags:" << endl;
284  BESIndent::Indent();
285  stack < string > temp_tags = _tags;
286  while (!temp_tags.empty()) {
287  string tag = temp_tags.top();
288  strm << BESIndent::LMarg << tag << endl;
289  temp_tags.pop();
290  }
291  BESIndent::UnIndent();
292  }
293  else {
294  strm << BESIndent::LMarg << "tags: empty" << endl;
295  }
296  BESIndent::UnIndent();
297 }
298 
BESError::get_bes_error_type
virtual int get_bes_error_type()
Return the return code for this error class.
Definition: BESError.h:143
BESError::get_line
virtual int get_line()
get the line number where the exception was thrown
Definition: BESError.h:115
BESInfo::print
virtual void print(std::ostream &strm)
print the information from this informational object to the specified stream
Definition: BESInfo.cc:261
BESError::get_message
virtual std::string get_message()
get the error message for this exception
Definition: BESError.h:99
BESError::get_file
virtual std::string get_file()
get the file name where the exception was thrown
Definition: BESError.h:107
BESInfo::add_data_from_file
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:182
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
BESInfo::add_exception
virtual void add_exception(BESError &e, const std::string &admin)
add exception information to this informational object
Definition: BESInfo.cc:234
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
TheBESKeys::get_value
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:272
BESInfo::add_data
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:160
BESInfo::dump
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:275
BESInfo::begin_response
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
BESInfo::BESInfo
BESInfo()
constructs a BESInfo object
Definition: BESInfo.cc:52
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
BESError
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58