bes  Updated for version 3.20.13
FONcRequestHandler.cc
1 // FONcRequestHandler.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,2005 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 "config.h"
34 
35 #include <string>
36 #include <sstream>
37 
38 #include <BESResponseHandler.h>
39 #include <BESResponseNames.h>
40 #include <BESVersionInfo.h>
41 #include <BESDataNames.h>
42 #include <BESDataNames.h>
43 #include <TheBESKeys.h>
44 #include <BESDebug.h>
45 #include <BESUtil.h>
46 
47 #include "FONcRequestHandler.h"
48 #include "FONcNames.h"
49 
50 std::string FONcRequestHandler::temp_dir;
51 bool FONcRequestHandler::byte_to_short;
52 bool FONcRequestHandler::use_compression;
53 bool FONcRequestHandler::use_shuffle;
54 unsigned long long FONcRequestHandler::chunk_size;
55 bool FONcRequestHandler::classic_model;
56 bool FONcRequestHandler::no_global_attrs;
57 unsigned long long FONcRequestHandler::request_max_size_kb;
58 bool FONcRequestHandler::nc3_classic_format;
59 
60 using namespace std;
61 
71 static void read_key_value(const string &key_name, bool &key, const bool default_value)
72 {
73  bool key_found = false;
74  string value;
75  TheBESKeys::TheKeys()->get_value(key_name, value, key_found);
76  // 'key' holds the string value at this point if key_found is true
77  if (key_found) {
78  value = BESUtil::lowercase(value);
79  key = (value == "true" || value == "yes");
80  }
81  else {
82  key = default_value;
83  }
84 }
85 
86 static void read_key_value(const string &key_name, string &key, const string &default_value)
87 {
88  bool key_found = false;
89  TheBESKeys::TheKeys()->get_value(key_name, key, key_found);
90  // 'key' holds the string value at this point if key_found is true
91  if (key_found) {
93  }
94  else {
95  key = default_value;
96  }
97 }
98 
99 static void read_key_value(const string &key_name, size_t &key, const int default_value)
100 {
101  bool key_found = false;
102  string value;
103  TheBESKeys::TheKeys()->get_value(key_name, value, key_found);
104  // 'key' holds the string value at this point if key_found is true
105  if (key_found) {
106  istringstream iss(value);
107  iss >> key;
108  // if (iss.eof() || iss.bad() || iss.fail()) key = default_value;
109  if (iss.bad() || iss.fail()) key = default_value;
110  }
111  else {
112  key = default_value;
113  }
114 }
115 
116 static void read_key_value(const string &key_name, unsigned long long &key, const unsigned long long default_value)
117 {
118  bool key_found = false;
119  string value;
120  TheBESKeys::TheKeys()->get_value(key_name, value, key_found);
121  // 'key' holds the string value at this point if key_found is true
122  if (key_found) {
123  try {
124  key = stoull(value);
125  }
126  catch (const std::invalid_argument& ia) {
127  key = default_value;
128  }
129  }
130  else {
131  key = default_value;
132  }
133 }
134 
135 
145  : BESRequestHandler( name )
146 {
147  add_method( HELP_RESPONSE, FONcRequestHandler::build_help ) ;
149 
150  if (FONcRequestHandler::temp_dir.empty()) {
151  read_key_value(FONC_TEMP_DIR_KEY, FONcRequestHandler::temp_dir, FONC_TEMP_DIR);
152  }
153 
154  // Not currently used. jhrg 11/30/15
155  read_key_value(FONC_BYTE_TO_SHORT_KEY, FONcRequestHandler::byte_to_short, FONC_BYTE_TO_SHORT);
156 
157  read_key_value(FONC_USE_COMP_KEY, FONcRequestHandler::use_compression, FONC_USE_COMP);
158 
159  read_key_value(FONC_USE_SHUFFLE_KEY, FONcRequestHandler::use_shuffle, FONC_USE_SHUFFLE);
160 
161  read_key_value(FONC_CHUNK_SIZE_KEY, FONcRequestHandler::chunk_size, FONC_CHUNK_SIZE);
162 
163  read_key_value(FONC_CLASSIC_MODEL_KEY, FONcRequestHandler::classic_model, FONC_CLASSIC_MODEL);
164 
165  read_key_value(FONC_NO_GLOBAL_ATTRS_KEY, FONcRequestHandler::no_global_attrs, FONC_NO_GLOBAL_ATTRS);
166 
167  read_key_value(FONC_REQUEST_MAX_SIZE_KB_KEY, FONcRequestHandler::request_max_size_kb, FONC_REQUEST_MAX_SIZE_KB);
168 
169  read_key_value(FONC_NC3_CLASSIC_FORMAT_KEY, FONcRequestHandler::nc3_classic_format, FONC_NC3_CLASSIC_FORMAT);
170 
171  BESDEBUG("fonc", "FONcRequestHandler::temp_dir: " << FONcRequestHandler::temp_dir << endl);
172  BESDEBUG("fonc", "FONcRequestHandler::byte_to_short: " << FONcRequestHandler::byte_to_short << endl);
173  BESDEBUG("fonc", "FONcRequestHandler::use_compression: " << FONcRequestHandler::use_compression << endl);
174  BESDEBUG("fonc", "FONcRequestHandler::use_shuffle: " << FONcRequestHandler::use_shuffle << endl);
175  BESDEBUG("fonc", "FONcRequestHandler::chunk_size: " << FONcRequestHandler::chunk_size << endl);
176  BESDEBUG("fonc", "FONcRequestHandler::classic_model: " << FONcRequestHandler::classic_model << endl);
177  BESDEBUG("fonc", "FONcRequestHandler::turn_off_global_attrs: " << FONcRequestHandler::no_global_attrs << endl);
178  BESDEBUG("fonc", "FONcRequestHandler::request_max_size_kb: " << FONcRequestHandler::request_max_size_kb << endl);
179  BESDEBUG("fonc", "FONcRequestHandler::nc3_classic_format " << FONcRequestHandler::nc3_classic_format << endl);
180 }
181 
185 {
186 }
187 
200 {
201  BESResponseObject *response = dhi.response_handler->get_response_object();
202  BESInfo *info = dynamic_cast<BESInfo *>(response);
203  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
204 
205  bool found = false;
206  string key = "FONc.Reference";
207  string ref;
208  TheBESKeys::TheKeys()->get_value(key, ref, found);
209  if (ref.empty()) ref = "https://docs.opendap.org/index.php/BES_-_Modules_-_FileOut_Netcdf";
210  map<string, string> attrs;
211  attrs["name"] = MODULE_NAME;
212  attrs["version"] = MODULE_VERSION;
213  attrs["reference"] = ref;
214  info->begin_tag("module", &attrs);
215  info->end_tag("module");
216 
217  return true;
218 }
219 
228 {
229  BESResponseObject *response = dhi.response_handler->get_response_object();
230  BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(response);
231  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
232 
233  info->add_module(MODULE_NAME, MODULE_VERSION);
234 
235  return true;
236 }
237 
244 void
245 FONcRequestHandler::dump( ostream &strm ) const
246 {
247  strm << BESIndent::LMarg << "FONcRequestHandler::dump - ("
248  << (void *)this << ")" << endl ;
249  BESIndent::Indent() ;
250  BESRequestHandler::dump( strm ) ;
251  BESIndent::UnIndent() ;
252 }
253 
Structure storing information used by the BES to handle the request.
informational response object
Definition: BESInfo.h:63
exception thrown if internal error encountered
Represents a specific data type request handler.
virtual bool add_method(const std::string &name, p_request_handler_method method)
add a handler method to the request handler that knows how to fill in a specific response object
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual BESResponseObject * get_response_object()
return the current response object
Abstract base class representing a specific set of information in response to a request to the BES.
static std::string lowercase(const std::string &s)
Definition: BESUtil.cc:254
static void trim_if_trailing_slash(std::string &value)
If the string ends in a slash, remove it This function works for empty strings (doing nothing)....
Definition: BESUtil.cc:110
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual ~FONcRequestHandler(void)
Any cleanup that needs to take place.
FONcRequestHandler(const std::string &name)
Constructor for FileOut NetCDF module.
static bool build_help(BESDataHandlerInterface &dhi)
adds help information for FileOut NetCDF to a help request
static bool build_version(BESDataHandlerInterface &dhi)
add version information to a version response
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:340
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71