bes  Updated for version 3.20.6
W10nShowPathInfoResponseHandler.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 //
3 // W10NResponseHandler.cc
4 //
5 // This file is part of BES w10n handler
6 //
7 // Copyright (c) 2015v 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 "W10nShowPathInfoResponseHandler.h"
29 #include "W10nShowPathInfoCommand.h"
30 #include "W10NNames.h"
31 #include "w10n_utils.h"
32 #include "BESDebug.h"
33 
34 #include "BESInfoList.h"
35 #include "BESInfo.h"
36 #include "BESRequestHandlerList.h"
37 #include "BESRequestHandler.h"
38 #include "BESNames.h"
39 #include "BESDapNames.h"
40 #include "BESDataNames.h"
41 #include "BESCatalogList.h"
42 #include "BESCatalog.h"
43 #include "BESCatalogEntry.h"
44 #include "BESCatalogUtils.h"
45 #include "BESSyntaxUserError.h"
46 #include "BESNotFoundError.h"
47 #include "BESStopWatch.h"
48 
49 #define W10N_PATH_INFO_RESPONSE "W10nPathInfo"
50 
51 #define PATH "path"
52 #define VALID_PATH "validPath"
53 #define REMAINDER "remainder"
54 #define IS_DATA "isData"
55 #define IS_FILE "isFile"
56 #define IS_DIR "isDir"
57 
58 W10nShowPathInfoResponseHandler::W10nShowPathInfoResponseHandler(const string &name) :
59  BESResponseHandler(name)
60 {
61 }
62 
63 W10nShowPathInfoResponseHandler::~W10nShowPathInfoResponseHandler()
64 {
65 }
66 
78 {
79 
80  BESStopWatch sw;
81  if (BESISDEBUG(TIMING_LOG)) sw.start("W10NShowPathInfoResponseHandler::execute", dhi.data[REQUEST_ID]);
82 
83  BESDEBUG(W10N_DEBUG_KEY, "W10NShowPathInfoResponseHandler::execute() - BEGIN" << endl );
84 
85  BESInfo *info = BESInfoList::TheList()->build_info();
86  d_response_object = info;
87 
88  string container = dhi.data[CONTAINER];
89 #if 0
90  string catname;
92 #endif
93 
95  if (!defcat)
96  throw BESInternalError("Not able to find the default catalog.", __FILE__, __LINE__);
97 
98  // remove all of the leading slashes from the container name
99  string::size_type notslash = container.find_first_not_of("/", 0);
100  if (notslash != string::npos) {
101  container = container.substr(notslash);
102  }
103 
104  // see if there is a catalog name here. It's only a possible catalog name
105  string catname;
106  string::size_type slash = container.find_first_of("/", 0);
107  if (slash != string::npos) {
108  catname = container.substr(0, slash);
109  }
110  else {
111  catname = container;
112  }
113 
114  // see if this catalog exists. If it does, then remove the catalog
115  // name from the container (node)
116  BESCatalog *catobj = BESCatalogList::TheCatalogList()->find_catalog(catname);
117  if (catobj) {
118  if (slash != string::npos) {
119  container = container.substr(slash + 1);
120 
121  // remove repeated slashes
122  notslash = container.find_first_not_of("/", 0);
123  if (notslash != string::npos) {
124  container = container.substr(notslash);
125  }
126  }
127  else {
128  container = "";
129  }
130  }
131 
132  if (container.empty()) container = "/";
133 
134  BESDEBUG(W10N_DEBUG_KEY, "W10NShowPathInfoResponseHandler::execute() - w10n_id: " << container << endl );
135 
136  info->begin_response(W10N_SHOW_PATH_INFO_REQUEST, dhi);
137  //string coi = dhi.data[CATALOG_OR_INFO];
138 
139  map<string, string> pathInfoAttrs;
140  pathInfoAttrs[PATH] = container;
141 
142  info->begin_tag(W10N_PATH_INFO_RESPONSE, &pathInfoAttrs);
143 
144  string validPath, remainder;
145  bool isFile, isDir;
146 
148  w10n::eval_resource_path(container, utils->get_root_dir(), utils->follow_sym_links(), validPath, isFile, isDir,
149  remainder);
150 
151  // Now that we know what part of the path is actually something
152  // we can access, find out if the BES sees it as a dataset
153  bool isData = false;
154 
155  // If the valid path is an empty string then we KNOW it's not a dataset
156  if (validPath.length() != 0) {
157 
158  // Get the catalog entry.
159  BESCatalogEntry *entry = 0;
160  //string coi = dhi.data[CATALOG];
161  entry = defcat->show_catalog(validPath, /*coi,*/entry);
162  if (!entry) {
163  string err = (string) "Failed to find the validPath node " + validPath
164  + " this should not be possible. Some thing BAD is happening.";
165  throw BESInternalError(err, __FILE__, __LINE__);
166  }
167 
168  // Retrieve the valid services list
169  list<string> services = entry->get_service_list();
170 
171  // See if there's an OPENDAP_SERVICE available for the node.
172  if (services.size()) {
173  list<string>::const_iterator si = services.begin();
174  list<string>::const_iterator se = services.end();
175  for (; si != se; si++) {
176  if ((*si) == OPENDAP_SERVICE) isData = true;
177  }
178  }
179  }
180 
181  map<string, string> validPathAttrs;
182  validPathAttrs[IS_DATA] = isData ? "true" : "false";
183  validPathAttrs[IS_FILE] = isFile ? "true" : "false";
184  validPathAttrs[IS_DIR] = isDir ? "true" : "false";
185 
186  info->add_tag(VALID_PATH, validPath, &validPathAttrs);
187  info->add_tag(REMAINDER, remainder);
188 
189  info->end_tag(W10N_PATH_INFO_RESPONSE);
190 
191  // end the response object
192  info->end_response();
193 
194  BESDEBUG(W10N_DEBUG_KEY, "W10nShowPathInfoResponseHandler::execute() - END" << endl );
195  }
196 
209 {
210  if (d_response_object) {
211  BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
212  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
213  info->transmit(transmitter, dhi);
214  }
215 }
216 
223 void W10nShowPathInfoResponseHandler::dump(ostream &strm) const
224 {
225  strm << BESIndent::LMarg << "W10nShowPathInfoResponseHandler::dump - (" << (void *) this << ")" << std::endl;
226  BESIndent::Indent();
228  BESIndent::UnIndent();
229 }
230 
232 W10nShowPathInfoResponseHandler::W10nShowPathInfoResponseBuilder(const string &name)
233 {
234  return new W10nShowPathInfoResponseHandler(name);
235 }
236 
BESStopWatch::start
virtual bool start(std::string name)
Definition: BESStopWatch.cc:58
BESCatalog::show_catalog
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
BESCatalogUtils
Definition: BESCatalogUtils.h:61
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
BESCatalogList::default_catalog
virtual BESCatalog * default_catalog() const
The the default catalog.
Definition: BESCatalogList.h:118
BESCatalogList::TheCatalogList
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Definition: BESCatalogList.cc:81
W10nShowPathInfoResponseHandler::execute
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
Definition: W10nShowPathInfoResponseHandler.cc:77
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
BESCatalog::get_catalog_utils
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
Definition: BESCatalog.h:113
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
W10nShowPathInfoResponseHandler::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: W10nShowPathInfoResponseHandler.cc:223
BESCatalogEntry
Definition: BESCatalogEntry.h:46
BESCatalogUtils::get_root_dir
const std::string & get_root_dir() const
Get the root directory of the catalog.
Definition: BESCatalogUtils.h:102
W10nShowPathInfoResponseHandler
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
Definition: W10nShowPathInfoResponseHandler.h:48
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
W10nShowPathInfoResponseHandler::transmit
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
Definition: W10nShowPathInfoResponseHandler.cc:208
BESCatalogList::default_catalog_name
virtual std::string default_catalog_name() const
The name of the default catalog.
Definition: BESCatalogList.h:116