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