bes  Updated for version 3.20.6
SiteMapResponseHandler.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 //
3 // SiteMapResponseHandler.cc
4 //
5 // This file is part of the BES default command set
6 //
7 // Copyright (c) 2018 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@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 "SiteMapResponseHandler.h"
41 
42 
43 #include "BESTextInfo.h"
44 #include "BESDataNames.h"
45 #include "BESNames.h"
46 #include "TheBESKeys.h"
47 #include "BESCatalog.h"
48 #include "BESCatalogList.h"
49 
50 #include "BESError.h"
51 #include "BESDebug.h"
52 #include "BESStopWatch.h"
53 
54 using namespace std;
55 
56 SiteMapResponseHandler::SiteMapResponseHandler(const string &name) :
57  BESResponseHandler(name)
58 {
59 }
60 
61 SiteMapResponseHandler::~SiteMapResponseHandler()
62 {
63 }
64 
76 {
77  BESStopWatch sw;
78  if (BESISDEBUG(TIMING_LOG)) sw.start("SiteMapResponseHandler::execute", dhi.data[REQUEST_ID]);
79 
80  // Force this command to use a TextInfo object. The default Info object type
81  // is set using a key in bes.conf. jheg 11/27/18
82  BESInfo *info = new BESTextInfo(); // BESInfoList::TheList()->build_info();
83  d_response_object = info;
84 
85  if (dhi.data[SITE_MAP_RESPONSE] != SITE_MAP_RESPONSE)
86  throw BESInternalError("Not a Site Map command in SiteMapResponseHandler::execute().", __FILE__, __LINE__);
87 
88  // remove trailing '/' if present
89  if (*(dhi.data[PREFIX].end()-1) == '/')
90  dhi.data[PREFIX].erase(dhi.data[PREFIX].end()-1);
91 
92  ostringstream oss;
93  const BESCatalogList *catalog_list = BESCatalogList::TheCatalogList(); // convenience variable
94 
95  info->begin_response(SITE_MAP_RESPONSE_STR, dhi);
96 
97  // If no catalog is named, return the concatenation of the site maps for
98  // all of the catalogs. This mimics the way shwoNode returns information
99  // for the path '/'.
100  if (dhi.data[CATALOG].empty()) {
101  BESCatalogList::catalog_citer i = catalog_list->first_catalog();
102  BESCatalogList::catalog_citer e = catalog_list->end_catalog();
103  for (; i != e; ++i) {
104  BESCatalog *catalog = i->second;
105  if (!catalog)
106  throw BESInternalError(string("Build site map found a null catalog in the catalog list."), __FILE__, __LINE__);
107 
108  // Don't add 'default' to the prefix value here. For other catalogs, do add their
109  // names to the prefix (i->first is the name of the catalog). This matches the
110  // behavior of showNode, where the non-default catalogs 'appear' in the default
111  // catalog's top level as 'phantom' directories.
112  string prefix = (i->first == catalog_list->default_catalog_name()) ? dhi.data[PREFIX]: dhi.data[PREFIX] + "/" + i->first;
113  catalog->get_site_map(prefix, dhi.data[NODE_SUFFIX], dhi.data[LEAF_SUFFIX], oss, "/");
114 
115  info->add_data(oss.str());
116 
117  // clearing oss to keep the size of the object small.
118  // Maybe change get_site_map so it takes an Info object? jhrg 11/28/18
119  oss.str("");
120  oss.clear();
121  }
122  }
123  else {
124  BESCatalog *catalog = catalog_list->find_catalog(dhi.data[CATALOG]);
125  if (!catalog)
126  throw BESInternalError(string("Build site map could not find the catalog: ") + dhi.data[CATALOG], __FILE__, __LINE__);
127 
128  string prefix = (dhi.data[CATALOG] == catalog_list->default_catalog_name()) ? dhi.data[PREFIX]: dhi.data[PREFIX] + "/" + dhi.data[CATALOG];
129  catalog->get_site_map(prefix, dhi.data[NODE_SUFFIX], dhi.data[LEAF_SUFFIX], oss, "/");
130 
131  info->add_data(oss.str());
132 
133  // As above, no sense keeping this in memory any longer than needed
134  oss.str("");
135  oss.clear();
136  }
137 
138  info->end_response();
139 }
140 
153 {
154  if (d_response_object) {
155  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
156  if (!info) throw BESInternalError("Could not get the Info object in SiteMapResponseHandler::transmit()", __FILE__, __LINE__);
157  info->transmit(transmitter, dhi);
158  }
159 }
160 
167 void SiteMapResponseHandler::dump(ostream &strm) const
168 {
169  strm << BESIndent::LMarg << "SiteMapResponseHandler::dump - (" << (void *) this << ")" << std::endl;
170  BESIndent::Indent();
172  BESIndent::UnIndent();
173 }
174 
176 SiteMapResponseHandler::SiteMapResponseBuilder(const string &name)
177 {
178  return new SiteMapResponseHandler(name);
179 }
BESStopWatch::start
virtual bool start(std::string name)
Definition: BESStopWatch.cc:58
BESCatalogList::end_catalog
virtual catalog_citer end_catalog() const
Iterator to the last catalog.
Definition: BESCatalogList.h:133
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
SiteMapResponseHandler::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
Definition: SiteMapResponseHandler.cc:152
SiteMapResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: SiteMapResponseHandler.cc:167
BESCatalogList::TheCatalogList
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Definition: BESCatalogList.cc:81
BESCatalogList
List of all registered catalogs.
Definition: BESCatalogList.h:84
SiteMapResponseHandler::execute
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
Definition: SiteMapResponseHandler.cc:75
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
BESInfo::add_data
virtual void add_data(const std::string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition: BESInfo.cc:160
BESCatalog
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
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
BESCatalogList::default_catalog_name
virtual std::string default_catalog_name() const
The name of the default catalog.
Definition: BESCatalogList.h:116
BESCatalogList::first_catalog
virtual catalog_citer first_catalog() const
Iterator to the first catalog.
Definition: BESCatalogList.h:130
SiteMapResponseHandler
Response handler that returns a site map.
Definition: SiteMapResponseHandler.h:58
BESTextInfo
represents simple text information in a response object, such as version and help information.
Definition: BESTextInfo.h:48