OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
DapRequestHandler.cc
Go to the documentation of this file.
1 // DapRequestHandler.cc
2 
3 // Copyright (c) 2013 OPeNDAP, Inc. Author: James Gallagher
4 // <jgallagher@opendap.org>, Patrick West <pwest@opendap.org>
5 // Nathan Potter <npotter@opendap.org>
6 //
7 // modify it under the terms of the GNU Lesser General Public License
8 // as published by the Free Software Foundation; either version 2.1 of
9 // the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 U\ SA
19 //
20 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI.
21 // 02874-0112.
22 #include "config.h"
23 
24 #include "DapRequestHandler.h"
25 #include "DapResponseNames.h"
26 
27 #include <BESResponseHandler.h>
28 #include <BESResponseNames.h>
29 #include <BESVersionInfo.h>
30 #include <BESTextInfo.h>
31 #include <BESDapNames.h>
32 #include <BESDataDDSResponse.h>
33 #include <BESDDSResponse.h>
34 #include <BESDASResponse.h>
35 #include <BESConstraintFuncs.h>
36 #include <BESServiceRegistry.h>
37 #include <BESUtil.h>
38 #include <BESDapError.h>
39 #include <BESInternalFatalError.h>
40 #include <BESDebug.h>
41 
42 #include <BaseTypeFactory.h>
43 #include <Ancillary.h>
44 #include <Connect.h>
45 #include <Response.h>
46 #include <InternalErr.h>
47 #include <mime_util.h>
48 
49 using namespace libdap;
50 
52  BESRequestHandler(name)
53 {
59 }
60 
62 {
63 }
64 
66 {
68  BESDASResponse *bdas = dynamic_cast<BESDASResponse *>(response);
69  if (!bdas)
70  throw BESInternalError("cast error", __FILE__, __LINE__);
71  try {
73  DAS *das = bdas->get_das();
74  string accessed = dhi.container->access();
75  das->parse(accessed);
76  bdas->clear_container();
77  }
78  catch (BESError &e) {
79  throw e;
80  }
81  catch (InternalErr & e) {
82  BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
83  throw ex;
84  }
85  catch (Error & e) {
86  BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
87  throw ex;
88  }
89  catch (...) {
90  string s = "unknown exception caught building DAS";
91  BESInternalFatalError ex(s, __FILE__, __LINE__);
92  throw ex;
93  }
94 
95  return true;
96 }
97 
99 {
101  BESDDSResponse *bdds = dynamic_cast<BESDDSResponse *>(response);
102  if (!bdds)
103  throw BESInternalError("cast error", __FILE__, __LINE__);
104  try {
106  DDS *dds = bdds->get_dds();
107  string accessed = dhi.container->access();
108  BaseTypeFactory factory;
109  dds->set_factory(&factory);
110  dds->filename(accessed);
111  dds->set_dataset_name(name_path(accessed));
112  dds->parse(accessed);
113  dds->set_factory(0);
114 
115  DAS *das = new DAS;
116  BESDASResponse bdas(das);
118  Ancillary::read_ancillary_das(*das, accessed);
119 
120  dds->transfer_attributes(das);
121 
122  bdds->set_constraint(dhi);
123 
124  bdds->clear_container();
125  }
126  catch (BESError &e) {
127  throw e;
128  }
129  catch (InternalErr & e) {
130  BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
131  throw ex;
132  }
133  catch (Error & e) {
134  BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
135  throw ex;
136  }
137  catch (...) {
138  string s = "unknown exception caught building DDS";
139  BESInternalFatalError ex(s, __FILE__, __LINE__);
140  throw ex;
141  }
142 
143  return true;
144 }
145 
147 {
149  BESDataDDSResponse *bdds = dynamic_cast<BESDataDDSResponse *>(response);
150  if (!bdds)
151  throw BESInternalError("cast error", __FILE__, __LINE__);
152 
153  try {
155  DataDDS *dds = bdds->get_dds();
156  string accessed = dhi.container->access();
157  BaseTypeFactory factory;
158  dds->set_factory(&factory);
159  dds->filename(accessed);
160  dds->set_dataset_name(name_path(accessed));
161  Connect *url = new Connect(accessed);
162  Response *r = new Response(fopen(accessed.c_str(), "r"), 0);
163 
164  if (!r->get_stream())
165  throw Error(string("The input source: ") + accessed + string(" could not be opened"));
166 
167  url->read_data_no_mime(*dds, r);
168  dds->set_factory(0);
169 
170  // mark everything as read.
171  DDS::Vars_iter i = dds->var_begin();
172  DDS::Vars_iter e = dds->var_end();
173  for (; i != e; i++) {
174  BaseType *b = (*i);
175  b->set_read_p(true);
176  }
177 
178  DAS *das = new DAS;
179  BESDASResponse bdas(das);
181  Ancillary::read_ancillary_das(*das, accessed);
182  dds->transfer_attributes(das);
183 
184  bdds->set_constraint(dhi);
185 
186  bdds->clear_container();
187  }
188  catch (BESError &e) {
189  throw e;
190  }
191  catch (InternalErr & e) {
192  BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
193  throw ex;
194  }
195  catch (Error & e) {
196  BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
197  throw ex;
198  }
199  catch (...) {
200  string s = "unknown exception caught building DAS";
201  BESInternalFatalError ex(s, __FILE__, __LINE__);
202  throw ex;
203  }
204 
205  return true;
206 }
207 
209 {
210  bool ret = true;
211  BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(dhi.response_handler->get_response_object());
213  return ret;
214 }
215 
217 {
218  bool ret = true;
219  BESInfo *info = dynamic_cast<BESInfo *>(dhi.response_handler->get_response_object());
220 
221  // This is an example. If you had a help file you could load it like
222  // this and if your handler handled the following responses.
223  map<string, string> attrs;
224  attrs["name"] = PACKAGE_NAME;
225  attrs["version"] = PACKAGE_VERSION;
226  list<string> services;
227  BESServiceRegistry::TheRegistry()->services_handled("dapreader", services);
228  if (services.size() > 0) {
229  string handles = BESUtil::implode(services, ',');
230  attrs["handles"] = handles;
231  }
232  info->begin_tag("module", &attrs);
233  //info->add_data_from_file( "Dap.Help", "Dap Help" ) ;
234  info->end_tag("module");
235 
236  return ret;
237 }
238 
239 void DapRequestHandler::dump(ostream &strm) const
240 {
241  strm << BESIndent::LMarg << "DapRequestHandler::dump - (" << (void *) this << ")" << endl;
245 }
246 
brief represents simple text information in a response object, such as version and help inforamtion...
exception thrown if an internal error is found and is fatal to the BES
#define DAPREADER_VERSION
Definition: config.h:16
exception thrown if inernal error encountered
virtual void dump(ostream &strm) const
dumps information about this object
string get_symbolic_name() const
retrieve the symbolic name for this container
Definition: BESContainer.h:159
Represents an OPeNDAP DDS DAP2 data object within the BES.
virtual void clear_container()
clear the container in the DAP response object
static bool dap_build_data(BESDataHandlerInterface &dhi)
#define DAPREADER_PACKAGE
Definition: config.h:13
static void Indent()
Definition: BESIndent.cc:38
virtual string access()=0
returns the true name of this container
virtual void clear_container()
clear the container in the DAP response object
#define HELP_RESPONSE
informational response object
Definition: BESInfo.h:68
static string implode(const list< string > &values, char delim)
implode a list of values into a single string delimited by delim
Definition: BESUtil.cc:530
DapRequestHandler(const string &name)
virtual BESResponseObject * get_response_object()
return the current response object
Abstract exception class for the BES with basic string message.
Definition: BESError.h:51
#define PACKAGE_NAME
Definition: config.h:135
#define DATA_RESPONSE
Definition: BESDapNames.h:69
static BESServiceRegistry * TheRegistry()
BESResponseHandler * response_handler
virtual void set_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void begin_tag(const string &tag_name, map< string, string > *attrs=0)
Definition: BESInfo.cc:142
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual void set_container(const string &cn)
set the container in the DAP response object
error object created from libdap error objects and can handle those errors
Definition: BESDapError.h:51
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
virtual void clear_container()
clear the container in the DAP response object
Represents a specific data type request handler.
static bool dap_build_help(BESDataHandlerInterface &dhi)
Structure storing information used by the BES to handle the request.
static bool dap_build_dds(BESDataHandlerInterface &dhi)
virtual ~DapRequestHandler(void)
#define VERS_RESPONSE
virtual void set_container(const string &cn)
set the container in the DAP response object
virtual bool add_handler(const string &handler_name, p_request_handler handler_method)
add a handler method to the request handler that knows how to fill in a specific response object ...
#define PACKAGE_VERSION
Definition: config.h:147
Represents an OPeNDAP DAS DAP2 data object within the BES.
static void UnIndent()
Definition: BESIndent.cc:44
static bool dap_build_das(BESDataHandlerInterface &dhi)
virtual void set_container(const string &cn)
set the container in the DAP response object
virtual void dump(ostream &strm) const
dumps information about this object
virtual void add_module(const string &n, const string &v)
Abstract base class representing a specific set of information in response to a request to the BES...
#define DDS_RESPONSE
Definition: BESDapNames.h:59
BESContainer * container
pointer to current container in this interface
#define DAS_RESPONSE
Definition: BESDapNames.h:54
virtual void end_tag(const string &tag_name)
Definition: BESInfo.cc:149
static bool dap_build_vers(BESDataHandlerInterface &dhi)
virtual void services_handled(const string &handler, list< string > &services)
returns the list of servies provided by the handler in question