bes Updated for version 3.20.10
BESXMLInterface.cc
1// BESXMLInterface.cc
2
3// This file is part of bes, A C++ back-end server implementation framework
4// for the OPeNDAP Data Access Protocol.
5
6// Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact University Corporation for Atmospheric Research at
24// 3080 Center Green Drive, Boulder, CO 80301
25
26// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27// Please read the full copyright statement in the file COPYRIGHT_UCAR.
28//
29// Authors:
30// pwest Patrick West <pwest@ucar.edu>
31// jgarcia Jose Garcia <jgarcia@ucar.edu>
32
33#include "config.h"
34
35#include <iostream>
36#include <sstream>
37
38using namespace std;
39
40#include "BESXMLInterface.h"
41#include "BESXMLCommand.h"
42#include "BESXMLUtils.h"
43#include "BESDataNames.h"
44#include "BESResponseNames.h"
45#include "BESContextManager.h"
46
47#include "BESResponseHandler.h"
48#include "BESReturnManager.h"
49#include "BESInfo.h"
50#include "BESStopWatch.h"
51#include "TheBESKeys.h"
52
53#include "BESDebug.h"
54#include "BESLog.h"
55#include "BESSyntaxUserError.h"
56
57#define LOG_ONLY_GET_COMMANDS
58#define MODULE "bes"
59#define prolog std::string("BESXMLInterface::").append(__func__).append("() - ")
60
61BESXMLInterface::BESXMLInterface(const string &xml_doc, ostream *strm) :
62 BESInterface(strm), d_xml_document(xml_doc)
63{
64 // This is needed because we want the parent to have access to the information
65 // added to the DHI
66 d_dhi_ptr = &d_xml_interface_dhi;
67}
68
69BESXMLInterface::~BESXMLInterface()
70{
71 clean();
72}
73
77{
78 BESDEBUG("bes", prolog << "BEGIN" << endl);
79 BESDEBUG("bes", prolog << "Building request plan for xml document: " << endl << d_xml_document << endl);
80
81 // I do not know why, but uncommenting this macro breaks some tests
82 // on Linux but not OSX (CentOS 6, Ubuntu 12 versus OSX 10.11) by
83 // causing some XML elements in DMR responses to be twiddled in the
84 // responses build on Linux but not on OSX.
85 //
86 // LIBXML_TEST_VERSION
87
88 xmlDoc *doc = NULL;
89 xmlNode *root_element = NULL;
90 xmlNode *current_node = NULL;
91
92 try {
93 // set the default error function to my own
94 vector<string> parseerrors;
95 xmlSetGenericErrorFunc((void *) &parseerrors, BESXMLUtils::XMLErrorFunc);
96
97 // XML_PARSE_NONET
98 doc = xmlReadMemory(d_xml_document.c_str(), d_xml_document.size(), "" /* base URL */,
99 NULL /* encoding */, XML_PARSE_NONET /* xmlParserOption */);
100
101 if (doc == NULL) {
102 string err = "Problem parsing the request xml document:\n";
103 bool isfirst = true;
104 vector<string>::const_iterator i = parseerrors.begin();
105 vector<string>::const_iterator e = parseerrors.end();
106 for (; i != e; i++) {
107 if (!isfirst && (*i).compare(0, 6, "Entity") == 0) {
108 err += "\n";
109 }
110 err += (*i);
111 isfirst = false;
112 }
113 throw BESSyntaxUserError(err, __FILE__, __LINE__);
114 }
115
116 // get the root element and make sure it exists and is called request
117 root_element = xmlDocGetRootElement(doc);
118 if (!root_element) throw BESSyntaxUserError("There is no root element in the xml document", __FILE__, __LINE__);
119
120 string root_name;
121 string root_val;
122 map<string, string> attributes;
123 BESXMLUtils::GetNodeInfo(root_element, root_name, root_val, attributes);
124 if (root_name != "request")
125 throw BESSyntaxUserError(
126 string("The root element should be a request element, name is ").append((char *) root_element->name),
127 __FILE__, __LINE__);
128
129 if (!root_val.empty())
130 throw BESSyntaxUserError(string("The request element must not contain a value, ").append(root_val),
131 __FILE__, __LINE__);
132
133 // there should be a request id property with one value.
134 string &reqId = attributes[REQUEST_ID];
135 if (reqId.empty()) throw BESSyntaxUserError("The request id value empty", __FILE__, __LINE__);
136
137 d_dhi_ptr->data[REQUEST_ID] = reqId;
138
139 BESDEBUG("besxml", "request id = " << d_dhi_ptr->data[REQUEST_ID] << endl);
140
141 // iterate through the children of the request element. Each child is an
142 // individual command.
143 bool has_response = false; // set to true when a command with a response is found.
144 current_node = root_element->children;
145
146 while (current_node) {
147 if (current_node->type == XML_ELEMENT_NODE) {
148 // given the name of this node we should be able to find a
149 // BESXMLCommand object
150 string node_name = (char *) current_node->name;
151
152 if(node_name == SETCONTAINER_STR){
153 string name;
154 string value;
155 map<string,string> props;
156 BESXMLUtils::GetNodeInfo(current_node, name, value, props);
157 BESDEBUG(MODULE, prolog << "In " << SETCONTAINER_STR << " element. Value: " << value << endl);
159 }
160
161 // The Command Builder scheme is a kind of factory, but which uses lists and
162 // a static method defined by each child of BESXMLCommand (called CommandBuilder).
163 // These static methods make new instances of the specific commands and, in so
164 // doing, _copy_ the DataHandlerInterface instance using that class' clone() method.
165 // jhrg 11/7/17
166 p_xmlcmd_builder bldr = BESXMLCommand::find_command(node_name);
167 if (!bldr)
168 throw BESSyntaxUserError(string("Unable to find command for ").append(node_name), __FILE__,
169 __LINE__);
170
171 BESXMLCommand *current_cmd = bldr(d_xml_interface_dhi);
172 if (!current_cmd)
173 throw BESInternalError(string("Failed to build command object for ").append(node_name), __FILE__,
174 __LINE__);
175
176 // push this new command to the back of the list
177 d_xml_cmd_list.push_back(current_cmd);
178
179 // only one of the commands in a request can build a response
180 bool cmd_has_response = current_cmd->has_response();
181 if (has_response && cmd_has_response)
182 throw BESSyntaxUserError("Commands with multiple responses not supported.", __FILE__, __LINE__);
183
184 has_response = cmd_has_response;
185
186 // parse the request given the current node
187 current_cmd->parse_request(current_node);
188
189 // Check if the correct transmitter is present. We look for it again in do_transmit()
190 // where it is actually used. This test just keeps us from building a response that
191 // cannot be transmitted. jhrg 11/8/17
192 //
193 // TODO We could add the 'transmitter' to the DHI.
194 BESDataHandlerInterface &current_dhi = current_cmd->get_xmlcmd_dhi();
195
196 string return_as = current_dhi.data[RETURN_CMD];
197 if (!return_as.empty() && !BESReturnManager::TheManager()->find_transmitter(return_as))
198 throw BESSyntaxUserError(string("Unable to find transmitter ").append(return_as), __FILE__,
199 __LINE__);
200 }
201
202 current_node = current_node->next;
203 }
204 }
205 catch (...) {
206 xmlFreeDoc(doc);
207 xmlCleanupParser();
208 throw;
209 }
210
211 xmlFreeDoc(doc);
212
213 // Removed since the docs indicate it's not needed and it might be
214 // contributing to memory issues flagged by valgrind. 2/25/09 jhrg
215 //
216 // Added this back in. It seems to the the cause of BES-40 - where
217 // When certain tests are run, the order of <Dimension..> elements
218 // in a DMR for a server function result is different when the BESDEBUG
219 // output is on versus when it is not. This was true only when the
220 // BESDEBUG context was 'besxml' or timing,' which lead me here.
221 // Making this call removes the errant behavior. I've run tests using
222 // valgrind and I see no memory problems from this call. jhrg 9/25/15
223 xmlCleanupParser();
224
225 BESDEBUG("bes", "Done building request plan" << endl);
226}
227
231{
232 vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
233 vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
234 for (; i != e; i++) {
235 (*i)->prep_request(); // TODO remove this if possible jhrg 1/7/19
236
237 d_dhi_ptr = &(*i)->get_xmlcmd_dhi();
238
239 // In 'verbose' logging mode, log all the commands.
240 VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] executing" << endl);
241
242 // This is the main log entry when the server is not in 'verbose' mode.
243 // There are two ways we can do this, one writes a log line for only the
244 // get commands, the other write the set container, define and get commands.
245 // TODO Make this configurable? jhrg 11/14/17
246#ifdef LOG_ONLY_GET_COMMANDS
247 // Special logging action for the 'get' command. In non-verbose logging mode,
248 // only log the get command.
249 if (d_dhi_ptr->action.find("get.") != string::npos) {
250
251 string log_delim="|&|"; //",";
252
253 string new_log_info = "";
254
255 // If the OLFS sent it's log info, integrate that into the log output
256 bool found = false;
257 string olfs_log_line = BESContextManager::TheManager()->get_context("olfsLog", found);
258 if(found){
259 new_log_info.append("OLFS").append(log_delim).append(olfs_log_line).append(log_delim);
260 new_log_info.append("BES").append(log_delim);
261 }
262
263 new_log_info.append(d_dhi_ptr->action);
264
265
266 if (!d_dhi_ptr->data[RETURN_CMD].empty())
267 new_log_info.append(log_delim).append(d_dhi_ptr->data[RETURN_CMD]);
268
269 // Assume this is DAP and thus there is at most one container. Log a warning if that's
270 // not true. jhrg 11/14/17
271 BESContainer *c = *(d_dhi_ptr->containers.begin());
272 if (c) {
273 if (!c->get_real_name().empty()) new_log_info.append(log_delim).append(c->get_real_name());
274
275 if (!c->get_constraint().empty()) {
276 new_log_info.append(log_delim).append(c->get_constraint());
277 }
278 else {
279 if (!c->get_dap4_constraint().empty()) new_log_info.append(log_delim).append(c->get_dap4_constraint());
280 if (!c->get_dap4_function().empty()) new_log_info.append(log_delim).append(c->get_dap4_function());
281 }
282 }
283
284 REQUEST_LOG(new_log_info << endl);
285
286 if (d_dhi_ptr->containers.size() > 1)
287 ERROR_LOG("The previous command had multiple containers defined, but only the was logged.");
288 }
289#else
290 if (!BESLog::TheLog()->is_verbose()) {
291 if (d_dhi_ptr->action.find("set.context") == string::npos
292 && d_dhi_ptr->action.find("show.catalog") == string::npos) {
293 LOG(d_dhi_ptr->data[LOG_INFO] << endl);
294 }
295 }
296#endif
297
299
300 // Here's where we could look at the dynamic type to do something different
301 // for a new kind of XMLCommand (e.g., SimpleXMLCommand). for that new command,
302 // move the code now in the response_handler->execute() and ->transmit() into
303 // it. This would eliminate the ResponseHandlers. However, that might not be the
304 // best way to handle the 'get' command, which uses a different ResponseHandler
305 // for each different 'type' of thing it will 'get'. jhrg 3/14/18
306
308
309 if (!d_dhi_ptr->response_handler)
310 throw BESInternalError(string("The response handler '") + d_dhi_ptr->action + "' does not exist", __FILE__,
311 __LINE__);
312
313 d_dhi_ptr->response_handler->execute(*d_dhi_ptr);
314
315 transmit_data(); // TODO move method body in here? jhrg 11/8/17
316
317 }
318}
319
334{
335 if (d_dhi_ptr->error_info) {
336 VERBOSE(d_dhi_ptr->data[SERVER_PID] << " from " << d_dhi_ptr->data[REQUEST_FROM] << " ["
337 << d_dhi_ptr->data[LOG_INFO] << "] Error" << endl);
338
339 ostringstream strm;
340 d_dhi_ptr->error_info->print(strm);
341 INFO_LOG("Transmitting error content: " << strm.str() << endl);
342
344 }
345 else if (d_dhi_ptr->response_handler) {
346 VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] transmitting" << endl);
347
348 BESStopWatch sw;
349 if (BESDebug::IsSet(TIMING_LOG_KEY)) sw.start(d_dhi_ptr->data[LOG_INFO] + " transmitting", d_dhi_ptr->data[REQUEST_ID]);
350
351 string return_as = d_dhi_ptr->data[RETURN_CMD];
352 if (!return_as.empty()) {
353 d_transmitter = BESReturnManager::TheManager()->find_transmitter(return_as);
354 if (!d_transmitter) {
355 throw BESSyntaxUserError(string("Unable to find transmitter ") + return_as, __FILE__, __LINE__);
356 }
357 }
358
359 d_dhi_ptr->response_handler->transmit(d_transmitter, *d_dhi_ptr);
360 }
361}
362
371{
372 if (BESLog::TheLog()->is_verbose()) {
373 vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
374 vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
375 for (; i != e; i++) {
376 d_dhi_ptr = &(*i)->get_xmlcmd_dhi();
377
378 // IF the DHI's error_info object pointer is null, the request was successful.
379 string result = (!d_dhi_ptr->error_info) ? "completed" : "failed";
380
381 // This is only printed for verbose logging.
382 VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] " << result << endl);
383 }
384 }
385}
386
390{
391 vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
392 vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
393 for (; i != e; i++) {
394 BESXMLCommand *cmd = *i;
395 d_dhi_ptr = &cmd->get_xmlcmd_dhi();
396
397 if (d_dhi_ptr) {
398 VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] cleaning" << endl);
399
400 d_dhi_ptr->clean(); // Delete the ResponseHandler if present
401 }
402
403 delete cmd;
404 }
405
406 d_xml_cmd_list.clear();
407}
408
415void BESXMLInterface::dump(ostream &strm) const
416{
417 strm << BESIndent::LMarg << "BESXMLInterface::dump - (" << (void *) this << ")" << endl;
418 BESIndent::Indent();
419 BESInterface::dump(strm);
420 vector<BESXMLCommand *>::const_iterator i = d_xml_cmd_list.begin();
421 vector<BESXMLCommand *>::const_iterator e = d_xml_cmd_list.end();
422 for (; i != e; i++) {
423 BESXMLCommand *cmd = *i;
424 cmd->dump(strm);
425 }
426 BESIndent::UnIndent();
427}
428
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:65
std::string get_dap4_constraint() const
retrieve the constraint expression for this container
Definition: BESContainer.h:203
std::string get_dap4_function() const
retrieve the constraint expression for this container
Definition: BESContainer.h:212
std::string get_real_name() const
retrieve the real name for this container, such as a file name.
Definition: BESContainer.h:180
std::string get_constraint() const
retrieve the constraint expression for this container
Definition: BESContainer.h:194
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
std::string action
the response object requested, e.g. das, dds
void clean()
clean up any information created within this data handler interface
BESInfo * error_info
error information object
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void print(std::ostream &strm)
print the information from this informational object to the specified stream
Definition: BESInfo.cc:261
Entry point into BES, building responses to given requests.
Definition: BESInterface.h:118
virtual void dump(std::ostream &strm) const
dumps information about this object
BESDataHandlerInterface * d_dhi_ptr
Allocated by the child class.
Definition: BESInterface.h:124
BESTransmitter * d_transmitter
The Transmitter to use for the result.
Definition: BESInterface.h:125
exception thrown if internal error encountered
virtual void execute(BESDataHandlerInterface &dhi)=0
knows how to build a requested response object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the response object built by the execute command using the specified transmitter object
virtual bool start(std::string name)
Definition: BESStopWatch.cc:67
error thrown if there is a user syntax error in the request or any other user error
Base class for the BES's commands.
Definition: BESXMLCommand.h:63
virtual bool has_response()=0
Does this command return a response to the client?
virtual BESDataHandlerInterface & get_xmlcmd_dhi()
Return the current BESDataHandlerInterface.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void parse_request(xmlNode *node)=0
Parse the XML request document beginning at the given node.
static p_xmlcmd_builder find_command(const std::string &cmd_str)
Find the BESXMLCommand creation function with the given name.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void transmit_data()
Transmit the response object.
virtual void clean()
Clean up after the request is completed.
virtual void build_data_request_plan()
Build the data request plan using the BESCmdParser.
virtual void log_status()
Log the status of the request to the BESLog file.
virtual void execute_data_request_plan()
Execute the data request plan.
static void GetNodeInfo(xmlNode *node, std::string &name, std::string &value, std::map< std::string, std::string > &props)
get the name, value if any, and any properties for the specified node
Definition: BESXMLUtils.cc:109
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:54
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71
void load_dynamic_config(std::string name)
Loads the the applicable dynamic configuration or nothing if no configuration is applicable.
Definition: TheBESKeys.cc:681