bes  Updated for version 3.20.6
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 <DataDDS.h>
45 #include <BaseType.h>
46 #include <escaping.h>
47 #include <ConstraintEvaluator.h>
48 
49 #include <BESUtil.h>
50 #include <BESInternalError.h>
51 #include <BESDapError.h>
52 #include <TheBESKeys.h>
53 #include <BESContextManager.h>
54 #include <BESDataDDSResponse.h>
55 #include <BESDDSResponse.h>
56 #include <BESDapNames.h>
57 #include <BESDataNames.h>
58 #include <BESDapResponseBuilder.h>
59 #include <BESDebug.h>
60 #include <DapFunctionUtils.h>
61 
62 #include "FoDapCovJsonTransmitter.h"
63 #include "FoDapCovJsonTransform.h"
64 
65 using namespace ::libdap;
66 
67 #define FO_COVJSON_TEMP_DIR "/tmp"
68 
69 string FoDapCovJsonTransmitter::temp_dir;
70 
83 {
84  add_method(DATA_SERVICE, FoDapCovJsonTransmitter::send_data);
85  add_method(DDX_SERVICE, FoDapCovJsonTransmitter::send_metadata);
86 
87  if (FoDapCovJsonTransmitter::temp_dir.empty()) {
88  // Where is the temp directory for creating these files
89  bool found = false;
90  string key = "FoCovJson.Tempdir";
91  TheBESKeys::TheKeys()->get_value(key, FoDapCovJsonTransmitter::temp_dir, found);
92  if (!found || FoDapCovJsonTransmitter::temp_dir.empty()) {
93  FoDapCovJsonTransmitter::temp_dir = FO_COVJSON_TEMP_DIR;
94  }
95  string::size_type len = FoDapCovJsonTransmitter::temp_dir.length();
96  if (FoDapCovJsonTransmitter::temp_dir[len - 1] == '/') {
97  FoDapCovJsonTransmitter::temp_dir = FoDapCovJsonTransmitter::temp_dir.substr(0, len - 1);
98  }
99  }
100 }
101 
118 {
119  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN" << endl);
120 
121  try {
122  BESDapResponseBuilder responseBuilder;
123 
124  BESDEBUG("focovjson", "FoCovJsonTransmitter::send_data - Reading data into DataDDS" << endl);
125 
126  // Now that we are ready to start reading the response data we
127  // cancel any pending timeout alarm according to the configuration.
129 
130  // The response object will manage loaded_dds
131  // Use the DDS from the ResponseObject along with the parameters
132  // from the DataHandlerInterface to load the DDS with values.
133  // Note that the BESResponseObject will manage the loaded_dds object's
134  // memory. Make this a shared_ptr<>. jhrg 9/6/16
135  DDS *loaded_dds = responseBuilder.intern_dap2_data(obj, dhi);
136 
137  ostream &o_strm = dhi.get_output_stream();
138  if (!o_strm)
139  throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
140 
141  FoDapCovJsonTransform ft(loaded_dds);
142  ft.transform(o_strm, true, false); // Send metadata and data; Test override false
143  }
144  catch (Error &e) {
145  throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
146  }
147  catch (BESError &e) {
148  throw;
149  }
150  catch (std::exception &e) {
151  throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
152  }
153  catch (...) {
154  throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
155  }
156 
157  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
158 }
159 
176 {
177  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - BEGIN transmitting COVJSON" << endl);
178 
179  try {
180  BESDapResponseBuilder responseBuilder;
181 
182  // processed_dds managed by response builder
183  DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
184 
185  ostream &o_strm = dhi.get_output_stream();
186  if (!o_strm)
187  throw BESInternalError("Output stream is not set, can not return as COVJSON", __FILE__, __LINE__);
188 
189  FoDapCovJsonTransform ft(processed_dds);
190 
191  // Now that we are ready to start building the response data we
192  // cancel any pending timeout alarm according to the configuration.
194 
195  ft.transform(o_strm, false, false); // Send metadata only; Test override false
196  }
197  catch (Error &e) {
198  throw BESDapError("Failed to transform data to COVJSON: " + e.get_error_message(), false, e.get_error_code(),
199  __FILE__, __LINE__);
200  }
201  catch (BESError &e) {
202  throw;
203  }
204  catch (...) {
205  throw BESInternalError("Failed to transform to COVJSON: Unknown exception caught", __FILE__, __LINE__);
206  }
207 
208  BESDEBUG("focovjson", "FoDapCovJsonTransmitter::send_data - done transmitting COVJSON" << endl);
209 }
FoDapCovJsonTransmitter::send_metadata
static void send_metadata(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
Definition: FoDapCovJsonTransmitter.cc:175
BESUtil::conditional_timeout_cancel
static void conditional_timeout_cancel()
Definition: BESUtil.cc:967
libdap
Definition: BESDapFunctionResponseCache.h:35
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
FoDapCovJsonTransmitter::send_data
static void send_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
Definition: FoDapCovJsonTransmitter.cc:117
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
TheBESKeys::get_value
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:272
BESTransmitter
Definition: BESTransmitter.h:47
BESDapResponseBuilder::intern_dap2_data
virtual libdap::DDS * intern_dap2_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Definition: BESDapResponseBuilder.cc:980
FoDapCovJsonTransform
Definition: FoDapCovJsonTransform.h:51
BESDapResponseBuilder
Definition: BESDapResponseBuilder.h:53
Error
BESDapResponseBuilder::process_dap2_dds
virtual libdap::DDS * process_dap2_dds(BESResponseObject *obj, BESDataHandlerInterface &dhi)
Process a DDS (i.e., apply a constraint) for a non-DAP transmitter.
Definition: BESDapResponseBuilder.cc:911
BESDapError
error object created from libdap error objects and can handle those errors
Definition: BESDapError.h:59
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
BESError
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
FoDapCovJsonTransmitter::FoDapCovJsonTransmitter
FoDapCovJsonTransmitter()
Construct the FoW10nJsonTransmitter.
Definition: FoDapCovJsonTransmitter.cc:82
BESResponseObject
Abstract base class representing a specific set of information in response to a request to the BES.
Definition: BESResponseObject.h:45