-
Notifications
You must be signed in to change notification settings - Fork 2
/
NCRequestHandler.cc
628 lines (532 loc) · 21 KB
/
NCRequestHandler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of nc_handler, a data handler for the OPeNDAP data
// server.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your
// option) any later version.
//
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// NCRequestHandler.cc
#include "config_nc.h"
#include <string>
#include <sstream>
#include <exception>
#include <DMR.h>
#include <DataDDS.h>
#include <mime_util.h>
#include <D4BaseTypeFactory.h>
#include <BESResponseHandler.h>
#include <BESResponseNames.h>
#include <BESDapNames.h>
#include <BESDASResponse.h>
#include <BESDDSResponse.h>
#include <BESDataDDSResponse.h>
#include <BESVersionInfo.h>
#include <BESDapError.h>
#include <BESInternalFatalError.h>
#include <BESDataNames.h>
#include <TheBESKeys.h>
#include <BESServiceRegistry.h>
#include <BESUtil.h>
#include <BESDebug.h>
#include <BESStopWatch.h>
#include <BESContextManager.h>
#include <BESDMRResponse.h>
#include <ObjMemCache.h>
#include <InternalErr.h>
#include <Ancillary.h>
#include "NCRequestHandler.h"
#define NC_NAME "nc"
using namespace libdap;
bool NCRequestHandler::_show_shared_dims = true;
bool NCRequestHandler::_show_shared_dims_set = false;
bool NCRequestHandler::_ignore_unknown_types = false;
bool NCRequestHandler::_ignore_unknown_types_set = false;
bool NCRequestHandler::_promote_byte_to_short = false;
bool NCRequestHandler::_promote_byte_to_short_set = false;
unsigned int NCRequestHandler::_cache_entries = 100;
float NCRequestHandler::_cache_purge_level = 0.2;
ObjMemCache *NCRequestHandler::das_cache = 0;
ObjMemCache *NCRequestHandler::dds_cache = 0;
ObjMemCache *NCRequestHandler::dmr_cache = 0;
extern void nc_read_dataset_attributes(DAS & das, const string & filename);
extern void nc_read_dataset_variables(DDS & dds, const string & filename);
/** Is the version number string greater than or equal to the value.
* @note Works only for versions with zero or one dot. If the conversion of
* the string to a float fails for any reason, this returns false.
* @param version The string value (e.g., 3.2)
* @param value A floating point value.
*/
static bool version_ge(const string &version, float value)
{
try {
float v;
istringstream iss(version);
iss >> v;
//cerr << "version: " << v << ", value: " << value << endl;
return (v >= value);
}
catch (...) {
return false;
}
return false; // quiet warnings...
}
#if 0
/**
* Stolen from the HDF5 handler code
*/
static bool get_bool_key(const string &key, bool def_val)
{
bool found = false;
string doset = "";
const string dosettrue = "true";
const string dosetyes = "yes";
TheBESKeys::TheKeys()->get_value(key, doset, found);
if (true == found) {
doset = BESUtil::lowercase(doset);
return (dosettrue == doset || dosetyes == doset);
}
return def_val;
}
#endif
static unsigned int get_uint_key(const string &key, unsigned int def_val)
{
bool found = false;
string doset = "";
TheBESKeys::TheKeys()->get_value(key, doset, found);
if (true == found) {
return atoi(doset.c_str()); // use better code TODO
}
else {
return def_val;
}
}
static float get_float_key(const string &key, float def_val)
{
bool found = false;
string doset = "";
TheBESKeys::TheKeys()->get_value(key, doset, found);
if (true == found) {
return atof(doset.c_str()); // use better code TODO
}
else {
return def_val;
}
}
NCRequestHandler::NCRequestHandler(const string &name) :
BESRequestHandler(name)
{
BESDEBUG(NC_NAME, "In NCRequestHandler::NCRequestHandler" << endl);
add_handler(DAS_RESPONSE, NCRequestHandler::nc_build_das);
add_handler(DDS_RESPONSE, NCRequestHandler::nc_build_dds);
add_handler(DATA_RESPONSE, NCRequestHandler::nc_build_data);
add_handler(DMR_RESPONSE, NCRequestHandler::nc_build_dmr);
add_handler(DAP4DATA_RESPONSE, NCRequestHandler::nc_build_dmr);
add_handler(HELP_RESPONSE, NCRequestHandler::nc_build_help);
add_handler(VERS_RESPONSE, NCRequestHandler::nc_build_version);
// TODO replace with get_bool_key above 5/21/16 jhrg
// Look for the SHowSharedDims property, if it has not been set
if (NCRequestHandler::_show_shared_dims_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.ShowSharedDimensions", doset, key_found);
if (key_found) {
// It was set in the conf file
NCRequestHandler::_show_shared_dims_set = true;
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes") {
NCRequestHandler::_show_shared_dims = true;
}
else
NCRequestHandler::_show_shared_dims = false;
}
}
if (NCRequestHandler::_ignore_unknown_types_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.IgnoreUnknownTypes", doset, key_found);
if (key_found) {
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes")
NCRequestHandler::_ignore_unknown_types = true;
else
NCRequestHandler::_ignore_unknown_types = false;
NCRequestHandler::_ignore_unknown_types_set = true;
}
}
if (NCRequestHandler::_promote_byte_to_short_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.PromoteByteToShort", doset, key_found);
if (key_found) {
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes")
NCRequestHandler::_promote_byte_to_short = true;
else
NCRequestHandler::_promote_byte_to_short = false;
NCRequestHandler::_promote_byte_to_short_set = true;
}
}
NCRequestHandler::_cache_entries = get_uint_key("NC.CacheEntries", 0);
NCRequestHandler::_cache_purge_level = get_float_key("NC.CachePurgeLevel", 0.2);
if (get_cache_entries()) { // else it stays at its default of null
das_cache = new ObjMemCache(get_cache_entries(), get_cache_purge_level());
dds_cache = new ObjMemCache(get_cache_entries(), get_cache_purge_level());
dmr_cache = new ObjMemCache(get_cache_entries(), get_cache_purge_level());
}
BESDEBUG(NC_NAME, "Exiting NCRequestHandler::NCRequestHandler" << endl);
}
NCRequestHandler::~NCRequestHandler()
{
delete das_cache;
delete dds_cache;
delete dmr_cache;
}
bool NCRequestHandler::nc_build_das(BESDataHandlerInterface & dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_das", dhi.data[REQUEST_ID]);
BESDEBUG(NC_NAME, "In NCRequestHandler::nc_build_das" << endl);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDASResponse *bdas = dynamic_cast<BESDASResponse *> (response);
if (!bdas)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
string container_name = bdas->get_explicit_containers() ? dhi.container->get_symbolic_name(): "";
DAS *das = bdas->get_das();
if (!container_name.empty()) das->container_name(container_name);
string accessed = dhi.container->access();
// Look in memory cache if it's initialized
DAS *cached_das_ptr = 0;
if (das_cache && (cached_das_ptr = static_cast<DAS*>(das_cache->get(accessed)))) {
// copy the cached DAS into the BES response object
BESDEBUG(NC_NAME, "DAS Cached hit for : " << accessed << endl);
*das = *cached_das_ptr;
}
else {
nc_read_dataset_attributes(*das, accessed);
Ancillary::read_ancillary_das(*das, accessed);
if (das_cache) {
// add a copy
BESDEBUG(NC_NAME, "DAS added to the cache for : " << accessed << endl);
das_cache->add(new DAS(*das), accessed);
}
}
bdas->clear_container();
}
catch (BESError &e) {
throw;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DAS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
BESDEBUG(NC_NAME, "Exiting NCRequestHandler::nc_build_das" << endl);
return true;
}
/**
* @brief Using the empty DDS object, build a DDS
* @param dataset_name
* @param container_name
* @param dds
*/
void NCRequestHandler::get_dds_with_attributes(const string& dataset_name, const string& container_name, DDS* dds)
{
// Look in memory cache if it's initialized
DDS* cached_dds_ptr = 0;
if (dds_cache && (cached_dds_ptr = static_cast<DDS*>(dds_cache->get(dataset_name)))) {
// copy the cached DDS into the BES response object. Assume that any cached DDS
// includes the DAS information.
BESDEBUG(NC_NAME, "DDS Cached hit for : " << dataset_name << endl);
*dds = *cached_dds_ptr; // Copy the referenced object
}
else {
if (!container_name.empty()) dds->container_name(container_name);
dds->filename(dataset_name);
nc_read_dataset_variables(*dds, dataset_name);
DAS* das = 0;
if (das_cache && (das = static_cast<DAS*>(das_cache->get(dataset_name)))) {
BESDEBUG(NC_NAME, "DAS Cached hit for : " << dataset_name << endl);
dds->transfer_attributes(das); // no need to cop the cached DAS
}
else {
das = new DAS;
// This looks at the 'use explicit containers' prop, and if true
// sets the current container for the DAS.
if (!container_name.empty()) das->container_name(container_name);
nc_read_dataset_attributes(*das, dataset_name);
Ancillary::read_ancillary_das(*das, dataset_name);
dds->transfer_attributes(das);
// Only free the DAS if it's not added to the cache
if (das_cache) {
// add a copy
BESDEBUG(NC_NAME, "DAS added to the cache for : " << dataset_name << endl);
das_cache->add(das, dataset_name);
}
else {
delete das;
}
}
if (dds_cache) {
// add a copy
BESDEBUG(NC_NAME, "DDS added to the cache for : " << dataset_name << endl);
dds_cache->add(new DDS(*dds), dataset_name);
}
}
}
bool NCRequestHandler::nc_build_dds(BESDataHandlerInterface & dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_dds", dhi.data[REQUEST_ID]);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDDSResponse *bdds = dynamic_cast<BESDDSResponse *> (response);
if (!bdds)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
// If there's no value for this set in the conf file, look at the context
// and set the default behavior based on the protocol version clients say
// they will accept.
if (NCRequestHandler::_show_shared_dims_set == false) {
bool context_found = false;
string context_value = BESContextManager::TheManager()->get_context("xdap_accept", context_found);
if (context_found) {
BESDEBUG(NC_NAME, "xdap_accept: " << context_value << endl);
if (version_ge(context_value, 3.2))
NCRequestHandler::_show_shared_dims = false;
else
NCRequestHandler::_show_shared_dims = true;
}
}
string container_name = bdds->get_explicit_containers() ? dhi.container->get_symbolic_name(): "";
DDS *dds = bdds->get_dds();
// Build a DDS in the empty DDS object
get_dds_with_attributes(dhi.container->access(), container_name, dds);
bdds->set_constraint(dhi);
bdds->clear_container();
}
catch (BESError &e) {
throw e;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DDS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
return true;
}
bool NCRequestHandler::nc_build_data(BESDataHandlerInterface & dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_data", dhi.data[REQUEST_ID]);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDataDDSResponse *bdds = dynamic_cast<BESDataDDSResponse *> (response);
if (!bdds)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
if (NCRequestHandler::_show_shared_dims_set == false) {
bool context_found = false;
string context_value = BESContextManager::TheManager()->get_context("xdap_accept", context_found);
if (context_found) {
BESDEBUG(NC_NAME, "xdap_accept: " << context_value << endl);
if (version_ge(context_value, 3.2))
NCRequestHandler::_show_shared_dims = false;
else
NCRequestHandler::_show_shared_dims = true;
}
}
string container_name = bdds->get_explicit_containers() ? dhi.container->get_symbolic_name(): "";
DDS *dds = bdds->get_dds();
// Build a DDS in the empty DDS object
get_dds_with_attributes(dhi.container->access(), container_name, dds);
bdds->set_constraint(dhi);
bdds->clear_container();
}
catch (BESError &e) {
throw;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DAS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
return true;
}
bool NCRequestHandler::nc_build_dmr(BESDataHandlerInterface &dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_dmr", dhi.data[REQUEST_ID]);
// Extract the DMR Response object - this holds the DMR used by the
// other parts of the framework.
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDMRResponse &bdmr = dynamic_cast<BESDMRResponse &>(*response);
// Because this code does not yet know how to build a DMR directly, use
// the DMR ctor that builds a DMR using a 'full DDS' (a DDS with attributes).
// First step, build the 'full DDS'
string dataset_name = dhi.container->access();
// Get the DMR made by the BES in the BES/dap/BESDMRResponseHandler, make sure there's a
// factory we can use and then dump the DAP2 variables and attributes in using the
// BaseType::transform_to_dap4() method that transforms individual variables
DMR *dmr = bdmr.get_dmr();
try {
DMR* cached_dmr_ptr = 0;
if (dmr_cache && (cached_dmr_ptr = static_cast<DMR*>(dmr_cache->get(dataset_name)))) {
// copy the cached DMR into the BES response object
BESDEBUG(NC_NAME, "DMR Cached hit for : " << dataset_name << endl);
*dmr = *cached_dmr_ptr; // Copy the referenced object
}
else {
#if 0
// this version builds and caches the DDS/DAS info.
BaseTypeFactory factory;
DDS dds(&factory, name_path(dataset_name), "3.2");
// This will get the DDS, either by building it or from the cache
get_dds_with_attributes(dataset_name, "", &dds);
dmr->set_factory(new D4BaseTypeFactory);
dmr->build_using_dds(dds);
#else
// This version builds a DDS only to build the resulting DMR. The DDS is
// not cached. It does look in the DDS cache, just in case...
dmr->set_factory(new D4BaseTypeFactory);
DDS *dds_ptr = 0;
if (dds_cache && (dds_ptr = static_cast<DDS*>(dds_cache->get(dataset_name)))) {
// Use the cached DDS; Assume that all cached DDS objects hold DAS info too
BESDEBUG(NC_NAME, "DDS Cached hit (while building DMR) for : " << dataset_name << endl);
dmr->build_using_dds(*dds_ptr);
}
else {
// Build a throw-away DDS; don't bother to cache it. DMR's don't support
// containers.
BaseTypeFactory factory;
DDS dds(&factory, name_path(dataset_name), "3.2");
dds.filename(dataset_name);
nc_read_dataset_variables(dds, dataset_name);
DAS das;
nc_read_dataset_attributes(das, dataset_name);
Ancillary::read_ancillary_das(das, dataset_name);
dds.transfer_attributes(&das);
dmr->build_using_dds(dds);
}
#endif
if (dmr_cache) {
// add a copy
BESDEBUG(NC_NAME, "DMR added to the cache for : " << dataset_name << endl);
dmr_cache->add(new DMR(*dmr), dataset_name);
}
}
// Instead of fiddling with the internal storage of the DHI object,
// (by setting dhi.data[DAP4_CONSTRAINT], etc., directly) use these
// methods to set the constraints. But, why? Ans: from Patrick is that
// in the 'container' mode of BES each container can have a different
// CE.
bdmr.set_dap4_constraint(dhi);
bdmr.set_dap4_function(dhi);
}
catch (InternalErr &e) {
throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
}
catch (Error &e) {
throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
}
catch (...) {
throw BESDapError("Caught unknown error build NC DMR response", true, unknown_error, __FILE__, __LINE__);
}
return true;
}
bool NCRequestHandler::nc_build_help(BESDataHandlerInterface & dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_help", dhi.data[REQUEST_ID]);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESInfo *info = dynamic_cast<BESInfo *> (response);
if (!info)
throw BESInternalError("cast error", __FILE__, __LINE__);
map < string, string > attrs;
attrs["name"] = MODULE_NAME ;
attrs["version"] = MODULE_VERSION ;
#if 0
attrs["name"] = PACKAGE_NAME;
attrs["version"] = PACKAGE_VERSION;
#endif
list < string > services;
BESServiceRegistry::TheRegistry()->services_handled(NC_NAME, services);
if (services.size() > 0) {
string handles = BESUtil::implode(services, ',');
attrs["handles"] = handles;
}
info->begin_tag("module", &attrs);
info->end_tag("module");
return true;
}
bool NCRequestHandler::nc_build_version(BESDataHandlerInterface & dhi)
{
BESStopWatch sw;
if (BESISDEBUG( TIMING_LOG ))
sw.start("NCRequestHandler::nc_build_version", dhi.data[REQUEST_ID]);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESVersionInfo *info = dynamic_cast<BESVersionInfo *> (response);
if (!info)
throw BESInternalError("cast error", __FILE__, __LINE__);
#if 0
info->add_module(PACKAGE_NAME, PACKAGE_VERSION);
#endif
info->add_module(MODULE_NAME, MODULE_VERSION);
return true;
}