bes Updated for version 3.20.13
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 "config.h"
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
33#include <time.h>
34
35#include <cerrno>
36#include <cstring>
37
38#include <sstream>
39#include <fstream>
40
41#include "ShowBesKeyCommand.h"
42#include "ShowBesKeyResponseHandler.h"
43
44#include "BESDebug.h"
45#include "BESError.h"
46#include "BESInfo.h"
47#include "BESInfoList.h"
48#include "BESDataNames.h"
49#include "TheBESKeys.h"
50
51#include "BESStopWatch.h"
52
53using namespace std;
54
55ShowBesKeyResponseHandler::ShowBesKeyResponseHandler(const string &name) :
57{
58}
59
60ShowBesKeyResponseHandler::~ShowBesKeyResponseHandler()
61{
62}
63
75{
76
77 BESStopWatch sw;
78 if (BESDebug::IsSet(TIMING_LOG_KEY)) sw.start("ShowBesKeyResponseHandler::execute", dhi.data[REQUEST_ID]);
79
80 BESInfo *info = BESInfoList::TheList()->build_info();
81 d_response_object = info;
82
83 string requested_bes_key = dhi.data[BES_KEY];
84
85 BESDEBUG(SBK_DEBUG_KEY, __func__ << "() - requested key: " << requested_bes_key << endl);
86
87 vector<string> key_values;
88 getBesKeyValue(requested_bes_key, key_values);
89
90 map<string, string> attrs;
91
92 attrs[KEY] = requested_bes_key;
93
94 info->begin_response(SHOW_BES_KEY_RESPONSE_STR, &attrs, dhi);
95
96 // I think we can replace/remove 'emptyAttrs' and pass nullptr in its place.
97 // jhrg 11/26/18
98 map<string, string> emptyAttrs;
99 for(unsigned long i = 0; i < key_values.size(); ++i)
100 info->add_tag("value", key_values[i], &emptyAttrs);
101
102 // end the response object
103 info->end_response();
104
105}
106
119{
120 if (d_response_object) {
121 BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
122 if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
123 info->transmit(transmitter, dhi);
124 }
125}
126
133void ShowBesKeyResponseHandler::dump(ostream &strm) const
134{
135 strm << BESIndent::LMarg << "ShowBesKeyResponseHandler::dump - (" << (void *) this << ")" << std::endl;
136 BESIndent::Indent();
138 BESIndent::UnIndent();
139}
140
142ShowBesKeyResponseHandler::ShowBesKeyResponseBuilder(const string &name)
143{
144 return new ShowBesKeyResponseHandler(name);
145}
146
147void ShowBesKeyResponseHandler::getBesKeyValue(string key, vector<string> &values)
148{
149 bool found;
150
151 TheBESKeys::TheKeys()->get_values(key, values, found);
152 if (!found) {
153 BESDEBUG(SBK_DEBUG_KEY, __func__ << "() Failed to located BES key '" << key << "'" << endl);
154 throw BESError("Ouch! The Key name '"+key+"' was not found in BESKeys",BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
155 }
156}
157
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
Base exception class for the BES with basic string message.
Definition: BESError.h:59
informational response object
Definition: BESInfo.h:63
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
exception thrown if internal error encountered
handler object that knows how to create a specific response object
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual bool start(std::string name)
Definition: BESStopWatch.cc:67
Response handler that returns the value(s) of a BES key.
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71
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:371