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