bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
NCMLRequestHandler.cc
1
2// This file is part of the "NcML Module" project, a BES module designed
3// to allow NcML files to be used to be used as a wrapper to add
4// AIS to existing datasets of any format.
5//
6// Copyright (c) 2009 OPeNDAP, Inc.
7// Author: Michael Johnson <m.johnson@opendap.org>
8//
9// For more information, please also see the main website: http://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// Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26//
27// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29
30#include "config.h"
31
32#include <memory>
33
34#include <libdap/DMR.h>
35#include <libdap/DataDDS.h>
36
37#include <libdap/mime_util.h>
38#include <libdap/D4BaseTypeFactory.h>
39
40#include "NCMLRequestHandler.h"
41
42#include <BESConstraintFuncs.h>
43#include <BESContainerStorage.h>
44#include <BESContainerStorageList.h>
45#include <BESDapNames.h>
46#include "BESDataDDSResponse.h"
47#include <BESDataNames.h>
48#include <BESDASResponse.h>
49#include <BESDDSResponse.h>
50#include <BESDMRResponse.h>
51
52#include <BESDebug.h>
53#include "BESStopWatch.h"
54#include <BESInternalError.h>
55#include <BESDapError.h>
56#include <BESError.h>
57#include <BESRequestHandlerList.h>
58#include <BESResponseHandler.h>
59#include <BESResponseNames.h>
60#include <BESServiceRegistry.h>
61#include <BESTextInfo.h>
62#include <BESUtil.h>
63#include <BESVersionInfo.h>
64#include <TheBESKeys.h>
65
66#include "DDSLoader.h"
67
68#include "NCMLDebug.h"
69#include "NCMLUtil.h"
70#include "NCMLParser.h"
71#include "NCMLResponseNames.h"
72#include "SimpleLocationParser.h"
73
74using namespace agg_util;
75using namespace ncml_module;
76using namespace libdap;
77
78#define MODULE "ncml"
79#define prolog std::string("NCMLRequestHandler::").append(__func__).append("() - ")
80
81
82bool NCMLRequestHandler::_global_attributes_container_name_set = false;
83string NCMLRequestHandler::_global_attributes_container_name;
84
85NCMLRequestHandler::NCMLRequestHandler(const string &name) :
87{
88 add_method(DAS_RESPONSE, NCMLRequestHandler::ncml_build_das);
89 add_method(DDS_RESPONSE, NCMLRequestHandler::ncml_build_dds);
90 add_method(DATA_RESPONSE, NCMLRequestHandler::ncml_build_data);
91
92 add_method(DMR_RESPONSE, NCMLRequestHandler::ncml_build_dmr);
93 add_method(DAP4DATA_RESPONSE, NCMLRequestHandler::ncml_build_dmr);
94
95 add_method(VERS_RESPONSE, NCMLRequestHandler::ncml_build_vers);
96 add_method(HELP_RESPONSE, NCMLRequestHandler::ncml_build_help);
97
98 if (NCMLRequestHandler::_global_attributes_container_name_set == false) {
99 bool key_found = false;
100 string value;
101 TheBESKeys::TheKeys()->get_value("NCML.GlobalAttributesContainerName", value, key_found);
102 if (key_found) {
103 // It was set in the conf file
104 NCMLRequestHandler::_global_attributes_container_name_set = true;
105
106 NCMLRequestHandler::_global_attributes_container_name = value;
107 }
108 }
109}
110
111NCMLRequestHandler::~NCMLRequestHandler()
112{
113}
114
115#if 0
116// Not used. jhrg 4/16/14
117
118// This is the original example from Patrick or James for loading local file within the BES...
119// Still used by DataDDS call, but the other callbacks use DDSLoader
120// to get a brandy new DDX response.
121// @see DDSLoader
122bool NCMLRequestHandler::ncml_build_redirect(BESDataHandlerInterface &dhi, const string& location)
123{
124 // The current container in dhi is a reference to the ncml file.
125 // Need to parse the ncml file here and get the list of locations
126 // that we will be using. Any constraints defined?
127
128 // do this for each of the locations retrieved from the ncml file.
129 // If there are more than one locations in the ncml then we can't
130 // set the context for dap_format to dap2. This will create a
131 // structure for each of the locations in the resulting dap object.
132 string sym_name = dhi.container->get_symbolic_name();
133 BESContainerStorageList *store_list = BESContainerStorageList::TheList();
134 BESContainerStorage *store = store_list->find_persistence("catalog");
135 if (!store) {
136 throw BESInternalError("couldn't find the catalog storage", __FILE__, __LINE__);
137 }
138 // this will throw an exception if the location isn't found in the
139 // catalog. Might want to catch this. Wish the add returned the
140 // container object created. Might want to change it.
141 string new_sym = sym_name + "_location1";
142 store->add_container(new_sym, location, "");
143
144 BESContainer *container = store->look_for(new_sym);
145 if (!container) {
146 throw BESInternalError("couldn't find the container" + sym_name, __FILE__, __LINE__);
147 }
148 BESContainer *ncml_container = dhi.container;
149 dhi.container = container;
150
151 // this will throw an exception if there is a problem building the
152 // response for this container. Might want to catch this
153 BESRequestHandlerList::TheList()->execute_current(dhi);
154
155 // clean up
156 dhi.container = ncml_container;
157 store->del_container(new_sym);
158
159 return true;
160}
161#endif
162
163// Here we load the DDX response with by hijacking the current dhi via DDSLoader
164// and hand it to our parser to load the ncml, load the DDX for the location,
165// apply ncml transformations to it, then return the modified DDS.
166bool NCMLRequestHandler::ncml_build_das(BESDataHandlerInterface &dhi)
167{
168 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timer", &dhi);
169
170 string filename = dhi.container->access();
171
172 // Any exceptions winding through here will cause the loader and parser dtors
173 // to clean up dhi state, etc.
174 DDSLoader loader(dhi);
175 NCMLParser parser(loader);
176 unique_ptr<BESDapResponse> loaded_bdds = parser.parse(filename, DDSLoader::eRT_RequestDDX);
177
178 // Now fill in the desired DAS response object from the DDS
179 DDS* dds = NCMLUtil::getDDSFromEitherResponse(loaded_bdds.get());
180 VALID_PTR(dds);
181
182 BESDASResponse *bdas = dynamic_cast<BESDASResponse *>(dhi.response_handler->get_response_object());
183 VALID_PTR(bdas);
184
185 // Copy the modified DDS attributes into the DAS response object!
186 DAS *das = bdas->get_das();
187
188 if (dds->get_dap_major() < 4)
189 NCMLUtil::hackGlobalAttributesForDAP2(dds->get_attr_table(),
190 NCMLRequestHandler::get_global_attributes_container_name());
191
193
194 // loaded_bdds destroys itself.
195 return true;
196}
197
198bool NCMLRequestHandler::ncml_build_dds(BESDataHandlerInterface &dhi)
199{
200#if 0
201 // original version 8/13/15
202 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timer", &dhi);
203
204 string filename = dhi.container->access();
205
206 // Any exceptions winding through here will cause the loader and parser dtors
207 // to clean up dhi state, etc.
208 unique_ptr<BESDapResponse> loaded_bdds(0);
209 {
210 DDSLoader loader(dhi);
211 NCMLParser parser(loader);
212 loaded_bdds = parser.parse(filename, DDSLoader::eRT_RequestDDX);
213 }
214 if (!loaded_bdds.get()) {
215 throw BESInternalError("Null BESDDSResonse in ncml DDS handler.", __FILE__, __LINE__);
216 }
217
218 // Poke the handed down original response object with the loaded and modified one.
219 DDS* dds = NCMLUtil::getDDSFromEitherResponse(loaded_bdds.get());
220 VALID_PTR(dds);
221 BESResponseObject *response = dhi.response_handler->get_response_object();
222 BESDDSResponse *bdds_out = dynamic_cast<BESDDSResponse *>(response);
223 VALID_PTR(bdds_out);
224 DDS *dds = bdds_out->get_dds();
225 VALID_PTR(dds);
226
227 if (dds->get_dap_major() < 4)
228 NCMLUtil::hackGlobalAttributesForDAP2(dds->get_attr_table(),
229 NCMLRequestHandler::get_global_attributes_container_name());
230
231 // If we just use DDS::operator=, we get into trouble with copied
232 // pointers, bashing of the dataset name, etc etc so I specialize the copy for now.
234
235 // Apply constraints to the result
236 // See comment below. jhrg 8/12/15 dhi.data[POST_CONSTRAINT] = dhi.container->get_constraint();
237 bdds_out->set_constraint(dhi);
238
239 // Also copy in the name of the original ncml request
240 // TODO @HACK Not sure I want just the basename for the filename,
241 // but since the DDS/DataDDS response fills the dataset name with it,
242 // Our bes-testsuite fails since we get local path info in the dataset name.
243 dds->filename(name_path(filename));
244 dds->set_dataset_name(name_path(filename));
245
246 return true;
247#endif
248
249 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timer", &dhi);
250
251 string filename = dhi.container->access();
252
253 // it better be a data response!
254 BESDDSResponse* ddsResponse = dynamic_cast<BESDDSResponse *>(dhi.response_handler->get_response_object());
255 NCML_ASSERT_MSG(ddsResponse,
256 "NCMLRequestHandler::ncml_build_data(): expected BESDDSResponse* but didn't get it!!");
257
258 // Block it up to force cleanup of DHI.
259 {
260 DDSLoader loader(dhi);
261 NCMLParser parser(loader);
262 parser.parseInto(filename, DDSLoader::eRT_RequestDDX, ddsResponse);
263 }
264
265 DDS *dds = ddsResponse->get_dds();
266 VALID_PTR(dds);
267
268 if (dds->get_dap_major() < 4)
269 NCMLUtil::hackGlobalAttributesForDAP2(dds->get_attr_table(),
270 NCMLRequestHandler::get_global_attributes_container_name());
271
272 // Apply constraints to the result
273 // See comment below. jhrg 8/12/15 dhi.data[POST_CONSTRAINT] = dhi.container->get_constraint();
274 ddsResponse->set_constraint(dhi);
275
276 // Also copy in the name of the original ncml request
277 dds->filename(name_path(filename));
278 dds->set_dataset_name(name_path(filename));
279
280 return true;
281}
282
283bool NCMLRequestHandler::ncml_build_data(BESDataHandlerInterface &dhi)
284{
285 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timer", &dhi);
286
287 string filename = dhi.container->access();
288
289 // it better be a data response!
290 BESDataDDSResponse* dataResponse = dynamic_cast<BESDataDDSResponse *>(dhi.response_handler->get_response_object());
291 NCML_ASSERT_MSG(dataResponse,
292 "NCMLRequestHandler::ncml_build_data(): expected BESDataDDSResponse* but didn't get it!!");
293
294 // Block it up to force cleanup of DHI.
295 {
296 DDSLoader loader(dhi);
297 NCMLParser parser(loader);
298 parser.parseInto(filename, DDSLoader::eRT_RequestDataDDS, dataResponse);
299 }
300
301 // Apply constraints to the result
302
303 // dhi.data[POST_CONSTRAINT] = dhi.container->get_constraint();
304 // Replaced the above with the code below. P West said, a while ago, that using set_constraint
305 // was better because BES containers would be supported. Not sure if that's a factor in this
306 // code... jhrg 8/12/15
307 dataResponse->set_constraint(dhi);
308
309 // Also copy in the name of the original ncml request
310 DDS* dds = NCMLUtil::getDDSFromEitherResponse(dataResponse);
311 VALID_PTR(dds);
312
313 dds->filename(name_path(filename));
314 dds->set_dataset_name(name_path(filename));
315
316 return true;
317}
318
319bool NCMLRequestHandler::ncml_build_dmr(BESDataHandlerInterface &dhi)
320{
321 BES_STOPWATCH_START_DHI(MODULE, prolog + "Timer", &dhi);
322
323 // Because this code does not yet know how to build a DMR directly, use
324 // the DMR ctor that builds a DMR using a 'full DDS' (a DDS with attributes).
325 // First step, build the 'full DDS'
326 string data_path = dhi.container->access();
327
328 DDS *dds = 0; // This will be deleted when loaded_bdds goes out of scope.
329 unique_ptr<BESDapResponse> loaded_bdds;
330 try {
331 DDSLoader loader(dhi);
332 NCMLParser parser(loader);
333 loaded_bdds = parser.parse(data_path, DDSLoader::eRT_RequestDDX);
334 if (!loaded_bdds.get()) throw BESInternalError("Null BESDDSResonse in the NCML DDS handler.", __FILE__, __LINE__);
335 dds = NCMLUtil::getDDSFromEitherResponse(loaded_bdds.get());
336 VALID_PTR(dds);
337 dds->filename(data_path);
338 dds->set_dataset_name(data_path);
339 }
340 catch (InternalErr &e) {
341 throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
342 }
343 catch (Error &e) {
344 throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
345 }
346 catch (BESError &e){
347 throw;
348 }
349 catch (...) {
350 throw BESDapError("Caught unknown error build ** DMR response", true, unknown_error, __FILE__, __LINE__);
351 }
352
353 // Extract the DMR Response object - this holds the DMR used by the
354 // other parts of the framework.
355 BESResponseObject *response = dhi.response_handler->get_response_object();
356 BESDMRResponse &bdmr = dynamic_cast<BESDMRResponse &>(*response);
357
358 // Get the DMR made by the BES in the BES/dap/BESDMRResponseHandler, make sure there's a
359 // factory we can use and then dump the DAP2 variables and attributes in using the
360 // BaseType::transform_to_dap4() method that transforms individual variables
361 DMR *dmr = bdmr.get_dmr();
362 dmr->set_factory(new D4BaseTypeFactory);
363 dmr->build_using_dds(*dds);
364
365 // Instead of fiddling with the internal storage of the DHI object,
366 // (by setting dhi.data[DAP4_CONSTRAINT], etc., directly) use these
367 // methods to set the constraints. But, why? Ans: from Patrick is that
368 // in the 'container' mode of BES each container can have a different
369 // CE.
370 bdmr.set_dap4_constraint(dhi);
371 bdmr.set_dap4_function(dhi);
372
373 return true;
374}
375
376bool NCMLRequestHandler::ncml_build_vers(BESDataHandlerInterface &dhi)
377{
378 BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(dhi.response_handler->get_response_object());
379 if (!info) throw InternalErr(__FILE__, __LINE__, "Expected a BESVersionInfo instance");
380
381 info->add_module(MODULE_NAME, MODULE_VERSION);
382 return true;
383}
384
385bool NCMLRequestHandler::ncml_build_help(BESDataHandlerInterface &dhi)
386{
387 BESInfo *info = dynamic_cast<BESInfo *>(dhi.response_handler->get_response_object());
388 if (!info) throw InternalErr(__FILE__, __LINE__, "Expected a BESVersionInfo instance");
389
390 // This is an example. If you had a help file you could load it like
391 // this and if your handler handled the following responses.
392 map<string, string, std::less<>> attrs;
393 attrs["name"] = MODULE_NAME;
394 attrs["version"] = MODULE_VERSION;
395
396 list<string> services;
397 BESServiceRegistry::TheRegistry()->services_handled(ncml_module::ModuleConstants::NCML_NAME, services);
398 if (services.size() > 0) {
399 string handles = BESUtil::implode(services, ',');
400 attrs["handles"] = handles;
401 }
402 info->begin_tag("module", &attrs);
403 //info->add_data_from_file( "NCML.Help", "NCML Help" ) ;
404 info->add_data("Please consult the online documentation at " + ncml_module::ModuleConstants::DOC_WIKI_URL);
405 info->end_tag("module");
406
407 return true;
408}
409
411{
412 strm << BESIndent::LMarg << "NCMLRequestHandler::dump - (" << (void *) this << ")" << endl;
413 BESIndent::Indent();
415 BESIndent::UnIndent();
416}
417
virtual bool del_container(const std::string &s_name)=0
removes a container with the given symbolic name
virtual void add_container(const std::string &sym_name, const std::string &real_name, const std::string &type)=0
adds a container with the provided information
virtual BESContainer * look_for(const std::string &sym_name)=0
looks for a container in this persistent store
std::string get_symbolic_name() const
retrieve the symbolic name for this container
virtual std::string access()=0
returns the true name of this container
libdap::DDS * get_dds()
virtual void set_dap4_function(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void set_dap4_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void set_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
BESContainer * container
pointer to current container in this interface
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
Represents a specific data type request handler.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual BESResponseObject * get_response_object()
return the current response object
virtual void services_handled(const std::string &handler, std::list< std::string > &services)
returns the list of servies provided by the handler in question
static std::string implode(const std::list< std::string > &values, char delim)
Definition BESUtil.cc:620
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
virtual void dump(std::ostream &strm) const
dumps information about this object
static libdap::DDS * getDDSFromEitherResponse(BESDapResponse *response)
Definition NCMLUtil.cc:356
static void populateDASFromDDS(libdap::DAS *das, const libdap::DDS &dds_const)
Definition NCMLUtil.cc:276
static void copyVariablesAndAttributesInto(libdap::DDS *dds_out, const libdap::DDS &dds_in)
Definition NCMLUtil.cc:332
STL class.
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
static const std::string DOC_WIKI_URL
static const std::string NCML_NAME