bes  Updated for version 3.20.6
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 
49 #define FONC_TEMP_DIR "/tmp"
50 #define FONC_TEMP_DIR_KEY "FONc.Tempdir"
51 
52 // I think this should be true always. I'm leaving it in for now
53 // but the code does not use the key. Maybe this will be used when
54 // there is more comprehensive support for DAP4. jhrg 11/30/15
55 #define FONC_BYTE_TO_SHORT true
56 #define FONC_BYTE_TO_SHORT_KEY "FONc.ByteToShort"
57 
58 #define FONC_USE_COMP true
59 #define FONC_USE_COMP_KEY "FONc.UseCompression"
60 
61 #define FONC_CHUNK_SIZE 4096
62 #define FONC_CHUNK_SIZE_KEY "FONc.ChunkSize"
63 
64 #define FONC_CLASSIC_MODEL true
65 #define FONC_CLASSIC_MODEL_KEY "FONc.ClassicModel"
66 
67 std::string FONcRequestHandler::temp_dir;
68 bool FONcRequestHandler::byte_to_short;
69 bool FONcRequestHandler::use_compression;
70 int FONcRequestHandler::chunk_size;
71 bool FONcRequestHandler::classic_model;
72 
73 using namespace std;
74 
84 static void read_key_value(const string &key_name, bool &key, const bool default_value)
85 {
86  bool key_found = false;
87  string value;
88  TheBESKeys::TheKeys()->get_value(key_name, value, key_found);
89  // 'key' holds the string value at this point if key_found is true
90  if (key_found) {
91  value = BESUtil::lowercase(value);
92  key = (value == "true" || value == "yes");
93  }
94  else {
95  key = default_value;
96  }
97 }
98 
99 static void read_key_value(const string &key_name, string &key, const string &default_value)
100 {
101  bool key_found = false;
102  TheBESKeys::TheKeys()->get_value(key_name, key, key_found);
103  // 'key' holds the string value at this point if key_found is true
104  if (key_found) {
105  if (key[key.length() - 1] == '/') key.erase(key.length() - 1);
106  }
107  else {
108  key = default_value;
109  }
110 }
111 
112 static void read_key_value(const string &key_name, int &key, const int default_value)
113 {
114  bool key_found = false;
115  string value;
116  TheBESKeys::TheKeys()->get_value(key_name, value, key_found);
117  // 'key' holds the string value at this point if key_found is true
118  if (key_found) {
119  istringstream iss(value);
120  iss >> key;
121  if (iss.eof() || iss.bad() || iss.fail()) key = default_value;
122  }
123  else {
124  key = default_value;
125  }
126 }
127 
128 
138  : BESRequestHandler( name )
139 {
140  add_method( HELP_RESPONSE, FONcRequestHandler::build_help ) ;
142 
143  if (FONcRequestHandler::temp_dir.empty()) {
144  read_key_value(FONC_TEMP_DIR_KEY, FONcRequestHandler::temp_dir, FONC_TEMP_DIR);
145  }
146 
147  // Not currently used. jhrg 11/30/15
148  read_key_value(FONC_BYTE_TO_SHORT_KEY, FONcRequestHandler::byte_to_short, FONC_BYTE_TO_SHORT);
149 
150  read_key_value(FONC_USE_COMP_KEY, FONcRequestHandler::use_compression, FONC_USE_COMP);
151 
152  read_key_value(FONC_CHUNK_SIZE_KEY, FONcRequestHandler::chunk_size, FONC_CHUNK_SIZE);
153 
154  read_key_value(FONC_CLASSIC_MODEL_KEY, FONcRequestHandler::classic_model, FONC_CLASSIC_MODEL);
155 
156  BESDEBUG("fonc", "FONcRequestHandler::temp_dir: " << FONcRequestHandler::temp_dir << endl);
157  BESDEBUG("fonc", "FONcRequestHandler::byte_to_short: " << FONcRequestHandler::byte_to_short << endl);
158  BESDEBUG("fonc", "FONcRequestHandler::use_compression: " << FONcRequestHandler::use_compression << endl);
159  BESDEBUG("fonc", "FONcRequestHandler::chunk_size: " << FONcRequestHandler::chunk_size << endl);
160  BESDEBUG("fonc", "FONcRequestHandler::classic_model: " << FONcRequestHandler::classic_model << endl);
161 }
162 
166 {
167 }
168 
181 {
182  BESResponseObject *response = dhi.response_handler->get_response_object();
183  BESInfo *info = dynamic_cast<BESInfo *>(response);
184  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
185 
186  bool found = false;
187  string key = "FONc.Reference";
188  string ref;
189  TheBESKeys::TheKeys()->get_value(key, ref, found);
190  if (ref.empty()) ref = "http://docs.opendap.org/index.php/BES_-_Modules_-_FileOut_Netcdf";
191  map<string, string> attrs;
192  attrs["name"] = MODULE_NAME;
193  attrs["version"] = MODULE_VERSION;
194  attrs["reference"] = ref;
195  info->begin_tag("module", &attrs);
196  info->end_tag("module");
197 
198  return true;
199 }
200 
209 {
210  BESResponseObject *response = dhi.response_handler->get_response_object();
211  BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(response);
212  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
213 
214  info->add_module(MODULE_NAME, MODULE_VERSION);
215 
216  return true;
217 }
218 
225 void
226 FONcRequestHandler::dump( ostream &strm ) const
227 {
228  strm << BESIndent::LMarg << "FONcRequestHandler::dump - ("
229  << (void *)this << ")" << endl ;
230  BESIndent::Indent() ;
231  BESRequestHandler::dump( strm ) ;
232  BESIndent::UnIndent() ;
233 }
234 
BESRequestHandler
Represents a specific data type request handler.
Definition: BESRequestHandler.h:74
FONcRequestHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: FONcRequestHandler.cc:226
FONcRequestHandler::build_version
static bool build_version(BESDataHandlerInterface &dhi)
add version information to a version response
Definition: FONcRequestHandler.cc:208
BESInfo
informational response object
Definition: BESInfo.h:63
BESRequestHandler::add_method
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
Definition: BESRequestHandler.cc:58
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
BESResponseHandler::get_response_object
virtual BESResponseObject * get_response_object()
return the current response object
Definition: BESResponseHandler.cc:82
BESVersionInfo
Definition: BESVersionInfo.h:47
FONcRequestHandler::~FONcRequestHandler
virtual ~FONcRequestHandler(void)
Any cleanup that needs to take place.
Definition: FONcRequestHandler.cc:165
FONcRequestHandler::build_help
static bool build_help(BESDataHandlerInterface &dhi)
adds help information for FileOut NetCDF to a help request
Definition: FONcRequestHandler.cc:180
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
BESRequestHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESRequestHandler.cc:163
BESUtil::lowercase
static std::string lowercase(const std::string &s)
Definition: BESUtil.cc:200
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
FONcRequestHandler::FONcRequestHandler
FONcRequestHandler(const std::string &name)
Constructor for FileOut NetCDF module.
Definition: FONcRequestHandler.cc:137
BESResponseObject
Abstract base class representing a specific set of information in response to a request to the BES.
Definition: BESResponseObject.h:45