bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
FoDapJsonTransmitter.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2//
3// FoDapJsonTransmitter.cc
4//
5// This file is part of BES JSON File Out Module
6//
7// Copyright (c) 2014 OPeNDAP, Inc.
8// Author: Nathan Potter <ndp@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25// (c) COPYRIGHT URI/MIT 1995-1999
26// Please read the full copyright statement in the file COPYRIGHT_URI.
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 <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 "FoDapJsonTransmitter.h"
63#include "FoDapJsonTransform.h"
64
65using namespace ::libdap;
66
67#define FO_JSON_TEMP_DIR "/tmp"
68
69string FoDapJsonTransmitter::temp_dir;
70
83{
84 add_method(DATA_SERVICE, FoDapJsonTransmitter::send_data);
85 add_method(DDX_SERVICE, FoDapJsonTransmitter::send_metadata);
86
87 if (FoDapJsonTransmitter::temp_dir.empty()) {
88 // Where is the temp directory for creating these files
89 bool found = false;
90 string key = "FoJson.Tempdir";
91 TheBESKeys::TheKeys()->get_value(key, FoDapJsonTransmitter::temp_dir, found);
92 if (!found || FoDapJsonTransmitter::temp_dir.empty()) {
93 FoDapJsonTransmitter::temp_dir = FO_JSON_TEMP_DIR;
94 }
95 string::size_type len = FoDapJsonTransmitter::temp_dir.size();
96 if (FoDapJsonTransmitter::temp_dir[len - 1] == '/') {
97 FoDapJsonTransmitter::temp_dir = FoDapJsonTransmitter::temp_dir.substr(0, len - 1);
98 }
99 }
100}
101
118{
119 BESDEBUG("fojson", "FoDapJsonTransmitter::send_data - BEGIN" << endl);
120
121 try {
122 BESDapResponseBuilder responseBuilder;
123
124 BESDEBUG("fojson", "FoJsonTransmitter::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 JSON", __FILE__, __LINE__);
140
141 FoDapJsonTransform ft(loaded_dds);
142
143 ft.transform(o_strm, true /* send data */);
144 }
145 catch (Error &e) {
146 throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
147 }
148 catch (BESError &e) {
149 throw;
150 }
151 catch (std::exception &e) {
152 throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
153 }
154 catch (...) {
155 throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
156 }
157
158 BESDEBUG("fojson", "FoDapJsonTransmitter::send_data - done transmitting JSON" << endl);
159}
160
177{
178 BESDEBUG("fojson", "FoDapJsonTransmitter::send_data - BEGIN transmitting JSON" << endl);
179
180 try {
181 BESDapResponseBuilder responseBuilder;
182
183 // processed_dds managed by response builder
184 DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
185
186 ostream &o_strm = dhi.get_output_stream();
187 if (!o_strm)
188 throw BESInternalError("Output stream is not set, can not return as JSON", __FILE__, __LINE__);
189
190 FoDapJsonTransform ft(processed_dds);
191
192 // Now that we are ready to start building the response data we
193 // cancel any pending timeout alarm according to the configuration.
195
196 ft.transform(o_strm, false /* do not send data */);
197 }
198 catch (Error &e) {
199 throw BESDapError("Failed to transform data to JSON: " + e.get_error_message(), false, e.get_error_code(),
200 __FILE__, __LINE__);
201 }
202 catch (BESError &e) {
203 throw;
204 }
205 catch (...) {
206 throw BESInternalError("Failed to transform to JSON: Unknown exception caught", __FILE__, __LINE__);
207 }
208
209 BESDEBUG("fojson", "FoDapJsonTransmitter::send_data - done transmitting JSON" << endl);
210}
error object created from libdap error objects and can handle those errors
Definition BESDapError.h:50
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.
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
FoDapJsonTransmitter()
Construct the FoW10nJsonTransmitter.
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.
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.