bes  Updated for version 3.20.6
ShowBesKeyResponseHandler.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 //
3 // ShowBesKeyResponseHandler.cc
4 //
5 // This file is part of the BES default command set
6 //
7 // Copyright (c) 2018 OPeNDAP, Inc.
8 // Author: Nathan Potter <ndp@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 // Please read the full copyright statement in the file COPYRIGHT_URI.
26 //
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <time.h>
32 
33 #include <cerrno>
34 #include <cstring>
35 
36 #include <sstream>
37 #include <fstream>
38 
39 #include "ShowBesKeyCommand.h"
40 #include "ShowBesKeyResponseHandler.h"
41 
42 #include "BESDebug.h"
43 #include "BESError.h"
44 #include "BESInfo.h"
45 #include "BESInfoList.h"
46 #include "BESDataNames.h"
47 #include "TheBESKeys.h"
48 
49 #include "BESStopWatch.h"
50 
51 using namespace std;
52 
53 ShowBesKeyResponseHandler::ShowBesKeyResponseHandler(const string &name) :
54  BESResponseHandler(name)
55 {
56 }
57 
58 ShowBesKeyResponseHandler::~ShowBesKeyResponseHandler()
59 {
60 }
61 
73 {
74 
75  BESStopWatch sw;
76  if (BESISDEBUG(TIMING_LOG)) sw.start("ShowBesKeyResponseHandler::execute", dhi.data[REQUEST_ID]);
77 
78  BESInfo *info = BESInfoList::TheList()->build_info();
79  d_response_object = info;
80 
81  string requested_bes_key = dhi.data[BES_KEY];
82 
83  BESDEBUG(SBK_DEBUG_KEY, __func__ << "() - requested key: " << requested_bes_key << endl);
84 
85  vector<string> key_values;
86  getBesKeyValue(requested_bes_key, key_values);
87 
88  map<string, string> attrs;
89 
90  attrs[KEY] = requested_bes_key;
91 
92  info->begin_response(SHOW_BES_KEY_RESPONSE_STR, &attrs, dhi);
93 
94  // I think we can replace/remove 'emptyAttrs' and pass nullptr in its place.
95  // jhrg 11/26/18
96  map<string, string> emptyAttrs;
97  for(unsigned long i = 0; i < key_values.size(); ++i)
98  info->add_tag("value", key_values[i], &emptyAttrs);
99 
100  // end the response object
101  info->end_response();
102 
103 }
104 
117 {
118  if (d_response_object) {
119  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
120  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
121  info->transmit(transmitter, dhi);
122  }
123 }
124 
131 void ShowBesKeyResponseHandler::dump(ostream &strm) const
132 {
133  strm << BESIndent::LMarg << "ShowBesKeyResponseHandler::dump - (" << (void *) this << ")" << std::endl;
134  BESIndent::Indent();
136  BESIndent::UnIndent();
137 }
138 
140 ShowBesKeyResponseHandler::ShowBesKeyResponseBuilder(const string &name)
141 {
142  return new ShowBesKeyResponseHandler(name);
143 }
144 
145 void ShowBesKeyResponseHandler::getBesKeyValue(string key, vector<string> &values)
146 {
147  bool found;
148 
149  TheBESKeys::TheKeys()->get_values(key, values, found);
150  if (!found) {
151  BESDEBUG(SBK_DEBUG_KEY, __func__ << "() Failed to located BES key '" << key << "'" << endl);
152  throw BESError("Ouch! The Key name '"+key+"' was not found in BESKeys",BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
153  }
154 }
155 
BESStopWatch::start
virtual bool start(std::string name)
Definition: BESStopWatch.cc:58
ShowBesKeyResponseHandler::execute
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
Definition: ShowBesKeyResponseHandler.cc:72
ShowBesKeyResponseHandler
Response handler that returns the value(s) of a BES key.
Definition: ShowBesKeyResponseHandler.h:43
BESResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESResponseHandler.cc:102
BESInfo::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
BESInfo
informational response object
Definition: BESInfo.h:63
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
ShowBesKeyResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: ShowBesKeyResponseHandler.cc:131
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
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESResponseHandler
handler object that knows how to create a specific response object
Definition: BESResponseHandler.h:77
BESStopWatch
Definition: BESStopWatch.h:55
BESTransmitter
Definition: BESTransmitter.h:47
TheBESKeys::get_values
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Definition: TheBESKeys.cc:303
ShowBesKeyResponseHandler::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
Definition: ShowBesKeyResponseHandler.cc:116
BESInfo::begin_response
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
BESDataHandlerInterface
Structure storing information used by the BES to handle the request.
Definition: BESDataHandlerInterface.h:56
BESError
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58