bes  Updated for version 3.20.6
BESXMLDefineCommand.cc
1 // BESXMLDefineCommand.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 "BESXMLDefineCommand.h"
34 #include "BESContainerStorageList.h"
35 #include "BESContainerStorage.h"
36 
37 #include "BESXMLUtils.h"
38 #include "BESUtil.h"
39 #include "BESResponseNames.h"
40 #include "BESDataNames.h"
41 
42 #include "BESSyntaxUserError.h"
43 #include "BESInternalFatalError.h"
44 #include "BESDebug.h"
45 
46 using std::endl;
47 using std::string;
48 using std::vector;
49 using std::ostream;
50 using std::map;
51 
52 BESXMLDefineCommand::BESXMLDefineCommand(const BESDataHandlerInterface &base_dhi) :
53  BESXMLCommand(base_dhi), _default_constraint(""), _default_dap4_constraint(""), _default_dap4_function("")
54 {
55 }
56 
86 {
87  string value; // element value, should not be any
88  string action; // element name, which is the request action
89  map<string, string> props; // element attributes; 'name' and maybe 'space'
90 
91  BESXMLUtils::GetNodeInfo(node, action, value, props);
92  if (action != DEFINE_RESPONSE_STR) {
93  string err = "The specified command " + action + " is not a set context command";
94  throw BESInternalFatalError(err, __FILE__, __LINE__);
95  }
96 
97  d_xmlcmd_dhi.action = DEFINE_RESPONSE;
98 
99  string def_name = props["name"];
100  if (def_name.empty())
101  throw BESSyntaxUserError(string(action) + " command: definition name missing", __FILE__, __LINE__);
102 
103  d_xmlcmd_dhi.data[DEF_NAME] = def_name;
104  d_cmd_log_info = (string) "define " + def_name;
105 
106  d_xmlcmd_dhi.data[STORE_NAME] = props["space"].empty() ? DEFAULT: props["space"];
107  d_cmd_log_info += " in " + d_xmlcmd_dhi.data[STORE_NAME];
108 
109  int num_containers = 0;
110  string child_name;
111  string child_value;
112  props.clear();
113  xmlNode *child_node = BESXMLUtils::GetFirstChild(node, child_name, child_value, props);
114  while (child_node) {
115  if (child_name == "constraint") {
116  // default constraint for all containers
117  _default_constraint = child_value;
118  }
119  else if (child_name == "dap4constraint") {
120  // default function for all containers
121  _default_dap4_constraint = child_value;
122  }
123  else if (child_name == "dap4function") {
124  // default function for all containers
125  _default_dap4_function = child_value;
126  }
127  else if (child_name == "container") {
128  handle_container_element(action, child_node, child_value, props);
129  num_containers++;
130  }
131  else {
132  throw BESSyntaxUserError(string(action) + " Unrecognized child element: " + child_name, __FILE__, __LINE__);
133  }
134 #if 0
135  else if (child_name == "aggregate") {
136  handle_aggregate_element(action, child_node, child_value, props);
137  }
138 #endif
139 
140  // get the next child element
141  props.clear();
142  child_name.clear();
143  child_value.clear();
144  child_node = BESXMLUtils::GetNextChild(child_node, child_name, child_value, props);
145  }
146 
147  if (num_containers < 1)
148  throw BESSyntaxUserError(string(action) + " The define element must contain at least one container element", __FILE__, __LINE__);
149 
150  d_cmd_log_info += " as ";
151  bool first = true;
152  vector<string>::iterator i = container_names.begin();
153  vector<string>::iterator e = container_names.end();
154  for (; i != e; i++) {
155  if (!first) d_cmd_log_info += ",";
156  d_cmd_log_info += (*i);
157  first = false;
158  }
159 
160  if (container_constraints.size() || container_dap4constraints.size() || container_dap4functions.size() || container_attributes.size()) {
161  d_cmd_log_info += " with ";
162  first = true;
163  i = container_names.begin();
164  e = container_names.end();
165  for (; i != e; i++) {
166  if (container_constraints.count((*i))) {
167  if (!first) d_cmd_log_info += ",";
168  first = false;
169  d_cmd_log_info += (*i) + ".constraint=\"" + container_constraints[(*i)] + "\"";
170  }
171  if (container_dap4constraints.count((*i))) {
172  if (!first) d_cmd_log_info += ",";
173  first = false;
174  d_cmd_log_info += (*i) + ".dap4constraint=\"" + container_dap4constraints[(*i)] + "\"";
175  }
176  if (container_dap4functions.count((*i))) {
177  if (!first) d_cmd_log_info += ",";
178  first = false;
179  d_cmd_log_info += (*i) + ".dap4function=\"" + container_dap4functions[(*i)] + "\"";
180  }
181  if (container_attributes.count((*i))) {
182  if (!first) d_cmd_log_info += ",";
183  first = false;
184  d_cmd_log_info += (*i) + ".attributes=\"" + container_attributes[(*i)] + "\"";
185  }
186  }
187  }
188 
189  d_cmd_log_info += ";";
190 
191  // now that we've set the action, go get the response handler for the action
193 }
194 
218 void BESXMLDefineCommand::handle_container_element(const string &action, xmlNode *node, const string &/*value*/,
219  map<string, string> &props)
220 {
221  string name = props["name"];
222  if (name.empty()) {
223  string err = action + " command: container element missing name prop";
224  throw BESSyntaxUserError(err, __FILE__, __LINE__);
225  }
226 
227  container_names.push_back(name);
228 
229  container_store_names[name] = props["space"];
230 
231  bool have_constraint = false;
232  bool have_dap4constraint = false;
233  bool have_dap4function = false;
234  bool have_attributes = false;
235  string child_name;
236  string child_value;
237  string constraint;
238  string attributes;
239  map<string, string> child_props;
240  xmlNode *child_node = BESXMLUtils::GetFirstChild(node, child_name, child_value, child_props);
241  while (child_node) {
242  if (child_name == "constraint") {
243  if (child_props.size()) {
244  string err = action + " command: constraint element " + "should not contain properties";
245  throw BESSyntaxUserError(err, __FILE__, __LINE__);
246  }
247  if (child_value.empty()) {
248  string err = action + " command: constraint element " + "missing value";
249  throw BESSyntaxUserError(err, __FILE__, __LINE__);
250  }
251  if (have_constraint) {
252  string err = action + " command: container element " + "contains multiple constraint elements";
253  throw BESSyntaxUserError(err, __FILE__, __LINE__);
254  }
255  have_constraint = true;
256  container_constraints[name] = child_value;
257  }
258  else if (child_name == "dap4constraint") {
259  if (child_props.size()) {
260  string err = action + " command: constraint element " + "should not contain properties";
261  throw BESSyntaxUserError(err, __FILE__, __LINE__);
262  }
263  if (child_value.empty()) {
264  string err = action + " command: constraint element " + "missing value";
265  throw BESSyntaxUserError(err, __FILE__, __LINE__);
266  }
267  if (have_dap4constraint) {
268  string err = action + " command: container element " + "contains multiple constraint elements";
269  throw BESSyntaxUserError(err, __FILE__, __LINE__);
270  }
271  have_dap4constraint = true;
272  container_dap4constraints[name] = child_value;
273  }
274  else if (child_name == "dap4function") {
275  if (child_props.size()) {
276  string err = action + " command: dap4_function element " + "should not contain properties";
277  throw BESSyntaxUserError(err, __FILE__, __LINE__);
278  }
279  if (child_value.empty()) {
280  string err = action + " command: dap4_function element " + "missing value";
281  throw BESSyntaxUserError(err, __FILE__, __LINE__);
282  }
283  if (have_dap4function) {
284  string err = action + " command: container element " + "contains multiple dap4_function elements";
285  throw BESSyntaxUserError(err, __FILE__, __LINE__);
286  }
287  have_dap4function = true;
288  container_dap4functions[name] = child_value;
289  }
290  else if (child_name == "attributes") {
291  if (child_props.size()) {
292  string err = action + " command: attributes element " + "should not contain properties";
293  throw BESSyntaxUserError(err, __FILE__, __LINE__);
294  }
295  if (child_value.empty()) {
296  string err = action + " command: attributes element " + "missing value";
297  throw BESSyntaxUserError(err, __FILE__, __LINE__);
298  }
299  if (have_attributes) {
300  string err = action + " command: container element " + "contains multiple attributes elements";
301  throw BESSyntaxUserError(err, __FILE__, __LINE__);
302  }
303  have_attributes = true;
304  container_attributes[name] = child_value;
305  }
306 
307  // get the next child element
308  props.clear();
309  child_name.clear();
310  child_value.clear();
311  child_node = BESXMLUtils::GetNextChild(child_node, child_name, child_value, props);
312  }
313 }
314 
315 #if 0
316 
329 void BESXMLDefineCommand::handle_aggregate_element(const string &action, xmlNode */*node*/, const string &/*value*/,
330  map<string, string> &props)
331 {
332  string handler = props["handler"];
333  string cmd = props["cmd"];
334  if (handler.empty()) {
335  string err = action + " command: must specify aggregation handler";
336  throw BESSyntaxUserError(err, __FILE__, __LINE__);
337  }
338  if (cmd.empty()) {
339  string err = action + " command: must specify aggregation cmd";
340  throw BESSyntaxUserError(err, __FILE__, __LINE__);
341  }
342 
343  d_xmlcmd_dhi.data[AGG_HANDLER] = handler;
344  d_xmlcmd_dhi.data[AGG_CMD] = cmd;
345  d_cmd_log_info += " aggregate using " + handler + " by " + cmd;
346 }
347 #endif
348 
349 
359 {
360  vector<string>::iterator i = container_names.begin();
361  vector<string>::iterator e = container_names.end();
362  for (; i != e; i++) {
363  // look for the specified container
364  BESContainer *c = 0;
365 
366  // Is a particular store is being used - this is container store
367  // not the definition store. If no store is named, search them all.
368  string store = container_store_names[(*i)];
369  if (!store.empty()) {
370  BESContainerStorage *cs = BESContainerStorageList::TheList()->find_persistence(store);
371  if (cs) c = cs->look_for((*i));
372  }
373  else {
374  c = BESContainerStorageList::TheList()->look_for((*i));
375  }
376 
377  if (c == 0)
378  throw BESSyntaxUserError(string("Could not find the container ") + (*i), __FILE__, __LINE__);
379 
380  // What use case do we have in which the "default" value of the constraint is not an empty string?
381  string constraint = container_constraints[(*i)];
382  if (constraint.empty()) constraint = _default_constraint;
383  c->set_constraint(constraint);
384 
385  // What use case do we have in which the "default" value of the dap4constraint is not an empty string?
386  string dap4constraint = container_dap4constraints[(*i)];
387  if (dap4constraint.empty()) dap4constraint = _default_dap4_constraint;
388  c->set_dap4_constraint(dap4constraint);
389 
390  // What use case do we have in which the "default" value of the dap4function is not an empty string?
391  string function = container_dap4functions[(*i)];
392  if (function.empty()) function = _default_dap4_function;
393  c->set_dap4_function(function);
394 
395  string attrs = container_attributes[(*i)];
396  c->set_attributes(attrs);
397  d_xmlcmd_dhi.containers.push_back(c);
398 
399  BESDEBUG("xml", "BESXMLDefineCommand::prep_request() - define using container: " << endl << *c << endl);
400  }
401 }
402 
410 void BESXMLDefineCommand::dump(ostream &strm) const
411 {
412  strm << BESIndent::LMarg << "BESXMLDefineCommand::dump - (" << (void *) this << ")" << endl;
413  BESIndent::Indent();
414  BESXMLCommand::dump(strm);
415  BESIndent::UnIndent();
416 }
417 
419 BESXMLDefineCommand::CommandBuilder(const BESDataHandlerInterface &base_dhi)
420 {
421  return new BESXMLDefineCommand(base_dhi);
422 }
423 
BESContainerStorageList::look_for
virtual BESContainer * look_for(const std::string &sym_name)
look for the specified container information in the list of persistent stores.
Definition: BESContainerStorageList.cc:272
BESContainerStorage
provides persistent storage for data storage information represented by a container.
Definition: BESContainerStorage.h:67
BESInternalFatalError
exception thrown if an internal error is found and is fatal to the BES
Definition: BESInternalFatalError.h:43
BESXMLCommand::d_cmd_log_info
std::string d_cmd_log_info
Used only for the log.
Definition: BESXMLCommand.h:74
BESDataHandlerInterface::action
std::string action
the response object requested, e.g. das, dds
Definition: BESDataHandlerInterface.h:79
BESXMLCommand::set_response
virtual void set_response()
The request has been parsed, use the command action name to set the response handler.
Definition: BESXMLCommand.cc:63
BESContainer::set_attributes
void set_attributes(const std::string &attrs)
set desired attributes for this container
Definition: BESContainer.h:170
BESXMLDefineCommand::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESXMLDefineCommand.cc:410
BESXMLUtils::GetNodeInfo
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:105
BESContainerStorage::look_for
virtual BESContainer * look_for(const std::string &sym_name)=0
looks for a container in this persistent store
BESContainer::set_dap4_function
void set_dap4_function(const std::string &s)
set the constraint for this container
Definition: BESContainer.h:136
BESXMLCommand::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESXMLCommand.cc:119
BESXMLDefineCommand::prep_request
virtual void prep_request()
prepare the define command by making sure the containers exist
Definition: BESXMLDefineCommand.cc:358
BESXMLDefineCommand::parse_request
virtual void parse_request(xmlNode *node)
parse a define command.
Definition: BESXMLDefineCommand.cc:85
BESXMLDefineCommand
Definition: BESXMLDefineCommand.h:41
BESSyntaxUserError
error thrown if there is a user syntax error in the request or any other user error
Definition: BESSyntaxUserError.h:41
BESContainerStorageList::find_persistence
virtual BESContainerStorage * find_persistence(const std::string &persist_name)
find the persistence store with the given name
Definition: BESContainerStorageList.cc:212
BESDataHandlerInterface::data
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
Definition: BESDataHandlerInterface.h:90
BESXMLCommand
Base class for the BES's commands.
Definition: BESXMLCommand.h:63
BESContainer
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:65
BESContainer::set_constraint
void set_constraint(const std::string &s)
set the constraint for this container
Definition: BESContainer.h:118
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
BESXMLUtils::GetFirstChild
static xmlNode * GetFirstChild(xmlNode *node, std::string &child_name, std::string &child_value, std::map< std::string, std::string > &child_props)
get the first element child node for the given node
Definition: BESXMLUtils.cc:137
BESXMLUtils::GetNextChild
static xmlNode * GetNextChild(xmlNode *child_node, std::string &next_name, std::string &next_value, std::map< std::string, std::string > &next_props)
get the next element child node after the given child node
Definition: BESXMLUtils.cc:164
BESContainer::set_dap4_constraint
void set_dap4_constraint(const std::string &s)
set the constraint for this container
Definition: BESContainer.h:127