bes  Updated for version 3.20.10
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 
66 using namespace ::libdap;
67 
68 #define FO_COVJSON_TEMP_DIR "/tmp"
69 
70 string FoDapCovJsonTransmitter::temp_dir;
71 
84 {
85  add_method(DATA_SERVICE, FoDapCovJsonTransmitter::send_data);
86  add_method(DDX_SERVICE, FoDapCovJsonTransmitter::send_metadata);
87 
88  if (FoDapCovJsonTransmitter::temp_dir.empty()) {
89  // Where is the temp directory for creating these files
90  bool found = false;
91  string key = "FoCovJson.Tempdir";
92  TheBESKeys::TheKeys()->get_value(key, FoDapCovJsonTransmitter::temp_dir, found);
93  if (!found || FoDapCovJsonTransmitter::temp_dir.empty()) {
94  FoDapCovJsonTransmitter::temp_dir = FO_COVJSON_TEMP_DIR;
95  }
96  string::size_type len = FoDapCovJsonTransmitter::temp_dir.length();
97  if (FoDapCovJsonTransmitter::temp_dir[len - 1] == '/') {
98  FoDapCovJsonTransmitter::temp_dir = FoDapCovJsonTransmitter::temp_dir.substr(0, len - 1);
99  }
100  }
101 }
102 
119 {
120  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN" << endl);
121 
122  try {
123  BESDapResponseBuilder responseBuilder;
124 
125  BESDEBUG("focovjson", "FoCovJsonTransmitter::send_data - Reading data into DataDDS" << endl);
126 
127  // Now that we are ready to start reading the response data we
128  // cancel any pending timeout alarm according to the configuration.
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  }
155  catch (Error &e) {
156  throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
157  }
158  catch (BESError &e) {
159  throw;
160  }
161  catch (std::exception &e) {
162  throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
163  }
164  catch (...) {
165  throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
166  }
167 
168  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
169 }
170 
187 {
188  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN transmitting COVJSON" << endl);
189 
190  try {
191  BESDapResponseBuilder responseBuilder;
192 
193  // processed_dds managed by response builder
194  DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
195 
196  ostream &o_strm = dhi.get_output_stream();
197  if (!o_strm)
198  throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
199 
200  FoDapCovJsonTransform ft(processed_dds);
201 
202  // Now that we are ready to start building the response data we
203  // cancel any pending timeout alarm according to the configuration.
205 
206  ft.transform(o_strm, false, false); // Send metadata only; Test override false
207  }
208  catch (Error &e) {
209  throw BESDapError("Failed to transform data to COVJSON: " + e.get_error_message(), false, e.get_error_code(),
210  __FILE__, __LINE__);
211  }
212  catch (BESError &e) {
213  throw;
214  }
215  catch (...) {
216  throw BESInternalError("Failed to transform to COVJSON: Unknown exception caught", __FILE__, __LINE__);
217  }
218 
219  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
220 }
error object created from libdap error objects and can handle those errors
Definition: BESDapError.h:59
virtual libdap::DDS * process_dap2_dds(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Process a DDS (i.e., apply a constraint) for a non-DAP transmitter.
virtual libdap::DDS * intern_dap2_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Structure storing information used by the BES to handle the request.
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
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:992
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.
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.
Definition: TheBESKeys.cc:340
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71