bes  Updated for version 3.20.6
XMLSetContextsCommand.cc
1 // XMLSetContextsCommand.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) 2018 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
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 OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "XMLSetContextsCommand.h"
26 
27 #include "BESXMLUtils.h"
28 #include "BESUtil.h"
29 #include "BESDataNames.h"
30 #include "BESSyntaxUserError.h"
31 #include "BESDebug.h"
32 
33 #include "SetContextsNames.h"
34 
35 #if !USE_CONTEXTS_RESPONSE_HANDLER
36 #include "BESContextManager.h"
37 #endif
38 
39 using namespace bes;
40 using namespace std;
41 
62 {
63  string value, name, action;
64  map<string, string> props;
65  BESXMLUtils::GetNodeInfo(node, action, value, props);
66  if (action != SET_CONTEXTS_STR) {
67  string err = "The specified command " + action + " is not a set context command";
68  throw BESSyntaxUserError(err, __FILE__, __LINE__);
69  }
70 
71  xmlNode *current_node = node->children;
72  while (current_node) {
73  if (current_node->type == XML_ELEMENT_NODE) {
74 
75  string name, value;
76  map<string, string> attributes;
77  BESXMLUtils::GetNodeInfo(current_node, name, value, attributes);
78 
79  if (name != "context")
80  throw BESSyntaxUserError(string("Inside setContexts, expected a 'context' but got '"
81  + name +"'."), __FILE__, __LINE__);
82 
83  if (value.empty())
84  throw BESSyntaxUserError(string("The 'context' element must contain a value"), __FILE__,
85  __LINE__);
86 
87  if (attributes.size() != 1 || attributes["name"].empty())
88  throw BESSyntaxUserError(string("The 'context' element must contain a 'name' attribute."),
89  __FILE__, __LINE__);
90 
91  // Set the context _or_ push the information into the DHI data[] map
92 #if USE_CONTEXTS_RESPONSE_HANDLER
93  // To record the information in the DHI data[] map, use one value to hold a
94  // list of all of Context names, then use the names to hold the values. Read
95  // this using names = data[CONTEXT_NAMES] and then:
96  //
97  // for each 'name' in names { value = data['context_'name] }
98  //
99  string context_key = string(CONTEXT_PREFIX).append(attributes["name"]);
100  d_xmlcmd_dhi.data[CONTEXT_NAMES] = d_xmlcmd_dhi.data[CONTEXT_NAMES].append(" ").append(context_key);
101  d_xmlcmd_dhi.data[context_key] = value;
102 
103  BESDEBUG("besxml", "d_xmlcmd_dhi.data[" << context_key << "] = " << value << endl);
104 #else
105  BESDEBUG("besxml", "In " << __func__ << " BESContextManager::TheManager()->set_context("
106  << name << ", " << value << ")" << endl);
107 
108  BESContextManager::TheManager()->set_context(attributes["name"], value);
109 #endif
110  }
111 
112  current_node = current_node->next;
113  }
114 
115  d_cmd_log_info = string("set contexts for ").append(d_xmlcmd_dhi.data[CONTEXT_NAMES]);
116 
117 
118 #if USE_CONTEXTS_RESPONSE_HANDLER
119  // The value set to DHI's action field is used to choose the matching ResponseHandler.
120  d_xmlcmd_dhi.action = SET_CONTEXTS_ACTION;
121 #else
122  // Set action_name here because the NULLResponseHandler (aka NULL_ACTION) won't know
123  // which command used it (current ResponseHandlers set this because there is a 1-to-1
124  // correlation between XMLCommands and ResponseHanlders). jhrg 2/8/18
125  d_xmlcmd_dhi.action_name = SET_CONTEXTS_STR;
126  d_xmlcmd_dhi.action = NULL_ACTION;
127 #endif
128 
129  // Set the response handler for the action in the command's DHI using the value of
130  // the DHI.action field. And, as a bonus, copy the d_cmd_log_info into DHI.data[LOG_INFO]
131  // and if VERBOSE logging is on, log that the command has been parsed.
133 }
134 
141 void XMLSetContextsCommand::dump(ostream &strm) const
142 {
143  strm << BESIndent::LMarg << "XMLSetContextsCommand::dump - (" << (void *) this << ")" << endl;
144  BESIndent::Indent();
145  BESXMLCommand::dump(strm);
146  BESIndent::UnIndent();
147 }
148 
150 XMLSetContextsCommand::CommandBuilder(const BESDataHandlerInterface &base_dhi)
151 {
152  return new XMLSetContextsCommand(base_dhi);
153 }
154 
bes::XMLSetContextsCommand::parse_request
virtual void parse_request(xmlNode *node)
parse a setContexts command.
Definition: XMLSetContextsCommand.cc:61
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
bes::XMLSetContextsCommand
Definition: XMLSetContextsCommand.h:33
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
BESXMLCommand::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESXMLCommand.cc:119
BESSyntaxUserError
error thrown if there is a user syntax error in the request or any other user error
Definition: BESSyntaxUserError.h:41
BESContextManager::set_context
virtual void set_context(const std::string &name, const std::string &value)
set context in the BES
Definition: BESContextManager.cc:53
BESXMLCommand
Base class for the BES's commands.
Definition: BESXMLCommand.h:63
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
bes::XMLSetContextsCommand::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: XMLSetContextsCommand.cc:141