bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
FoDapCovJsonTransmitter.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2//
3// FoDapCovJsonTransmitter.cc
4//
5// This file is part of BES CovJSON File Out Module
6//
7// Copyright (c) 2018 OPeNDAP, Inc.
8// Author: Corey Hemphill <hemphilc@oregonstate.edu>
9// Author: River Hendriksen <hendriri@oregonstate.edu>
10// Author: Riley Rimer <rrimer@oregonstate.edu>
11//
12// Adapted from the File Out JSON module implemented by Nathan Potter
13//
14// This library is free software; you can redistribute it and/or
15// modify it under the terms of the GNU Lesser General Public
16// License as published by the Free Software Foundation; either
17// version 2.1 of the License, or (at your option) any later version.
18//
19// This library is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22// Lesser General Public License for more details.
23//
24// You should have received a copy of the GNU Lesser General Public
25// License along with this library; if not, write to the Free Software
26// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27//
28
29#include "config.h"
30
31#include <stdio.h>
32#include <stdlib.h>
33
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38#include <sys/types.h> // For umask
39#include <sys/stat.h>
40
41#include <iostream>
42#include <fstream>
43
44#include <libdap/DataDDS.h>
45#include <libdap/BaseType.h>
46#include <libdap/escaping.h>
47#include <libdap/ConstraintEvaluator.h>
48
49#include <BESUtil.h>
50#include <BESInternalError.h>
51#include <BESSyntaxUserError.h>
52#include <BESDapError.h>
53#include <TheBESKeys.h>
54#include <BESContextManager.h>
55#include <BESDataDDSResponse.h>
56#include <BESDDSResponse.h>
57#include <BESDapNames.h>
58#include <BESDataNames.h>
59#include <BESDapResponseBuilder.h>
60#include <BESDebug.h>
61#include <DapFunctionUtils.h>
62
63#include "FoDapCovJsonTransmitter.h"
64#include "FoDapCovJsonTransform.h"
65
66using namespace ::libdap;
67
68#define FO_COVJSON_TEMP_DIR "/tmp"
69
70string FoDapCovJsonTransmitter::temp_dir;
71
84{
85 add_method(DATA_SERVICE, FoDapCovJsonTransmitter::send_data);
86 add_method(DDX_SERVICE, FoDapCovJsonTransmitter::send_metadata);
87
88 add_method(DAP4DATA_SERVICE, FoDapCovJsonTransmitter::send_dap4data);
89 add_method(DMR_SERVICE, FoDapCovJsonTransmitter::send_dap4metadata);
90
91
92 if (FoDapCovJsonTransmitter::temp_dir.empty()) {
93 // Where is the temp directory for creating these files
94 bool found = false;
95 string key = "FoCovJson.Tempdir";
96 TheBESKeys::TheKeys()->get_value(key, FoDapCovJsonTransmitter::temp_dir, found);
97 if (!found || FoDapCovJsonTransmitter::temp_dir.empty()) {
98 FoDapCovJsonTransmitter::temp_dir = FO_COVJSON_TEMP_DIR;
99 }
100 string::size_type len = FoDapCovJsonTransmitter::temp_dir.size();
101 if (FoDapCovJsonTransmitter::temp_dir[len - 1] == '/') {
102 FoDapCovJsonTransmitter::temp_dir = FoDapCovJsonTransmitter::temp_dir.substr(0, len - 1);
103 }
104 }
105}
106
123{
124 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN" << endl);
125
126 try {
127 BESDapResponseBuilder responseBuilder;
128
129 BESDEBUG("focovjson", "FoCovJsonTransmitter::send_data - Reading data into DataDDS" << endl);
130
131 // The response object will manage loaded_dds
132 // Use the DDS from the ResponseObject along with the parameters
133 // from the DataHandlerInterface to load the DDS with values.
134 // Note that the BESResponseObject will manage the loaded_dds object's
135 // memory. Make this a shared_ptr<>. jhrg 9/6/16
136
137 // TODO improve this pattern by enabling reading then transforming one varaible
138 // at a time so that the whole dataset is not loaded into memory. jhrg 6/11/20
139 DDS *loaded_dds;
140 try {
141 // Added this try block to debug memory use issues in this handler. jhrg 6/11/20
142 loaded_dds = responseBuilder.intern_dap2_data(obj, dhi);
143 }
144 catch(std::exception &e) {
145 throw BESSyntaxUserError(string("Caught a C++ standard exception in responseBuilder.intern_dap2_data. The error was: ").append(e.what()), __FILE__, __LINE__);
146 }
147
148 ostream &o_strm = dhi.get_output_stream();
149 if (!o_strm)
150 throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
151
152 FoDapCovJsonTransform ft(loaded_dds);
153 ft.transform(o_strm, true, false); // Send metadata and data; Test override false
154 //ft.transform(o_strm, true, false); // Send metadata and data; Test override false
155 }
156 catch (Error &e) {
157 throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
158 }
159 catch (BESError &e) {
160 throw;
161 }
162 catch (std::exception &e) {
163 throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
164 }
165 catch (...) {
166 throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
167 }
168
169 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
170}
171
188{
189 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN transmitting COVJSON" << endl);
190
191 try {
192 BESDapResponseBuilder responseBuilder;
193
194 // processed_dds managed by response builder
195 DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
196
197 ostream &o_strm = dhi.get_output_stream();
198 if (!o_strm)
199 throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
200
201 FoDapCovJsonTransform ft(processed_dds);
202
203 // Now that we are ready to start building the response data we
204 // cancel any pending timeout alarm according to the configuration.
206
207 ft.transform(o_strm, false, false); // Send metadata only; Test override false
208 //ft.transform(o_strm, false, false); // Send metadata only; Test override false
209 }
210 catch (Error &e) {
211 throw BESDapError("Failed to transform data to COVJSON: " + e.get_error_message(), false, e.get_error_code(),
212 __FILE__, __LINE__);
213 }
214 catch (BESError &e) {
215 throw;
216 }
217 catch (...) {
218 throw BESInternalError("Failed to transform to COVJSON: Unknown exception caught", __FILE__, __LINE__);
219 }
220
221 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
222}
223
240{
241 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_dap4data - BEGIN" << endl);
242
243 try {
244 BESDapResponseBuilder responseBuilder;
245
246 BESDEBUG("focovjson", "FoCovJsonTransmitter::send_dap4data - Reading data into DMR" << endl);
247
248 // The response object will manage loaded_dds
249 // Use the DDS from the ResponseObject along with the parameters
250 // from the DataHandlerInterface to load the DDS with values.
251 // Note that the BESResponseObject will manage the loaded_dds object's
252 // memory. Make this a shared_ptr<>. jhrg 9/6/16
253
254 // TODO improve this pattern by enabling reading then transforming one varaible
255 // at a time so that the whole dataset is not loaded into memory. jhrg 6/11/20
256 DMR *loaded_dmr;
257 try {
258 // Added this try block to debug memory use issues in this handler. jhrg 6/11/20
259 loaded_dmr = responseBuilder.intern_dap4_data(obj, dhi);
260 }
261 catch(std::exception &e) {
262 throw BESSyntaxUserError(string("Caught a C++ standard exception in responseBuilder.intern_dap4_data. The error was: ").append(e.what()), __FILE__, __LINE__);
263 }
264
265 ostream &o_strm = dhi.get_output_stream();
266 if (!o_strm)
267 throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
268
269 FoDapCovJsonTransform ft(loaded_dmr);
270 ft.transform_dap4(o_strm, true, false); // Send metadata and data; Test override false
271 }
272 catch (Error &e) {
273 throw BESDapError("Failed to read dap4 data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
274 }
275 catch (std::exception &e) {
276 throw BESInternalError("Failed to read dap4 data: STL Error: " + string(e.what()), __FILE__, __LINE__);
277 }
278 catch (...) {
279 throw BESInternalError("Failed to read dap4 data: Unknown exception caught", __FILE__, __LINE__);
280 }
281
282 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_dap4data - done transmitting COVJSON" << endl);
283}
284
301{
302 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_dap4metadata - BEGIN transmitting COVJSON" << endl);
303
304 try {
305 BESDapResponseBuilder responseBuilder;
306
307 // processed_dds managed by response builder
308 DMR *processed_dmr = responseBuilder.process_dap4_dmr(obj, dhi);
309
310 ostream &o_strm = dhi.get_output_stream();
311 if (!o_strm)
312 throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
313
314 FoDapCovJsonTransform ft(processed_dmr);
315
316 // Now that we are ready to start building the response data we
317 // cancel any pending timeout alarm according to the configuration.
319
320 ft.transform_dap4(o_strm, false, false); // Send metadata only; Test override false
321 }
322 catch (Error &e) {
323 throw BESDapError("Failed to transform data to COVJSON: " + e.get_error_message(), false, e.get_error_code(),
324 __FILE__, __LINE__);
325 }
326 catch (...) {
327 throw BESInternalError("Failed to transform to COVJSON: Unknown exception caught", __FILE__, __LINE__);
328 }
329
330 BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_dap4metadata - done transmitting COVJSON" << endl);
331}
error object created from libdap error objects and can handle those errors
Definition BESDapError.h:50
virtual libdap::DMR * intern_dap4_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
virtual libdap::DDS * process_dap2_dds(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Transmit data.
virtual libdap::DDS * intern_dap2_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Structure storing information used by the BES to handle the request.
Base exception class for the BES with basic string message.
Definition BESError.h:66
exception thrown if internal error encountered
Abstract base class representing a specific set of information in response to a request to the BES.
error thrown if there is a user syntax error in the request or any other user error
static void conditional_timeout_cancel()
Checks if the timeout alarm should be canceled based on the value of the BES key BES....
Definition BESUtil.cc:898
static void send_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
static void send_metadata(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
static void send_dap4metadata(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
static void send_dap4data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
FoDapCovJsonTransmitter()
Construct the FoW10nJsonTransmitter.
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
STL class.