bes  Updated for version 3.20.5
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 
38 using namespace std;
39 
40 #include "BESXMLInterface.h"
41 #include "BESXMLCommand.h"
42 #include "BESXMLUtils.h"
43 #include "BESDataNames.h"
44 
45 #include "BESResponseHandler.h"
46 #include "BESReturnManager.h"
47 #include "BESInfo.h"
48 #include "BESStopWatch.h"
49 
50 #include "BESDebug.h"
51 #include "BESLog.h"
52 #include "BESSyntaxUserError.h"
53 
54 #define LOG_ONLY_GET_COMMANDS
55 
56 BESXMLInterface::BESXMLInterface(const string &xml_doc, ostream *strm) :
57  BESInterface(strm), d_xml_document(xml_doc)
58 {
59  // This is needed because we want the parent to have access to the information
60  // added to the DHI
61  d_dhi_ptr = &d_xml_interface_dhi;
62 }
63 
64 BESXMLInterface::~BESXMLInterface()
65 {
66  clean();
67 }
68 
72 {
73  BESDEBUG("bes", "Entering: " << __PRETTY_FUNCTION__ << endl);
74  BESDEBUG("bes", "building request plan for xml document: " << endl << d_xml_document << endl);
75 
76  // I do not know why, but uncommenting this macro breaks some tests
77  // on Linux but not OSX (CentOS 6, Ubuntu 12 versus OSX 10.11) by
78  // causing some XML elements in DMR responses to be twiddled in the
79  // responses build on Linux but not on OSX.
80  //
81  // LIBXML_TEST_VERSION
82 
83  xmlDoc *doc = NULL;
84  xmlNode *root_element = NULL;
85  xmlNode *current_node = NULL;
86 
87  try {
88  // set the default error function to my own
89  vector<string> parseerrors;
90  xmlSetGenericErrorFunc((void *) &parseerrors, BESXMLUtils::XMLErrorFunc);
91 
92  // XML_PARSE_NONET
93  doc = xmlReadMemory(d_xml_document.c_str(), d_xml_document.size(), "" /* base URL */,
94  NULL /* encoding */, XML_PARSE_NONET /* xmlParserOption */);
95 
96  if (doc == NULL) {
97  string err = "Problem parsing the request xml document:\n";
98  bool isfirst = true;
99  vector<string>::const_iterator i = parseerrors.begin();
100  vector<string>::const_iterator e = parseerrors.end();
101  for (; i != e; i++) {
102  if (!isfirst && (*i).compare(0, 6, "Entity") == 0) {
103  err += "\n";
104  }
105  err += (*i);
106  isfirst = false;
107  }
108  throw BESSyntaxUserError(err, __FILE__, __LINE__);
109  }
110 
111  // get the root element and make sure it exists and is called request
112  root_element = xmlDocGetRootElement(doc);
113  if (!root_element) throw BESSyntaxUserError("There is no root element in the xml document", __FILE__, __LINE__);
114 
115  string root_name;
116  string root_val;
117  map<string, string> attributes;
118  BESXMLUtils::GetNodeInfo(root_element, root_name, root_val, attributes);
119  if (root_name != "request")
120  throw BESSyntaxUserError(
121  string("The root element should be a request element, name is ").append((char *) root_element->name),
122  __FILE__, __LINE__);
123 
124  if (!root_val.empty())
125  throw BESSyntaxUserError(string("The request element must not contain a value, ").append(root_val),
126  __FILE__, __LINE__);
127 
128  // there should be a request id property with one value.
129  string &reqId = attributes[REQUEST_ID];
130  if (reqId.empty()) throw BESSyntaxUserError("The request id value empty", __FILE__, __LINE__);
131 
132  d_dhi_ptr->data[REQUEST_ID] = reqId;
133 
134  BESDEBUG("besxml", "request id = " << d_dhi_ptr->data[REQUEST_ID] << endl);
135 
136  // iterate through the children of the request element. Each child is an
137  // individual command.
138  bool has_response = false; // set to true when a command with a response is found.
139  current_node = root_element->children;
140 
141  while (current_node) {
142  if (current_node->type == XML_ELEMENT_NODE) {
143  // given the name of this node we should be able to find a
144  // BESXMLCommand object
145  string node_name = (char *) current_node->name;
146 
147  // The Command Builder scheme is a kind of factory, but which uses lists and
148  // a static method defined by each child of BESXMLCommand (called CommandBuilder).
149  // These static methods make new instances of the specific commands and, in so
150  // doing, _copy_ the DataHandlerInterface instance using that class' clone() method.
151  // jhrg 11/7/17
152  p_xmlcmd_builder bldr = BESXMLCommand::find_command(node_name);
153  if (!bldr)
154  throw BESSyntaxUserError(string("Unable to find command for ").append(node_name), __FILE__,
155  __LINE__);
156 
157  BESXMLCommand *current_cmd = bldr(d_xml_interface_dhi);
158  if (!current_cmd)
159  throw BESInternalError(string("Failed to build command object for ").append(node_name), __FILE__,
160  __LINE__);
161 
162  // push this new command to the back of the list
163  d_xml_cmd_list.push_back(current_cmd);
164 
165  // only one of the commands in a request can build a response
166  bool cmd_has_response = current_cmd->has_response();
167  if (has_response && cmd_has_response)
168  throw BESSyntaxUserError("Commands with multiple responses not supported.", __FILE__, __LINE__);
169 
170  has_response = cmd_has_response;
171 
172  // parse the request given the current node
173  current_cmd->parse_request(current_node);
174 
175  // Check if the correct transmitter is present. We look for it again in do_transmit()
176  // where it is actually used. This test just keeps us from building a response that
177  // cannot be transmitted. jhrg 11/8/17
178  //
179  // TODO We could add the 'transmitter' to the DHI.
180  BESDataHandlerInterface &current_dhi = current_cmd->get_xmlcmd_dhi();
181 
182  string return_as = current_dhi.data[RETURN_CMD];
183  if (!return_as.empty() && !BESReturnManager::TheManager()->find_transmitter(return_as))
184  throw BESSyntaxUserError(string("Unable to find transmitter ").append(return_as), __FILE__,
185  __LINE__);
186  }
187 
188  current_node = current_node->next;
189  }
190  }
191  catch (...) {
192  xmlFreeDoc(doc);
193  xmlCleanupParser();
194  throw;
195  }
196 
197  xmlFreeDoc(doc);
198 
199  // Removed since the docs indicate it's not needed and it might be
200  // contributing to memory issues flagged by valgrind. 2/25/09 jhrg
201  //
202  // Added this back in. It seems to the the cause of BES-40 - where
203  // When certain tests are run, the order of <Dimension..> elements
204  // in a DMR for a server function result is different when the BESDEBUG
205  // output is on versus when it is not. This was true only when the
206  // BESDEBUG context was 'besxml' or timing,' which lead me here.
207  // Making this call removes the errant behavior. I've run tests using
208  // valgrind and I see no memory problems from this call. jhrg 9/25/15
209  xmlCleanupParser();
210 
211  BESDEBUG("bes", "Done building request plan" << endl);
212 }
213 
217 {
218  vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
219  vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
220  for (; i != e; i++) {
221  (*i)->prep_request(); // TODO remove this if possible jhrg 1/7/19
222 
223  d_dhi_ptr = &(*i)->get_xmlcmd_dhi();
224 
225  // In 'verbose' logging mode, log all the commands.
226  VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] executing" << endl);
227 
228  // This is the main log entry when the server is not in 'verbose' mode.
229  // There are two ways we can do this, one writes a log line for only the
230  // get commands, the other write the set container, define and get commands.
231  // TODO Make this configurable? jhrg 11/14/17
232 #ifdef LOG_ONLY_GET_COMMANDS
233  // Special logging action for the 'get' command. In non-verbose logging mode,
234  // only log the get command.
235  if (d_dhi_ptr->action.find("get.") != string::npos) {
236 
237  string new_log_info = d_dhi_ptr->action;
238  if (!d_dhi_ptr->data[RETURN_CMD].empty())
239  new_log_info.append(",").append(d_dhi_ptr->data[RETURN_CMD]);
240 
241  // Assume this is DAP and thus there is at most one container. Log a warning if that's
242  // not true. jhrg 11/14/17
243  BESContainer *c = *(d_dhi_ptr->containers.begin());
244  if (c) {
245  if (!c->get_real_name().empty()) new_log_info.append(",").append(c->get_real_name());
246 
247  if (!c->get_constraint().empty()) {
248  new_log_info.append(",").append(c->get_constraint());
249  }
250  else {
251  if (!c->get_dap4_constraint().empty()) new_log_info.append(",").append(c->get_dap4_constraint());
252  if (!c->get_dap4_function().empty()) new_log_info.append(",").append(c->get_dap4_function());
253  }
254  }
255 
256  LOG(new_log_info << endl);
257 
258  if (d_dhi_ptr->containers.size() > 1)
259  LOG("Warning: The previous command had multiple containers defined, but only the was logged.");
260  }
261 #else
262  if (!BESLog::TheLog()->is_verbose()) {
263  if (d_dhi_ptr->action.find("set.context") == string::npos
264  && d_dhi_ptr->action.find("show.catalog") == string::npos) {
265  LOG(d_dhi_ptr->data[LOG_INFO] << endl);
266  }
267  }
268 #endif
269 
271 
272  // Here's where we could look at the dynamic type to do something different
273  // for a new kind of XMLCommand (e.g., SimpleXMLCommand). for that new command,
274  // move the code now in the response_handler->execute() and ->transmit() into
275  // it. This would eliminate the ResponseHandlers. However, that might not be the
276  // best way to handle the 'get' command, which uses a different ResponseHandler
277  // for each different 'type' of thing it will 'get'. jhrg 3/14/18
278 
280 
281  if (!d_dhi_ptr->response_handler)
282  throw BESInternalError(string("The response handler '") + d_dhi_ptr->action + "' does not exist", __FILE__,
283  __LINE__);
284 
285  d_dhi_ptr->response_handler->execute(*d_dhi_ptr);
286 
287  transmit_data(); // TODO move method body in here? jhrg 11/8/17
288  }
289 }
290 
305 {
306  if (d_dhi_ptr->error_info) {
307  VERBOSE(d_dhi_ptr->data[SERVER_PID] << " from " << d_dhi_ptr->data[REQUEST_FROM] << " ["
308  << d_dhi_ptr->data[LOG_INFO] << "] Error" << endl);
309 
310  ostringstream strm;
311  d_dhi_ptr->error_info->print(strm);
312  LOG("Transmitting error: " << strm.str() << endl);
313 
315  }
316  else if (d_dhi_ptr->response_handler) {
317  VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] transmitting" << endl);
318 
319  BESStopWatch sw;
320  if (BESISDEBUG(TIMING_LOG)) sw.start(d_dhi_ptr->data[LOG_INFO] + " transmitting", d_dhi_ptr->data[REQUEST_ID]);
321 
322  string return_as = d_dhi_ptr->data[RETURN_CMD];
323  if (!return_as.empty()) {
324  d_transmitter = BESReturnManager::TheManager()->find_transmitter(return_as);
325  if (!d_transmitter) {
326  throw BESSyntaxUserError(string("Unable to find transmitter ") + return_as, __FILE__, __LINE__);
327  }
328  }
329 
330  d_dhi_ptr->response_handler->transmit(d_transmitter, *d_dhi_ptr);
331  }
332 }
333 
342 {
343  if (BESLog::TheLog()->is_verbose()) {
344  vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
345  vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
346  for (; i != e; i++) {
347  d_dhi_ptr = &(*i)->get_xmlcmd_dhi();
348 
349  // IF the DHI's error_info object pointer is null, the request was successful.
350  string result = (!d_dhi_ptr->error_info) ? "completed" : "failed";
351 
352  // This is only printed for verbose logging.
353  LOG(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] " << result << endl);
354  }
355  }
356 }
357 
361 {
362  vector<BESXMLCommand *>::iterator i = d_xml_cmd_list.begin();
363  vector<BESXMLCommand *>::iterator e = d_xml_cmd_list.end();
364  for (; i != e; i++) {
365  BESXMLCommand *cmd = *i;
366  d_dhi_ptr = &cmd->get_xmlcmd_dhi();
367 
368  if (d_dhi_ptr) {
369  VERBOSE(d_dhi_ptr->data[REQUEST_FROM] << " [" << d_dhi_ptr->data[LOG_INFO] << "] cleaning" << endl);
370 
371  d_dhi_ptr->clean(); // Delete the ResponseHandler if present
372  }
373 
374  delete cmd;
375  }
376 
377  d_xml_cmd_list.clear();
378 }
379 
386 void BESXMLInterface::dump(ostream &strm) const
387 {
388  strm << BESIndent::LMarg << "BESXMLInterface::dump - (" << (void *) this << ")" << endl;
389  BESIndent::Indent();
390  BESInterface::dump(strm);
391  vector<BESXMLCommand *>::const_iterator i = d_xml_cmd_list.begin();
392  vector<BESXMLCommand *>::const_iterator e = d_xml_cmd_list.end();
393  for (; i != e; i++) {
394  BESXMLCommand *cmd = *i;
395  cmd->dump(strm);
396  }
397  BESIndent::UnIndent();
398 }
399 
void clean()
clean up any information created within this data handler interface
exception thrown if inernal error encountered
virtual void dump(ostream &strm) const
dumps information about this object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the response object built by the execute command using the specified transmitter object
virtual void dump(std::ostream &strm) const
Manage any exceptions thrown during the whole process.
static void GetNodeInfo(xmlNode *node, string &name, string &value, map< string, string > &props)
get the name, value if any, and any properties for the specified node
Definition: BESXMLUtils.cc:101
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
bool is_verbose()
Returns true if verbose logging is requested.
Definition: BESLog.h:185
BESDataHandlerInterface * d_dhi_ptr
Allocated by the child class.
Definition: BESInterface.h:124
virtual void execute(BESDataHandlerInterface &dhi)=0
knows how to build a requested response object
virtual void parse_request(xmlNode *node)=0
Parse the XML request document beginning at the given node.
string get_dap4_constraint() const
retrieve the constraint expression for this container
Definition: BESContainer.h:206
BESTransmitter * d_transmitter
The Transmitter to use for the result.
Definition: BESInterface.h:125
virtual void log_status()
Log the status of the request to the BESLog file.
virtual bool start(string name)
Definition: BESStopWatch.cc:57
error thrown if there is a user syntax error in the request or any other user error
virtual void build_data_request_plan()
Build the data request plan using the BESCmdParser.
static p_xmlcmd_builder find_command(const std::string &cmd_str)
Find the BESXMLCommand creation function with the given name.
virtual void execute_data_request_plan()
Execute the data request plan.
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:46
virtual void clean()
Clean up after the request is completed.
Entry point into BES, building responses to given requests.
Definition: BESInterface.h:118
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.
string get_constraint() const
retrieve the constraint expression for this container
Definition: BESContainer.h:197
virtual void transmit_data()
Transmit the response object.
string get_real_name() const
retrieve the real name for this container, such as a file name.
Definition: BESContainer.h:183
Base class for the BES's commands.
Definition: BESXMLCommand.h:63
BESInfo * error_info
error information object
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:68
virtual BESDataHandlerInterface & get_xmlcmd_dhi()
Return the current BESDataHandlerInterface.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void print(ostream &strm)
print the information from this informational object to the specified stream
Definition: BESInfo.cc:261
virtual bool has_response()=0
Does this command return a response to the client?
string action
the response object requested, e.g. das, dds
string get_dap4_function() const
retrieve the constraint expression for this container
Definition: BESContainer.h:215