bes  Updated for version 3.20.13
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  // The response object will manage loaded_dds
128  // Use the DDS from the ResponseObject along with the parameters
129  // from the DataHandlerInterface to load the DDS with values.
130  // Note that the BESResponseObject will manage the loaded_dds object's
131  // memory. Make this a shared_ptr<>. jhrg 9/6/16
132 
133  // TODO improve this pattern by enabling reading then transforming one varaible
134  // at a time so that the whole dataset is not loaded into memory. jhrg 6/11/20
135  DDS *loaded_dds;
136  try {
137  // Added this try block to debug memory use issues in this handler. jhrg 6/11/20
138  loaded_dds = responseBuilder.intern_dap2_data(obj, dhi);
139  }
140  catch(std::exception &e) {
141  throw BESSyntaxUserError(string("Caught a C++ standard exception in responseBuilder.intern_dap2_data. The error was: ").append(e.what()), __FILE__, __LINE__);
142  }
143 
144  ostream &o_strm = dhi.get_output_stream();
145  if (!o_strm)
146  throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
147 
148  FoDapCovJsonTransform ft(loaded_dds);
149  ft.transform(o_strm, true, false); // Send metadata and data; Test override false
150  //ft.transform(o_strm, true, false); // Send metadata and data; Test override false
151  }
152  catch (Error &e) {
153  throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
154  }
155  catch (BESError &e) {
156  throw;
157  }
158  catch (std::exception &e) {
159  throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
160  }
161  catch (...) {
162  throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
163  }
164 
165  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
166 }
167 
184 {
185  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN transmitting COVJSON" << endl);
186 
187  try {
188  BESDapResponseBuilder responseBuilder;
189 
190  // processed_dds managed by response builder
191  DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
192 
193  ostream &o_strm = dhi.get_output_stream();
194  if (!o_strm)
195  throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
196 
197  FoDapCovJsonTransform ft(processed_dds);
198 
199  // Now that we are ready to start building the response data we
200  // cancel any pending timeout alarm according to the configuration.
202 
203  ft.transform(o_strm, false, false); // Send metadata only; Test override false
204  //ft.transform(o_strm, false, false); // Send metadata only; Test override false
205  }
206  catch (Error &e) {
207  throw BESDapError("Failed to transform data to COVJSON: " + e.get_error_message(), false, e.get_error_code(),
208  __FILE__, __LINE__);
209  }
210  catch (BESError &e) {
211  throw;
212  }
213  catch (...) {
214  throw BESInternalError("Failed to transform to COVJSON: Unknown exception caught", __FILE__, __LINE__);
215  }
216 
217  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
218 }
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)
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:59
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:895
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