bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
FoInstanceJsonTransmitter.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2//
3// FoInstanceJsonTransmitter.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 <BESInternalError.h>
50#include <BESDapError.h>
51#include <TheBESKeys.h>
52#include <BESContextManager.h>
53#include <BESDataDDSResponse.h>
54#include <BESDDSResponse.h>
55#include <BESDapNames.h>
56#include <BESDataNames.h>
57#include <BESDapResponseBuilder.h>
58#include <BESDebug.h>
59
60#include "FoInstanceJsonTransmitter.h"
61#include "FoInstanceJsonTransform.h"
62
63using namespace libdap;
64
65#define FO_JSON_TEMP_DIR "/tmp"
66
67string FoInstanceJsonTransmitter::temp_dir;
68
80{
81 add_method(DATA_SERVICE, FoInstanceJsonTransmitter::send_data);
82 add_method(DDX_SERVICE, FoInstanceJsonTransmitter::send_metadata);
83
84 if (FoInstanceJsonTransmitter::temp_dir.empty()) {
85 // Where is the temp directory for creating these files
86 bool found = false;
87 string key = "FoJson.Tempdir";
88 TheBESKeys::TheKeys()->get_value(key, FoInstanceJsonTransmitter::temp_dir, found);
89 if (!found || FoInstanceJsonTransmitter::temp_dir.empty()) {
90 FoInstanceJsonTransmitter::temp_dir = FO_JSON_TEMP_DIR;
91 }
92 string::size_type len = FoInstanceJsonTransmitter::temp_dir.size();
93 if (FoInstanceJsonTransmitter::temp_dir[len - 1] == '/') {
94 FoInstanceJsonTransmitter::temp_dir = FoInstanceJsonTransmitter::temp_dir.substr(0, len - 1);
95 }
96 }
97}
98
119{
120 BESDEBUG("fojson", "FoJsonTransmitter::send_data - BEGIN transmitting JSON" << endl);
121
122 try {
123 BESDapResponseBuilder responseBuilder;
124
125 // processed_dds managed by response builder
126 DDS *processed_dds = responseBuilder.process_dap2_dds(obj, dhi);
127
128 ostream &o_strm = dhi.get_output_stream();
129 if (!o_strm)
130 throw BESInternalError("Output stream is not set, can not return as JSON", __FILE__, __LINE__);
131
132 FoInstanceJsonTransform ft(processed_dds);
133
134 ft.transform(o_strm, false /* do not send data */);
135 }
136 catch (Error &e) {
137 throw BESDapError("Failed to transform data to JSON: " + e.get_error_message(), false, e.get_error_code(),
138 __FILE__, __LINE__);
139 }
140 catch (BESError &e) {
141 throw;
142 }
143 catch (...) {
144 throw BESInternalError("Failed to transform to JSON: Unknown exception caught", __FILE__, __LINE__);
145 }
146
147 BESDEBUG("fojson", "FoJsonTransmitter::send_data - done transmitting JSON" << endl);
148}
149
166{
167 BESDEBUG("fojson", "FoJsonTransmitter::send_data - BEGIN transmitting JSON" << endl);
168
169 try {
170 BESDapResponseBuilder responseBuilder;
171
172 BESDEBUG("fojson", "FoJsonTransmitter::send_data - Reading data into DataDDS" << endl);
173
174 // Use the DDS from the ResponseObject along with the parameters
175 // from the DataHandlerInterface to load the DDS with values.
176 // Note that the BESResponseObject will manage the loaded_dds object's
177 // memory. Make this a shared_ptr<>. jhrg 9/6/16
178 DDS *loaded_dds = responseBuilder.intern_dap2_data(obj, dhi);
179
180 ostream &o_strm = dhi.get_output_stream();
181 if (!o_strm)
182 throw BESInternalError("Output stream is not set, can not return as JSON", __FILE__, __LINE__);
183
184 FoInstanceJsonTransform ft(loaded_dds);
185
186 ft.transform(o_strm, true /* send data */);
187 }
188 catch (Error &e) {
189 throw BESDapError("Failed to read data: " + e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
190 }
191 catch (BESError &e) {
192 throw;
193 }
194 catch (std::exception &e) {
195 throw BESInternalError("Failed to read data: STL Error: " + string(e.what()), __FILE__, __LINE__);
196 }
197 catch (...) {
198 throw BESInternalError("Failed to get read data: Unknown exception caught", __FILE__, __LINE__);
199 }
200
201 BESDEBUG("fojson", "FoJsonTransmitter::send_data - done transmitting JSON" << endl);
202}
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.
Transforms a DDS into JSON document on disk.
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.
FoInstanceJsonTransmitter()
Construct the FoJsonTransmitter.
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.