bes  Updated for version 3.17.0
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 <iostream>
34 #include <sstream>
35 
36 using std::endl;
37 using std::cout;
38 using std::stringstream;
39 
40 #include "BESXMLInterface.h"
41 #include "BESXMLCommand.h"
42 #include "BESXMLUtils.h"
43 #include "BESDataNames.h"
44 #include "BESDebug.h"
45 #include "BESLog.h"
46 #include "BESSyntaxUserError.h"
47 #include "BESReturnManager.h"
48 
49 BESXMLInterface::BESXMLInterface(const string &xml_doc, ostream *strm) :
50  BESBasicInterface(strm)
51 {
52  _dhi = &_base_dhi;
53  _dhi->data[DATA_REQUEST] = "xml document";
54  _dhi->data["XMLDoc"] = xml_doc;
55 }
56 
57 BESXMLInterface::~BESXMLInterface()
58 {
59  clean();
60 }
61 
62 int BESXMLInterface::execute_request(const string &from)
63 {
65 }
66 
70 {
72 }
73 
77 {
79 }
80 
84 {
85  BESDEBUG("bes", "Entering: " << __PRETTY_FUNCTION__ << endl);
86  BESDEBUG("besxml", "building request plan for xml document: " << endl << _dhi->data["XMLDoc"] << endl);
87 
88  if (BESLog::TheLog()->is_verbose()) {
89  *(BESLog::TheLog()) << _dhi->data[SERVER_PID] << " from " << _dhi->data[REQUEST_FROM] << "] building" << endl;
90  }
91 
92  // I do not know why, but uncommenting this macro breaks some tests
93  // on linux but not OSX (CentOS 6, Ubuntu 12 versus OSX 10.11) by
94  // causing some XML elements in DMR responses to be twiddled in the
95  // responses build on linux but not on OSX.
96  // LIBXML_TEST_VERSION
97 
98  xmlDoc *doc = NULL;
99  xmlNode *root_element = NULL;
100  xmlNode *current_node = NULL;
101 
102  try {
103  // set the default error function to my own
104  vector<string> parseerrors;
105  xmlSetGenericErrorFunc((void *) &parseerrors, BESXMLUtils::XMLErrorFunc);
106 
107  // XML_PARSE_NONET
108  doc = xmlReadMemory(_dhi->data["XMLDoc"].c_str(), _dhi->data["XMLDoc"].size(), "" /* base URL */,
109  NULL /* encoding */, XML_PARSE_NONET /* xmlParserOption */);
110 
111  if (doc == NULL) {
112  string err = "Problem parsing the request xml document:\n";
113  bool isfirst = true;
114  vector<string>::const_iterator i = parseerrors.begin();
115  vector<string>::const_iterator e = parseerrors.end();
116  for (; i != e; i++) {
117  if (!isfirst && (*i).compare(0, 6, "Entity") == 0) {
118  err += "\n";
119  }
120  err += (*i);
121  isfirst = false;
122  }
123  throw BESSyntaxUserError(err, __FILE__, __LINE__);
124  }
125 
126  // get the root element and make sure it exists and is called request
127  root_element = xmlDocGetRootElement(doc);
128  if (!root_element) {
129  string err = "There is no root element in the xml document";
130  throw BESSyntaxUserError(err, __FILE__, __LINE__);
131  }
132 
133  string root_name;
134  string root_val;
135  map<string, string> props;
136  BESXMLUtils::GetNodeInfo(root_element, root_name, root_val, props);
137  if (root_name != "request") {
138  string err = (string) "The root element should be a request element, " + "name is "
139  + (char *) root_element->name;
140  throw BESSyntaxUserError(err, __FILE__, __LINE__);
141  }
142  if (root_val != "") {
143  string err = (string) "The request element must not contain a value, " + root_val;
144  throw BESSyntaxUserError(err, __FILE__, __LINE__);
145  }
146 
147  // there should be a request id property with one value.
148  string &reqId = props[REQUEST_ID];
149  if (reqId.empty()) {
150  string err = (string) "request id value empty";
151  throw BESSyntaxUserError(err, __FILE__, __LINE__);
152  }
153  _dhi->data[REQUEST_ID] = reqId;
154 
155  BESDEBUG("besxml", "request id = " << _dhi->data[REQUEST_ID] << endl);
156 
157  // iterate through the children of the request element. Each child is an
158  // individual command.
159  bool has_response = false;
160  current_node = root_element->children;
161 
162  while (current_node) {
163  if (current_node->type == XML_ELEMENT_NODE) {
164  // given the name of this node we should be able to find a
165  // BESXMLCommand object
166  string node_name = (char *) current_node->name;
167 
168  p_xmlcmd_builder bldr = BESXMLCommand::find_command(node_name);
169  if (bldr) {
170  BESXMLCommand *current_cmd = bldr(_base_dhi);
171  if (!current_cmd) {
172  string err = (string) "Failed to build command object for " + node_name;
173  throw BESInternalError(err, __FILE__, __LINE__);
174  }
175 
176  // push this new command to the back of the list
177  _cmd_list.push_back(current_cmd);
178 
179  // only one of the commands can build a response. If more
180  // than one builds a response, throw an error
181  bool cmd_has_response = current_cmd->has_response();
182  if (has_response && cmd_has_response) {
183  string err = "Multiple responses not allowed";
184  throw BESSyntaxUserError(err, __FILE__, __LINE__);
185  }
186  has_response = cmd_has_response;
187 
188  // parse the request given the current node
189  BESDEBUG("besxml", "parse request using " << node_name << endl);
190  current_cmd->parse_request(current_node);
191 
192  BESDataHandlerInterface &current_dhi = current_cmd->get_dhi();
193 
194  BESDEBUG("besxml", node_name << " parsed request, dhi = " << current_dhi << endl);
195 
196  string returnAs = current_dhi.data[RETURN_CMD];
197  if (returnAs != "") {
198  BESDEBUG("xml", "Finding transmitter: " << returnAs << " ... " << endl);
199  BESTransmitter *transmitter = BESReturnManager::TheManager()->find_transmitter(returnAs);
200  if (!transmitter) {
201  string s = (string) "Unable to find transmitter " + returnAs;
202  throw BESSyntaxUserError(s, __FILE__, __LINE__);
203  }
204  BESDEBUG("xml", "OK" << endl);
205  }
206  }
207  else {
208  string err = (string) "Unable to find command for " + node_name;
209  throw BESSyntaxUserError(err, __FILE__, __LINE__);
210  }
211  }
212  current_node = current_node->next;
213  }
214  }
215  catch (... /* BESError &e; changed 7/1/15 jhrg */) {
216  xmlFreeDoc(doc);
217  xmlCleanupParser();
218  throw /*e*/;
219  }
220 
221  xmlFreeDoc(doc);
222 
223  // Removed since the docs indicate it's not needed and it might be
224  // contributing to memory issues flagged by valgrind. 2/25/09 jhrg
225  //
226  // Added this back in. It seems to the the cause of BES-40 - where
227  // When certain tests are run, the order of <Dimension..> elements
228  // in a DMR for a server function result is different when the BESDEBUG
229  // output is on versus when it is not. This was true only when the
230  // BESDEBUG context was 'besxml' or timing,' which lead me here.
231  // Making this call removes the errant behavior. I've run tests using
232  // valgrind and I see no memory problems from this call. jhrg 9/25/15
233  xmlCleanupParser();
234 
235  BESDEBUG("besxml", "Done building request plan" << endl);
236 
238 }
239 
243 {
244  vector<BESXMLCommand *>::iterator i = _cmd_list.begin();
245  vector<BESXMLCommand *>::iterator e = _cmd_list.end();
246  for (; i != e; i++) {
247  (*i)->prep_request();
248  _dhi = &(*i)->get_dhi();
250  }
251 }
252 
256 {
258 }
259 
263 {
264  string returnAs = _dhi->data[RETURN_CMD];
265  if (returnAs != "") {
266  BESDEBUG("xml", "Setting transmitter: " << returnAs << " ... " << endl);
267  _transmitter = BESReturnManager::TheManager()->find_transmitter(returnAs);
268  if (!_transmitter) {
269  string s = (string) "Unable to find transmitter " + returnAs;
270  throw BESSyntaxUserError(s, __FILE__, __LINE__);
271  }
272  BESDEBUG("xml", "OK" << endl);
273  }
274 
276 }
277 
283 {
284  vector<BESXMLCommand *>::iterator i = _cmd_list.begin();
285  vector<BESXMLCommand *>::iterator e = _cmd_list.end();
286  for (; i != e; i++) {
287  _dhi = &(*i)->get_dhi();
289  }
290 }
291 
308 {
309  vector<BESXMLCommand *>::iterator i = _cmd_list.begin();
310  vector<BESXMLCommand *>::iterator e = _cmd_list.end();
311  for (; i != e; i++) {
312  _dhi = &(*i)->get_dhi();
314  }
315 }
316 
320 {
321  vector<BESXMLCommand *>::iterator i = _cmd_list.begin();
322  vector<BESXMLCommand *>::iterator e = _cmd_list.end();
323  for (; i != e; i++) {
324  BESXMLCommand *cmd = *i;
325  _dhi = &cmd->get_dhi();
327  delete cmd;
328  }
329  _cmd_list.clear();
330 }
331 
338 void BESXMLInterface::dump(ostream &strm) const
339 {
340  strm << BESIndent::LMarg << "BESXMLInterface::dump - (" << (void *) this << ")" << endl;
341  BESIndent::Indent();
343  vector<BESXMLCommand *>::const_iterator i = _cmd_list.begin();
344  vector<BESXMLCommand *>::const_iterator e = _cmd_list.end();
345  for (; i != e; i++) {
346  BESXMLCommand *cmd = *i;
347  cmd->dump(strm);
348  }
349  BESIndent::UnIndent();
350 }
351 
virtual int execute_request(const string &from)
Override execute_request in order to register memory pool.
exception thrown if inernal error encountered
Entry point into BES using string command requests.
virtual void invoke_aggregation()
Invoke the aggregation server, if there is one.
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:99
virtual void initialize()
Initialize the BES.
virtual void parse_request(xmlNode *node)=0
Parse the XML request document begining at the given node.
virtual void log_status()
Log the status of the request to the BESLog file.
virtual BESDataHandlerInterface & get_dhi()
Return the current BESDataHandlerInterface.
Definition: BESXMLCommand.h:91
virtual void transmit_data()
Transmit the response object.
virtual void build_data_request_plan()
Build the data request plan using the BESCmdParser.
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.
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 log_status()
Log the status of the request to the BESLog file.
virtual void clean()
Clean up after the request is completed.
virtual void report_request()
Report the request and status of the request to BESReporterList::TheList()
virtual void invoke_aggregation()
Invoke the aggregation server, if there is one.
virtual void report_request()
Report the request and status of the request.
virtual void validate_data_request()
Validate the incoming request information.
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.
virtual void transmit_data()
Transmit the response object.
virtual void validate_data_request()
Validate the incoming request information.
virtual void execute_data_request_plan()
Execute the data request plan.
virtual void initialize()
Initialize the BES.
virtual void clean()
Clean up after the request is completed.
virtual bool has_response()=0
Has a response handler been created given the request document?
virtual void dump(ostream &strm) const
dumps information about this object
virtual void dump(ostream &strm) const
dumps information about this object
virtual int execute_request(const string &from)
Override execute_request in order to register memory pool.
static p_xmlcmd_builder find_command(const string &cmd_str)
Find the BESXMLCommand creation function with the given name.
virtual void dump(ostream &strm) const
dumps information about this object