38 #include <sys/types.h>
47 #include <ConstraintEvaluator.h>
69 #define W10N_JSON_TEMP_DIR "/tmp"
71 string W10nJsonTransmitter::temp_dir;
89 if (W10nJsonTransmitter::temp_dir.empty()) {
92 string key =
"W10nJson.Tempdir";
94 if (!found || W10nJsonTransmitter::temp_dir.empty()) {
97 string::size_type len = W10nJsonTransmitter::temp_dir.length();
98 if (W10nJsonTransmitter::temp_dir[len - 1] ==
'/') {
99 W10nJsonTransmitter::temp_dir = W10nJsonTransmitter::temp_dir.substr(0, len - 1);
110 void W10nJsonTransmitter::checkConstraintForW10nCompatibility(
const string &ce){
111 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::checkConstraintForW10nCompatibility() - BEGIN. ce: "<< ce << endl);
113 string projectionClause = getProjectionClause(ce);
114 int firstComma = projectionClause.find(
",");
116 if(firstComma != -1){
117 string msg =
"The w10n protocol only allows one variable to be selected at a time. ";
118 msg +=
"The constraint expression '" + ce +
"' requests more than one.";
119 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::checkConstraintForW10nCompatibility() - ERROR! "<< msg << endl);
129 string W10nJsonTransmitter::getProjectionClause(
const string &constraintExpression){
131 string projectionClause = constraintExpression;
132 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::getProjectionClause() - constraintExpression: "<< constraintExpression << endl);
134 int firstAmpersand = constraintExpression.find(
"&");
135 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::getProjectionClause() - firstAmpersand: "<< firstAmpersand << endl);
136 if(firstAmpersand>=0)
137 projectionClause = constraintExpression.substr(0,firstAmpersand);
139 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::getProjectionClause() - CE projection clause: "<< projectionClause << endl);
141 return projectionClause;
147 string W10nJsonTransmitter::getProjectedVariableName(
const string &constraintExpression){
149 string varName = getProjectionClause(constraintExpression);
151 int firstSquareBracket = varName.find(
"[");
152 if(firstSquareBracket != -1){
153 varName = varName.substr(0,firstSquareBracket);
185 string msg =
"Dynamic cast of BESResponseObject to BESDataDDSResponse FAILED.";
190 DataDDS *dds = bdds->
get_dds();
192 string msg =
"No DataDDS has been created for transmit";
199 string msg =
"Output stream is not set, cannot return data as JSON";
206 ConstraintEvaluator &eval = bdds->
get_ce();
212 checkConstraintForW10nCompatibility(ce);
216 eval.parse_constraint(ce, *dds);
219 string msg =
"Failed to parse the constraint expression. Msg: "+ e.get_error_message();
221 throw BESDapError(msg, 0, e.get_error_code(),__FILE__, __LINE__);
224 string msg =
"Failed to parse the constraint expression. Unknown exception caught";
237 if (eval.function_clauses()) {
239 DataDDS *tmp_dds = eval.eval_function_clauses(*dds);
247 for (DDS::Vars_iter i = dds->var_begin(); i != dds->var_end(); i++) {
248 if ((*i)->send_p()) {
249 (*i)->intern_data(eval, *dds);
255 string msg =
"Failed to read data! Msg: " + e.get_error_message();
257 throw BESDapError(msg, 0, e.get_error_code(),__FILE__, __LINE__);
260 string msg =
"Failed to read data: Unknown exception caught";
270 string varName = getProjectedVariableName(ce);
272 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::send_metadata() - Sending w10n meta response for variable "
274 ft.sendW10nDataForVariable(varName);
277 string msg =
"Failed to transmit data as w10n JSON. Msg: " + e.
get_message();
282 string msg =
"Failed to transmit data as w10n JSON. Unknown Error.";
287 cleanupW10nContexts();
317 throw BESInternalError(
"No DDS has been created for transmit", __FILE__, __LINE__);
321 ConstraintEvaluator &eval = bdds->
get_ce();
325 throw BESInternalError(
"Output stream is not set, can not return as JSON", __FILE__, __LINE__);
330 checkConstraintForW10nCompatibility(ce);
333 eval.parse_constraint(ce, *dds);
336 throw BESInternalError(
"Failed to parse the constraint expression: " + e.get_error_message(), __FILE__, __LINE__);
339 throw BESInternalError(
"Failed to parse the constraint expression: Unknown exception caught", __FILE__, __LINE__);
345 string varName = getProjectedVariableName(ce);
347 if(varName.length() == 0){
348 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::send_metadata() - Sending w10n meta response for DDS" << endl);
349 ft.sendW10nMetaForDDS();
352 BESDEBUG(
W10N_DEBUG_KEY,
"W10nJsonTransmitter::send_metadata() - Sending w10n meta response for variable "
354 ft.sendW10nMetaForVariable(varName,
true);
357 cleanupW10nContexts();
371 void W10nJsonTransmitter::return_temp_stream(
const string &filename, ostream &strm)
375 os.open(filename.c_str(), ios::binary | ios::in);
377 string err =
"Can not connect to file " + filename;
384 os.read(block,
sizeof block);
385 nbytes = os.gcount();
387 strm.write(block, nbytes);
394 string err = (string)
"0XAAE234F: failed to stream. Internal server "
395 +
"error, got zero count on stream buffer." + filename;
400 os.read(block,
sizeof block);
401 nbytes = os.gcount();
402 strm.write(block, nbytes);
411 void W10nJsonTransmitter::cleanupW10nContexts()
static void send_metadata(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
static void send_data(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a JSON file.
void set_dds(DataDDS *ddsIn)
Set the response object's DDS.
exception thrown if inernal error encountered
ConstraintEvaluator & get_ce()
#define W10N_TRAVERSE_KEY
ostream & get_output_stream()
W10nJsonTransmitter()
Construct the W10nJsonTransmitter.
Represents an OPeNDAP DDS DAP2 data object within the BES.
void checkConstrainedDDSForW10nDataCompatibility(libdap::DDS *dds)
#define W10N_META_OBJECT_KEY
virtual bool add_method(string method_name, p_transmitter trans_method)
error thrown if there is a user syntax error in the request or any other user error ...
static class NCMLUtil overview
virtual string get_message()
get the error message for this exception
Abstract exception class for the BES with basic string message.
#define W10N_CALLBACK_KEY
error object created from libdap error objects and can handle those errors
virtual void unset_context(const string &name)
set context in the BES
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
#define W10N_JSON_TEMP_DIR
static BESContextManager * TheManager()
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
ConstraintEvaluator & get_ce()
Structure storing information used by the BES to handle the request.
map< string, string > data
the map of string data that will be required for the current request.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
static BESKeys * TheKeys()
Abstract base class representing a specific set of information in response to a request to the BES...